shoebill/internal/utils/kustomize/kustomize.go

98 lines
2.2 KiB
Go
Raw Normal View History

2023-07-20 09:26:25 +00:00
package kustomize
import (
"fmt"
"os"
2023-09-22 11:02:56 +00:00
"path/filepath"
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-25 08:36:04 +00:00
func (k *Kusmtomize) CmGeneratorFromFiles() []kustomize_types.ConfigMapArgs {
2023-09-22 11:02:56 +00:00
cmGens := []kustomize_types.ConfigMapArgs{}
for _, cm := range k.ConfigMaps {
2023-09-25 08:36:04 +00:00
cmName := filepath.Base(cm)
2023-09-22 11:02:56 +00:00
cmGen := &kustomize_types.ConfigMapArgs{
GeneratorArgs: kustomize_types.GeneratorArgs{
Namespace: "flux-system",
2023-09-25 08:36:04 +00:00
Name: cmName,
KvPairSources: kustomize_types.KvPairSources{
FileSources: []string{cm},
},
2023-09-22 11:02:56 +00:00
},
}
2023-09-25 08:36:04 +00:00
cmGens = append(cmGens, *cmGen)
2023-09-22 11:02:56 +00:00
}
2023-09-25 08:36:04 +00:00
return cmGens
2023-09-22 11:02:56 +00:00
}
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,
2023-09-25 08:36:04 +00:00
APIVersion: kustomize_types.KustomizationVersion,
2023-07-20 09:26:25 +00:00
},
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,
},
2023-09-25 08:36:04 +00:00
ConfigMapGenerator: kustomize.CmGeneratorFromFiles(),
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
}