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

chore(eks-v2): refactor kubectlProvider #33087

Merged
merged 6 commits into from
Jan 28, 2025
Merged
Show file tree
Hide file tree
Changes from 4 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
564 changes: 67 additions & 497 deletions packages/@aws-cdk/aws-eks-v2-alpha/lib/cluster.ts

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions packages/@aws-cdk/aws-eks-v2-alpha/lib/fargate-cluster.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { Construct } from 'constructs';
import { Cluster, ClusterOptions, CoreDnsComputeType } from './cluster';
import { Cluster, ClusterCommonOptions, CoreDnsComputeType } from './cluster';
import { FargateProfile, FargateProfileOptions } from './fargate-profile';

/**
* Configuration props for EKS Fargate.
*/
export interface FargateClusterProps extends ClusterOptions {
export interface FargateClusterProps extends ClusterCommonOptions {
/**
* Fargate Profile to create along with the cluster.
*
Expand Down
2 changes: 0 additions & 2 deletions packages/@aws-cdk/aws-eks-v2-alpha/lib/fargate-profile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,6 @@ export class FargateProfile extends Construct implements ITaggable {
managedPolicies: [iam.ManagedPolicy.fromAwsManagedPolicyName('AmazonEKSFargatePodExecutionRolePolicy')],
});

this.podExecutionRole.grantPassRole(props.cluster.adminRole);

if (props.subnetSelection && !props.vpc) {
Annotations.of(this).addWarningV2('@aws-cdk/aws-eks:fargateProfileDefaultToPrivateSubnets', 'Vpc must be defined to use a custom subnet selection. All private subnets belonging to the EKS cluster will be used by default');
}
Expand Down
8 changes: 5 additions & 3 deletions packages/@aws-cdk/aws-eks-v2-alpha/lib/helm-chart.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,10 @@ export class HelmChart extends Construct {

const stack = Stack.of(this);

const provider = KubectlProvider.getOrCreate(this, props.cluster);
const provider = KubectlProvider.getKubectlProvider(this, props.cluster);
if (!provider) {
throw new Error('Kubectl Provider is not defined in this cluster. Define it when creating the cluster');
}

const timeout = props.timeout?.toSeconds();
if (timeout && timeout > 900) {
Expand All @@ -159,14 +162,13 @@ export class HelmChart extends Construct {
// default to set atomic as false
const atomic = props.atomic ?? false;

this.chartAsset?.grantRead(provider.handlerRole);
this.chartAsset?.grantRead(provider.role!);

new CustomResource(this, 'Resource', {
serviceToken: provider.serviceToken,
resourceType: HelmChart.RESOURCE_TYPE,
properties: {
ClusterName: props.cluster.clusterName,
RoleArn: provider.roleArn, // TODO: bake into the provider's environment
Release: props.release ?? Names.uniqueId(this).slice(-53).toLowerCase(), // Helm has a 53 character limit for the name
Chart: this.chart,
ChartAssetURL: this.chartAsset?.s3ObjectUrl,
Expand Down
6 changes: 4 additions & 2 deletions packages/@aws-cdk/aws-eks-v2-alpha/lib/k8s-manifest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,10 @@ export class KubernetesManifest extends Construct {
super(scope, id);

const stack = Stack.of(this);
const provider = KubectlProvider.getOrCreate(this, props.cluster);
const provider = KubectlProvider.getKubectlProvider(this, props.cluster);
if (!provider) {
throw new Error('Kubectl Provider is not defined in this cluster. Define it when creating the cluster');
}

const prune = props.prune ?? props.cluster.prune;
const pruneLabel = prune
Expand All @@ -144,7 +147,6 @@ export class KubernetesManifest extends Construct {
// StepFunctions, CloudWatch Dashboards etc).
Manifest: stack.toJsonString(props.manifest),
ClusterName: props.cluster.clusterName,
RoleArn: provider.roleArn, // TODO: bake into provider's environment
PruneLabel: pruneLabel,
Overwrite: props.overwrite,
SkipValidation: props.skipValidation,
Expand Down
7 changes: 5 additions & 2 deletions packages/@aws-cdk/aws-eks-v2-alpha/lib/k8s-object-value.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,17 @@ export class KubernetesObjectValue extends Construct {
constructor(scope: Construct, id: string, props: KubernetesObjectValueProps) {
super(scope, id);

const provider = KubectlProvider.getOrCreate(this, props.cluster);
const provider = KubectlProvider.getKubectlProvider(this, props.cluster);

if (!provider) {
throw new Error('Kubectl Provider is not defined in this cluster. Define it when creating the cluster');
}

this._resource = new CustomResource(this, 'Resource', {
resourceType: KubernetesObjectValue.RESOURCE_TYPE,
serviceToken: provider.serviceToken,
properties: {
ClusterName: props.cluster.clusterName,
RoleArn: provider.roleArn,
ObjectType: props.objectType,
ObjectName: props.objectName,
ObjectNamespace: props.objectNamespace ?? 'default',
Expand Down
7 changes: 5 additions & 2 deletions packages/@aws-cdk/aws-eks-v2-alpha/lib/k8s-patch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,11 @@ export class KubernetesPatch extends Construct {
super(scope, id);

const stack = Stack.of(this);
const provider = KubectlProvider.getOrCreate(this, props.cluster);

const provider = KubectlProvider.getKubectlProvider(this, props.cluster);
if (!provider) {
throw new Error('Kubectl Provider is not defined in this cluster. Define it when creating the cluster');
}

new CustomResource(this, 'Resource', {
serviceToken: provider.serviceToken,
Expand All @@ -83,7 +87,6 @@ export class KubernetesPatch extends Construct {
ApplyPatchJson: stack.toJsonString(props.applyPatch),
RestorePatchJson: stack.toJsonString(props.restorePatch),
ClusterName: props.cluster.clusterName,
RoleArn: provider.roleArn, // TODO: bake into provider's environment
PatchType: props.patchType ?? PatchType.STRATEGIC,
},
});
Expand Down
Loading
Loading