-
Notifications
You must be signed in to change notification settings - Fork 82
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix ordering of on-cluster resources (#1221)
The creating of on-cluster resources requires access to the cluster. Per default, the IAM principal creating the cluster gets admin access. But the k8s provider that's used to create on-cluster can be configured to use a different IAM role. If that's done, the auth settings (aws-auth ConfigMap or access entries) need to be applied before creating the on-cluster resources. This change adds the missing dependency links for on-cluster resources.
- Loading branch information
1 parent
0fa2f99
commit dbc9824
Showing
11 changed files
with
220 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
name: example-cluster | ||
description: EKS cluster example | ||
runtime: nodejs |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import * as aws from "@pulumi/aws"; | ||
import * as pulumi from "@pulumi/pulumi"; | ||
|
||
const managedPolicyArns: string[] = [ | ||
"arn:aws:iam::aws:policy/AmazonEKSWorkerNodePolicy", | ||
"arn:aws:iam::aws:policy/AmazonEKS_CNI_Policy", | ||
"arn:aws:iam::aws:policy/AmazonEC2ContainerRegistryReadOnly", | ||
]; | ||
|
||
// Creates a role and attches the EKS worker node IAM managed policies | ||
export function createRole(name: string): aws.iam.Role { | ||
const role = new aws.iam.Role(name, { | ||
assumeRolePolicy: aws.iam.assumeRolePolicyForPrincipal({ | ||
Service: "ec2.amazonaws.com", | ||
}), | ||
}); | ||
|
||
let counter = 0; | ||
for (const policy of managedPolicyArns) { | ||
// Create RolePolicyAttachment without returning it. | ||
const rpa = new aws.iam.RolePolicyAttachment(`${name}-policy-${counter++}`, | ||
{ policyArn: policy, role: role }, | ||
); | ||
} | ||
|
||
return role; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
import * as pulumi from "@pulumi/pulumi"; | ||
import * as awsx from "@pulumi/awsx"; | ||
import * as eks from "@pulumi/eks"; | ||
import * as aws from "@pulumi/aws"; | ||
import * as iam from "./iam"; | ||
|
||
const projectName = pulumi.getProject(); | ||
|
||
// Create a VPC with public subnets only | ||
const vpc = new awsx.ec2.Vpc(`${projectName}-vpc`, { | ||
tags: {"Name": `${projectName}-2`}, | ||
subnetSpecs: [ | ||
{ type: "Public" } | ||
], | ||
natGateways: { | ||
strategy: "None", | ||
} | ||
}); | ||
|
||
const accessIamRole = new aws.iam.Role(`${projectName}-role`, { | ||
assumeRolePolicy: { | ||
Version: "2012-10-17", | ||
Statement: [{ | ||
Action: "sts:AssumeRole", | ||
Effect: "Allow", | ||
Principal: { | ||
AWS: aws.getCallerIdentityOutput().arn, | ||
}, | ||
}], | ||
}, | ||
}); | ||
|
||
/** | ||
* Identical IAM for all NodeGroups: all NodeGroups share the same `instanceRole`. | ||
*/ | ||
const role0 = iam.createRole("example-role0"); | ||
const instanceProfile0 = new aws.iam.InstanceProfile("example-instanceProfile0", {role: role0}); | ||
|
||
const cluster = new eks.Cluster(`${projectName}-cluster`, { | ||
vpcId: vpc.vpcId, | ||
publicSubnetIds: vpc.publicSubnetIds, | ||
skipDefaultNodeGroup: true, | ||
authenticationMode: eks.AuthenticationMode.API, | ||
instanceRole: role0, | ||
storageClasses: { | ||
"mygp2": { | ||
type: "gp2", | ||
default: true, | ||
encrypted: true, | ||
}, | ||
}, | ||
accessEntries: { | ||
// Grant the IAM role admin access to the cluster | ||
[`${projectName}-role`]: { | ||
principalArn: accessIamRole.arn, | ||
accessPolicies: { | ||
admin: { | ||
policyArn: "arn:aws:eks::aws:cluster-access-policy/AmazonEKSClusterAdminPolicy", | ||
accessScope: { | ||
type: "cluster", | ||
}, | ||
} | ||
} | ||
} | ||
}, | ||
providerCredentialOpts: { | ||
// Use the IAM role as the provider's credentials source | ||
roleArn: accessIamRole.arn, | ||
} | ||
}); | ||
|
||
cluster.createNodeGroup("example-ng-simple-ondemand", { | ||
instanceType: "t3.medium", | ||
desiredCapacity: 1, | ||
minSize: 1, | ||
maxSize: 2, | ||
labels: {"ondemand": "true"}, | ||
instanceProfile: instanceProfile0, | ||
}); | ||
|
||
// Export the clusters' kubeconfig. | ||
export const kubeconfig = cluster.kubeconfig; | ||
|
||
// export the IAM Role ARN | ||
export const iamRoleArn = accessIamRole.arn; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
{ | ||
"name": "multi-role", | ||
"devDependencies": { | ||
"typescript": "^4.0.0", | ||
"@types/node": "latest" | ||
}, | ||
"dependencies": { | ||
"@pulumi/pulumi": "^3.0.0", | ||
"@pulumi/awsx": "^2.0.2", | ||
"@pulumi/eks": "latest" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
{ | ||
"compilerOptions": { | ||
"outDir": "bin", | ||
"target": "es6", | ||
"lib": [ | ||
"es6" | ||
], | ||
"module": "commonjs", | ||
"moduleResolution": "node", | ||
"declaration": true, | ||
"sourceMap": true, | ||
"stripInternal": true, | ||
"experimentalDecorators": true, | ||
"pretty": true, | ||
"noFallthroughCasesInSwitch": true, | ||
"noImplicitAny": true, | ||
"noImplicitReturns": true, | ||
"forceConsistentCasingInFileNames": true, | ||
"strictNullChecks": true | ||
}, | ||
"files": [ | ||
"index.ts" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters