How to manipulate kubernetes PodSpec object

8/27/2019

I am looking for some help with the golang code to modify the podspec based on user input.

This is my flow.

User provides an incomplete yaml file to create deployment. Assume they are missing/update environment variables information. User also gives a variable foo [{name: "abc", value: "xyz"}, {name: "ab", value: "12"}] which has the environment variable information. I need to read the yaml file and merge the variable before i create the deployment.

This is what I have figured out so far, Read yaml file.

decode := scheme.Codecs.UniversalDeserializer().Decode
data, _ := readyamlfile(file)
obj, _, _ := decode(data, nil, nil)
dep := obj.(*appsv1.Deployment)

From dep I can find the podspec. Now I need to update the object based on user input foo. Not sure if i can use XXX_Merge function or not. Please recommend and if possible provide a working example pointer of XXX_Merge function. https://godoc.org/k8s.io/apiserver/pkg/apis/example/v1#PodSpec.XXX_Merge

Please let me know if there is any other way.

-- Nitin Mathur
client-go
go
kubernetes
proto

1 Answer

8/28/2019

You should not use XXX_Merge. Just fill the dep object with user inputs. For instance:

dep.Spec.Template.Env = append(dep.Spec.Template.Env, EnvVar{Name: "abc", Value: "xyz"})
dep.Spec.Template.Env = append(dep.Spec.Template.Env, EnvVar{Name: "ab", Value: "12"})
-- hoozecn
Source: StackOverflow