|
| 1 | +// Copyright 2025 Nutanix. All rights reserved. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +package generic |
| 5 | + |
| 6 | +import ( |
| 7 | + "context" |
| 8 | + "fmt" |
| 9 | + "net/url" |
| 10 | + |
| 11 | + "github.com/go-logr/logr" |
| 12 | + "github.com/regclient/regclient" |
| 13 | + "github.com/regclient/regclient/config" |
| 14 | + "github.com/regclient/regclient/types/ping" |
| 15 | + "github.com/regclient/regclient/types/ref" |
| 16 | + corev1 "k8s.io/api/core/v1" |
| 17 | + "k8s.io/apimachinery/pkg/types" |
| 18 | + clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1" |
| 19 | + ctrlclient "sigs.k8s.io/controller-runtime/pkg/client" |
| 20 | + |
| 21 | + carenv1 "github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/api/v1alpha1" |
| 22 | + "github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/pkg/webhook/preflight" |
| 23 | +) |
| 24 | + |
| 25 | +var registryMirrorVarPath = "cluster.spec.topology.variables[.name=clusterConfig].value.globalImageRegistryMirror" |
| 26 | + |
| 27 | +type registryCheck struct { |
| 28 | + field string |
| 29 | + kclient ctrlclient.Client |
| 30 | + cluster *clusterv1.Cluster |
| 31 | + regClientPingerGetter regClientPingerFactory |
| 32 | + log logr.Logger |
| 33 | + |
| 34 | + registryURL string |
| 35 | + credentials *carenv1.RegistryCredentials |
| 36 | +} |
| 37 | + |
| 38 | +func (r *registryCheck) Name() string { |
| 39 | + return "RegistryCredentials" |
| 40 | +} |
| 41 | + |
| 42 | +func (r *registryCheck) Run(ctx context.Context) preflight.CheckResult { |
| 43 | + return r.checkRegistry( |
| 44 | + ctx, |
| 45 | + r.registryURL, |
| 46 | + r.credentials, |
| 47 | + r.regClientPingerGetter, |
| 48 | + ) |
| 49 | +} |
| 50 | + |
| 51 | +type regClientPinger interface { |
| 52 | + Ping(context.Context, ref.Ref) (ping.Result, error) |
| 53 | +} |
| 54 | + |
| 55 | +type regClientPingerFactory func(...regclient.Opt) regClientPinger |
| 56 | + |
| 57 | +func defaultRegClientGetter(opts ...regclient.Opt) regClientPinger { |
| 58 | + return regclient.New(opts...) |
| 59 | +} |
| 60 | + |
| 61 | +func pingFailedReasonString(registryURL string, err error) string { |
| 62 | + return fmt.Sprintf("failed to ping registry %s with err: %s", registryURL, err.Error()) |
| 63 | +} |
| 64 | + |
| 65 | +func (r *registryCheck) checkRegistry( |
| 66 | + ctx context.Context, |
| 67 | + registryURL string, |
| 68 | + credentials *carenv1.RegistryCredentials, |
| 69 | + regClientGetter regClientPingerFactory, |
| 70 | +) preflight.CheckResult { |
| 71 | + result := preflight.CheckResult{ |
| 72 | + Allowed: false, |
| 73 | + } |
| 74 | + if r.registryURL == "" { |
| 75 | + result.Allowed = true |
| 76 | + return result |
| 77 | + } |
| 78 | + registryURLParsed, err := url.ParseRequestURI(registryURL) |
| 79 | + if err != nil { |
| 80 | + result.Allowed = false |
| 81 | + result.Error = true |
| 82 | + result.Causes = append(result.Causes, |
| 83 | + preflight.Cause{ |
| 84 | + Message: fmt.Sprintf("failed to parse registry url %s with error : %s", registryURL, err), |
| 85 | + Field: registryMirrorVarPath, |
| 86 | + }, |
| 87 | + ) |
| 88 | + return result |
| 89 | + } |
| 90 | + mirrorHost := config.Host{ |
| 91 | + Name: registryURLParsed.Host, |
| 92 | + } |
| 93 | + if credentials != nil && credentials.SecretRef != nil { |
| 94 | + mirrorCredentialsSecret := &corev1.Secret{} |
| 95 | + err := r.kclient.Get( |
| 96 | + ctx, |
| 97 | + types.NamespacedName{ |
| 98 | + Namespace: r.cluster.Namespace, |
| 99 | + Name: credentials.SecretRef.Name, |
| 100 | + }, |
| 101 | + mirrorCredentialsSecret, |
| 102 | + ) |
| 103 | + if err != nil { |
| 104 | + result.Allowed = false |
| 105 | + result.Error = true |
| 106 | + result.Causes = append(result.Causes, |
| 107 | + preflight.Cause{ |
| 108 | + Message: fmt.Sprintf("failed to get Registry credentials Secret: %s", err), |
| 109 | + Field: fmt.Sprintf("%s.credentials.secretRef", registryMirrorVarPath), |
| 110 | + }, |
| 111 | + ) |
| 112 | + return result |
| 113 | + } |
| 114 | + username, ok := mirrorCredentialsSecret.Data["username"] |
| 115 | + if ok { |
| 116 | + mirrorHost.User = string(username) |
| 117 | + } |
| 118 | + password, ok := mirrorCredentialsSecret.Data["password"] |
| 119 | + if ok { |
| 120 | + mirrorHost.Pass = string(password) |
| 121 | + } |
| 122 | + if caCert, ok := mirrorCredentialsSecret.Data["ca.crt"]; ok { |
| 123 | + mirrorHost.RegCert = string(caCert) |
| 124 | + } |
| 125 | + } |
| 126 | + rc := regClientGetter( |
| 127 | + regclient.WithConfigHost(mirrorHost), |
| 128 | + regclient.WithUserAgent("regclient/example"), |
| 129 | + ) |
| 130 | + mirrorRef, err := ref.NewHost(registryURLParsed.Host) |
| 131 | + if err != nil { |
| 132 | + result.Allowed = false |
| 133 | + result.Error = true |
| 134 | + result.Causes = append(result.Causes, |
| 135 | + preflight.Cause{ |
| 136 | + Message: fmt.Sprintf("failed to create a client to verify registry configuration %s", err.Error()), |
| 137 | + Field: registryMirrorVarPath, |
| 138 | + }, |
| 139 | + ) |
| 140 | + return result |
| 141 | + } |
| 142 | + _, err = rc.Ping(ctx, mirrorRef) // ping will return an error for anything that's not 200 |
| 143 | + if err != nil { |
| 144 | + result.Allowed = false |
| 145 | + result.Causes = append(result.Causes, |
| 146 | + preflight.Cause{ |
| 147 | + Message: pingFailedReasonString(registryURL, err), |
| 148 | + Field: registryMirrorVarPath, |
| 149 | + }, |
| 150 | + ) |
| 151 | + return result |
| 152 | + } |
| 153 | + result.Allowed = true |
| 154 | + result.Error = false |
| 155 | + return result |
| 156 | +} |
| 157 | + |
| 158 | +func newRegistryCheck( |
| 159 | + cd *checkDependencies, |
| 160 | +) []preflight.Check { |
| 161 | + checks := []preflight.Check{} |
| 162 | + if cd.genericClusterConfigSpec != nil && |
| 163 | + cd.genericClusterConfigSpec.GlobalImageRegistryMirror != nil { |
| 164 | + checks = append(checks, ®istryCheck{ |
| 165 | + field: "cluster.spec.topology.variables[.name=clusterConfig].value.globalImageRegistryMirror", |
| 166 | + kclient: cd.kclient, |
| 167 | + cluster: cd.cluster, |
| 168 | + regClientPingerGetter: defaultRegClientGetter, |
| 169 | + log: cd.log, |
| 170 | + registryURL: cd.genericClusterConfigSpec.GlobalImageRegistryMirror.DeepCopy().URL, |
| 171 | + credentials: cd.genericClusterConfigSpec.GlobalImageRegistryMirror.DeepCopy().Credentials, |
| 172 | + }) |
| 173 | + } |
| 174 | + if cd.genericClusterConfigSpec != nil && len(cd.genericClusterConfigSpec.ImageRegistries) > 0 { |
| 175 | + for i := range cd.genericClusterConfigSpec.ImageRegistries { |
| 176 | + registry := cd.genericClusterConfigSpec.ImageRegistries[i] |
| 177 | + checks = append(checks, ®istryCheck{ |
| 178 | + field: fmt.Sprintf( |
| 179 | + "cluster.spec.topology.variables[.name=clusterConfig].value.imageRegistries[%d]", |
| 180 | + i, |
| 181 | + ), |
| 182 | + kclient: cd.kclient, |
| 183 | + cluster: cd.cluster, |
| 184 | + regClientPingerGetter: defaultRegClientGetter, |
| 185 | + log: cd.log, |
| 186 | + registryURL: registry.DeepCopy().URL, |
| 187 | + credentials: registry.DeepCopy().Credentials, |
| 188 | + }) |
| 189 | + } |
| 190 | + } |
| 191 | + return checks |
| 192 | +} |
0 commit comments