kubernetes-go-client

client-go: parse kubernetes json files to k8s structures

我怕爱的太早我们不能终老 提交于 2019-12-23 21:23:09
问题 i would like to parse kubernetes manifest file (json/yaml) and be able to convert them to k8s structures (to later on manipulate them) I know there is the NewYAMLOrJSONDecoder().Decode() function (https://github.com/kubernetes/apimachinery/blob/master/pkg/util/yaml/decoder.go) to read a json/yaml file, but the next step is: how to convert them to k8s structure/type? i.e. if I read a yaml file with a Namespace object, how to convert it to a core/v1/namespace interface for example Regards, 回答1:

kubernetes go client patch example

放肆的年华 提交于 2019-12-23 02:43:24
问题 after some searching I'm unable to find a golang Kube client example that performs at Patch using any strategy...I'm looking for a golang example of doing this: kubectl patch pod valid-pod --type='json' -p='[{"op": "replace", "path": "/spec/containers/0/image", "value":"new image"}]' I'm using https://github.com/kubernetes/client-go v2.0.0 can anyone point me to an example? thanks. 回答1: so, I think I have an example working after digging thru the kubectl resource helper.go code, here it is:

Are Kubernetes API calls secret update and configmap update atomic calls?

♀尐吖头ヾ 提交于 2019-12-11 04:34:33
问题 Is client.Secrets(namespace).Update(secret) an atomic call? If this call fails somehow, does the original secret stored in Kubernetes API server get corrupted? https://github.com/kubernetes/client-go/blob/d1b30110f1abd3b2fb21c5c2daad4345ede8a9fc/kubernetes/typed/core/v1/secret.go#L41 Similarly, is core.ConfigMaps(namespace).Update(configmap) an atomic call? If this call fails, does the existing configmap get corrupted? 回答1: client-go UPDATE is an HTTP PUT, so it will replace the object, and

How to deserialize Kubernetes YAML file

南笙酒味 提交于 2019-12-07 17:09:42
问题 How can I deserialize a Kubernetes YAML file into an Go struct? I took a look into the kubectl code, but somehow I get an error for every YAML file: no kind "Deployment" is registered for version "apps/v1beta1" This is an MWE: package main import ( "fmt" "k8s.io/client-go/pkg/api" ) var service = ` apiVersion: apps/v1beta1 kind: Deployment metadata: name: my-nginx spec: replicas: 2 template: metadata: labels: run: my-nginx spec: containers: - name: my-nginx image: nginx ports: - containerPort

Watch kubernetes pod status to be completed in client-go

醉酒当歌 提交于 2019-12-07 10:41:48
问题 I am creating a pod in k8 client go and making a watch to get notified for when the pod has completed so that i can read the logs of the pod. The watch interface doesnt seem to provide any events on the channel. Here is the code, how would I get notified that the pod status is now completed and is ready to read the logs func readLogs(clientset *kubernetes.Clientset) { // namespace := "default" // label := "cithu" var ( pod *v1.Pod // watchface watch.Interface err error ) // returns a pod

How to deserialize Kubernetes YAML file

我的未来我决定 提交于 2019-12-05 21:48:29
How can I deserialize a Kubernetes YAML file into an Go struct? I took a look into the kubectl code, but somehow I get an error for every YAML file: no kind "Deployment" is registered for version "apps/v1beta1" This is an MWE: package main import ( "fmt" "k8s.io/client-go/pkg/api" ) var service = ` apiVersion: apps/v1beta1 kind: Deployment metadata: name: my-nginx spec: replicas: 2 template: metadata: labels: run: my-nginx spec: containers: - name: my-nginx image: nginx ports: - containerPort: 80 ` func main() { decode := api.Codecs.UniversalDecoder().Decode //decode := api.Codecs

Watch kubernetes pod status to be completed in client-go

戏子无情 提交于 2019-12-05 15:41:49
I am creating a pod in k8 client go and making a watch to get notified for when the pod has completed so that i can read the logs of the pod. The watch interface doesnt seem to provide any events on the channel. Here is the code, how would I get notified that the pod status is now completed and is ready to read the logs func readLogs(clientset *kubernetes.Clientset) { // namespace := "default" // label := "cithu" var ( pod *v1.Pod // watchface watch.Interface err error ) // returns a pod after creation pod, err = createPod(clientset) fmt.Println(pod.Name, pod.Status, err) if watchface, err =