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

service/eks: Add OIDC issuer attributes to aws_eks_cluster resource and data source #10006

Merged
merged 2 commits into from
Sep 6, 2019
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
25 changes: 25 additions & 0 deletions aws/data_source_aws_eks_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,26 @@ func dataSourceAwsEksCluster() *schema.Resource {
Type: schema.TypeString,
Computed: true,
},
"identity": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"oidc": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"issuer": {
Type: schema.TypeString,
Computed: true,
},
},
},
},
},
},
},
"name": {
Type: schema.TypeString,
Required: true,
Expand Down Expand Up @@ -134,6 +154,11 @@ func dataSourceAwsEksClusterRead(d *schema.ResourceData, meta interface{}) error
return fmt.Errorf("error setting enabled_cluster_log_types: %s", err)
}
d.Set("endpoint", cluster.Endpoint)

if err := d.Set("identity", flattenEksIdentity(cluster.Identity)); err != nil {
return fmt.Errorf("error setting identity: %s", err)
}

d.Set("name", cluster.Name)
d.Set("platform_version", cluster.PlatformVersion)
d.Set("role_arn", cluster.RoleArn)
Expand Down
3 changes: 3 additions & 0 deletions aws/data_source_aws_eks_cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ func TestAccAWSEksClusterDataSource_basic(t *testing.T) {
resource.TestCheckResourceAttr(dataSourceResourceName, "enabled_cluster_log_types.2902841359", "api"),
resource.TestCheckResourceAttr(dataSourceResourceName, "enabled_cluster_log_types.2451111801", "audit"),
resource.TestCheckResourceAttrPair(resourceName, "endpoint", dataSourceResourceName, "endpoint"),
resource.TestCheckResourceAttrPair(resourceName, "identity.#", dataSourceResourceName, "identity.#"),
resource.TestCheckResourceAttrPair(resourceName, "identity.0.oidc.#", dataSourceResourceName, "identity.0.oidc.#"),
resource.TestCheckResourceAttrPair(resourceName, "identity.0.oidc.0.issuer", dataSourceResourceName, "identity.0.oidc.0.issuer"),
resource.TestMatchResourceAttr(dataSourceResourceName, "platform_version", regexp.MustCompile(`^eks\.\d+$`)),
resource.TestCheckResourceAttrPair(resourceName, "role_arn", dataSourceResourceName, "role_arn"),
resource.TestCheckResourceAttrPair(resourceName, "status", dataSourceResourceName, "status"),
Expand Down
49 changes: 49 additions & 0 deletions aws/resource_aws_eks_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,26 @@ func resourceAwsEksCluster() *schema.Resource {
Type: schema.TypeString,
Computed: true,
},
"identity": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"oidc": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"issuer": {
Type: schema.TypeString,
Computed: true,
},
},
},
},
},
},
},
"name": {
Type: schema.TypeString,
Required: true,
Expand Down Expand Up @@ -233,6 +253,11 @@ func resourceAwsEksClusterRead(d *schema.ResourceData, meta interface{}) error {

d.Set("created_at", aws.TimeValue(cluster.CreatedAt).String())
d.Set("endpoint", cluster.Endpoint)

if err := d.Set("identity", flattenEksIdentity(cluster.Identity)); err != nil {
return fmt.Errorf("error setting identity: %s", err)
}

d.Set("name", cluster.Name)
d.Set("platform_version", cluster.PlatformVersion)
d.Set("role_arn", cluster.RoleArn)
Expand Down Expand Up @@ -430,6 +455,30 @@ func flattenEksCertificate(certificate *eks.Certificate) []map[string]interface{
return []map[string]interface{}{m}
}

func flattenEksIdentity(identity *eks.Identity) []map[string]interface{} {
if identity == nil {
return []map[string]interface{}{}
}

m := map[string]interface{}{
"oidc": flattenEksOidc(identity.Oidc),
}

return []map[string]interface{}{m}
}

func flattenEksOidc(oidc *eks.OIDC) []map[string]interface{} {
if oidc == nil {
return []map[string]interface{}{}
}

m := map[string]interface{}{
"issuer": aws.StringValue(oidc.Issuer),
}

return []map[string]interface{}{m}
}

func flattenEksVpcConfigResponse(vpcConfig *eks.VpcConfigResponse) []map[string]interface{} {
if vpcConfig == nil {
return []map[string]interface{}{}
Expand Down
11 changes: 7 additions & 4 deletions aws/resource_aws_eks_cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,9 @@ func TestAccAWSEksCluster_basic(t *testing.T) {
resource.TestCheckResourceAttr(resourceName, "certificate_authority.#", "1"),
resource.TestCheckResourceAttrSet(resourceName, "certificate_authority.0.data"),
resource.TestMatchResourceAttr(resourceName, "endpoint", regexp.MustCompile(`^https://`)),
resource.TestCheckResourceAttr(resourceName, "identity.#", "1"),
resource.TestCheckResourceAttr(resourceName, "identity.0.oidc.#", "1"),
resource.TestMatchResourceAttr(resourceName, "identity.0.oidc.0.issuer", regexp.MustCompile(`^https://`)),
resource.TestCheckResourceAttr(resourceName, "name", rName),
resource.TestMatchResourceAttr(resourceName, "platform_version", regexp.MustCompile(`^eks\.\d+$`)),
resource.TestMatchResourceAttr(resourceName, "role_arn", regexp.MustCompile(fmt.Sprintf("%s$", rName))),
Expand Down Expand Up @@ -124,10 +127,10 @@ func TestAccAWSEksCluster_Version(t *testing.T) {
CheckDestroy: testAccCheckAWSEksClusterDestroy,
Steps: []resource.TestStep{
{
Config: testAccAWSEksClusterConfig_Version(rName, "1.10"),
Config: testAccAWSEksClusterConfig_Version(rName, "1.13"),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSEksClusterExists(resourceName, &cluster1),
resource.TestCheckResourceAttr(resourceName, "version", "1.10"),
resource.TestCheckResourceAttr(resourceName, "version", "1.13"),
),
},
{
Expand All @@ -136,11 +139,11 @@ func TestAccAWSEksCluster_Version(t *testing.T) {
ImportStateVerify: true,
},
{
Config: testAccAWSEksClusterConfig_Version(rName, "1.11"),
Config: testAccAWSEksClusterConfig_Version(rName, "1.14"),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSEksClusterExists(resourceName, &cluster2),
testAccCheckAWSEksClusterNotRecreated(&cluster1, &cluster2),
resource.TestCheckResourceAttr(resourceName, "version", "1.11"),
resource.TestCheckResourceAttr(resourceName, "version", "1.14"),
),
},
},
Expand Down
8 changes: 8 additions & 0 deletions website/docs/d/eks_cluster.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ output "endpoint" {
output "kubeconfig-certificate-authority-data" {
value = "${data.aws_eks_cluster.example.certificate_authority.0.data}"
}

# Only available on Kubernetes version 1.13 and 1.14 clusters created or upgraded on or after September 3, 2019.
output "identity-oidc-issuer" {
value = "${data.aws_eks_cluster.example.identity.0.oidc.0.issuer}"
}
```

## Argument Reference
Expand All @@ -39,6 +44,9 @@ output "kubeconfig-certificate-authority-data" {
* `created_at` - The Unix epoch time stamp in seconds for when the cluster was created.
* `enabled_cluster_log_types` - The enabled control plane logs.
* `endpoint` - The endpoint for your Kubernetes API server.
* `identity` - Nested attribute containing identity provider information for your cluster. Only available on Kubernetes version 1.13 and 1.14 clusters created or upgraded on or after September 3, 2019. For an example using this information to enable IAM Roles for Service Accounts, see the [`aws_eks_cluster` resource documentation](/docs/providers/aws/r/eks_cluster.html).
* `oidc` - Nested attribute containing [OpenID Connect](https://openid.net/connect/) identity provider information for the cluster.
* `issuer` - Issuer URL for the OpenID Connect identity provider.
* `platform_version` - The platform version for the cluster.
* `role_arn` - The Amazon Resource Name (ARN) of the IAM role that provides permissions for the Kubernetes control plane to make calls to AWS API operations on your behalf.
* `status` - The status of the EKS cluster. One of `CREATING`, `ACTIVE`, `DELETING`, `FAILED`.
Expand Down
46 changes: 46 additions & 0 deletions website/docs/r/eks_cluster.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,49 @@ resource "aws_cloudwatch_log_group" "example" {
}
```

### Enabling IAM Roles for Service Accounts

Only available on Kubernetes version 1.13 and 1.14 clusters created or upgraded on or after September 3, 2019. For more information about this feature, see the [EKS User Guide](https://docs.aws.amazon.com/eks/latest/userguide/enable-iam-roles-for-service-accounts.html).

```hcl
resource "aws_eks_cluster" "example" {
# ... other configuration ...
}

resource "aws_iam_openid_connect_provider" "example" {
client_id_list = ["sts.amazonaws.com"]
thumbprint_list = []
url = "${aws_eks_cluster.example.identity.0.oidc.0.issuer}"
}

data "aws_caller_identity" "current" {}

data "aws_iam_policy_document" "example_assume_role_policy" {
statement {
actions = ["sts:AssumeRoleWithWebIdentity"]
effect = "Allow"

condition {
test = "StringEquals"
variable = "${replace(aws_iam_openid_connect_provider.example.url, "https://", "")}:sub"
values = ["system:serviceaccount:kube-system:aws-node"]
}

principals {
identifiers = ["${aws_iam_openid_connect_provider.example.arn}"]
type = "Federated"
}
}
}

resource "aws_iam_role" "example" {
assume_role_policy = "${data.aws_iam_policy_document.example_assume_role_policy.json}"
name = "example"
}
```

After adding inline IAM Policies (e.g. [`aws_iam_role_policy` resource](/docs/providers/aws/r/iam_role_policy.html)) or attaching IAM Policies (e.g. [`aws_iam_policy` resource](/docs/providers/aws/r/iam_policy.html) and [`aws_iam_role_policy_attachment` resource](/docs/providers/aws/r/iam_policy.html)) with the desired permissions to the IAM Role, annotate the Kubernetes service account (e.g. [`kubernetes_service_account` resource](/docs/providers/kubernetes/r/service_account.html)) and recreate any pods.

## Argument Reference

The following arguments are supported:
Expand All @@ -88,6 +131,9 @@ In addition to all arguments above, the following attributes are exported:
* `certificate_authority` - Nested attribute containing `certificate-authority-data` for your cluster.
* `data` - The base64 encoded certificate data required to communicate with your cluster. Add this to the `certificate-authority-data` section of the `kubeconfig` file for your cluster.
* `endpoint` - The endpoint for your Kubernetes API server.
* `identity` - Nested attribute containing identity provider information for your cluster. Only available on Kubernetes version 1.13 and 1.14 clusters created or upgraded on or after September 3, 2019.
* `oidc` - Nested attribute containing [OpenID Connect](https://openid.net/connect/) identity provider information for the cluster.
* `issuer` - Issuer URL for the OpenID Connect identity provider.
* `platform_version` - The platform version for the cluster.
* `status` - The status of the EKS cluster. One of `CREATING`, `ACTIVE`, `DELETING`, `FAILED`.
* `version` - The Kubernetes server version for the cluster.
Expand Down