From 83bd9e7deffa9e45531389a5c10c8da9670512b6 Mon Sep 17 00:00:00 2001 From: "Jerry R. Jackson" Date: Tue, 29 Jun 2021 08:06:55 -0600 Subject: [PATCH] Fix status when multiple kustomizations or helm releases share a prefix (#405) * Fix status when multiple kustomizations or helm releases share a prefix * Return an error indicating no app found if we get no matches for the app name * fix status unit test Co-authored-by: Jerry Jackson --- pkg/cmdimpl/status.go | 17 ++++++++++------- pkg/cmdimpl/status_test.go | 34 +++++++++++++++++----------------- 2 files changed, 27 insertions(+), 24 deletions(-) diff --git a/pkg/cmdimpl/status.go b/pkg/cmdimpl/status.go index d17b954b4c..36b0575065 100644 --- a/pkg/cmdimpl/status.go +++ b/pkg/cmdimpl/status.go @@ -24,7 +24,7 @@ type StatusParams struct { func Status(allParams StatusParams) error { deploymentType, err := getDeploymentType(allParams.Namespace, allParams.Name) if err != nil { - return fmt.Errorf("error getting deployment type [%s]", err) + return err } latestDeploymentTime, err := getLatestSuccessfulDeploymentTime(allParams.Namespace, allParams.Name, deploymentType) @@ -53,8 +53,8 @@ type Yaml struct { func getLatestSuccessfulDeploymentTime(namespace, appName string, deploymentType DeploymentType) (string, error) { c := fmt.Sprintf(`kubectl \ - -n %s \ - get %s/%s -oyaml`, + -n %s \ + get %s/%s -oyaml`, namespace, deploymentType, appName, @@ -82,13 +82,16 @@ func getDeploymentType(namespace, appName string) (DeploymentType, error) { return "", err } - var re = regexp.MustCompile(fmt.Sprintf(`(?m)(kustomization|helmrelease)\/%s`, appName)) + var re = regexp.MustCompile(fmt.Sprintf(`(?m)(kustomization|helmrelease)\/%s[[:space:]]`, appName)) matches := re.FindAllStringSubmatch(string(stdout), -1) - if len(matches) != 1 { + switch len(matches) { + case 0: + return "", fmt.Errorf("no app found with name: %s\n", appName) + case 1: + return DeploymentType(matches[0][1]), nil + default: return "", fmt.Errorf("error trying to get the deployment type of the app. raw output => %s", stdout) } - - return DeploymentType(matches[0][1]), nil } diff --git a/pkg/cmdimpl/status_test.go b/pkg/cmdimpl/status_test.go index 46c0cef0da..4695eaa53a 100644 --- a/pkg/cmdimpl/status_test.go +++ b/pkg/cmdimpl/status_test.go @@ -37,20 +37,20 @@ var _ = Describe("Run Command Status Test", func() { // flux mocks case0 := "get all -n wego-system" - output0 := `NAME READY MESSAGE REVISION SUSPENDED -gitrepository/wego True Fetched revision: main/00b92bf6606e040c59404a7257508d65d300bc91 main/00b92bf6606e040c59404a7257508d65d300bc91False -gitrepository/kustomize-app True Fetched revision: main/a2b5b8c0919f405e52619bfc52b5304240d9ef76 main/a2b5b8c0919f405e52619bfc52b5304240d9ef76False + output0 := `NAME READY MESSAGE REVISION SUSPENDED +gitrepository/wego True Fetched revision: main/00b92bf6606e040c59404a7257508d65d300bc91 main/00b92bf6606e040c59404a7257508d65d300bc91False +gitrepository/kustomize-app True Fetched revision: main/a2b5b8c0919f405e52619bfc52b5304240d9ef76 main/a2b5b8c0919f405e52619bfc52b5304240d9ef76False -NAME READY MESSAGE REVISION SUSPENDED -kustomization/wego True Applied revision: main/00b92bf6606e040c59404a7257508d65d300bc91 main/00b92bf6606e040c59404a7257508d65d300bc91False -kustomization/kustomize-app True Applied revision: main/a2b5b8c0919f405e52619bfc52b5304240d9ef76 main/a2b5b8c0919f405e52619bfc52b5304240d9ef76False` +NAME READY MESSAGE REVISION SUSPENDED +kustomization/wego True Applied revision: main/00b92bf6606e040c59404a7257508d65d300bc91 main/00b92bf6606e040c59404a7257508d65d300bc91False +kustomization/kustomize-app True Applied revision: main/a2b5b8c0919f405e52619bfc52b5304240d9ef76 main/a2b5b8c0919f405e52619bfc52b5304240d9ef76False` case1 := "get all -A kustomize-app" - output1 := `NAMESPACE NAME READY MESSAGE REVISION SUSPENDED -wego-system gitrepository/kustomize-app True Fetched revision: main/a2b5b8c0919f405e52619bfc52b5304240d9ef76 main/a2b5b8c0919f405e52619bfc52b5304240d9ef76 False + output1 := `NAMESPACE NAME READY MESSAGE REVISION SUSPENDED +wego-system gitrepository/kustomize-app True Fetched revision: main/a2b5b8c0919f405e52619bfc52b5304240d9ef76 main/a2b5b8c0919f405e52619bfc52b5304240d9ef76 False -NAMESPACE NAME READY MESSAGE REVISION SUSPENDED -wego-system kustomization/kustomize-app True Applied revision: main/a2b5b8c0919f405e52619bfc52b5304240d9ef76 main/a2b5b8c0919f405e52619bfc52b5304240d9ef76 False +NAMESPACE NAME READY MESSAGE REVISION SUSPENDED +wego-system kustomization/kustomize-app True Applied revision: main/a2b5b8c0919f405e52619bfc52b5304240d9ef76 main/a2b5b8c0919f405e52619bfc52b5304240d9ef76 False ` fakeHandler := &fluxopsfakes.FakeFluxHandler{ @@ -77,17 +77,17 @@ wego-system kustomization/kustomize-app True Applied revision: main/a2b5b8c0919 conditions: - lastTransitionTime: "2021-05-24T22:48:28Z"` case1Kubectl := `kubectl \ - -n wego-system \ - get kustomization/kustomize-app -oyaml` + -n wego-system \ + get kustomization/kustomize-app -oyaml` _ = override.WithOverrides(func() override.Result { expectedOutput := `Latest successful deployment time: 2021-05-24T22:48:28Z -NAMESPACE NAME READY MESSAGE REVISION SUSPENDED -wego-system gitrepository/kustomize-app True Fetched revision: main/a2b5b8c0919f405e52619bfc52b5304240d9ef76 main/a2b5b8c0919f405e52619bfc52b5304240d9ef76 False +NAMESPACE NAME READY MESSAGE REVISION SUSPENDED +wego-system gitrepository/kustomize-app True Fetched revision: main/a2b5b8c0919f405e52619bfc52b5304240d9ef76 main/a2b5b8c0919f405e52619bfc52b5304240d9ef76 False -NAMESPACE NAME READY MESSAGE REVISION SUSPENDED -wego-system kustomization/kustomize-app True Applied revision: main/a2b5b8c0919f405e52619bfc52b5304240d9ef76 main/a2b5b8c0919f405e52619bfc52b5304240d9ef76 False +NAMESPACE NAME READY MESSAGE REVISION SUSPENDED +wego-system kustomization/kustomize-app True Applied revision: main/a2b5b8c0919f405e52619bfc52b5304240d9ef76 main/a2b5b8c0919f405e52619bfc52b5304240d9ef76 False ` @@ -167,7 +167,7 @@ var _ = Describe("GetDeployment tests", func() { fluxops.SetFluxHandler(fakeHandler) deploymentType, err := getDeploymentType(ns, myAppName) - Expect(err).Should(MatchError("error trying to get the deployment type of the app. raw output => wronginfo")) + Expect(err).Should(MatchError("no app found with name: my-app-name\n")) Expect(deploymentType).To(BeEmpty()) })