Skip to content

Commit 83bd9e7

Browse files
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 <[email protected]>
1 parent 4f1aa2d commit 83bd9e7

File tree

2 files changed

+27
-24
lines changed

2 files changed

+27
-24
lines changed

pkg/cmdimpl/status.go

+10-7
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ type StatusParams struct {
2424
func Status(allParams StatusParams) error {
2525
deploymentType, err := getDeploymentType(allParams.Namespace, allParams.Name)
2626
if err != nil {
27-
return fmt.Errorf("error getting deployment type [%s]", err)
27+
return err
2828
}
2929

3030
latestDeploymentTime, err := getLatestSuccessfulDeploymentTime(allParams.Namespace, allParams.Name, deploymentType)
@@ -53,8 +53,8 @@ type Yaml struct {
5353

5454
func getLatestSuccessfulDeploymentTime(namespace, appName string, deploymentType DeploymentType) (string, error) {
5555
c := fmt.Sprintf(`kubectl \
56-
-n %s \
57-
get %s/%s -oyaml`,
56+
-n %s \
57+
get %s/%s -oyaml`,
5858
namespace,
5959
deploymentType,
6060
appName,
@@ -82,13 +82,16 @@ func getDeploymentType(namespace, appName string) (DeploymentType, error) {
8282
return "", err
8383
}
8484

85-
var re = regexp.MustCompile(fmt.Sprintf(`(?m)(kustomization|helmrelease)\/%s`, appName))
85+
var re = regexp.MustCompile(fmt.Sprintf(`(?m)(kustomization|helmrelease)\/%s[[:space:]]`, appName))
8686

8787
matches := re.FindAllStringSubmatch(string(stdout), -1)
8888

89-
if len(matches) != 1 {
89+
switch len(matches) {
90+
case 0:
91+
return "", fmt.Errorf("no app found with name: %s\n", appName)
92+
case 1:
93+
return DeploymentType(matches[0][1]), nil
94+
default:
9095
return "", fmt.Errorf("error trying to get the deployment type of the app. raw output => %s", stdout)
9196
}
92-
93-
return DeploymentType(matches[0][1]), nil
9497
}

pkg/cmdimpl/status_test.go

+17-17
Original file line numberDiff line numberDiff line change
@@ -37,20 +37,20 @@ var _ = Describe("Run Command Status Test", func() {
3737

3838
// flux mocks
3939
case0 := "get all -n wego-system"
40-
output0 := `NAME READY MESSAGE REVISION SUSPENDED
41-
gitrepository/wego True Fetched revision: main/00b92bf6606e040c59404a7257508d65d300bc91 main/00b92bf6606e040c59404a7257508d65d300bc91False
42-
gitrepository/kustomize-app True Fetched revision: main/a2b5b8c0919f405e52619bfc52b5304240d9ef76 main/a2b5b8c0919f405e52619bfc52b5304240d9ef76False
40+
output0 := `NAME READY MESSAGE REVISION SUSPENDED
41+
gitrepository/wego True Fetched revision: main/00b92bf6606e040c59404a7257508d65d300bc91 main/00b92bf6606e040c59404a7257508d65d300bc91False
42+
gitrepository/kustomize-app True Fetched revision: main/a2b5b8c0919f405e52619bfc52b5304240d9ef76 main/a2b5b8c0919f405e52619bfc52b5304240d9ef76False
4343
44-
NAME READY MESSAGE REVISION SUSPENDED
45-
kustomization/wego True Applied revision: main/00b92bf6606e040c59404a7257508d65d300bc91 main/00b92bf6606e040c59404a7257508d65d300bc91False
46-
kustomization/kustomize-app True Applied revision: main/a2b5b8c0919f405e52619bfc52b5304240d9ef76 main/a2b5b8c0919f405e52619bfc52b5304240d9ef76False`
44+
NAME READY MESSAGE REVISION SUSPENDED
45+
kustomization/wego True Applied revision: main/00b92bf6606e040c59404a7257508d65d300bc91 main/00b92bf6606e040c59404a7257508d65d300bc91False
46+
kustomization/kustomize-app True Applied revision: main/a2b5b8c0919f405e52619bfc52b5304240d9ef76 main/a2b5b8c0919f405e52619bfc52b5304240d9ef76False`
4747

4848
case1 := "get all -A kustomize-app"
49-
output1 := `NAMESPACE NAME READY MESSAGE REVISION SUSPENDED
50-
wego-system gitrepository/kustomize-app True Fetched revision: main/a2b5b8c0919f405e52619bfc52b5304240d9ef76 main/a2b5b8c0919f405e52619bfc52b5304240d9ef76 False
49+
output1 := `NAMESPACE NAME READY MESSAGE REVISION SUSPENDED
50+
wego-system gitrepository/kustomize-app True Fetched revision: main/a2b5b8c0919f405e52619bfc52b5304240d9ef76 main/a2b5b8c0919f405e52619bfc52b5304240d9ef76 False
5151
52-
NAMESPACE NAME READY MESSAGE REVISION SUSPENDED
53-
wego-system kustomization/kustomize-app True Applied revision: main/a2b5b8c0919f405e52619bfc52b5304240d9ef76 main/a2b5b8c0919f405e52619bfc52b5304240d9ef76 False
52+
NAMESPACE NAME READY MESSAGE REVISION SUSPENDED
53+
wego-system kustomization/kustomize-app True Applied revision: main/a2b5b8c0919f405e52619bfc52b5304240d9ef76 main/a2b5b8c0919f405e52619bfc52b5304240d9ef76 False
5454
`
5555

5656
fakeHandler := &fluxopsfakes.FakeFluxHandler{
@@ -77,17 +77,17 @@ wego-system kustomization/kustomize-app True Applied revision: main/a2b5b8c0919
7777
conditions:
7878
- lastTransitionTime: "2021-05-24T22:48:28Z"`
7979
case1Kubectl := `kubectl \
80-
-n wego-system \
81-
get kustomization/kustomize-app -oyaml`
80+
-n wego-system \
81+
get kustomization/kustomize-app -oyaml`
8282

8383
_ = override.WithOverrides(func() override.Result {
8484

8585
expectedOutput := `Latest successful deployment time: 2021-05-24T22:48:28Z
86-
NAMESPACE NAME READY MESSAGE REVISION SUSPENDED
87-
wego-system gitrepository/kustomize-app True Fetched revision: main/a2b5b8c0919f405e52619bfc52b5304240d9ef76 main/a2b5b8c0919f405e52619bfc52b5304240d9ef76 False
86+
NAMESPACE NAME READY MESSAGE REVISION SUSPENDED
87+
wego-system gitrepository/kustomize-app True Fetched revision: main/a2b5b8c0919f405e52619bfc52b5304240d9ef76 main/a2b5b8c0919f405e52619bfc52b5304240d9ef76 False
8888
89-
NAMESPACE NAME READY MESSAGE REVISION SUSPENDED
90-
wego-system kustomization/kustomize-app True Applied revision: main/a2b5b8c0919f405e52619bfc52b5304240d9ef76 main/a2b5b8c0919f405e52619bfc52b5304240d9ef76 False
89+
NAMESPACE NAME READY MESSAGE REVISION SUSPENDED
90+
wego-system kustomization/kustomize-app True Applied revision: main/a2b5b8c0919f405e52619bfc52b5304240d9ef76 main/a2b5b8c0919f405e52619bfc52b5304240d9ef76 False
9191
9292
`
9393

@@ -167,7 +167,7 @@ var _ = Describe("GetDeployment tests", func() {
167167
fluxops.SetFluxHandler(fakeHandler)
168168

169169
deploymentType, err := getDeploymentType(ns, myAppName)
170-
Expect(err).Should(MatchError("error trying to get the deployment type of the app. raw output => wronginfo"))
170+
Expect(err).Should(MatchError("no app found with name: my-app-name\n"))
171171
Expect(deploymentType).To(BeEmpty())
172172

173173
})

0 commit comments

Comments
 (0)