Skip to content

Commit 2488f2f

Browse files
committed
fix: use ObjectReference for secretRef in API
1 parent 55cb650 commit 2488f2f

File tree

7 files changed

+50
-17
lines changed

7 files changed

+50
-17
lines changed

api/v1alpha1/clusterconfig_types.go

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
package v1alpha1
55

66
import (
7+
corev1 "k8s.io/api/core/v1"
78
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
89

910
"github.com/d2iq-labs/capi-runtime-extensions/common/pkg/capi/clustertopology/variables"
@@ -185,11 +186,11 @@ type ImageRegistryCredentials struct {
185186
// Registry URL.
186187
URL string `json:"url"`
187188

188-
// Name of the Secret containing the registry credentials.
189+
// The Secret containing the registry credentials.
189190
// The Secret should have keys 'username' and 'password'.
190191
// This credentials Secret is not required for some registries, e.g. ECR.
191192
// +optional
192-
Secret string `json:"secret,omitempty"`
193+
Secret *corev1.ObjectReference `json:"secretRef,omitempty"`
193194
}
194195

195196
func (ImageRegistryCredentials) VariableSchema() clusterv1.VariableSchema {
@@ -202,11 +203,23 @@ func (ImageRegistryCredentials) VariableSchema() clusterv1.VariableSchema {
202203
Description: "Registry URL.",
203204
Type: "string",
204205
},
205-
"secret": {
206-
Description: "Name of the Secret containing the registry credentials. " +
206+
"secretRef": {
207+
Description: "The Secret containing the registry credentials. " +
207208
"The Secret should have keys 'username' and 'password'. " +
208209
"This credentials Secret is not required for some registries, e.g. ECR.",
209-
Type: "string",
210+
Type: "object",
211+
Properties: map[string]clusterv1.JSONSchemaProps{
212+
"name": {
213+
Description: "The name of the Secret containing the registry credentials.",
214+
Type: "string",
215+
},
216+
"namespace": {
217+
Description: "The namespace of the Secret containing the registry credentials. " +
218+
"Defaults to the namespace of the KubeadmControlPlaneTemplate and KubeadmConfigTemplate" +
219+
" that reference this variable.",
220+
Type: "string",
221+
},
222+
},
210223
},
211224
},
212225
Required: []string{"url"},

api/v1alpha1/zz_generated.deepcopy.go

Lines changed: 7 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/content/customization/_index.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@ spec:
4141
- no-proxy-2.example.com
4242
imageRegistryCredentials:
4343
url: https://my-registry.io
44-
secret: my-registry-credentials
44+
secretRef:
45+
name: my-registry-credentials
4546
cni:
4647
provider: calico
4748
```

docs/content/customization/generic/image-registry-credentials.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ spec:
3737
value:
3838
imageRegistryCredentials:
3939
url: https://my-registry.io
40-
secret: my-registry-credentials
40+
secretRef:
41+
name: my-registry-credentials
4142
```
4243
4344
Applying this configuration will result in new files and preKubeadmCommands

pkg/handlers/generic/mutation/imageregistrycredentials/inject.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -296,14 +296,19 @@ func secretForImageRegistryCredentials(
296296
ctx context.Context,
297297
c ctrlclient.Reader,
298298
credentials v1alpha1.ImageRegistryCredentials,
299-
namespace string,
299+
objectNamespace string,
300300
) (*corev1.Secret, error) {
301-
if credentials.Secret == "" {
301+
if credentials.Secret == nil {
302302
return nil, nil
303303
}
304304

305+
namespace := objectNamespace
306+
if credentials.Secret.Namespace != "" {
307+
namespace = credentials.Secret.Namespace
308+
}
309+
305310
key := ctrlclient.ObjectKey{
306-
Name: credentials.Secret,
311+
Name: credentials.Secret.Name,
307312
Namespace: namespace,
308313
}
309314
secret := &corev1.Secret{}

pkg/handlers/generic/mutation/imageregistrycredentials/inject_test.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,10 @@ func TestGeneratePatches(t *testing.T) {
7272
capitest.VariableWithValue(
7373
variableName,
7474
v1alpha1.ImageRegistryCredentials{
75-
URL: "https://my-registry.io",
76-
Secret: validSecretName,
75+
URL: "https://my-registry.io",
76+
Secret: &corev1.ObjectReference{
77+
Name: validSecretName,
78+
},
7779
},
7880
),
7981
},
@@ -129,8 +131,10 @@ func TestGeneratePatches(t *testing.T) {
129131
capitest.VariableWithValue(
130132
variableName,
131133
v1alpha1.ImageRegistryCredentials{
132-
URL: "https://my-registry.io",
133-
Secret: validSecretName,
134+
URL: "https://my-registry.io",
135+
Secret: &corev1.ObjectReference{
136+
Name: validSecretName,
137+
},
134138
},
135139
),
136140
capitest.VariableWithValue(

pkg/handlers/generic/mutation/imageregistrycredentials/variables_test.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package imageregistrycredentials
66
import (
77
"testing"
88

9+
corev1 "k8s.io/api/core/v1"
910
"k8s.io/utils/ptr"
1011

1112
"github.com/d2iq-labs/capi-runtime-extensions/api/v1alpha1"
@@ -28,8 +29,10 @@ func TestVariableValidation(t *testing.T) {
2829
capitest.VariableTestDef{
2930
Name: "with a Secret",
3031
Vals: v1alpha1.ImageRegistryCredentials{
31-
URL: "http://a.b.c.example.com",
32-
Secret: "a.b.c.example.com-creds",
32+
URL: "http://a.b.c.example.com",
33+
Secret: &corev1.ObjectReference{
34+
Name: "a.b.c.example.com-creds",
35+
},
3336
},
3437
},
3538
)

0 commit comments

Comments
 (0)