-
Notifications
You must be signed in to change notification settings - Fork 4k
/
Copy pathaddon.ts
147 lines (136 loc) · 4.17 KB
/
addon.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
import { Construct } from 'constructs';
import { ICluster } from './cluster';
import { CfnAddon } from './eks.generated';
import { ArnFormat, IResource, Resource, Stack, Fn } from '../../core';
/**
* Represents an Amazon EKS Add-On.
*/
export interface IAddon extends IResource {
/**
* Name of the Add-On.
* @attribute
*/
readonly addonName: string;
/**
* ARN of the Add-On.
* @attribute
*/
readonly addonArn: string;
}
/**
* Properties for creating an Amazon EKS Add-On.
*/
export interface AddonProps {
/**
* Name of the Add-On.
*/
readonly addonName: string;
/**
* Version of the Add-On. You can check all available versions with describe-addon-versons.
* For example, this lists all available versions for the `eks-pod-identity-agent` addon:
* $ aws eks describe-addon-versions --addon-name eks-pod-identity-agent \
* --query 'addons[*].addonVersions[*].addonVersion'
*
* @default the latest version.
*/
readonly addonVersion?: string;
/**
* The EKS cluster the Add-On is associated with.
*/
readonly cluster: ICluster;
/**
* Specifying this option preserves the add-on software on your cluster but Amazon EKS stops managing any settings for the add-on.
* If an IAM account is associated with the add-on, it isn't removed.
*
* @default true
*/
readonly preserveOnDelete?: boolean;
}
/**
* Represents the attributes of an addon for an Amazon EKS cluster.
*/
export interface AddonAttributes {
/**
* The name of the addon.
*/
readonly addonName: string;
/**
* The name of the Amazon EKS cluster the addon is associated with.
*/
readonly clusterName: string;
}
/**
* Represents an Amazon EKS Add-On.
*/
export class Addon extends Resource implements IAddon {
/**
* Creates an `IAddon` instance from the given addon attributes.
*
* @param scope - The parent construct.
* @param id - The construct ID.
* @param attrs - The attributes of the addon, including the addon name and the cluster name.
* @returns An `IAddon` instance.
*/
public static fromAddonAttributes(scope: Construct, id: string, attrs: AddonAttributes): IAddon {
class Import extends Resource implements IAddon {
public readonly addonName = attrs.addonName;
public readonly addonArn = Stack.of(scope).formatArn({
service: 'eks',
resource: 'addon',
resourceName: `${attrs.clusterName}/${attrs.addonName}`,
});
}
return new Import(scope, id);
}
/**
* Creates an `IAddon` from an existing addon ARN.
*
* @param scope - The parent construct.
* @param id - The ID of the construct.
* @param addonArn - The ARN of the addon.
* @returns An `IAddon` implementation.
*/
public static fromAddonArn(scope: Construct, id: string, addonArn: string): IAddon {
const parsedArn = Stack.of(scope).splitArn(addonArn, ArnFormat.COLON_RESOURCE_NAME);
const splitResourceName = Fn.split('/', parsedArn.resourceName!);
class Import extends Resource implements IAddon {
public readonly addonName = Fn.select(1, splitResourceName);
public readonly addonArn = addonArn;
}
return new Import(scope, id);
}
/**
* Name of the addon.
*/
public readonly addonName: string;
/**
* Arn of the addon.
*/
public readonly addonArn: string;
private readonly clusterName: string;
/**
* Creates a new Amazon EKS Add-On.
* @param scope The parent construct.
* @param id The construct ID.
* @param props The properties for the Add-On.
*/
constructor(scope: Construct, id: string, props: AddonProps) {
super(scope, id, {
physicalName: props.addonName,
});
this.clusterName = props.cluster.clusterName;
this.addonName = props.addonName;
const resource = new CfnAddon(this, 'Resource', {
addonName: props.addonName,
clusterName: this.clusterName,
addonVersion: props.addonVersion,
preserveOnDelete: props.preserveOnDelete,
});
this.addonName = this.getResourceNameAttribute(resource.ref);
this.addonArn = this.getResourceArnAttribute(resource.attrArn, {
service: 'eks',
resource: 'addon',
resourceName: `${this.clusterName}/${this.addonName}/`,
});
}
}