Skip to content

Commit

Permalink
fix(cluster): crash on empty namespace (#198)
Browse files Browse the repository at this point in the history
  • Loading branch information
MrLuje authored Aug 15, 2022
1 parent 408af72 commit 8d03743
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 0 deletions.
55 changes: 55 additions & 0 deletions argocd/resource_argocd_cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,25 @@ func TestAccArgoCDCluster_uniqueByServerTrimmed(t *testing.T) {
})
}

func TestAccArgoCDCluster_namespacesErrorWhenEmpty(t *testing.T) {
name := acctest.RandString(10)

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t); testAccPreCheckFeatureSupported(t, featureProjectScopedClusters) },
ProviderFactories: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccArgoCDClusterNamespacesContainsEmptyString(name),
ExpectError: regexp.MustCompile("namespaces: must contain non-empty strings"),
},
{
Config: testAccArgoCDClusterNamespacesContainsEmptyString_MultipleItems(name),
ExpectError: regexp.MustCompile("namespaces: must contain non-empty strings"),
},
},
})
}

func testAccArgoCDClusterBearerToken(clusterName string) string {
return fmt.Sprintf(`
resource "argocd_cluster" "simple" {
Expand Down Expand Up @@ -610,6 +629,42 @@ resource "argocd_cluster" "cluster_metadata" {
`, clusterName)
}

func testAccArgoCDClusterNamespacesContainsEmptyString(clusterName string) string {
return fmt.Sprintf(`
resource "argocd_cluster" "simple" {
server = "https://kubernetes.default.svc.cluster.local"
name = "%s"
shard = "1"
namespaces = [""]
config {
# Uses Kind's bootstrap token whose ttl is 24 hours after cluster bootstrap.
bearer_token = "abcdef.0123456789abcdef"
tls_client_config {
insecure = true
}
}
}
`, clusterName)
}

func testAccArgoCDClusterNamespacesContainsEmptyString_MultipleItems(clusterName string) string {
return fmt.Sprintf(`
resource "argocd_cluster" "simple" {
server = "https://kubernetes.default.svc.cluster.local"
name = "%s"
shard = "1"
namespaces = ["default", ""]
config {
# Uses Kind's bootstrap token whose ttl is 24 hours after cluster bootstrap.
bearer_token = "abcdef.0123456789abcdef"
tls_client_config {
insecure = true
}
}
}
`, clusterName)
}

// getInternalRestConfig returns the internal Kubernetes cluster REST config.
func getInternalRestConfig() (*rest.Config, error) {
rc := &rest.Config{}
Expand Down
5 changes: 5 additions & 0 deletions argocd/structure_cluster.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package argocd

import (
"fmt"

application "github.com/argoproj/argo-cd/v2/pkg/apis/application/v1alpha1"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
Expand All @@ -24,6 +26,9 @@ func expandCluster(d *schema.ResourceData) (*application.Cluster, error) {
}
if ns, ok := d.GetOk("namespaces"); ok {
for _, n := range ns.([]interface{}) {
if n == nil {
return nil, fmt.Errorf("namespaces: must contain non-empty strings")
}
cluster.Namespaces = append(cluster.Namespaces, n.(string))
}
}
Expand Down

0 comments on commit 8d03743

Please sign in to comment.