blob: d21c71dd5255d3dc7f6446b1e9f6d5bf8fc38950 [file] [log] [blame]
gioe72b54f2024-04-22 10:44:41 +04001package io
Giorgi Lekveishvili23ef7f82023-05-26 11:57:48 +04002
3import (
4 "bytes"
Giorgi Lekveishvili0ccd1482023-06-21 15:02:24 +04005 "golang.org/x/exp/slices"
Giorgi Lekveishvili23ef7f82023-05-26 11:57:48 +04006 "io"
Giorgi Lekveishvili23ef7f82023-05-26 11:57:48 +04007
8 "sigs.k8s.io/yaml"
9)
10
11type Kustomization struct {
12 ApiVersion string `json:"apiVersion,omitempty"`
13 Kind string `json:"kind,omitempty"`
14 Resources []string `json:"resources,omitempty"`
15}
16
17func NewKustomization() Kustomization {
18 return Kustomization{
19 ApiVersion: "kustomize.config.k8s.io/v1beta1",
20 Kind: "Kustomization",
21 Resources: []string{},
22 }
23}
24
Giorgi Lekveishvili23ef7f82023-05-26 11:57:48 +040025func (k Kustomization) Write(w io.Writer) error {
26 contents, err := yaml.Marshal(k)
27 if err != nil {
28 return err
29 }
30 if _, err := io.Copy(w, bytes.NewReader(contents)); err != nil {
31 return err
32 }
33 return nil
34}
Giorgi Lekveishvili3550b432023-06-09 19:37:51 +040035
36func (k *Kustomization) AddResources(names ...string) {
Giorgi Lekveishvili0ccd1482023-06-21 15:02:24 +040037 for _, name := range names {
38 if !slices.Contains(k.Resources, name) {
39 k.Resources = append(k.Resources, name)
40 }
41 }
Giorgi Lekveishvili3550b432023-06-09 19:37:51 +040042}
Giorgi Lekveishvili4257b902023-07-07 17:08:42 +040043
44func (k *Kustomization) RemoveResources(names ...string) {
45 for _, name := range names {
46 for i, r := range k.Resources {
47 if r == name {
48 k.Resources = slices.Delete(k.Resources, i, i+1)
49 break
50 }
51 }
52 }
53}