3

I'm not sure if the title is the correct terminology..But I basically want to use the go-client and get kubectl compliant objects (yamls).

i.e a deployment resource would be:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
  labels:
    app: nginx
spec:
  replicas: 3
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:1.15.4
        ports:
        - containerPort: 80

I can get deployments from my k8s cluster via go-client like such:

    Deployments, err := clientset.AppsV1().Deployments().List(metav1.ListOptions{})
    //and then loop through each deployment:
    for _, deploy := range Deployments.Items{
     //deploy is type v1.Deployment
    }

if I was to marshal deployment and save to file, the struct is:

type Deployment struct {
    v1.TypeMeta    `json:",inline"`
    v1.ObjectMeta  `json:"metadata,omitempty" protobuf:"bytes,1,opt,name=metadata"`
    Spec              DeploymentSpec    `json:"spec,omitempty" protobuf:"bytes,2,opt,name=spec"`
    Status            DeploymentStatus  `json:"status,omitempty" protobuf:"bytes,3,opt,name=status"`
}

obviously much different from what kubectl file is expecting.

(Though i can use go-client deployment.create(obj) to create that deployment).

If I wanted to create a kubectl valid resource, I could create a custom struct that adheres to that type, and then manually fill in the values.

Is there any way to do this automatically? or some helper functions that is there currently?

in essence i want to convert the v1.Deployment struct to the generic kubectl yaml resource.

2 Answers 2

4

Following is the way I use to encode objects to json with kubectl like structure. As shown below this is specifically for objects from CoreV1 but you can easily register other apis with the used scheme.

scheme := runtime.NewScheme()
corev1.AddToScheme(scheme)
codec := serializer.NewCodecFactory(scheme).LegacyCodec(corev1.SchemeGroupVersion)
output, _ := runtime.Encode(codec, stripped)
Sign up to request clarification or add additional context in comments.

Comments

3

I think, you can simply Marshal the struct and get the yaml.

I used "github.com/ghodss/yaml" for struct marshaling. Please include this in import.

Deployments, err := clientset.AppsV1().Deployments().List(metav1.ListOptions{})
//and then loop through each deployment:
for _, deploy := range Deployments.Items{
    y, err := yaml.Marshal(deploy)
    if err != nil {
       panic(err)
    }
    fmt.Println("deployment print in yaml: ", string(y))
}

Hope it'll help.

1 Comment

hey @abu this unfortunately doesn't work, as marshaling the deploy, marshals it to v1.Deployment, which is not a valid kubectl resource

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.