Last active
August 4, 2019 22:03
-
-
Save tom-code/fe56544064d51ea84270f3145f33e536 to your computer and use it in GitHub Desktop.
kustomize as library experiments
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package main | |
| import ( | |
| "sigs.k8s.io/kustomize/v3/pkg/transformers" | |
| "sigs.k8s.io/kustomize/v3/pkg/resmap" | |
| "sigs.k8s.io/kustomize/v3/pkg/transformers/config" | |
| "sigs.k8s.io/kustomize/v3/k8sdeps/kunstruct" | |
| "sigs.k8s.io/kustomize/v3/pkg/gvk" | |
| "sigs.k8s.io/kustomize/v3/pkg/resource" | |
| "fmt" | |
| ) | |
| func main() { | |
| zz := ` | |
| apiVersion: apps/v1 | |
| kind: Deployment | |
| metadata: | |
| name: nginx | |
| labels: | |
| app: nginx | |
| spec: | |
| template: | |
| metadata: | |
| labels: | |
| app: nginx | |
| spec: | |
| containers: | |
| - name: nginx | |
| image: nginx | |
| volumeMounts: | |
| - name: nginx-persistent-storage | |
| mountPath: /tmp/ps | |
| volumes: | |
| - name: $(abc) | |
| emptyDir: {} | |
| - configMap: | |
| name: configmap-in-base | |
| name: configmap-in-base | |
| ` | |
| patch := ` | |
| apiVersion: apps/v1 | |
| kind: Deployment | |
| metadata: | |
| name: nginx | |
| spec: | |
| template: | |
| spec: | |
| containers: | |
| - name: nginx | |
| image: "{{ .Values.image_repo }}imagename:{{ .Values.image_tag }}" | |
| ` | |
| kipl := kunstruct.NewKunstructuredFactoryImpl() | |
| factory := resource.NewFactory(kipl) | |
| res, err := factory.FromBytes([]byte(zz)) | |
| if err != nil { | |
| panic(err) | |
| } | |
| fmt.Println(res) | |
| res2, err := factory.FromBytes([]byte(patch)) | |
| if err != nil { | |
| panic(err) | |
| } | |
| err = res.Patch(res2) | |
| if err != nil { | |
| panic(err) | |
| } | |
| res.SetNamespace("oo1") | |
| transformers.MutateField(res.Map(), | |
| []string{"spec", "template", "metadata", "labels", "lelel"}, | |
| true, | |
| //m.mutate | |
| func (in interface{}) (interface{}, error) { | |
| return "xxx", nil | |
| }) | |
| tr := transformers.NewRefVarTransformer(map[string]interface{}{"abc": "transformed"}, | |
| []config.FieldSpec{{Gvk: gvk.Gvk{Version: "v1", Kind: "Deployment"}, Path: "spec/template/spec/volumes/name"},}) | |
| rm := resmap.New() | |
| fmt.Printf("id: %s\n", res.CurId()) | |
| rm.Append(res) | |
| tr.Transform(rm) | |
| //rm.AsYaml() | |
| yaml, _ := rm.AsYaml() | |
| fmt.Println(string(yaml)) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment