|
| 1 | +package statefulset |
| 2 | + |
| 3 | +import ( |
| 4 | + "github.com/arttor/helmify/pkg/processor/pod" |
| 5 | + "io" |
| 6 | + "strings" |
| 7 | + "text/template" |
| 8 | + |
| 9 | + "github.com/arttor/helmify/pkg/helmify" |
| 10 | + "github.com/arttor/helmify/pkg/processor" |
| 11 | + yamlformat "github.com/arttor/helmify/pkg/yaml" |
| 12 | + "github.com/iancoleman/strcase" |
| 13 | + "github.com/pkg/errors" |
| 14 | + appsv1 "k8s.io/api/apps/v1" |
| 15 | + "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" |
| 16 | + "k8s.io/apimachinery/pkg/runtime" |
| 17 | + "k8s.io/apimachinery/pkg/runtime/schema" |
| 18 | +) |
| 19 | + |
| 20 | +var statefulsetGVC = schema.GroupVersionKind{ |
| 21 | + Group: "apps", |
| 22 | + Version: "v1", |
| 23 | + Kind: "StatefulSet", |
| 24 | +} |
| 25 | + |
| 26 | +var statefulsetTempl, _ = template.New("statefulset").Parse( |
| 27 | + `{{- .Meta }} |
| 28 | +spec: |
| 29 | +{{ .Spec }}`) |
| 30 | + |
| 31 | +// New creates processor for k8s StatefulSet resource. |
| 32 | +func New() helmify.Processor { |
| 33 | + return &statefulset{} |
| 34 | +} |
| 35 | + |
| 36 | +type statefulset struct{} |
| 37 | + |
| 38 | +// Process k8s StatefulSet object into template. Returns false if not capable of processing given resource type. |
| 39 | +func (d statefulset) Process(appMeta helmify.AppMetadata, obj *unstructured.Unstructured) (bool, helmify.Template, error) { |
| 40 | + if obj.GroupVersionKind() != statefulsetGVC { |
| 41 | + return false, nil, nil |
| 42 | + } |
| 43 | + ss := appsv1.StatefulSet{} |
| 44 | + err := runtime.DefaultUnstructuredConverter.FromUnstructured(obj.Object, &ss) |
| 45 | + if err != nil { |
| 46 | + return true, nil, errors.Wrap(err, "unable to cast to StatefulSet") |
| 47 | + } |
| 48 | + meta, err := processor.ProcessObjMeta(appMeta, obj) |
| 49 | + if err != nil { |
| 50 | + return true, nil, err |
| 51 | + } |
| 52 | + |
| 53 | + ssSpec := ss.Spec |
| 54 | + ssSpecMap, err := runtime.DefaultUnstructuredConverter.ToUnstructured(&ssSpec) |
| 55 | + if err != nil { |
| 56 | + return true, nil, err |
| 57 | + } |
| 58 | + delete((ssSpecMap["template"].(map[string]interface{}))["metadata"].(map[string]interface{}), "creationTimestamp") |
| 59 | + |
| 60 | + values := helmify.Values{} |
| 61 | + |
| 62 | + name := appMeta.TrimName(obj.GetName()) |
| 63 | + nameCamel := strcase.ToLowerCamel(name) |
| 64 | + |
| 65 | + if ssSpec.ServiceName != "" { |
| 66 | + servName := appMeta.TemplatedName(ssSpec.ServiceName) |
| 67 | + ssSpecMap["serviceName"] = servName |
| 68 | + } |
| 69 | + |
| 70 | + if ssSpec.Replicas != nil { |
| 71 | + repl, err := values.Add(*ssSpec.Replicas, nameCamel, "replicas") |
| 72 | + if err != nil { |
| 73 | + return true, nil, err |
| 74 | + } |
| 75 | + ssSpecMap["replicas"] = repl |
| 76 | + } |
| 77 | + |
| 78 | + for i, claim := range ssSpec.VolumeClaimTemplates { |
| 79 | + volName := claim.ObjectMeta.Name |
| 80 | + delete(((ssSpecMap["volumeClaimTemplates"].([]interface{}))[i]).(map[string]interface{}), "status") |
| 81 | + if claim.Spec.StorageClassName != nil { |
| 82 | + scName := appMeta.TemplatedName(*claim.Spec.StorageClassName) |
| 83 | + err = unstructured.SetNestedField(((ssSpecMap["volumeClaimTemplates"].([]interface{}))[i]).(map[string]interface{}), scName, "spec", "storageClassName") |
| 84 | + if err != nil { |
| 85 | + return true, nil, err |
| 86 | + } |
| 87 | + } |
| 88 | + if claim.Spec.VolumeName != "" { |
| 89 | + vName := appMeta.TemplatedName(claim.Spec.VolumeName) |
| 90 | + err = unstructured.SetNestedField(((ssSpecMap["volumeClaimTemplates"].([]interface{}))[i]).(map[string]interface{}), vName, "spec", "volumeName") |
| 91 | + if err != nil { |
| 92 | + return true, nil, err |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + resMap, err := runtime.DefaultUnstructuredConverter.ToUnstructured(&claim.Spec.Resources) |
| 97 | + if err != nil { |
| 98 | + return true, nil, err |
| 99 | + } |
| 100 | + resName, err := values.AddYaml(resMap, 8, true, nameCamel, "volumeClaims", volName) |
| 101 | + if err != nil { |
| 102 | + return true, nil, err |
| 103 | + } |
| 104 | + err = unstructured.SetNestedField(((ssSpecMap["volumeClaimTemplates"].([]interface{}))[i]).(map[string]interface{}), resName, "spec", "resources") |
| 105 | + if err != nil { |
| 106 | + return true, nil, err |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + // process pod spec: |
| 111 | + podSpecMap, podValues, err := pod.ProcessSpec(nameCamel, appMeta, ssSpec.Template.Spec) |
| 112 | + if err != nil { |
| 113 | + return true, nil, err |
| 114 | + } |
| 115 | + err = values.Merge(podValues) |
| 116 | + if err != nil { |
| 117 | + return true, nil, err |
| 118 | + } |
| 119 | + err = unstructured.SetNestedMap(ssSpecMap, podSpecMap, "template", "spec") |
| 120 | + if err != nil { |
| 121 | + return true, nil, err |
| 122 | + } |
| 123 | + |
| 124 | + spec, err := yamlformat.Marshal(ssSpecMap, 2) |
| 125 | + if err != nil { |
| 126 | + return true, nil, err |
| 127 | + } |
| 128 | + spec = strings.ReplaceAll(spec, "'", "") |
| 129 | + |
| 130 | + return true, &result{ |
| 131 | + values: values, |
| 132 | + data: struct { |
| 133 | + Meta string |
| 134 | + Spec string |
| 135 | + }{ |
| 136 | + Meta: meta, |
| 137 | + Spec: spec, |
| 138 | + }, |
| 139 | + }, nil |
| 140 | +} |
| 141 | + |
| 142 | +type result struct { |
| 143 | + data struct { |
| 144 | + Meta string |
| 145 | + Spec string |
| 146 | + } |
| 147 | + values helmify.Values |
| 148 | +} |
| 149 | + |
| 150 | +func (r *result) Filename() string { |
| 151 | + return "statefulset.yaml" |
| 152 | +} |
| 153 | + |
| 154 | +func (r *result) Values() helmify.Values { |
| 155 | + return r.values |
| 156 | +} |
| 157 | + |
| 158 | +func (r *result) Write(writer io.Writer) error { |
| 159 | + return statefulsetTempl.Execute(writer, r.data) |
| 160 | +} |
0 commit comments