Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: raise error if app dest namespace not managed by argocd (#4329) #4352

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions util/argo/argo.go
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,7 @@ func enrichSpec(spec *argoappv1.ApplicationSpec, appDetails *apiclient.RepoAppDe
// ValidateDestination checks:
// if we used destination name we infer the server url
// if we used both name and server then we return an invalid spec error
// if we specified a namespace that is not managed by argo, then we return an invalid spec error
func ValidateDestination(ctx context.Context, dest *argoappv1.ApplicationDestination, db db.ArgoDB) error {
if dest.Name != "" {
if dest.Server == "" {
Expand All @@ -252,6 +253,25 @@ func ValidateDestination(ctx context.Context, dest *argoappv1.ApplicationDestina
}
}
}

cluster, err := db.GetCluster(ctx, dest.Server)
if err != nil {
return fmt.Errorf("unable to find cluster for destination server: %s: %v", dest.Server, err)
}
if len(cluster.Namespaces) > 0 {
found := false
for _, v := range cluster.Namespaces {
if v == dest.Namespace {
// don't return nil here, otherwise if other checks are added after, this code would need to be changed
found = true
break
}
}
if !found {
return fmt.Errorf("application destination namespace is not managed by ArgoCD: %s", dest.Namespace)
}
}

return nil
}

Expand Down
73 changes: 72 additions & 1 deletion util/argo/argo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -557,7 +557,13 @@ func TestValidateDestination(t *testing.T) {
Namespace: "default",
}

appCond := ValidateDestination(context.Background(), &dest, nil)
db := &dbmocks.ArgoDB{}
db.On("GetCluster", context.Background(), "https://127.0.0.1:6443").Return(&argoappv1.Cluster{
Name: "minikube",
Server: "https://127.0.0.1:6443",
}, nil)

appCond := ValidateDestination(context.Background(), &dest, db)
assert.Nil(t, appCond)
assert.False(t, dest.IsServerInferred())
})
Expand All @@ -577,6 +583,11 @@ func TestValidateDestination(t *testing.T) {
},
}, nil)

db.On("GetCluster", context.Background(), "https://127.0.0.1:6443").Return(&argoappv1.Cluster{
Name: "minikube",
Server: "https://127.0.0.1:6443",
}, nil)

appCond := ValidateDestination(context.Background(), &dest, db)
assert.Nil(t, appCond)
assert.Equal(t, "https://127.0.0.1:6443", dest.Server)
Expand Down Expand Up @@ -652,4 +663,64 @@ func TestValidateDestination(t *testing.T) {
assert.False(t, dest.IsServerInferred())
})

t.Run("Validate cluster manages all namespaces", func(t *testing.T) {
dest := argoappv1.ApplicationDestination{
Server: "https://127.0.0.1:6443",
Namespace: "default",
}

db := &dbmocks.ArgoDB{}
db.On("GetCluster", context.Background(), "https://127.0.0.1:6443").Return(&argoappv1.Cluster{
Name: "minikube",
Server: "https://127.0.0.1:6443",
Namespaces: nil,
}, nil)

appCond := ValidateDestination(context.Background(), &dest, db)
assert.Nil(t, appCond)

db = &dbmocks.ArgoDB{}
db.On("GetCluster", context.Background(), "https://127.0.0.1:6443").Return(&argoappv1.Cluster{
Name: "minikube",
Server: "https://127.0.0.1:6443",
Namespaces: []string{},
}, nil)

appCond = ValidateDestination(context.Background(), &dest, db)
assert.Nil(t, appCond)
})

t.Run("Validate destination namespace in cluster managed namespaces", func(t *testing.T) {
dest := argoappv1.ApplicationDestination{
Server: "https://127.0.0.1:6443",
Namespace: "develop",
}

db := &dbmocks.ArgoDB{}
db.On("GetCluster", context.Background(), "https://127.0.0.1:6443").Return(&argoappv1.Cluster{
Name: "minikube",
Server: "https://127.0.0.1:6443",
Namespaces: []string{"default", "develop"},
}, nil)

appCond := ValidateDestination(context.Background(), &dest, db)
assert.Nil(t, appCond)
})

t.Run("Validate destination namespace not in cluster managed namespaces", func(t *testing.T) {
dest := argoappv1.ApplicationDestination{
Server: "https://127.0.0.1:6443",
Namespace: "develop",
}

db := &dbmocks.ArgoDB{}
db.On("GetCluster", context.Background(), "https://127.0.0.1:6443").Return(&argoappv1.Cluster{
Name: "minikube",
Server: "https://127.0.0.1:6443",
Namespaces: []string{"default"},
}, nil)

err := ValidateDestination(context.Background(), &dest, db)
assert.Equal(t, "application destination namespace is not managed by ArgoCD: develop", err.Error())
})
}