go-k8s-utils
This repository contains utils for the work with Kubernetes, in specific with the go-client library.
Testing
This package contains utils which are useful for testing (e.g. with the fake-client).
Reactors
Having the ReactionFunc signature, the following helper exist:
GenerateNameReactor
: setting theObjectMeta.Name
field, based onObjectMeta.GenerateName
(asObjectMeta.Name
is not set automatically by the fake-client, if onlyObjectMeta.GenerateName
is set).SecretDataReactor
: setting theSecret.Data
field based onSecret.StringData
(asSecret.Data
is not set automatically by the fake-client, if onlySecret.StringData
is set).
Examples:
clientset := fake.NewSimpleClientset()
clientset.PrependReactor("create", "pods", GenerateNameReactor)
pod := &v1.Pod{
ObjectMeta: metav1.ObjectMeta{
Namespace: "default",
GenerateName: "testpod-",
},
Spec: v1.PodSpec{
Containers: []v1.Container{
{
Name: "nginx",
Image: "nginx",
},
},
},
}
createdPod, _ := clientset.CoreV1().Pods(pod.Namespace).Create(ctx, pod, metav1.CreateOptions{})
fmt.Printf("Name: %s", createdPod.Name) //remember: the pods Name field was not set, only GenerateName
clientset := fake.NewSimpleClientset()
clientset.PrependReactor("create", "secrets", SecretDataReactor)
secret := &v1.Secret{
ObjectMeta: metav1.ObjectMeta{
Namespace: "default",
Name: "my-secret",
},
StringData: map[string]string{
"my-key": "my-value",
},
Type: v1.SecretTypeOpaque,
}
createdSecret, _ := clientset.CoreV1().Secrets(secret.Namespace).Create(ctx, secret, metav1.CreateOptions{})
fmt.Printf("Secrets data: %+v", createdSecret.Data) //remember: the secrets StringData field was set, not the Data field