Skip to content

Commit

Permalink
Add conformance test (#1)
Browse files Browse the repository at this point in the history
  • Loading branch information
anjmao authored Jul 31, 2022
1 parent 2b3fe5c commit d8016d1
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 11 deletions.
11 changes: 6 additions & 5 deletions .github/workflows/pull_request.yaml
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
name: Test conformance
name: Test

on: pull_request

jobs:
create-cluster:
test:
runs-on: ubuntu-latest
steps:
- name: Checkout
Expand All @@ -24,7 +24,7 @@ jobs:
- name: Build Go binary
run: |
cd ./conformance
go build -ldflags "-s -w" -o conformance .
go build -ldflags "-s -w" -o bin/conformance .
env:
GOOS: linux
GOARCH: amd64
Expand All @@ -41,12 +41,13 @@ jobs:
uses: docker/build-push-action@v2
with:
context: ./conformance
push: true
tags: ghcr.io/castai/k8s-client-go/conformance:${{ github.sha }}

- name: Create k8s cluster
uses: helm/kind-action@v1.3.0

- name: Run conformance
- name: Run tests
run: |
cd ./conformance
./run.sh
IMG=ghcr.io/castai/k8s-client-go/conformance:${{ github.sha }} ./run.sh
33 changes: 32 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,34 @@
# k8s-client-go

Minimal Go Kubernetes client
Minimal Go Kubernetes client based on Generics

## Usage

```go
import (
"context"
"log"
"fmt"
client "github.com/castai/k8s-client-go"
)

func main() {
kc, err := client.NewInCluster()
if err != nil {
log.Fatal(err)
}
ctx := context.Backgroud()
endpoints, err := client.Get[*client.Endpoints](kc, ctx, "/api/v1/namespaces/kube-system/endpoints/kubelet", client.GetOptions{})
if err != nil {
log.Fatal(err)
}
fmt.Printf("%+v", endpoints)
}
```

See more in [Examples](https://github.com/castai/k8s-client-go/blob/master/client_test.go#L10)

## Use cases

* Embedding in Go applications for minimal binary size overhead.
* Service discovery by listing and watching [endpoints](https://kubernetes.io/docs/reference/kubernetes-api/service-resources/endpoints-v1/). See [kuberesolver](https://github.com/sercand/kuberesolver) as example for gRPC client side load balancing.
22 changes: 21 additions & 1 deletion client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,33 @@ func ExampleClient() {
kc, err := client.NewInCluster()
if err != nil {
// Handle err
return
}

ctx := context.Background()
endpoints, err := client.GetEndpoints(kc, ctx, "kube-system", "kubelet", client.GetOptions{})

// Generic get.
endpoints, err := client.Get[*client.Endpoints](kc, ctx, "/api/v1/namespaces/kube-system/endpoints/kubelet", client.GetOptions{})
if err != nil {
// Handle err
return
}

// Typed methods. Simple wrapper for Get.
endpoints, err = client.GetEndpoints(kc, ctx, "kube-system", "kubelet", client.GetOptions{})
if err != nil {
// Handle err
return
}
fmt.Printf("%+v", endpoints)

// Watch support.
events, err := client.Watch[*client.Endpoints](kc, ctx, "/api/v1/namespaces/kube-system/endpoints/kubelet", client.ListOptions{})
if err != nil {
// Handle err
return
}
for event := range events.ResultChan() {
fmt.Println(event.Type, event.Object)
}
}
3 changes: 3 additions & 0 deletions conformance/job.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ metadata:
namespace: conformance
spec:
template:
metadata:
labels:
app: conformance
spec:
serviceAccount: conformance
containers:
Expand Down
7 changes: 5 additions & 2 deletions conformance/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ func testEndpoints(nativeClient *kubernetes.Clientset, kc *client.Client) error
}

compare := func(a *corev1.Endpoints, b *client.Endpoints) error {
fmt.Printf("compare endpoints:\na=%+v\nb=%+v\n", a, b)
ip1 := a.Subsets[0].Addresses[0].IP
ip2 := b.Subsets[0].Addresses[0].IP
if ip1 != ip2 {
Expand Down Expand Up @@ -92,8 +93,10 @@ func testEndpoints(nativeClient *kubernetes.Clientset, kc *client.Client) error
deleted = true
}
if added && deleted {
a = event.Object.(*corev1.Endpoints)
e.Stop()
if v, ok := event.Object.(*corev1.Endpoints); ok {
a = v
e.Stop()
}
}
}
return nil
Expand Down
14 changes: 12 additions & 2 deletions conformance/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,17 @@ if [ "$IMG" == "" ]; then
docker push ${img}
fi

function log()
{
echo "Test failed!"
echo "Pods:"
kubectl get pods -owide -n conformance
echo "Logs:"
kubectl logs -l app=conformance -n conformance
}
trap log ERR

kubectl delete ns conformance || true
kubectl create ns conformance
kubectl apply -f job.yaml --dry-run=client -oyaml | sed "s/replace-img/$(echo "$img" | sed 's/\//\\\//g')/" | kubectl apply -f -
kubectl wait --for=condition=complete --timeout=10s job/conformance
kubectl apply -f job.yaml --dry-run=client -oyaml | sed "s/replace-img/$(echo "$img" | sed 's/\//\\\//g')/" | kubectl apply -f - -n conformance
kubectl wait --for=condition=complete --timeout=10s job/conformance -n conformance

0 comments on commit d8016d1

Please sign in to comment.