@@ -19,9 +19,9 @@ import (
19
19
"encoding/base64"
20
20
"fmt"
21
21
22
- "github.com/aws/aws-sdk-go/aws"
23
22
"github.com/aws/aws-sdk-go/service/ec2"
24
23
"github.com/aws/aws-sdk-go/service/ssm"
24
+ cpv1alpha1 "github.com/awslabs/kit/operator/pkg/apis/controlplane/v1alpha1"
25
25
"github.com/awslabs/kit/operator/pkg/apis/dataplane/v1alpha1"
26
26
"github.com/awslabs/kit/operator/pkg/awsprovider"
27
27
"github.com/awslabs/kit/operator/pkg/awsprovider/securitygroup"
@@ -33,6 +33,7 @@ import (
33
33
"github.com/awslabs/kit/operator/pkg/utils/secrets"
34
34
"go.uber.org/zap"
35
35
"k8s.io/apimachinery/pkg/types"
36
+ "knative.dev/pkg/ptr"
36
37
)
37
38
38
39
const (
@@ -57,7 +58,7 @@ func (c *Controller) Reconcile(ctx context.Context, dataplane *v1alpha1.DataPlan
57
58
return fmt .Errorf ("getting launch template, %w" , err )
58
59
}
59
60
if ! existingTemplateMatchesDesired (templates , dataplane .Spec .ClusterName ) { // TODO check if existing LT is same as desired LT
60
- // if not present create launch template
61
+ // create launch template
61
62
if err := c .createLaunchTemplate (ctx , dataplane ); err != nil {
62
63
return fmt .Errorf ("creating launch template, %w" , err )
63
64
}
@@ -68,8 +69,12 @@ func (c *Controller) Reconcile(ctx context.Context, dataplane *v1alpha1.DataPlan
68
69
}
69
70
70
71
func (c * Controller ) Finalize (ctx context.Context , dataplane * v1alpha1.DataPlane ) error {
72
+ return c .deleteLaunchTemplate (ctx , TemplateName (dataplane .Spec .ClusterName ))
73
+ }
74
+
75
+ func (c * Controller ) deleteLaunchTemplate (ctx context.Context , templateName string ) error {
71
76
if _ , err := c .ec2api .DeleteLaunchTemplateWithContext (ctx , & ec2.DeleteLaunchTemplateInput {
72
- LaunchTemplateName : aws .String (TemplateName ( dataplane . Spec . ClusterName ) ),
77
+ LaunchTemplateName : ptr .String (templateName ),
73
78
}); err != nil {
74
79
if errors .IsLaunchTemplateDoNotExist (err ) {
75
80
return nil
@@ -96,35 +101,32 @@ func (c *Controller) createLaunchTemplate(ctx context.Context, dataplane *v1alph
96
101
return fmt .Errorf ("getting control plane ca certificate, %w" , err )
97
102
}
98
103
_ , clusterCA := secrets .Parse (caSecret )
99
- paramOutput , err := c .ssm .GetParameterWithContext (ctx , & ssm.GetParameterInput {
100
- Name : aws .String ("/aws/service/eks/optimized-ami/1.20/amazon-linux-2/recommended/image_id" ),
101
- })
104
+ amiID , err := c .amiID (ctx , dataplane )
102
105
if err != nil {
103
- return fmt .Errorf ("getting ssm parameter , %w" , err )
106
+ return fmt .Errorf ("getting ami id for worker nodes , %w" , err )
104
107
}
105
- amiID := * paramOutput .Parameter .Value
106
108
input := & ec2.CreateLaunchTemplateInput {
107
109
LaunchTemplateData : & ec2.RequestLaunchTemplateData {
108
110
BlockDeviceMappings : []* ec2.LaunchTemplateBlockDeviceMappingRequest {{
109
- DeviceName : aws .String ("/dev/xvda" ),
111
+ DeviceName : ptr .String ("/dev/xvda" ),
110
112
Ebs : & ec2.LaunchTemplateEbsBlockDeviceRequest {
111
- DeleteOnTermination : aws .Bool (true ),
112
- Iops : aws .Int64 (3000 ),
113
- VolumeSize : aws .Int64 (40 ),
114
- VolumeType : aws .String ("gp3" ),
113
+ DeleteOnTermination : ptr .Bool (true ),
114
+ Iops : ptr .Int64 (3000 ),
115
+ VolumeSize : ptr .Int64 (40 ),
116
+ VolumeType : ptr .String ("gp3" ),
115
117
}},
116
118
},
117
- InstanceType : aws .String ("t2.xlarge" ), // TODO get this from dataplane spec
118
- ImageId : aws .String (amiID ),
119
+ InstanceType : ptr .String ("t2.xlarge" ), // TODO get this from dataplane spec
120
+ ImageId : ptr .String (amiID ),
119
121
IamInstanceProfile : & ec2.LaunchTemplateIamInstanceProfileSpecificationRequest {
120
- Name : aws .String (fmt .Sprintf ("KitNodeInstanceProfile-%s" , dataplane .Spec .ClusterName )),
122
+ Name : ptr .String (fmt .Sprintf ("KitNodeInstanceProfile-%s" , dataplane .Spec .ClusterName )),
121
123
},
122
- Monitoring : & ec2.LaunchTemplatesMonitoringRequest {Enabled : aws .Bool (true )},
123
- SecurityGroupIds : []* string {aws .String (securityGroupID )},
124
- UserData : aws .String (base64 .StdEncoding .EncodeToString ([]byte (fmt .Sprintf (userData ,
124
+ Monitoring : & ec2.LaunchTemplatesMonitoringRequest {Enabled : ptr .Bool (true )},
125
+ SecurityGroupIds : []* string {ptr .String (securityGroupID )},
126
+ UserData : ptr .String (base64 .StdEncoding .EncodeToString ([]byte (fmt .Sprintf (userData ,
125
127
dataplane .Spec .ClusterName , v1alpha1 .SchemeGroupVersion .Group , base64 .StdEncoding .EncodeToString (clusterCA ), clusterEndpoint )))),
126
128
},
127
- LaunchTemplateName : aws .String (TemplateName (dataplane .Spec .ClusterName )),
129
+ LaunchTemplateName : ptr .String (TemplateName (dataplane .Spec .ClusterName )),
128
130
TagSpecifications : generateEC2Tags ("launch-template" , dataplane .Spec .ClusterName ),
129
131
}
130
132
if _ , err := c .ec2api .CreateLaunchTemplate (input ); err != nil {
@@ -133,6 +135,28 @@ func (c *Controller) createLaunchTemplate(ctx context.Context, dataplane *v1alph
133
135
return nil
134
136
}
135
137
138
+ func (c * Controller ) amiID (ctx context.Context , dataplane * v1alpha1.DataPlane ) (string , error ) {
139
+ kubeVersion , err := c .desiredKubernetesVersion (ctx , dataplane )
140
+ if err != nil {
141
+ return "" , fmt .Errorf ("getting kubernetes version, %w" , err )
142
+ }
143
+ paramOutput , err := c .ssm .GetParameterWithContext (ctx , & ssm.GetParameterInput {
144
+ Name : ptr .String (fmt .Sprintf ("/aws/service/eks/optimized-ami/%s/amazon-linux-2/recommended/image_id" , kubeVersion )),
145
+ })
146
+ if err != nil {
147
+ return "" , fmt .Errorf ("getting ssm parameter, %w" , err )
148
+ }
149
+ return * paramOutput .Parameter .Value , nil
150
+ }
151
+
152
+ func (c * Controller ) desiredKubernetesVersion (ctx context.Context , dataplane * v1alpha1.DataPlane ) (string , error ) {
153
+ cp := & cpv1alpha1.ControlPlane {}
154
+ if err := c .kubeclient .Get (ctx , types.NamespacedName {dataplane .GetNamespace (), dataplane .Spec .ClusterName }, cp ); err != nil {
155
+ return "" , fmt .Errorf ("getting control plane object, %w" , err )
156
+ }
157
+ return cp .Spec .KubernetesVersion , nil
158
+ }
159
+
136
160
func (c * Controller ) getLaunchTemplates (ctx context.Context , clusterName string ) ([]* ec2.LaunchTemplate , error ) {
137
161
output , err := c .ec2api .DescribeLaunchTemplatesWithContext (ctx , & ec2.DescribeLaunchTemplatesInput {
138
162
Filters : ec2FilterFor (clusterName ),
@@ -157,23 +181,23 @@ func existingTemplateMatchesDesired(templates []*ec2.LaunchTemplate, clusterName
157
181
158
182
func ec2FilterFor (clusterName string ) []* ec2.Filter {
159
183
return []* ec2.Filter {{
160
- Name : aws .String (fmt .Sprintf ("tag:%s" , TagKeyNameForAWSResources )),
161
- Values : []* string {aws .String (clusterName )},
184
+ Name : ptr .String (fmt .Sprintf ("tag:%s" , TagKeyNameForAWSResources )),
185
+ Values : []* string {ptr .String (clusterName )},
162
186
}}
163
187
}
164
188
165
189
func generateEC2Tags (svcName , clusterName string ) []* ec2.TagSpecification {
166
190
return []* ec2.TagSpecification {{
167
- ResourceType : aws .String (svcName ),
191
+ ResourceType : ptr .String (svcName ),
168
192
Tags : []* ec2.Tag {{
169
- Key : aws .String (TagKeyNameForAWSResources ),
170
- Value : aws .String (clusterName ),
193
+ Key : ptr .String (TagKeyNameForAWSResources ),
194
+ Value : ptr .String (clusterName ),
171
195
}, {
172
- Key : aws .String ("Name" ),
173
- Value : aws .String (fmt .Sprintf ("%s-%s" , clusterName , svcName )),
196
+ Key : ptr .String ("Name" ),
197
+ Value : ptr .String (fmt .Sprintf ("%s-%s" , clusterName , svcName )),
174
198
}, {
175
- Key : aws .String (fmt .Sprintf ("kubernetes.io/cluster/%s" , clusterName )),
176
- Value : aws .String ("owned" ),
199
+ Key : ptr .String (fmt .Sprintf ("kubernetes.io/cluster/%s" , clusterName )),
200
+ Value : ptr .String ("owned" ),
177
201
}},
178
202
}}
179
203
}
0 commit comments