Skip to content

Commit 8188aaf

Browse files
estela-ramirezeramirez8
andauthored
Add al2023 support (#491)
* added support for al2023 Signed-off-by: eramirez8 <[email protected]> * trim quotes from override al os family Signed-off-by: eramirez8 <[email protected]> --------- Signed-off-by: eramirez8 <[email protected]> Co-authored-by: eramirez8 <[email protected]>
1 parent 1c41cef commit 8188aaf

File tree

11 files changed

+215
-14
lines changed

11 files changed

+215
-14
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,4 @@ vendor/*
88
.DS_Store
99
*.idea
1010
.windsurfrules
11+
.qodo

api/instancemgr/v1alpha1/instancegroup_types.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,9 @@ const (
5353
ReconcileErr ReconcileState = "Error"
5454

5555
// Userdata bootstrap stages
56-
PreBootstrapStage = "PreBootstrap"
57-
PostBootstrapStage = "PostBootstrap"
56+
PreBootstrapStage = "PreBootstrap"
57+
PostBootstrapStage = "PostBootstrap"
58+
NodeConfigYamlStage = "NodeConfigYaml"
5859

5960
LifecycleStateNormal = "normal"
6061
LifecycleStateSpot = "spot"
@@ -840,6 +841,7 @@ func (c *EKSConfiguration) GetManagedPolicies() []string {
840841
func (c *EKSConfiguration) GetUserData() []UserDataStage {
841842
return c.UserData
842843
}
844+
843845
func (c *EKSConfiguration) SetManagedPolicies(policies []string) {
844846
c.ManagedPolicies = policies
845847
}

config/crd/bases/instancemgr.keikoproj.io_instancegroups.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,8 @@ spec:
253253
- stage
254254
type: object
255255
type: array
256+
amazonLinuxOsFamily:
257+
type: string
256258
volumes:
257259
items:
258260
properties:

controllers/instancegroup_controller.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ type InstanceGroupReconciler struct {
5656
Metrics *common.MetricsCollector
5757
DisableWinClusterInjection bool
5858
DefaultScalingConfiguration *v1alpha1.ScalingConfigurationType
59+
AmazonLinuxOsFamily string
5960
}
6061

6162
type InstanceGroupAuthenticator struct {
@@ -143,6 +144,7 @@ func (r *InstanceGroupReconciler) Reconcile(ctxt context.Context, req ctrl.Reque
143144
ConfigRetention: r.ConfigRetention,
144145
Metrics: r.Metrics,
145146
DisableWinClusterInjection: r.DisableWinClusterInjection,
147+
AmazonLinuxOsFamily: r.AmazonLinuxOsFamily,
146148
}
147149

148150
var (

controllers/provisioners/config.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,13 @@ import (
1919
"fmt"
2020
"strings"
2121

22-
"k8s.io/apimachinery/pkg/labels"
23-
2422
"github.com/ghodss/yaml"
2523
"github.com/keikoproj/instance-manager/api/instancemgr/v1alpha1"
2624
"github.com/keikoproj/instance-manager/controllers/common"
2725
"github.com/pkg/errors"
2826
corev1 "k8s.io/api/core/v1"
2927
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
28+
"k8s.io/apimachinery/pkg/labels"
3029
runtime "k8s.io/apimachinery/pkg/runtime"
3130
)
3231

controllers/provisioners/eks/create.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ func (ctx *EksInstanceGroupContext) Create() error {
4646
placement = configuration.GetPlacement()
4747
metadataOptions = configuration.GetMetadataOptions()
4848
)
49-
5049
ctx.SetState(v1alpha1.ReconcileModifying)
5150

5251
// no need to create a role if one is already provided

controllers/provisioners/eks/eks.go

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,12 @@ package eks
1717

1818
import (
1919
"fmt"
20+
"strings"
2021
"sync"
2122

2223
corev1 "k8s.io/api/core/v1"
2324

2425
"github.com/go-logr/logr"
25-
2626
"github.com/keikoproj/instance-manager/api/instancemgr/v1alpha1"
2727
"github.com/keikoproj/instance-manager/controllers/common"
2828
awsprovider "github.com/keikoproj/instance-manager/controllers/providers/aws"
@@ -41,9 +41,10 @@ const (
4141
CustomNetworkingHostPodsAnnotation = "instancemgr.keikoproj.io/custom-networking-host-pods"
4242
CustomNetworkingPrefixAssignmentEnabledAnnotation = "instancemgr.keikoproj.io/custom-networking-prefix-assignment-enabled"
4343

44-
OsFamilyWindows = "windows"
45-
OsFamilyBottleRocket = "bottlerocket"
46-
OsFamilyAmazonLinux2 = "amazonlinux2"
44+
OsFamilyWindows = "windows"
45+
OsFamilyBottleRocket = "bottlerocket"
46+
OsFamilyAmazonLinux2 = "amazonlinux2"
47+
OsFamilyAmazonLinux2023 = "amazonlinux2023"
4748
)
4849

4950
var (
@@ -54,7 +55,7 @@ var (
5455
InstanceMgrLifecycleLabel = "instancemgr.keikoproj.io/lifecycle"
5556
InstanceMgrImageLabel = "instancemgr.keikoproj.io/image"
5657

57-
AllowedOsFamilies = []string{OsFamilyWindows, OsFamilyBottleRocket, OsFamilyAmazonLinux2}
58+
AllowedOsFamilies = []string{OsFamilyWindows, OsFamilyBottleRocket, OsFamilyAmazonLinux2, OsFamilyAmazonLinux2023}
5859
DefaultManagedPolicies = []string{"AmazonEKSWorkerNodePolicy", "AmazonEC2ContainerRegistryReadOnly"}
5960
CNIManagedPolicy = "AmazonEKS_CNI_Policy"
6061
SupportedArchitectures = []string{"x86_64", "arm64"}
@@ -78,6 +79,7 @@ func New(p provisioners.ProvisionerInput) *EksInstanceGroupContext {
7879
ConfigRetention: p.ConfigRetention,
7980
Metrics: p.Metrics,
8081
DisableWinClusterInjection: p.DisableWinClusterInjection,
82+
AmazonLinuxOsFamily: p.AmazonLinuxOsFamily,
8183
}
8284

8385
ctx.SetState(v1alpha1.ReconcileInit)
@@ -99,11 +101,13 @@ type EksInstanceGroupContext struct {
99101
ResourcePrefix string
100102
Metrics *common.MetricsCollector
101103
DisableWinClusterInjection bool
104+
AmazonLinuxOsFamily string
102105
}
103106

104107
type UserDataPayload struct {
105-
PreBootstrap []string
106-
PostBootstrap []string
108+
PreBootstrap []string
109+
PostBootstrap []string
110+
NodeConfigYaml string
107111
}
108112

109113
type MountOpts struct {
@@ -125,6 +129,8 @@ type EKSUserData struct {
125129
PostBootstrap []string
126130
MountOptions []MountOpts
127131
MaxPods int64
132+
ClusterIP string
133+
NodeConfigYaml string
128134
}
129135

130136
func (ctx *EksInstanceGroupContext) GetInstanceGroup() *v1alpha1.InstanceGroup {
@@ -139,14 +145,16 @@ func (ctx *EksInstanceGroupContext) GetOsFamily() string {
139145
instanceGroup = ctx.GetInstanceGroup()
140146
annotations = instanceGroup.GetAnnotations()
141147
)
148+
overrideAmazonLinuxFamily := strings.Trim(ctx.AmazonLinuxOsFamily, "\" ")
142149

143150
if v, exists := annotations[OsFamilyAnnotation]; exists {
144151
if common.ContainsEqualFold(AllowedOsFamilies, v) {
145152
return annotations[OsFamilyAnnotation]
146153
}
147154
ctx.Log.Info("used unsupported annotation value '%v=%v', will default to 'amazonlinux2', allowed values: %+v", OsFamilyAnnotation, v, AllowedOsFamilies)
155+
} else if common.ContainsEqualFold(AllowedOsFamilies, overrideAmazonLinuxFamily) {
156+
return overrideAmazonLinuxFamily
148157
}
149-
150158
return OsFamilyAmazonLinux2
151159
}
152160

controllers/provisioners/eks/helpers.go

Lines changed: 67 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,8 @@ func (ctx *EksInstanceGroupContext) GetBasicUserData(clusterName, args string, k
121121
nodeLabels = ctx.GetComputedLabels()
122122
nodeTaints = configuration.GetTaints()
123123
bootstrapOptions = ctx.GetComputedBootstrapOptions()
124+
cluster = state.GetCluster()
125+
clusterIP = ctx.AwsWorker.GetDNSClusterIP(cluster)
124126
)
125127
var maxPods int64
126128

@@ -193,6 +195,59 @@ set -o xtrace
193195
/etc/eks/bootstrap.sh {{ .ClusterName }} {{ .Arguments }}
194196
set +o xtrace
195197
{{range $post := .PostBootstrap}}{{$post}}{{end}}`
198+
case OsFamilyAmazonLinux2023:
199+
UserDataTemplate = `MIME-Version: 1.0
200+
Content-Type: multipart/mixed; boundary="BOUNDARY"
201+
202+
--BOUNDARY
203+
Content-Type: text/x-shellscript; charset="us-ascii"
204+
205+
#!/bin/bash
206+
echo "IG manager using AL2023 amis"
207+
{{range $pre := .PreBootstrap}}{{$pre}}{{end}}
208+
{{- range .MountOptions}}
209+
mkfs.{{ .FileSystem | ToLower }} {{ .Device }}
210+
mkdir {{ .Mount }}
211+
mount {{ .Device }} {{ .Mount }}
212+
mount
213+
{{- if .Persistance}}
214+
echo "{{ .Device}} {{ .Mount }} {{ .FileSystem | ToLower }} defaults 0 2" >> /etc/fstab
215+
{{- end}}
216+
{{- end}}
217+
if [[ $(type -P $(which aws)) ]] && [[ $(type -P $(which jq)) ]] ; then
218+
TOKEN=$(curl -X PUT "http://169.254.169.254/latest/api/token" -H "X-aws-ec2-metadata-token-ttl-seconds: 21600")
219+
INSTANCE_ID=$(curl url -H "X-aws-ec2-metadata-token: $TOKEN" http://169.254.169.254/latest/meta-data/instance-id)
220+
REGION=$(curl url -H "X-aws-ec2-metadata-token: $TOKEN" http://169.254.169.254/latest/meta-data/placement/region)
221+
LIFECYCLE=$(curl url -H "X-aws-ec2-metadata-token: $TOKEN" http://169.254.169.254/latest/meta-data/autoscaling/target-lifecycle-state)
222+
if [[ $LIFECYCLE == *"Warmed"* ]]; then
223+
rm /var/lib/cloud/instances/$INSTANCE_ID/sem/config_scripts_user
224+
exit 0
225+
fi
226+
fi
227+
--BOUNDARY
228+
Content-Type: application/node.eks.aws
229+
230+
{{ .NodeConfigYaml }}
231+
232+
--BOUNDARY
233+
Content-Type: application/node.eks.aws
234+
235+
---
236+
apiVersion: node.eks.aws/v1alpha1
237+
kind: NodeConfig
238+
spec:
239+
kubelet:
240+
flags:
241+
- --node-labels={{ $first := true }}{{ range $key, $value := .NodeLabels }}{{if not $first}},{{end}}{{ $key }}={{ $value }}{{ $first = false}}{{- end}}
242+
- --register-with-taints={{ $first := true }}{{- range .NodeTaints}}{{if not $first}},{{end}}{{ .Key }}={{ .Value }}:{{ .Effect }}{{ $first = false}}{{- end}}
243+
244+
--BOUNDARY
245+
Content-Type: text/x-shellscript; charset="us-ascii"
246+
247+
#!/bin/bash
248+
set +o xtrace
249+
{{range $post := .PostBootstrap}}{{$post}}{{end}}
250+
--BOUNDARY--`
196251
}
197252

198253
data := EKSUserData{
@@ -206,7 +261,9 @@ set +o xtrace
206261
Arguments: args,
207262
PreBootstrap: payload.PreBootstrap,
208263
PostBootstrap: payload.PostBootstrap,
264+
NodeConfigYaml: payload.NodeConfigYaml,
209265
MountOptions: mounts,
266+
ClusterIP: clusterIP,
210267
}
211268
out := &bytes.Buffer{}
212269
tmpl := template.New("userData").Funcs(template.FuncMap{
@@ -246,6 +303,12 @@ func (ctx *EksInstanceGroupContext) GetUserDataStages() UserDataPayload {
246303
ctx.Log.Error(err, "failed to decode base64 stage data", "stage", stage.Stage, "data", stage.Data)
247304
}
248305
payload.PostBootstrap = append(payload.PostBootstrap, data)
306+
case strings.EqualFold(stage.Stage, v1alpha1.NodeConfigYamlStage):
307+
data, err := common.GetDecodedString(stage.Data)
308+
if err != nil {
309+
ctx.Log.Error(err, "failed to decode base64 stage data", "stage", stage.Stage, "data", stage.Data)
310+
}
311+
payload.NodeConfigYaml = data
249312
default:
250313
ctx.Log.Info("invalid userdata stage will not be rendered", "stage", stage.Stage, "data", stage.Data)
251314
}
@@ -562,7 +625,7 @@ func (ctx *EksInstanceGroupContext) GetBootstrapArgs() string {
562625
sb.WriteString(fmt.Sprintf("-ContainerRuntime %v ", bootstrapOptions.ContainerRuntime))
563626
}
564627
sb.WriteString(fmt.Sprintf("-KubeletExtraArgs '%v'", ctx.GetKubeletExtraArgs()))
565-
case OsFamilyAmazonLinux2:
628+
case OsFamilyAmazonLinux2, OsFamilyAmazonLinux2023:
566629
if bootstrapOptions != nil && bootstrapOptions.MaxPods > 0 {
567630
sb.WriteString("--use-max-pods false ")
568631
}
@@ -1225,10 +1288,13 @@ func (ctx *EksInstanceGroupContext) GetEksLatestAmi() (string, error) {
12251288
)
12261289
clusterVersion := state.GetClusterVersion()
12271290
annotations := instanceGroup.GetAnnotations()
1291+
overrideAmazonLinuxFamily := strings.Trim(ctx.AmazonLinuxOsFamily, "\" ")
12281292

12291293
var OSFamily string
12301294
if kubeprovider.HasAnnotation(annotations, OsFamilyAnnotation) {
12311295
OSFamily = annotations[OsFamilyAnnotation]
1296+
} else if overrideAmazonLinuxFamily != "" {
1297+
OSFamily = overrideAmazonLinuxFamily
12321298
} else {
12331299
OSFamily = OsFamilyAmazonLinux2
12341300
}

0 commit comments

Comments
 (0)