From 3807d5741be6cbe79bdf76cf8ffa3440a006d4dc Mon Sep 17 00:00:00 2001 From: Stefan Benz Date: Thu, 16 Jan 2020 16:07:25 +0100 Subject: [PATCH] fix(kustomize): fixed kustomize step only apply when bool true --- cmd/boom/main.go | 4 +-- .../application/applications/grafana/app.go | 2 +- .../applications/grafanastandalone/app.go | 2 +- internal/bundle/bundle.go | 34 ++++++++++--------- internal/kustomize/kustomize.go | 13 ++++--- 5 files changed, 31 insertions(+), 24 deletions(-) diff --git a/cmd/boom/main.go b/cmd/boom/main.go index 3433797..36240cf 100644 --- a/cmd/boom/main.go +++ b/cmd/boom/main.go @@ -149,7 +149,7 @@ func main() { } if !localMode { - cmd, err := kustomize.New("/crd") + cmd, err := kustomize.New("/crd", true) if err != nil { setupLog.Error(err, "unable to locate crd") os.Exit(1) @@ -190,7 +190,7 @@ func main() { setupLog.Info("starting manager") } else { - cmd, err := kustomize.New("../../config/crd") + cmd, err := kustomize.New("../../config/crd", true) if err != nil { setupLog.Error(err, "unable to locate crd") os.Exit(1) diff --git a/internal/bundle/application/applications/grafana/app.go b/internal/bundle/application/applications/grafana/app.go index 5509aa5..642daee 100644 --- a/internal/bundle/application/applications/grafana/app.go +++ b/internal/bundle/application/applications/grafana/app.go @@ -125,7 +125,7 @@ func getKustomizeOutput(folders []string) ([]string, error) { ret := make([]string, len(folders)) for n, folder := range folders { - cmd, err := kustomize.New(folder) + cmd, err := kustomize.New(folder, false) if err != nil { return nil, err } diff --git a/internal/bundle/application/applications/grafanastandalone/app.go b/internal/bundle/application/applications/grafanastandalone/app.go index 0e8183a..a024293 100644 --- a/internal/bundle/application/applications/grafanastandalone/app.go +++ b/internal/bundle/application/applications/grafanastandalone/app.go @@ -202,7 +202,7 @@ func getKustomizeOutput(folders []string) ([]string, error) { ret := make([]string, len(folders)) for n, folder := range folders { - cmd, err := kustomize.New(folder) + cmd, err := kustomize.New(folder, false) if err != nil { return nil, err } diff --git a/internal/bundle/bundle.go b/internal/bundle/bundle.go index e6fcb2e..f1932bc 100644 --- a/internal/bundle/bundle.go +++ b/internal/bundle/bundle.go @@ -5,6 +5,8 @@ import ( "github.com/caos/boom/internal/bundle/application" "github.com/caos/boom/internal/bundle/bundles" "github.com/caos/boom/internal/bundle/config" + "github.com/caos/boom/internal/helper" + "github.com/caos/boom/internal/kubectl" "github.com/caos/boom/internal/name" "github.com/caos/boom/internal/templator" helperTemp "github.com/caos/boom/internal/templator/helper" @@ -126,22 +128,22 @@ func (b *Bundle) ReconcileApplication(appName name.Application, spec *v1beta1.To b.logger.WithFields(logFields).Info("Reconciling") - // deploy := app.Deploy(spec) - // var command string - // if deploy { - // command = "apply" - // } else if !deploy && app.Changed(spec) && !app.Initial() { - // command = "delete" - // } - - // resultFunc := func(resultFilePath, namespace string) error { - // kubectlCmd := kubectl.New(command).AddParameter("-f", resultFilePath).AddParameter("-n", namespace) - // return errors.Wrapf(helper.Run(b.logger, kubectlCmd.Build()), "Failed to apply with file %s", resultFilePath) - // } - - // if command == "" { - resultFunc := func(resultFilePath, namespace string) error { return nil } - // } + deploy := app.Deploy(spec) + var command string + if deploy { + command = "apply" + } else if !deploy && app.Changed(spec) && !app.Initial() { + command = "delete" + } + + resultFunc := func(resultFilePath, namespace string) error { + kubectlCmd := kubectl.New(command).AddParameter("-f", resultFilePath).AddParameter("-n", namespace) + return errors.Wrapf(helper.Run(b.logger, kubectlCmd.Build()), "Failed to apply with file %s", resultFilePath) + } + + if command == "" { + resultFunc = func(resultFilePath, namespace string) error { return nil } + } b.status = b.Templator.Template(app, spec, resultFunc).GetStatus() if b.status != nil { diff --git a/internal/kustomize/kustomize.go b/internal/kustomize/kustomize.go index 12a3839..206d3b7 100644 --- a/internal/kustomize/kustomize.go +++ b/internal/kustomize/kustomize.go @@ -7,22 +7,27 @@ import ( ) type Kustomize struct { - path string + path string + apply bool } -func New(path string) (*Kustomize, error) { +func New(path string, apply bool) (*Kustomize, error) { abspath, err := filepath.Abs(path) if err != nil { return nil, err } return &Kustomize{ - path: abspath, + path: abspath, + apply: apply, }, nil } func (k *Kustomize) Build() exec.Cmd { - all := strings.Join([]string{"kustomize", "build", k.path, "| kubectl apply -f -"}, " ") + all := strings.Join([]string{"kustomize", "build", k.path}, " ") + if k.apply { + all = strings.Join([]string{all, "| kubectl apply -f -"}, " ") + } cmd := exec.Command("/bin/sh", "-c", all) return *cmd }