From 2373bacb0c6042737a099a61a543f2e2b45850a9 Mon Sep 17 00:00:00 2001 From: stefanprodan Date: Mon, 28 Sep 2020 18:01:47 +0300 Subject: [PATCH] Generate a kustomize package for the bootstrap manifests --- cmd/gotk/bootstrap.go | 6 ++- cmd/gotk/utils.go | 87 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 92 insertions(+), 1 deletion(-) diff --git a/cmd/gotk/bootstrap.go b/cmd/gotk/bootstrap.go index b0c8b0b8..95e36aff 100644 --- a/cmd/gotk/bootstrap.go +++ b/cmd/gotk/bootstrap.go @@ -209,11 +209,15 @@ func generateSyncManifests(url, branch, name, namespace, targetPath, tmpDir stri return err } + if err := utils.generateKustomizationYaml(filepath.Join(tmpDir, targetPath, namespace)); err != nil { + return err + } + return nil } func applySyncManifests(ctx context.Context, kubeClient client.Client, name, namespace, targetPath, tmpDir string) error { - command := fmt.Sprintf("kubectl apply -f %s", filepath.Join(tmpDir, targetPath, namespace)) + command := fmt.Sprintf("kubectl apply -k %s", filepath.Join(tmpDir, targetPath, namespace)) if _, err := utils.execCommand(ctx, ModeStderrOS, command); err != nil { return err } diff --git a/cmd/gotk/utils.go b/cmd/gotk/utils.go index 17a5a386..0712bde8 100644 --- a/cmd/gotk/utils.go +++ b/cmd/gotk/utils.go @@ -22,8 +22,10 @@ import ( "context" "fmt" "io" + "io/ioutil" "os" "os/exec" + "path/filepath" "strings" "text/template" @@ -32,6 +34,11 @@ import ( "k8s.io/apimachinery/pkg/runtime" "k8s.io/client-go/tools/clientcmd" "sigs.k8s.io/controller-runtime/pkg/client" + "sigs.k8s.io/kustomize/api/filesys" + "sigs.k8s.io/kustomize/api/k8sdeps/kunstruct" + "sigs.k8s.io/kustomize/api/konfig" + kustypes "sigs.k8s.io/kustomize/api/types" + "sigs.k8s.io/yaml" helmv2 "github.com/fluxcd/helm-controller/api/v2alpha1" kustomizev1 "github.com/fluxcd/kustomize-controller/api/v1alpha1" @@ -210,3 +217,83 @@ func (*Utils) makeDependsOn(deps []string) []dependency.CrossNamespaceDependency } return refs } + +// generateKustomizationYaml is the equivalent of running +// 'kustomize create --autodetect' in the specified dir +func (*Utils) generateKustomizationYaml(dirPath string) error { + fs := filesys.MakeFsOnDisk() + kfile := filepath.Join(dirPath, "kustomization.yaml") + + scan := func(base string) ([]string, error) { + var paths []string + uf := kunstruct.NewKunstructuredFactoryImpl() + err := fs.Walk(base, func(path string, info os.FileInfo, err error) error { + if err != nil { + return err + } + if path == base { + return nil + } + if info.IsDir() { + // If a sub-directory contains an existing kustomization file add the + // directory as a resource and do not decend into it. + for _, kfilename := range konfig.RecognizedKustomizationFileNames() { + if fs.Exists(filepath.Join(path, kfilename)) { + paths = append(paths, path) + return filepath.SkipDir + } + } + return nil + } + fContents, err := fs.ReadFile(path) + if err != nil { + return err + } + if _, err := uf.SliceFromBytes(fContents); err != nil { + return nil + } + paths = append(paths, path) + return nil + }) + return paths, err + } + + if _, err := os.Stat(kfile); err != nil { + abs, err := filepath.Abs(dirPath) + if err != nil { + return err + } + + files, err := scan(abs) + if err != nil { + return err + } + + f, err := fs.Create(kfile) + if err != nil { + return err + } + f.Close() + + kus := kustypes.Kustomization{ + TypeMeta: kustypes.TypeMeta{ + APIVersion: kustypes.KustomizationVersion, + Kind: kustypes.KustomizationKind, + }, + } + + var resources []string + for _, file := range files { + resources = append(resources, strings.Replace(file, abs, ".", 1)) + } + + kus.Resources = resources + kd, err := yaml.Marshal(kus) + if err != nil { + return err + } + + return ioutil.WriteFile(kfile, kd, os.ModePerm) + } + return nil +}