Skip to content

Commit 8d03743

Browse files
authored
fix(cluster): crash on empty namespace (#198)
1 parent 408af72 commit 8d03743

File tree

2 files changed

+60
-0
lines changed

2 files changed

+60
-0
lines changed

argocd/resource_argocd_cluster_test.go

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,25 @@ func TestAccArgoCDCluster_uniqueByServerTrimmed(t *testing.T) {
370370
})
371371
}
372372

373+
func TestAccArgoCDCluster_namespacesErrorWhenEmpty(t *testing.T) {
374+
name := acctest.RandString(10)
375+
376+
resource.Test(t, resource.TestCase{
377+
PreCheck: func() { testAccPreCheck(t); testAccPreCheckFeatureSupported(t, featureProjectScopedClusters) },
378+
ProviderFactories: testAccProviders,
379+
Steps: []resource.TestStep{
380+
{
381+
Config: testAccArgoCDClusterNamespacesContainsEmptyString(name),
382+
ExpectError: regexp.MustCompile("namespaces: must contain non-empty strings"),
383+
},
384+
{
385+
Config: testAccArgoCDClusterNamespacesContainsEmptyString_MultipleItems(name),
386+
ExpectError: regexp.MustCompile("namespaces: must contain non-empty strings"),
387+
},
388+
},
389+
})
390+
}
391+
373392
func testAccArgoCDClusterBearerToken(clusterName string) string {
374393
return fmt.Sprintf(`
375394
resource "argocd_cluster" "simple" {
@@ -610,6 +629,42 @@ resource "argocd_cluster" "cluster_metadata" {
610629
`, clusterName)
611630
}
612631

632+
func testAccArgoCDClusterNamespacesContainsEmptyString(clusterName string) string {
633+
return fmt.Sprintf(`
634+
resource "argocd_cluster" "simple" {
635+
server = "https://kubernetes.default.svc.cluster.local"
636+
name = "%s"
637+
shard = "1"
638+
namespaces = [""]
639+
config {
640+
# Uses Kind's bootstrap token whose ttl is 24 hours after cluster bootstrap.
641+
bearer_token = "abcdef.0123456789abcdef"
642+
tls_client_config {
643+
insecure = true
644+
}
645+
}
646+
}
647+
`, clusterName)
648+
}
649+
650+
func testAccArgoCDClusterNamespacesContainsEmptyString_MultipleItems(clusterName string) string {
651+
return fmt.Sprintf(`
652+
resource "argocd_cluster" "simple" {
653+
server = "https://kubernetes.default.svc.cluster.local"
654+
name = "%s"
655+
shard = "1"
656+
namespaces = ["default", ""]
657+
config {
658+
# Uses Kind's bootstrap token whose ttl is 24 hours after cluster bootstrap.
659+
bearer_token = "abcdef.0123456789abcdef"
660+
tls_client_config {
661+
insecure = true
662+
}
663+
}
664+
}
665+
`, clusterName)
666+
}
667+
613668
// getInternalRestConfig returns the internal Kubernetes cluster REST config.
614669
func getInternalRestConfig() (*rest.Config, error) {
615670
rc := &rest.Config{}

argocd/structure_cluster.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package argocd
22

33
import (
4+
"fmt"
5+
46
application "github.com/argoproj/argo-cd/v2/pkg/apis/application/v1alpha1"
57
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
68
)
@@ -24,6 +26,9 @@ func expandCluster(d *schema.ResourceData) (*application.Cluster, error) {
2426
}
2527
if ns, ok := d.GetOk("namespaces"); ok {
2628
for _, n := range ns.([]interface{}) {
29+
if n == nil {
30+
return nil, fmt.Errorf("namespaces: must contain non-empty strings")
31+
}
2732
cluster.Namespaces = append(cluster.Namespaces, n.(string))
2833
}
2934
}

0 commit comments

Comments
 (0)