2023-07-20 09:26:25 +00:00
|
|
|
package kustomize
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
2023-09-22 11:02:56 +00:00
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
2023-07-20 09:26:25 +00:00
|
|
|
|
2023-08-02 15:00:34 +00:00
|
|
|
"git.badhouseplants.net/allanger/shoebill/internal/utils/githelper"
|
2023-07-20 09:26:25 +00:00
|
|
|
kustomize_types "sigs.k8s.io/kustomize/api/types"
|
|
|
|
"sigs.k8s.io/yaml"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Kusmtomize struct {
|
2023-09-22 11:02:56 +00:00
|
|
|
Files []string
|
|
|
|
ConfigMaps []string
|
2023-07-20 09:26:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (k *Kusmtomize) PopulateResources(path string) error {
|
2023-09-22 11:02:56 +00:00
|
|
|
// Main sources
|
2023-07-20 09:26:25 +00:00
|
|
|
files, err := os.ReadDir(fmt.Sprintf("%s/src", path))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for _, file := range files {
|
2023-09-22 11:02:56 +00:00
|
|
|
if file.Name() != ".gitkeep" && !file.IsDir() {
|
2023-07-20 09:26:25 +00:00
|
|
|
k.Files = append(k.Files, fmt.Sprintf("src/%s", file.Name()))
|
|
|
|
}
|
|
|
|
}
|
2023-09-22 11:02:56 +00:00
|
|
|
// 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()))
|
|
|
|
}
|
2023-07-20 09:26:25 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-09-22 11:02:56 +00:00
|
|
|
func (k *Kusmtomize) CmGeneratorFromFiles() ([]kustomize_types.ConfigMapArgs, error) {
|
|
|
|
cmGens := []kustomize_types.ConfigMapArgs{}
|
|
|
|
for _, cm := range k.ConfigMaps {
|
|
|
|
cmName :=
|
|
|
|
cmGen := &kustomize_types.ConfigMapArgs{
|
|
|
|
GeneratorArgs: kustomize_types.GeneratorArgs{
|
|
|
|
Namespace: "flux-system",
|
|
|
|
Name: "",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2023-07-20 09:26:25 +00:00
|
|
|
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.ComponentVersion,
|
|
|
|
},
|
|
|
|
MetaData: &kustomize_types.ObjectMeta{
|
|
|
|
Name: "helm-root",
|
|
|
|
Namespace: "flux-system",
|
|
|
|
},
|
|
|
|
Resources: kustomize.Files,
|
2023-09-22 11:02:56 +00:00
|
|
|
GeneratorOptions: &kustomize_types.GeneratorOptions{
|
|
|
|
DisableNameSuffixHash: true,
|
|
|
|
},
|
|
|
|
ConfigMapGenerator: []kustomize_types.ConfigMapArgs{},
|
2023-07-20 09:26:25 +00:00
|
|
|
}
|
|
|
|
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
|
|
|
|
}
|