@@ -6,7 +6,7 @@ package generic
66import (
77 "context"
88 "fmt"
9- "regexp "
9+ "net/url "
1010
1111 "github.com/go-logr/logr"
1212 "github.com/regclient/regclient"
@@ -22,13 +22,7 @@ import (
2222 "github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/pkg/webhook/preflight"
2323)
2424
25- var (
26- registryMirrorVarPath = "cluster.spec.topology.variables[.name=clusterConfig].value.globalImageRegistryMirror"
27- mirrorURLValidationRegex = regexp .MustCompile (
28- `^https?://` ,
29- ) // in order to use regclient we need to pass just a hostname
30- // this regex allows us to strip it so we can verify connectivity for this test.
31- )
25+ var registryMirrorVarPath = "cluster.spec.topology.variables[.name=clusterConfig].value.globalImageRegistryMirror"
3226
3327type registryCheck struct {
3428 registryMirror * carenv1.GlobalImageRegistryMirror
@@ -87,12 +81,23 @@ func (r *registryCheck) checkRegistry(
8781 credentials * carenv1.RegistryCredentials ,
8882 regClientGetter regClientPingerFactory ,
8983) preflight.CheckResult {
90- registryURL = mirrorURLValidationRegex .ReplaceAllString (registryURL , "" )
9184 result := preflight.CheckResult {
9285 Allowed : false ,
9386 }
87+ registryURLParsed , err := url .ParseRequestURI (registryURL )
88+ if err != nil {
89+ result .Allowed = false
90+ result .Error = true
91+ result .Causes = append (result .Causes ,
92+ preflight.Cause {
93+ Message : fmt .Sprintf ("failed to parse registry url %s with error : %s" , registryURL , err ),
94+ Field : registryMirrorVarPath ,
95+ },
96+ )
97+ return result
98+ }
9499 mirrorHost := config.Host {
95- Name : registryURL ,
100+ Name : registryURLParsed . Host ,
96101 }
97102 if credentials != nil && credentials .SecretRef != nil {
98103 mirrorCredentialsSecret := & corev1.Secret {}
@@ -116,31 +121,13 @@ func (r *registryCheck) checkRegistry(
116121 return result
117122 }
118123 username , ok := mirrorCredentialsSecret .Data ["username" ]
119- if ! ok {
120- result .Allowed = false
121- result .Error = true
122- result .Causes = append (result .Causes ,
123- preflight.Cause {
124- Message : "failed to get username from Registry credentials Secret. secret must have field username." ,
125- Field : fmt .Sprintf ("%s.credentials.secretRef" , registryMirrorVarPath ),
126- },
127- )
128- return result
124+ if ok {
125+ mirrorHost .User = string (username )
129126 }
130127 password , ok := mirrorCredentialsSecret .Data ["password" ]
131- if ! ok {
132- result .Allowed = false
133- result .Error = true
134- result .Causes = append (result .Causes ,
135- preflight.Cause {
136- Message : "failed to get password from Registry credentials Secret. secret must have field password." ,
137- Field : fmt .Sprintf ("%s.credentials.secretRef" , registryMirrorVarPath ),
138- },
139- )
140- return result
128+ if ok {
129+ mirrorHost .Pass = string (password )
141130 }
142- mirrorHost .User = string (username )
143- mirrorHost .Pass = string (password )
144131 if caCert , ok := mirrorCredentialsSecret .Data ["ca.crt" ]; ok {
145132 mirrorHost .RegCert = string (caCert )
146133 }
@@ -149,7 +136,7 @@ func (r *registryCheck) checkRegistry(
149136 regclient .WithConfigHost (mirrorHost ),
150137 regclient .WithUserAgent ("regclient/example" ),
151138 )
152- mirrorRef , err := ref .NewHost (registryURL )
139+ mirrorRef , err := ref .NewHost (registryURLParsed . Host )
153140 if err != nil {
154141 result .Allowed = false
155142 result .Error = true
0 commit comments