Skip to content

Commit f38fafb

Browse files
committed
fix: schema validation for registry and mirror URL
1 parent c2de147 commit f38fafb

File tree

5 files changed

+50
-5
lines changed

5 files changed

+50
-5
lines changed

api/v1alpha1/clusterconfig_types.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@ func (ImageCredentials) VariableSchema() clusterv1.VariableSchema {
266266
},
267267
"namespace": {
268268
Description: "The namespace of the Secret containing the registry credentials. " +
269-
"Defaults to the namespace of the KubeadmControlPlaneTemplate and KubeadmConfigTemplate" +
269+
"Defaults to the namespace of the KubeadmControlPlaneTemplate and KubeadmConfigTemplate " +
270270
"that reference this variable.",
271271
Type: "string",
272272
},
@@ -295,6 +295,8 @@ func (GlobalImageRegistryMirror) VariableSchema() clusterv1.VariableSchema {
295295
"url": {
296296
Description: "Registry mirror URL.",
297297
Type: "string",
298+
Format: "uri",
299+
Pattern: "^https?://",
298300
},
299301
"credentials": ImageCredentials{}.VariableSchema().OpenAPIV3Schema,
300302
},
@@ -320,6 +322,8 @@ func (ImageRegistry) VariableSchema() clusterv1.VariableSchema {
320322
"url": {
321323
Description: "Registry URL.",
322324
Type: "string",
325+
Format: "uri",
326+
Pattern: "^https?://",
323327
},
324328
"credentials": ImageCredentials{}.VariableSchema().OpenAPIV3Schema,
325329
},

docs/content/customization/generic/global-mirror.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ If your registry mirror requires a private or self-signed CA certificate,
1717
create a Kubernetes Secret with the `ca.crt` key populated with the CA certificate in PEM format:
1818

1919
```shell
20-
kubectl create secret generic my-mirror-ca-cert-secret \
20+
kubectl create secret generic my-mirror-ca-cert \
2121
--from-file=ca.crt=registry-ca.crt
2222
```
2323

@@ -35,7 +35,7 @@ spec:
3535
url: https://my-mirror.io
3636
credentials:
3737
secretRef:
38-
name: my-mirror-ca-cert-secret
38+
name: my-mirror-ca-cert
3939
```
4040
4141
Applying this configuration will result in following new files on the

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,8 @@ func (h *imageRegistriesPatchHandler) Mutate(
8484

8585
// TODO: Add support for multiple registries.
8686
if len(imageRegistries) > 1 {
87-
return fmt.Errorf("multiple Image Registry are not supported at this time")
87+
return fmt.Errorf("multiple Image Registry are not supported at this time. "+
88+
"Provide a single registry entry for %s variable", imageregistries.VariableName)
8889
}
8990

9091
imageRegistry := imageRegistries[0]

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

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ func TestVariableValidation(t *testing.T) {
3636
Vals: v1alpha1.GenericClusterConfig{
3737
ImageRegistries: []v1alpha1.ImageRegistry{
3838
{
39-
URL: "http://a.b.c.example.com",
39+
URL: "https://a.b.c.example.com/a/b/c",
4040
Credentials: &v1alpha1.ImageCredentials{
4141
SecretRef: &corev1.ObjectReference{
4242
Name: "a.b.c.example.com-creds",
@@ -46,5 +46,27 @@ func TestVariableValidation(t *testing.T) {
4646
},
4747
},
4848
},
49+
capitest.VariableTestDef{
50+
Name: "invalid registry URL",
51+
Vals: v1alpha1.GenericClusterConfig{
52+
ImageRegistries: []v1alpha1.ImageRegistry{
53+
{
54+
URL: "unsupportedformat://a.b.c.example.com",
55+
},
56+
},
57+
},
58+
ExpectError: true,
59+
},
60+
capitest.VariableTestDef{
61+
Name: "registry URL without format",
62+
Vals: v1alpha1.GenericClusterConfig{
63+
ImageRegistries: []v1alpha1.ImageRegistry{
64+
{
65+
URL: "a.b.c.example.com/a/b/c",
66+
},
67+
},
68+
},
69+
ExpectError: true,
70+
},
4971
)
5072
}

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,5 +42,23 @@ func TestVariableValidation(t *testing.T) {
4242
},
4343
},
4444
},
45+
capitest.VariableTestDef{
46+
Name: "invalid mirror registry URL",
47+
Vals: v1alpha1.GenericClusterConfig{
48+
GlobalImageRegistryMirror: &v1alpha1.GlobalImageRegistryMirror{
49+
URL: "unsupportedformat://a.b.c.example.com",
50+
},
51+
},
52+
ExpectError: true,
53+
},
54+
capitest.VariableTestDef{
55+
Name: "mirror URL without format",
56+
Vals: v1alpha1.GenericClusterConfig{
57+
GlobalImageRegistryMirror: &v1alpha1.GlobalImageRegistryMirror{
58+
URL: "a.b.c.example.com/a/b/c",
59+
},
60+
},
61+
ExpectError: true,
62+
},
4563
)
4664
}

0 commit comments

Comments
 (0)