Skip to content

Commit 5f3677a

Browse files
committed
fix: skip imageRegistries with no credentials
1 parent 6a96fa1 commit 5f3677a

File tree

2 files changed

+52
-37
lines changed

2 files changed

+52
-37
lines changed

pkg/handlers/generic/mutation/imageregistries/credentials/inject.go

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -131,19 +131,19 @@ func (h *imageRegistriesPatchHandler) Mutate(
131131
)
132132
}
133133

134-
needCredentials, err := needImageRegistryCredentialsConfiguration(
134+
registriesThatNeedConfiguration, err := providerConfigsThatNeedConfiguration(
135135
registriesWithOptionalCredentials,
136136
)
137137
if err != nil {
138138
return err
139139
}
140-
if !needCredentials {
141-
log.V(5).Info("Only Global Registry Mirror is defined but credentials are not needed")
140+
if len(registriesThatNeedConfiguration) == 0 {
141+
log.V(5).Info("Image registry credentials are not needed")
142142
return nil
143143
}
144144

145145
files, commands, generateErr := generateFilesAndCommands(
146-
registriesWithOptionalCredentials,
146+
registriesThatNeedConfiguration,
147147
clusterKey.Name,
148148
)
149149
if generateErr != nil {
@@ -185,7 +185,7 @@ func (h *imageRegistriesPatchHandler) Mutate(
185185
return err
186186
}
187187

188-
err = createSecretIfNeeded(ctx, h.client, registriesWithOptionalCredentials, cluster)
188+
err = createSecretIfNeeded(ctx, h.client, registriesThatNeedConfiguration, cluster)
189189
if err != nil {
190190
return err
191191
}
@@ -243,7 +243,7 @@ func (h *imageRegistriesPatchHandler) Mutate(
243243
return err
244244
}
245245

246-
err = createSecretIfNeeded(ctx, h.client, registriesWithOptionalCredentials, cluster)
246+
err = createSecretIfNeeded(ctx, h.client, registriesThatNeedConfiguration, cluster)
247247
if err != nil {
248248
return err
249249
}
@@ -444,30 +444,30 @@ func createSecretIfNeeded(
444444
// and if that is not the case we assume the users missed setting static credentials and return an error.
445445
// However, in addition to passing credentials with the globalImageRegistryMirror variable,
446446
// it can also be used to only set Containerd mirror configuration,
447-
// in that case it valid for static credentials to not be set and will return false, no error
447+
// in which case it is valid for static credentials to not be set and will be skipped, no error
448448
// and this handler will skip generating any credential plugin related configuration.
449-
func needImageRegistryCredentialsConfiguration(configs []providerConfig) (bool, error) {
450-
var needConfiguration bool
449+
func providerConfigsThatNeedConfiguration(configs []providerConfig) ([]providerConfig, error) {
450+
var needConfiguration []providerConfig //nolint:prealloc // We don't know the size of the slice yet.
451451
for _, config := range configs {
452452
requiresStaticCredentials, err := config.requiresStaticCredentials()
453453
if err != nil {
454-
return false,
454+
return nil,
455455
fmt.Errorf("error determining if Image Registry is a supported provider: %w", err)
456456
}
457457
// verify the credentials are actually set if the plugin requires static credentials
458458
if config.isCredentialsEmpty() && requiresStaticCredentials {
459459
if config.Mirror || config.HasCACert {
460-
// not setting credentials for a mirror is valid
461-
// not setting credentials for a registry with a CA cert is valid
460+
// not setting credentials for a mirror is valid, but won't need any configuration
461+
// not setting credentials for a registry with a CA cert is valid, but won't need any configuration
462462
continue
463463
}
464-
return false, fmt.Errorf(
464+
return nil, fmt.Errorf(
465465
"invalid image registry: %s: %w",
466466
config.URL,
467467
ErrCredentialsNotFound,
468468
)
469469
}
470-
needConfiguration = true
470+
needConfiguration = append(needConfiguration, config)
471471
}
472472

473473
return needConfiguration, nil

pkg/handlers/generic/mutation/imageregistries/credentials/inject_test.go

Lines changed: 38 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -33,21 +33,23 @@ const (
3333
validSecretName = "myregistry-credentials"
3434
)
3535

36-
func Test_needImageRegistryCredentialsConfiguration(t *testing.T) {
36+
func Test_providerConfigsThatNeedConfiguration(t *testing.T) {
3737
t.Parallel()
3838

3939
testCases := []struct {
40-
name string
41-
configs []providerConfig
42-
need bool
43-
wantErr error
40+
name string
41+
configs []providerConfig
42+
expected []providerConfig
43+
wantErr error
4444
}{
4545
{
4646
name: "ECR registry with no credentials",
4747
configs: []providerConfig{
4848
{URL: "https://123456789.dkr.ecr.us-east-1.amazonaws.com"},
4949
},
50-
need: true,
50+
expected: []providerConfig{
51+
{URL: "https://123456789.dkr.ecr.us-east-1.amazonaws.com"},
52+
},
5153
},
5254
{
5355
name: "registry with static credentials",
@@ -56,17 +58,22 @@ func Test_needImageRegistryCredentialsConfiguration(t *testing.T) {
5658
Username: "myuser",
5759
Password: "mypassword",
5860
}},
59-
need: true,
61+
expected: []providerConfig{{
62+
URL: "https://myregistry.com",
63+
Username: "myuser",
64+
Password: "mypassword",
65+
}},
6066
},
6167
{
6268
name: "ECR mirror with no credentials",
63-
configs: []providerConfig{
64-
{
65-
URL: "https://123456789.dkr.ecr.us-east-1.amazonaws.com",
66-
Mirror: true,
67-
},
68-
},
69-
need: true,
69+
configs: []providerConfig{{
70+
URL: "https://123456789.dkr.ecr.us-east-1.amazonaws.com",
71+
Mirror: true,
72+
}},
73+
expected: []providerConfig{{
74+
URL: "https://123456789.dkr.ecr.us-east-1.amazonaws.com",
75+
Mirror: true,
76+
}},
7077
},
7178
{
7279
name: "mirror with static credentials",
@@ -76,15 +83,20 @@ func Test_needImageRegistryCredentialsConfiguration(t *testing.T) {
7683
Password: "mypassword",
7784
Mirror: true,
7885
}},
79-
need: true,
86+
expected: []providerConfig{{
87+
URL: "https://mymirror.com",
88+
Username: "myuser",
89+
Password: "mypassword",
90+
Mirror: true,
91+
}},
8092
},
8193
{
8294
name: "mirror with no credentials",
8395
configs: []providerConfig{{
8496
URL: "https://mymirror.com",
8597
Mirror: true,
8698
}},
87-
need: false,
99+
expected: nil,
88100
},
89101
{
90102
name: "a registry with static credentials and a mirror with no credentials",
@@ -100,7 +112,14 @@ func Test_needImageRegistryCredentialsConfiguration(t *testing.T) {
100112
Mirror: true,
101113
},
102114
},
103-
need: true,
115+
expected: []providerConfig{
116+
{
117+
URL: "https://myregistry.com",
118+
Username: "myuser",
119+
Password: "mypassword",
120+
Mirror: false,
121+
},
122+
},
104123
},
105124
{
106125
name: "a registry with missing credentials and a mirror with no credentials",
@@ -114,15 +133,13 @@ func Test_needImageRegistryCredentialsConfiguration(t *testing.T) {
114133
Mirror: true,
115134
},
116135
},
117-
need: false,
118136
wantErr: ErrCredentialsNotFound,
119137
},
120138
{
121139
name: "registry with missing credentials",
122140
configs: []providerConfig{{
123141
URL: "https://myregistry.com",
124142
}},
125-
need: false,
126143
wantErr: ErrCredentialsNotFound,
127144
},
128145
{
@@ -131,7 +148,6 @@ func Test_needImageRegistryCredentialsConfiguration(t *testing.T) {
131148
URL: "https://myregistry.com",
132149
HasCACert: true,
133150
}},
134-
need: false,
135151
},
136152
{
137153
name: "mirror with missing credentials but with a CA",
@@ -140,7 +156,6 @@ func Test_needImageRegistryCredentialsConfiguration(t *testing.T) {
140156
HasCACert: true,
141157
Mirror: true,
142158
}},
143-
need: false,
144159
},
145160
}
146161

@@ -150,9 +165,9 @@ func Test_needImageRegistryCredentialsConfiguration(t *testing.T) {
150165
t.Run(tt.name, func(t *testing.T) {
151166
t.Parallel()
152167

153-
need, err := needImageRegistryCredentialsConfiguration(tt.configs)
168+
expected, err := providerConfigsThatNeedConfiguration(tt.configs)
154169
require.ErrorIs(t, err, tt.wantErr)
155-
assert.Equal(t, tt.need, need)
170+
assert.Equal(t, tt.expected, expected)
156171
})
157172
}
158173
}

0 commit comments

Comments
 (0)