Skip to content

Commit

Permalink
Fixes to argocd project changes
Browse files Browse the repository at this point in the history
- Mark source_namespaces as Optional
- Add feature/version constraint 
- Add test for project with source namespaces
  • Loading branch information
onematchfox committed Nov 11, 2022
1 parent 7814630 commit 21e8a56
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 92 deletions.
2 changes: 2 additions & 0 deletions argocd/features.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ const (
featureRepositoryCertificates
featureApplicationHelmSkipCrds
featureExecLogsPolicy
featureProjectSourceNamespaces
)

var featureVersionConstraintsMap = map[int]*semver.Version{
Expand All @@ -41,6 +42,7 @@ var featureVersionConstraintsMap = map[int]*semver.Version{
featureRepositoryCertificates: semver.MustParse("1.2.0"),
featureApplicationHelmSkipCrds: semver.MustParse("2.3.0"),
featureExecLogsPolicy: semver.MustParse("2.4.0"),
featureProjectSourceNamespaces: semver.MustParse("2.5.0"),
}

type ServerInterface struct {
Expand Down
24 changes: 24 additions & 0 deletions argocd/resource_argocd_project.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,30 @@ func resourceArgoCDProjectCreate(ctx context.Context, d *schema.ResourceData, me
}
}

featureProjectSourceNamespacesSupported, err := server.isFeatureSupported(featureProjectSourceNamespaces)
if err != nil {
return []diag.Diagnostic{
{
Severity: diag.Error,
Summary: "feature not supported",
Detail: err.Error(),
},
}
}
if !featureProjectSourceNamespacesSupported {
_, sourceNamespacesOk := d.GetOk("spec.0.source_namespaces")
if sourceNamespacesOk {
return []diag.Diagnostic{
{
Severity: diag.Error,
Summary: fmt.Sprintf(
"project source_namespaces is only supported from ArgoCD %s onwards",
featureVersionConstraintsMap[featureProjectSourceNamespaces].String()),
},
}
}
}

tokenMutexProjectMap[projectName].Lock()
p, err = c.Create(ctx, &projectClient.ProjectCreateRequest{
Project: &application.AppProject{
Expand Down
154 changes: 63 additions & 91 deletions argocd/resource_argocd_project_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,93 +222,32 @@ func TestAccArgoCDProjectWithLogsExecRolePolicy(t *testing.T) {
})
}

func testAccArgoCDProjectSimple(name string) string {
return fmt.Sprintf(`
resource "argocd_project" "simple" {
metadata {
name = "%s"
namespace = "argocd"
labels = {
acceptance = "true"
}
annotations = {
"this.is.a.really.long.nested.key" = "yes, really!"
}
}
spec {
description = "simple"
source_repos = ["*"]
func TestAccArgoCDProjectWithSourceNamespaces(t *testing.T) {
name := acctest.RandomWithPrefix("test-acc")

destination {
server = "https://kubernetes.default.svc"
namespace = "default"
}
destination {
server = "https://kubernetes.default.svc"
namespace = "foo"
}
cluster_resource_whitelist {
group = "rbac.authorization.k8s.io"
kind = "ClusterRoleBinding"
}
cluster_resource_whitelist {
group = "rbac.authorization.k8s.io"
kind = "ClusterRole"
}
cluster_resource_blacklist {
group = "*"
kind = "*"
}
namespace_resource_blacklist {
group = "networking.k8s.io"
kind = "Ingress"
}
namespace_resource_whitelist {
group = "*"
kind = "*"
}
orphaned_resources {
warn = true
ignore {
group = "apps/v1"
kind = "Deployment"
name = "ignored1"
}
ignore {
group = "apps/v1"
kind = "Deployment"
name = "ignored2"
}
}
sync_window {
kind = "allow"
applications = ["api-*"]
clusters = ["*"]
namespaces = ["*"]
duration = "3600s"
schedule = "10 1 * * *"
manual_sync = true
}
sync_window {
kind = "deny"
applications = ["foo"]
clusters = ["in-cluster"]
namespaces = ["default"]
duration = "12h"
schedule = "22 1 5 * *"
manual_sync = false
}
signature_keys = [
"4AEE18F83AFDEB23",
"07E34825A909B250"
]
}
}
`, name)
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t); testAccPreCheckFeatureSupported(t, featureProjectSourceNamespaces) },
ProviderFactories: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccArgoCDProjectWithSourceNamespaces(name),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrSet(
"argocd_project.simple",
"metadata.0.uid",
),
),
},
{
ResourceName: "argocd_project.simple",
ImportState: true,
ImportStateVerify: true,
},
},
})
}

func testAccArgoCDProjectSimpleWithSourceNamespaces(name string) string {
func testAccArgoCDProjectSimple(name string) string {
return fmt.Sprintf(`
resource "argocd_project" "simple" {
metadata {
Expand All @@ -325,7 +264,6 @@ resource "argocd_project" "simple" {
spec {
description = "simple"
source_repos = ["*"]
source_namespaces = ["*"]
destination {
server = "https://kubernetes.default.svc"
Expand Down Expand Up @@ -412,7 +350,6 @@ func testAccArgoCDProjectSimpleWithoutOrphaned(name string) string {
spec {
description = "simple project"
source_repos = ["*"]
source_namespaces = ["*"]
destination {
name = "anothercluster"
Expand Down Expand Up @@ -440,7 +377,6 @@ func testAccArgoCDProjectSimpleWithEmptyOrphaned(name string) string {
spec {
description = "simple project"
source_repos = ["*"]
source_namespaces = ["*"]
destination {
name = "anothercluster"
Expand Down Expand Up @@ -663,7 +599,6 @@ func testAccArgoCDProjectSimpleWithoutRole(name string) string {
spec {
description = "simple project"
source_repos = ["*"]
source_namespaces = ["*"]
destination {
name = "anothercluster"
Expand Down Expand Up @@ -699,7 +634,6 @@ func testAccArgoCDProjectSimpleWithRole(name string) string {
spec {
description = "simple project"
source_repos = ["*"]
source_namespaces = ["*"]
destination {
name = "anothercluster"
Expand Down Expand Up @@ -742,7 +676,6 @@ func testAccArgoCDProjectWithClustersRepositoriesRolePolicy(name string) string
spec {
description = "simple project"
source_repos = ["*"]
source_namespaces = ["*"]
destination {
name = "anothercluster"
Expand Down Expand Up @@ -785,7 +718,6 @@ func testAccArgoCDProjectWithExecLogsRolePolicy(name string) string {
spec {
description = "simple project"
source_repos = ["*"]
source_namespaces = ["*"]
destination {
name = "anothercluster"
Expand All @@ -810,3 +742,43 @@ func testAccArgoCDProjectWithExecLogsRolePolicy(name string) string {
}
`, name)
}

func testAccArgoCDProjectWithSourceNamespaces(name string) string {
return fmt.Sprintf(`
resource "argocd_project" "simple" {
metadata {
name = "%s"
namespace = "argocd"
labels = {
acceptance = "true"
}
annotations = {
"this.is.a.really.long.nested.key" = "yes, really!"
}
}
spec {
description = "simple project"
source_repos = ["*"]
source_namespaces = ["*"]
destination {
server = "https://kubernetes.default.svc"
namespace = "default"
}
destination {
server = "https://kubernetes.default.svc"
namespace = "foo"
}
orphaned_resources {
warn = true
ignore {
group = "apps/v1"
kind = "Deployment"
name = "ignored1"
}
}
}
}
`, name)
}
2 changes: 1 addition & 1 deletion argocd/schema_project.go
Original file line number Diff line number Diff line change
Expand Up @@ -578,7 +578,7 @@ func projectSpecSchemaV2() *schema.Schema {
},
"source_namespaces": {
Type: schema.TypeList,
Required: true,
Optional: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
"signature_keys": {
Expand Down

0 comments on commit 21e8a56

Please sign in to comment.