shoebill/internal/utils/kustomize/kustomize.go

65 lines
1.3 KiB
Go
Raw Normal View History

2023-07-20 09:26:25 +00:00
package kustomize
import (
"fmt"
"os"
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 {
Files []string
}
func (k *Kusmtomize) PopulateResources(path string) error {
files, err := os.ReadDir(fmt.Sprintf("%s/src", path))
if err != nil {
return err
}
for _, file := range files {
if file.Name() != ".gitkeep" {
k.Files = append(k.Files, fmt.Sprintf("src/%s", file.Name()))
}
}
return nil
}
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,
}
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
}