Skip to content

Commit

Permalink
Fix status when multiple kustomizations or helm releases share a pref…
Browse files Browse the repository at this point in the history
…ix (#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 <[email protected]>
  • Loading branch information
jrryjcksn and weave-e2e-quickstart authored Jun 29, 2021
1 parent 4f1aa2d commit 83bd9e7
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 24 deletions.
17 changes: 10 additions & 7 deletions pkg/cmdimpl/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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
}
34 changes: 17 additions & 17 deletions pkg/cmdimpl/status_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand All @@ -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
`

Expand Down Expand Up @@ -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())

})
Expand Down

0 comments on commit 83bd9e7

Please sign in to comment.