2023-07-20 09:26:25 +00:00
|
|
|
package kustomize
|
|
|
|
|
|
|
|
import (
|
2023-10-11 12:14:20 +00:00
|
|
|
"bytes"
|
|
|
|
"errors"
|
2023-07-20 09:26:25 +00:00
|
|
|
"fmt"
|
2023-10-11 12:14:20 +00:00
|
|
|
"html/template"
|
2023-07-20 09:26:25 +00:00
|
|
|
"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-10-11 12:14:20 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
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-10-11 12:14:20 +00:00
|
|
|
Secrets []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-10-11 12:14:20 +00:00
|
|
|
|
|
|
|
// Secrets
|
|
|
|
files, err = os.ReadDir(fmt.Sprintf("%s/src/secrets", path))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, file := range files {
|
|
|
|
k.Secrets = append(k.Secrets, fmt.Sprintf("src/secrets/%s", file.Name()))
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (k *Kusmtomize) SecGeneratorCreate(path string) error {
|
|
|
|
logrus.Info("preparing the secret generator file")
|
|
|
|
genFileTmpl := `---
|
|
|
|
apiVersion: viaduct.ai/v1
|
|
|
|
kind: ksops
|
|
|
|
metadata:
|
|
|
|
name: shoebill-secret-gen
|
|
|
|
files:
|
|
|
|
{{- range $val := . }}
|
|
|
|
- {{ $val }}
|
|
|
|
{{- end }}
|
|
|
|
`
|
|
|
|
|
|
|
|
destFileName := fmt.Sprintf("%s/sec-generator.yaml", path)
|
|
|
|
t := template.Must(template.New("tmpl").Parse(genFileTmpl))
|
|
|
|
var genFileData bytes.Buffer
|
|
|
|
t.Execute(&genFileData, k.Secrets)
|
|
|
|
var genFile *os.File
|
|
|
|
if _, err := os.Stat(destFileName); err == nil {
|
|
|
|
genFile, err := os.Open(destFileName)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer genFile.Close()
|
|
|
|
} else if errors.Is(err, os.ErrNotExist) {
|
|
|
|
genFile, err = os.Create(destFileName)
|
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
defer genFile.Close()
|
|
|
|
} else {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := os.WriteFile(destFileName, genFileData.Bytes(), os.ModeExclusive); err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
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-10-11 12:14:20 +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",
|
|
|
|
},
|
2023-10-20 11:31:30 +00:00
|
|
|
Resources: append(kustomize.Files, kustomize.ConfigMaps...),
|
2023-09-22 11:02:56 +00:00
|
|
|
GeneratorOptions: &kustomize_types.GeneratorOptions{
|
|
|
|
DisableNameSuffixHash: true,
|
|
|
|
},
|
2023-07-20 09:26:25 +00:00
|
|
|
}
|
2023-10-11 13:03:50 +00:00
|
|
|
|
2023-10-11 12:14:20 +00:00
|
|
|
if len(kustomize.Secrets) > 0 {
|
|
|
|
kustomization.Generators = []string{"sec-generator.yaml"}
|
|
|
|
if err := kustomize.SecGeneratorCreate(path); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-10-11 13:03:50 +00:00
|
|
|
} else {
|
|
|
|
if err := os.RemoveAll(fmt.Sprintf("%s/sec-generator.yaml", path)); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-10-11 12:14:20 +00:00
|
|
|
}
|
2023-10-11 13:03:50 +00:00
|
|
|
|
2023-07-20 09:26:25 +00:00
|
|
|
manifest, err := yaml.Marshal(kustomization)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-10-11 13:03:50 +00:00
|
|
|
dstFilePath := path + "/kustomization.yaml"
|
|
|
|
var dstFile *os.File
|
|
|
|
if _, err = os.Stat(dstFilePath); err == nil {
|
|
|
|
dstFile, err = os.Open(dstFilePath)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer dstFile.Close()
|
|
|
|
} else if errors.Is(err, os.ErrNotExist) {
|
|
|
|
dstFile, err = os.Create(dstFilePath)
|
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
defer dstFile.Close()
|
|
|
|
} else {
|
2023-07-20 09:26:25 +00:00
|
|
|
return err
|
|
|
|
}
|
2023-10-11 13:03:50 +00:00
|
|
|
|
|
|
|
if err := os.WriteFile(dstFilePath, manifest, os.ModeExclusive); err != nil {
|
|
|
|
return nil
|
2023-07-20 09:26:25 +00:00
|
|
|
}
|
|
|
|
|
2023-10-20 11:31:30 +00:00
|
|
|
if _, err := gh.AddAllAndCommit(path, "Update the root kustomization"); err != nil {
|
2023-07-20 09:26:25 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|