Skip to content

Commit c1b9688

Browse files
committed
Add support to update both legacy and default path for kubelet-extra-args for ubuntu (aws#1177)
* Add support to update both legacy and default path for kubelet-extra-args for ubuntu * fix: rename filepath to keep it consistent to variable name
1 parent 91d3bef commit c1b9688

File tree

4 files changed

+104
-59
lines changed

4 files changed

+104
-59
lines changed

credentialproviderpackage/charts/credential-provider-package/templates/_helpers.tpl

+2
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,8 @@ Function to figure out os name
8181
{{- printf "bottlerocket" -}}
8282
{{- else if contains "Amazon Linux" .status.nodeInfo.osImage -}}
8383
{{- printf "default" -}}
84+
{{- else if contains "Ubuntu" .status.nodeInfo.osImage -}}
85+
{{- printf "ubuntu" -}}
8486
{{- else -}}
8587
{{- printf "sysconfig" -}}
8688
{{- end }}

credentialproviderpackage/charts/credential-provider-package/templates/daemonset.yaml

+16
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,13 @@ spec:
4040
{{- if eq $os "bottlerocket" }}
4141
- mountPath: /run/api.sock
4242
name: socket
43+
{{- else if eq $os "ubuntu" }}
44+
- mountPath: /node-files/kubelet-extra-args
45+
name: kubelet-extra-args
46+
- mountPath: /node-files/ubuntu-legacy-kubelet-extra-args
47+
name: ubuntu-legacy-kubelet-extra-args
48+
- name: package-mounts
49+
mountPath: /eksa-packages
4350
{{- else}}
4451
- mountPath: /node-files/kubelet-extra-args
4552
name: kubelet-extra-args
@@ -72,6 +79,15 @@ spec:
7279
hostPath:
7380
path: /etc/default/kubelet
7481
type: FileOrCreate
82+
{{- else if eq $os "ubuntu"}}
83+
- name: kubelet-extra-args
84+
hostPath:
85+
path: /etc/default/kubelet
86+
type: FileOrCreate
87+
- name: ubuntu-legacy-kubelet-extra-args
88+
hostPath:
89+
path: /etc/sysconfig/kubelet
90+
type: FileOrCreate
7591
{{- else}}
7692
- name: kubelet-extra-args
7793
hostPath:

credentialproviderpackage/pkg/configurator/linux/linux.go

+35-18
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
_ "embed"
55
"fmt"
66
"io"
7-
"io/ioutil"
87
"os"
98
"strings"
109
"syscall"
@@ -22,31 +21,34 @@ import (
2221
var credProviderTemplate string
2322

2423
const (
25-
binPath = "/eksa-binaries/"
26-
basePath = "/eksa-packages/"
27-
credOutFile = "aws-creds"
28-
mountedExtraArgs = "/node-files/kubelet-extra-args"
29-
credProviderFile = "credential-provider-config.yaml"
24+
binPath = "/eksa-binaries/"
25+
basePath = "/eksa-packages/"
26+
credOutFile = "aws-creds"
27+
mountedExtraArgs = "/node-files/kubelet-extra-args"
28+
ubuntuLegacyExtraArgs = "/node-files/ubuntu-legacy-kubelet-extra-args"
29+
credProviderFile = "credential-provider-config.yaml"
3030

3131
// Binaries
3232
ecrCredProviderBinary = "ecr-credential-provider"
3333
iamRolesSigningBinary = "aws_signing_helper"
3434
)
3535

3636
type linuxOS struct {
37-
profile string
38-
extraArgsPath string
39-
basePath string
40-
config constants.CredentialProviderConfigOptions
37+
profile string
38+
extraArgsPath string
39+
legacyExtraArgsPath string
40+
basePath string
41+
config constants.CredentialProviderConfigOptions
4142
}
4243

4344
var _ configurator.Configurator = (*linuxOS)(nil)
4445

4546
func NewLinuxConfigurator() *linuxOS {
4647
return &linuxOS{
47-
profile: "",
48-
extraArgsPath: mountedExtraArgs,
49-
basePath: basePath,
48+
profile: "",
49+
extraArgsPath: mountedExtraArgs,
50+
legacyExtraArgsPath: ubuntuLegacyExtraArgs,
51+
basePath: basePath,
5052
}
5153
}
5254

@@ -62,9 +64,8 @@ func (c *linuxOS) UpdateAWSCredentials(sourcePath, profile string) error {
6264
return err
6365
}
6466

65-
func (c *linuxOS) UpdateCredentialProvider(_ string) error {
66-
// Adding to KUBELET_EXTRA_ARGS in place
67-
file, err := ioutil.ReadFile(c.extraArgsPath)
67+
func (c *linuxOS) updateConfigFile(configPath string) error {
68+
file, err := os.ReadFile(configPath)
6869
if err != nil {
6970
return err
7071
}
@@ -91,10 +92,26 @@ func (c *linuxOS) UpdateCredentialProvider(_ string) error {
9192
}
9293

9394
out := strings.Join(lines, "\n")
94-
err = ioutil.WriteFile(c.extraArgsPath, []byte(out), 0o644)
95+
err = os.WriteFile(configPath, []byte(out), 0o644)
9596
return err
9697
}
9798

99+
func (c *linuxOS) UpdateCredentialProvider(_ string) error {
100+
// Adding to KUBELET_EXTRA_ARGS in place
101+
if err := c.updateConfigFile(mountedExtraArgs); err != nil {
102+
return fmt.Errorf("failed to update kubelet args: %v", err)
103+
}
104+
105+
// Adding KUBELET_EXTRA_ARGS to legacy path for ubuntu
106+
if _, err := os.Stat(ubuntuLegacyExtraArgs); err == nil {
107+
if err := c.updateConfigFile(ubuntuLegacyExtraArgs); err != nil {
108+
return fmt.Errorf("failed to update legacy kubelet args for ubuntu: %v", err)
109+
}
110+
}
111+
112+
return nil
113+
}
114+
98115
func (c *linuxOS) CommitChanges() error {
99116
process, err := findKubeletProcess()
100117
if err != nil {
@@ -208,7 +225,7 @@ func (c *linuxOS) createConfig() (string, error) {
208225
if err != nil {
209226
return "", nil
210227
}
211-
err = ioutil.WriteFile(dstPath, bytes, 0o600)
228+
err = os.WriteFile(dstPath, bytes, 0o600)
212229
if err != nil {
213230
return "", err
214231
}

credentialproviderpackage/pkg/configurator/linux/linux_test.go

+51-41
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package linux
22

33
import (
44
"fmt"
5-
"io/ioutil"
65
"os"
76
"testing"
87

@@ -16,10 +15,11 @@ func Test_linuxOS_updateKubeletArguments(t *testing.T) {
1615
testDir, _ := test.NewWriter(t)
1716
dir := testDir + "/"
1817
type fields struct {
19-
profile string
20-
extraArgsPath string
21-
basePath string
22-
config constants.CredentialProviderConfigOptions
18+
profile string
19+
extraArgsPath string
20+
legacyExtraArgsPath string
21+
basePath string
22+
config constants.CredentialProviderConfigOptions
2323
}
2424
type args struct {
2525
line string
@@ -36,9 +36,10 @@ func Test_linuxOS_updateKubeletArguments(t *testing.T) {
3636
{
3737
name: "test empty string",
3838
fields: fields{
39-
profile: "eksa-packages",
40-
extraArgsPath: dir,
41-
basePath: dir,
39+
profile: "eksa-packages",
40+
extraArgsPath: dir,
41+
legacyExtraArgsPath: dir,
42+
basePath: dir,
4243
config: constants.CredentialProviderConfigOptions{
4344
ImagePatterns: []string{constants.DefaultImagePattern},
4445
DefaultCacheDuration: constants.DefaultCacheDuration,
@@ -52,9 +53,10 @@ func Test_linuxOS_updateKubeletArguments(t *testing.T) {
5253
{
5354
name: "test multiple match patterns",
5455
fields: fields{
55-
profile: "eksa-packages",
56-
extraArgsPath: dir,
57-
basePath: dir,
56+
profile: "eksa-packages",
57+
extraArgsPath: dir,
58+
legacyExtraArgsPath: dir,
59+
basePath: dir,
5860
config: constants.CredentialProviderConfigOptions{
5961
ImagePatterns: []string{
6062
"1234567.dkr.ecr.us-east-1.amazonaws.com",
@@ -71,9 +73,10 @@ func Test_linuxOS_updateKubeletArguments(t *testing.T) {
7173
{
7274
name: "skip credential provider if already provided",
7375
fields: fields{
74-
profile: "eksa-packages",
75-
extraArgsPath: dir,
76-
basePath: dir,
76+
profile: "eksa-packages",
77+
extraArgsPath: dir,
78+
legacyExtraArgsPath: dir,
79+
basePath: dir,
7780
config: constants.CredentialProviderConfigOptions{
7881
ImagePatterns: []string{constants.DefaultImagePattern},
7982
DefaultCacheDuration: constants.DefaultCacheDuration,
@@ -87,9 +90,10 @@ func Test_linuxOS_updateKubeletArguments(t *testing.T) {
8790
{
8891
name: "test alpha api",
8992
fields: fields{
90-
profile: "eksa-packages",
91-
extraArgsPath: dir,
92-
basePath: dir,
93+
profile: "eksa-packages",
94+
extraArgsPath: dir,
95+
legacyExtraArgsPath: dir,
96+
basePath: dir,
9397
config: constants.CredentialProviderConfigOptions{
9498
ImagePatterns: []string{constants.DefaultImagePattern},
9599
DefaultCacheDuration: constants.DefaultCacheDuration,
@@ -104,9 +108,10 @@ func Test_linuxOS_updateKubeletArguments(t *testing.T) {
104108
{
105109
name: "test v1 api 1.27",
106110
fields: fields{
107-
profile: "eksa-packages",
108-
extraArgsPath: dir,
109-
basePath: dir,
111+
profile: "eksa-packages",
112+
extraArgsPath: dir,
113+
legacyExtraArgsPath: dir,
114+
basePath: dir,
110115
config: constants.CredentialProviderConfigOptions{
111116
ImagePatterns: []string{constants.DefaultImagePattern},
112117
DefaultCacheDuration: constants.DefaultCacheDuration,
@@ -122,10 +127,11 @@ func Test_linuxOS_updateKubeletArguments(t *testing.T) {
122127
for _, tt := range tests {
123128
t.Run(tt.name, func(t *testing.T) {
124129
c := &linuxOS{
125-
profile: tt.fields.profile,
126-
extraArgsPath: tt.fields.extraArgsPath,
127-
basePath: tt.fields.basePath,
128-
config: tt.fields.config,
130+
profile: tt.fields.profile,
131+
extraArgsPath: tt.fields.extraArgsPath,
132+
legacyExtraArgsPath: tt.fields.legacyExtraArgsPath,
133+
basePath: tt.fields.basePath,
134+
config: tt.fields.config,
129135
}
130136
t.Setenv("K8S_VERSION", tt.k8sVersion)
131137

@@ -143,10 +149,11 @@ func Test_linuxOS_UpdateAWSCredentials(t *testing.T) {
143149
testDir, _ := test.NewWriter(t)
144150
dir := testDir + "/"
145151
type fields struct {
146-
profile string
147-
extraArgsPath string
148-
basePath string
149-
config constants.CredentialProviderConfigOptions
152+
profile string
153+
extraArgsPath string
154+
legacyExtraArgsPath string
155+
basePath string
156+
config constants.CredentialProviderConfigOptions
150157
}
151158
type args struct {
152159
sourcePath string
@@ -161,9 +168,10 @@ func Test_linuxOS_UpdateAWSCredentials(t *testing.T) {
161168
{
162169
name: "simple credential move",
163170
fields: fields{
164-
profile: "eksa-packages",
165-
extraArgsPath: dir,
166-
basePath: dir,
171+
profile: "eksa-packages",
172+
extraArgsPath: dir,
173+
legacyExtraArgsPath: dir,
174+
basePath: dir,
167175
config: constants.CredentialProviderConfigOptions{
168176
ImagePatterns: []string{constants.DefaultImagePattern},
169177
DefaultCacheDuration: constants.DefaultCacheDuration,
@@ -180,10 +188,11 @@ func Test_linuxOS_UpdateAWSCredentials(t *testing.T) {
180188
t.Run(tt.name, func(t *testing.T) {
181189
dstFile := tt.fields.basePath + credOutFile
182190
c := &linuxOS{
183-
profile: tt.fields.profile,
184-
extraArgsPath: tt.fields.extraArgsPath,
185-
basePath: tt.fields.basePath,
186-
config: tt.fields.config,
191+
profile: tt.fields.profile,
192+
extraArgsPath: tt.fields.extraArgsPath,
193+
legacyExtraArgsPath: tt.fields.legacyExtraArgsPath,
194+
basePath: tt.fields.basePath,
195+
config: tt.fields.config,
187196
}
188197
if err := c.UpdateAWSCredentials(tt.args.sourcePath, tt.args.profile); (err != nil) != tt.wantErr {
189198
t.Errorf("UpdateAWSCredentials() error = %v, wantErr %v", err, tt.wantErr)
@@ -199,12 +208,12 @@ func Test_linuxOS_UpdateAWSCredentials(t *testing.T) {
199208
if err != nil {
200209
t.Errorf("Failed to set file back to readable")
201210
}
202-
expectedCreds, err := ioutil.ReadFile(tt.args.sourcePath)
211+
expectedCreds, err := os.ReadFile(tt.args.sourcePath)
203212
if err != nil {
204213
t.Errorf("Failed to read source credential file")
205214
}
206215

207-
actualCreds, err := ioutil.ReadFile(dstFile)
216+
actualCreds, err := os.ReadFile(dstFile)
208217
if err != nil {
209218
t.Errorf("Failed to read created credential file")
210219
}
@@ -215,10 +224,11 @@ func Test_linuxOS_UpdateAWSCredentials(t *testing.T) {
215224

216225
func Test_linuxOS_Initialize(t *testing.T) {
217226
type fields struct {
218-
profile string
219-
extraArgsPath string
220-
basePath string
221-
config constants.CredentialProviderConfigOptions
227+
profile string
228+
extraArgsPath string
229+
legacyExtraArgsPath string
230+
basePath string
231+
config constants.CredentialProviderConfigOptions
222232
}
223233
type args struct {
224234
config constants.CredentialProviderConfigOptions

0 commit comments

Comments
 (0)