|
| 1 | +package poddisruptionbudget |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "fmt" |
| 6 | + "io" |
| 7 | + "strings" |
| 8 | + |
| 9 | + "github.com/arttor/helmify/pkg/processor" |
| 10 | + |
| 11 | + "github.com/arttor/helmify/pkg/helmify" |
| 12 | + yamlformat "github.com/arttor/helmify/pkg/yaml" |
| 13 | + "github.com/iancoleman/strcase" |
| 14 | + policyv1 "k8s.io/api/policy/v1" |
| 15 | + "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" |
| 16 | + "k8s.io/apimachinery/pkg/runtime" |
| 17 | + "k8s.io/apimachinery/pkg/runtime/schema" |
| 18 | + "sigs.k8s.io/yaml" |
| 19 | +) |
| 20 | + |
| 21 | +const ( |
| 22 | + pdbTempSpec = ` |
| 23 | +spec: |
| 24 | + minAvailable: {{ .Values.%[1]s.minAvailable }} |
| 25 | + maxUnavailable: {{ .Values.%[1]s.maxUnavailable }} |
| 26 | + selector: |
| 27 | +%[2]s |
| 28 | + {{- include "%[3]s.selectorLabels" . | nindent 6 }}` |
| 29 | +) |
| 30 | + |
| 31 | +var pdbGVC = schema.GroupVersionKind{ |
| 32 | + Group: "policy", |
| 33 | + Version: "v1", |
| 34 | + Kind: "PodDisruptionBudget", |
| 35 | +} |
| 36 | + |
| 37 | +// New creates processor for k8s Service resource. |
| 38 | +func New() helmify.Processor { |
| 39 | + return &pdb{} |
| 40 | +} |
| 41 | + |
| 42 | +type pdb struct{} |
| 43 | + |
| 44 | +// Process k8s Service object into template. Returns false if not capable of processing given resource type. |
| 45 | +func (r pdb) Process(appMeta helmify.AppMetadata, obj *unstructured.Unstructured) (bool, helmify.Template, error) { |
| 46 | + if obj.GroupVersionKind() != pdbGVC { |
| 47 | + return false, nil, nil |
| 48 | + } |
| 49 | + pdb := policyv1.PodDisruptionBudget{} |
| 50 | + err := runtime.DefaultUnstructuredConverter.FromUnstructured(obj.Object, &pdb) |
| 51 | + if err != nil { |
| 52 | + return true, nil, fmt.Errorf("%w: unable to cast to pdb", err) |
| 53 | + } |
| 54 | + spec := pdb.Spec |
| 55 | + values := helmify.Values{} |
| 56 | + |
| 57 | + meta, err := processor.ProcessObjMeta(appMeta, obj) |
| 58 | + if err != nil { |
| 59 | + return true, nil, err |
| 60 | + } |
| 61 | + |
| 62 | + name := appMeta.TrimName(obj.GetName()) |
| 63 | + shortName := strings.TrimPrefix(name, "controller-manager-") |
| 64 | + shortNameCamel := strcase.ToLowerCamel(shortName) |
| 65 | + |
| 66 | + selector, _ := yaml.Marshal(pdb.Spec.Selector) |
| 67 | + selector = yamlformat.Indent(selector, 4) |
| 68 | + selector = bytes.TrimRight(selector, "\n ") |
| 69 | + |
| 70 | + if spec.MaxUnavailable != nil { |
| 71 | + _, err := values.Add(spec.MaxUnavailable.IntValue(), shortNameCamel, "maxUnavailable") |
| 72 | + if err != nil { |
| 73 | + return true, nil, err |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + if spec.MinAvailable != nil { |
| 78 | + _, err := values.Add(spec.MinAvailable.IntValue(), shortNameCamel, "minAvailable") |
| 79 | + if err != nil { |
| 80 | + return true, nil, err |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + res := meta + fmt.Sprintf(pdbTempSpec, shortNameCamel, selector, appMeta.ChartName()) |
| 85 | + return true, &result{ |
| 86 | + name: shortName, |
| 87 | + data: res, |
| 88 | + values: values, |
| 89 | + }, nil |
| 90 | +} |
| 91 | + |
| 92 | +type result struct { |
| 93 | + name string |
| 94 | + data string |
| 95 | + values helmify.Values |
| 96 | +} |
| 97 | + |
| 98 | +func (r *result) Filename() string { |
| 99 | + return r.name + ".yaml" |
| 100 | +} |
| 101 | + |
| 102 | +func (r *result) Values() helmify.Values { |
| 103 | + return r.values |
| 104 | +} |
| 105 | + |
| 106 | +func (r *result) Write(writer io.Writer) error { |
| 107 | + _, err := writer.Write([]byte(r.data)) |
| 108 | + return err |
| 109 | +} |
0 commit comments