package kustomize import ( "fmt" "os" "path/filepath" "git.badhouseplants.net/allanger/shoebill/internal/utils/githelper" kustomize_types "sigs.k8s.io/kustomize/api/types" "sigs.k8s.io/yaml" ) type Kusmtomize struct { Files []string ConfigMaps []string } func (k *Kusmtomize) PopulateResources(path string) error { // Main sources files, err := os.ReadDir(fmt.Sprintf("%s/src", path)) if err != nil { return err } for _, file := range files { if file.Name() != ".gitkeep" && !file.IsDir() { k.Files = append(k.Files, fmt.Sprintf("src/%s", file.Name())) } } // Values files, err = os.ReadDir(fmt.Sprintf("%s/src/values", path)) if err != nil { return err } for _, file := range files { k.ConfigMaps = append(k.ConfigMaps, fmt.Sprintf("src/values/%s", file.Name())) } return nil } func (k *Kusmtomize) CmGeneratorFromFiles() []kustomize_types.ConfigMapArgs { cmGens := []kustomize_types.ConfigMapArgs{} for _, cm := range k.ConfigMaps { cmName := filepath.Base(cm) cmGen := &kustomize_types.ConfigMapArgs{ GeneratorArgs: kustomize_types.GeneratorArgs{ Namespace: "flux-system", Name: cmName, KvPairSources: kustomize_types.KvPairSources{ FileSources: []string{cm}, }, }, } cmGens = append(cmGens, *cmGen) } return cmGens } func Generate(path string, gh githelper.Githelper) error { kustomize := &Kusmtomize{} if err := kustomize.PopulateResources(path); err != nil { return err } kustomization := kustomize_types.Kustomization{ TypeMeta: kustomize_types.TypeMeta{ Kind: kustomize_types.KustomizationKind, APIVersion: kustomize_types.KustomizationVersion, }, MetaData: &kustomize_types.ObjectMeta{ Name: "helm-root", Namespace: "flux-system", }, Resources: kustomize.Files, GeneratorOptions: &kustomize_types.GeneratorOptions{ DisableNameSuffixHash: true, }, ConfigMapGenerator: kustomize.CmGeneratorFromFiles(), } manifest, err := yaml.Marshal(kustomization) if err != nil { return err } file, err := os.Create(path + "/kustomization.yaml") if err != nil { return err } if _, err := file.Write(manifest); err != nil { return err } if err := gh.AddAllAndCommit(path, "Update the root kustomization"); err != nil { return err } return nil }