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

Allow generate_name on cluster roles + bindings, and role bindings #1899

Merged
merged 3 commits into from
Jan 4, 2023
Merged
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
23 changes: 23 additions & 0 deletions .changelog/1899.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
```release-note:enhancement
`resource/kubernetes_cluster_role`: add attribute `generate_name` to produce a unique random name
```

```release-note:enhancement
`resource/kubernetes_cluster_role_v1`: add attribute `generate_name` to produce a unique random name
```

```release-note:enhancement
`resource/kubernetes_cluster_role_binding`: add attribute `generate_name` to produce a unique random name
```

```release-note:enhancement
`resource/kubernetes_cluster_role_binding_v1`: add attribute `generate_name` to produce a unique random name
```

```release-note:enhancement
`resource/kubernetes_role_binding`: add attribute `generate_name` to produce a unique random name
```

```release-note:enhancement
`resource/kubernetes_role_binding_v1`: add attribute `generate_name` to produce a unique random name
```
2 changes: 1 addition & 1 deletion kubernetes/resource_kubernetes_cluster_role.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func resourceKubernetesClusterRole() *schema.Resource {
},

Schema: map[string]*schema.Schema{
"metadata": metadataSchemaRBAC("clusterRole", false, false),
"metadata": metadataSchemaRBAC("clusterRole", true, false),
"rule": {
Type: schema.TypeList,
Description: "List of PolicyRules for this ClusterRole",
Expand Down
4 changes: 2 additions & 2 deletions kubernetes/resource_kubernetes_cluster_role_binding.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func resourceKubernetesClusterRoleBinding() *schema.Resource {
},

Schema: map[string]*schema.Schema{
"metadata": metadataSchemaRBAC("clusterRoleBinding", false, false),
"metadata": metadataSchemaRBAC("clusterRoleBinding", true, false),
"role_ref": {
Type: schema.TypeList,
Description: "RoleRef references the Cluster Role for this binding",
Expand Down Expand Up @@ -67,7 +67,7 @@ func resourceKubernetesClusterRoleBindingCreate(ctx context.Context, d *schema.R
return diag.FromErr(err)
}
log.Printf("[INFO] Submitted new ClusterRoleBinding: %#v", binding)
d.SetId(metadata.Name)
d.SetId(binding.Name)

return resourceKubernetesClusterRoleBindingRead(ctx, d, meta)
}
Expand Down
55 changes: 55 additions & 0 deletions kubernetes/resource_kubernetes_cluster_role_binding_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package kubernetes
import (
"context"
"fmt"
"regexp"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
Expand Down Expand Up @@ -120,6 +121,39 @@ func TestAccKubernetesClusterRoleBinding_basic(t *testing.T) {
})
}

func TestAccKubernetesClusterRoleBinding_generatedName(t *testing.T) {
var conf api.ClusterRoleBinding
prefix := "tf-acc-test-gen:"

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
IDRefreshName: "kubernetes_cluster_role_binding.test",
IDRefreshIgnore: []string{"metadata.0.resource_version"},
ProviderFactories: testAccProviderFactories,
CheckDestroy: testAccCheckKubernetesClusterRoleBindingDestroy,
Steps: []resource.TestStep{
{
Config: testAccKubernetesClusterRoleBindingConfig_generateName(prefix),
Check: resource.ComposeAggregateTestCheckFunc(
testAccCheckKubernetesClusterRoleBindingExists("kubernetes_cluster_role_binding.test", &conf),
resource.TestMatchResourceAttr("kubernetes_cluster_role_binding.test", "metadata.0.name", regexp.MustCompile("^"+prefix)),
resource.TestCheckResourceAttrSet("kubernetes_cluster_role_binding.test", "metadata.0.generation"),
resource.TestCheckResourceAttrSet("kubernetes_cluster_role_binding.test", "metadata.0.resource_version"),
resource.TestCheckResourceAttrSet("kubernetes_cluster_role_binding.test", "metadata.0.uid"),
resource.TestCheckResourceAttr("kubernetes_cluster_role_binding.test", "role_ref.#", "1"),
resource.TestCheckResourceAttr("kubernetes_cluster_role_binding.test", "role_ref.0.api_group", "rbac.authorization.k8s.io"),
resource.TestCheckResourceAttr("kubernetes_cluster_role_binding.test", "role_ref.0.kind", "ClusterRole"),
resource.TestCheckResourceAttr("kubernetes_cluster_role_binding.test", "role_ref.0.name", "cluster-admin"),
resource.TestCheckResourceAttr("kubernetes_cluster_role_binding.test", "subject.#", "1"),
resource.TestCheckResourceAttr("kubernetes_cluster_role_binding.test", "subject.0.api_group", "rbac.authorization.k8s.io"),
resource.TestCheckResourceAttr("kubernetes_cluster_role_binding.test", "subject.0.name", "notauser"),
resource.TestCheckResourceAttr("kubernetes_cluster_role_binding.test", "subject.0.kind", "User"),
),
},
},
})
}

func TestAccKubernetesClusterRoleBinding_serviceaccount_subject(t *testing.T) {
var conf api.ClusterRoleBinding
name := fmt.Sprintf("tf-acc-test:%s", acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum))
Expand Down Expand Up @@ -347,6 +381,27 @@ func testAccKubernetesClusterRoleBindingConfig_basic(name string) string {
`, name)
}

func testAccKubernetesClusterRoleBindingConfig_generateName(namePrefix string) string {
return fmt.Sprintf(`resource "kubernetes_cluster_role_binding" "test" {
metadata {
generate_name = "%s"
}

role_ref {
api_group = "rbac.authorization.k8s.io"
kind = "ClusterRole"
name = "cluster-admin"
}

subject {
kind = "User"
name = "notauser"
api_group = "rbac.authorization.k8s.io"
}
}
`, namePrefix)
}

func testAccKubernetesClusterRoleBindingConfig_modified(name string) string {
return fmt.Sprintf(`resource "kubernetes_cluster_role_binding" "test" {
metadata {
Expand Down
44 changes: 44 additions & 0 deletions kubernetes/resource_kubernetes_cluster_role_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package kubernetes
import (
"context"
"fmt"
"regexp"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
Expand Down Expand Up @@ -65,6 +66,34 @@ func TestAccKubernetesClusterRole_basic(t *testing.T) {
})
}

func TestAccKubernetesClusterRole_generatedName(t *testing.T) {
var conf api.ClusterRole
prefix := "tf-acc-test-gen:"
resourceName := "kubernetes_cluster_role.test"

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
IDRefreshName: "kubernetes_cluster_role.test",
IDRefreshIgnore: []string{"metadata.0.resource_version"},
ProviderFactories: testAccProviderFactories,
CheckDestroy: testAccCheckKubernetesClusterRoleDestroy,
Steps: []resource.TestStep{
{
Config: testAccKubernetesClusterRoleConfig_generateName(prefix),
Check: resource.ComposeAggregateTestCheckFunc(
testAccCheckKubernetesClusterRoleExists("kubernetes_cluster_role.test", &conf),
resource.TestMatchResourceAttr(resourceName, "metadata.0.name", regexp.MustCompile("^"+prefix)),
resource.TestCheckResourceAttr(resourceName, "rule.#", "1"),
resource.TestCheckResourceAttr(resourceName, "rule.0.resources.#", "1"),
resource.TestCheckResourceAttr(resourceName, "rule.0.resources.0", "pods"),
resource.TestCheckResourceAttr(resourceName, "rule.0.verbs.#", "1"),
resource.TestCheckResourceAttr(resourceName, "rule.0.verbs.0", "get"),
),
},
},
})
}

func TestAccKubernetesClusterRole_UpdatePatchOperationsOrderWithRemovals(t *testing.T) {
var conf api.ClusterRole
resourceName := "kubernetes_cluster_role.test"
Expand Down Expand Up @@ -324,6 +353,21 @@ func testAccKubernetesClusterRoleConfig_basic(name string) string {
`, name)
}

func testAccKubernetesClusterRoleConfig_generateName(prefixName string) string {
return fmt.Sprintf(`resource "kubernetes_cluster_role" "test" {
metadata {
generate_name = "%s"
}

rule {
api_groups = [""]
resources = ["pods"]
verbs = ["get"]
}
}
`, prefixName)
}

func testAccKubernetesClusterRoleConfig_modified(name string) string {
return fmt.Sprintf(`resource "kubernetes_cluster_role" "test" {
metadata {
Expand Down
2 changes: 1 addition & 1 deletion kubernetes/resource_kubernetes_role_binding.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func resourceKubernetesRoleBinding() *schema.Resource {
},

Schema: map[string]*schema.Schema{
"metadata": metadataSchemaRBAC("roleBinding", false, true),
"metadata": metadataSchemaRBAC("roleBinding", true, true),
"role_ref": {
Type: schema.TypeList,
Description: "RoleRef references the Role for this binding",
Expand Down
56 changes: 56 additions & 0 deletions kubernetes/resource_kubernetes_role_binding_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package kubernetes
import (
"context"
"fmt"
"regexp"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
Expand Down Expand Up @@ -126,6 +127,40 @@ func TestAccKubernetesRoleBinding_basic(t *testing.T) {
})
}

func TestAccKubernetesRoleBinding_generatedName(t *testing.T) {
var conf api.RoleBinding
prefix := "tf-acc-test-gen:"
resourceName := "kubernetes_role_binding.test"

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
IDRefreshName: "kubernetes_role_binding.test",
IDRefreshIgnore: []string{"metadata.0.resource_version"},
ProviderFactories: testAccProviderFactories,
CheckDestroy: testAccCheckKubernetesRoleBindingDestroy,
Steps: []resource.TestStep{
{
Config: testAccKubernetesRoleBindingConfig_generateName(prefix),
Check: resource.ComposeAggregateTestCheckFunc(
testAccCheckKubernetesRoleBindingExists("kubernetes_role_binding.test", &conf),
resource.TestMatchResourceAttr("kubernetes_role_binding.test", "metadata.0.name", regexp.MustCompile("^"+prefix)),
resource.TestCheckResourceAttrSet(resourceName, "metadata.0.generation"),
resource.TestCheckResourceAttrSet(resourceName, "metadata.0.resource_version"),
resource.TestCheckResourceAttrSet(resourceName, "metadata.0.uid"),
resource.TestCheckResourceAttr(resourceName, "role_ref.#", "1"),
resource.TestCheckResourceAttr(resourceName, "role_ref.0.api_group", "rbac.authorization.k8s.io"),
resource.TestCheckResourceAttr(resourceName, "role_ref.0.kind", "Role"),
resource.TestCheckResourceAttr(resourceName, "role_ref.0.name", "admin"),
resource.TestCheckResourceAttr(resourceName, "subject.#", "1"),
resource.TestCheckResourceAttr(resourceName, "subject.0.api_group", "rbac.authorization.k8s.io"),
resource.TestCheckResourceAttr(resourceName, "subject.0.name", "notauser"),
resource.TestCheckResourceAttr(resourceName, "subject.0.kind", "User"),
),
},
},
})
}

func TestAccKubernetesRoleBinding_sa_subject(t *testing.T) {
var conf api.RoleBinding
name := fmt.Sprintf("tf-acc-test:%s", acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum))
Expand Down Expand Up @@ -357,6 +392,27 @@ func testAccKubernetesRoleBindingConfig_basic(name string) string {
`, name)
}

func testAccKubernetesRoleBindingConfig_generateName(prefixName string) string {
return fmt.Sprintf(`resource "kubernetes_role_binding" "test" {
metadata {
generate_name = "%s"
}

role_ref {
api_group = "rbac.authorization.k8s.io"
kind = "Role"
name = "admin"
}

subject {
kind = "User"
name = "notauser"
api_group = "rbac.authorization.k8s.io"
}
}
`, prefixName)
}

func testAccKubernetesRoleBindingConfig_modified(name string) string {
return fmt.Sprintf(`resource "kubernetes_role_binding" "test" {
metadata {
Expand Down