|
| 1 | +// Copyright (c) Microsoft Corporation. |
| 2 | +// Licensed under the MIT license. |
| 3 | +package customizations |
| 4 | + |
| 5 | +import ( |
| 6 | + "context" |
| 7 | + "strings" |
| 8 | + |
| 9 | + . "github.com/Azure/azure-service-operator/v2/internal/logging" |
| 10 | + |
| 11 | + armstorage "github.com/Azure/ARO-HCP/test/sdk/resourcemanager/redhatopenshifthcp/armredhatopenshifthcp" |
| 12 | + "github.com/Azure/azure-sdk-for-go/sdk/azcore/runtime" |
| 13 | + "github.com/go-logr/logr" |
| 14 | + "github.com/rotisserie/eris" |
| 15 | + v1 "k8s.io/api/core/v1" |
| 16 | + "sigs.k8s.io/controller-runtime/pkg/conversion" |
| 17 | + |
| 18 | + "github.com/Azure/azure-service-operator/v2/api/redhatopenshift/v1api20240610preview/storage" |
| 19 | + "github.com/Azure/azure-service-operator/v2/internal/genericarmclient" |
| 20 | + "github.com/Azure/azure-service-operator/v2/internal/resolver" |
| 21 | + "github.com/Azure/azure-service-operator/v2/internal/set" |
| 22 | + "github.com/Azure/azure-service-operator/v2/internal/util/to" |
| 23 | + "github.com/Azure/azure-service-operator/v2/pkg/genruntime" |
| 24 | + "github.com/Azure/azure-service-operator/v2/pkg/genruntime/extensions" |
| 25 | + "github.com/Azure/azure-service-operator/v2/pkg/genruntime/secrets" |
| 26 | +) |
| 27 | + |
| 28 | +var _ extensions.PreReconciliationChecker = &HcpOpenShiftClusterExtension{} |
| 29 | + |
| 30 | +// PreReconcileCheck does a pre-reconcile check to see if the resource is in a state that can be reconciled. |
| 31 | +// ARM resources should implement this to avoid reconciliation attempts that cannot possibly succeed. |
| 32 | +// Returns ProceedWithReconcile if the reconciliation should go ahead. |
| 33 | +// Returns BlockReconcile and a human-readable reason if the reconciliation should be skipped. |
| 34 | +// ctx is the current operation context. |
| 35 | +// obj is the resource about to be reconciled. The resource's State will be freshly updated. |
| 36 | +// kubeClient allows access to the cluster for any required queries. |
| 37 | +// armClient allows access to ARM for any required queries. |
| 38 | +// log is the logger for the current operation. |
| 39 | +// next is the next (nested) implementation to call. |
| 40 | +func (ext *HcpOpenShiftClusterExtension) PreReconcileCheck(ctx context.Context, |
| 41 | + obj genruntime.MetaObject, |
| 42 | + owner genruntime.MetaObject, |
| 43 | + resourceResolver *resolver.Resolver, |
| 44 | + armClient *genericarmclient.GenericClient, |
| 45 | + log logr.Logger, |
| 46 | + next extensions.PreReconcileCheckFunc, |
| 47 | +) (extensions.PreReconcileCheckResult, error) { |
| 48 | + // This has to be the current hub storage version of the hcpOpenShiftCluster. |
| 49 | + // It will need to be updated if the hub storage version changes. |
| 50 | + hcpOpenShiftCluster, ok := obj.(*storage.HcpOpenShiftCluster) |
| 51 | + if !ok { |
| 52 | + return extensions.PreReconcileCheckResult{}, eris.Errorf("cannot run on unknown resource type %T, expected *storage.HcpOpenShiftCluster", obj) |
| 53 | + } |
| 54 | + |
| 55 | + // Type assert that we are the hub type. This will fail to compile if |
| 56 | + // the hub type has been changed but this extension has not |
| 57 | + var _ conversion.Hub = hcpOpenShiftCluster |
| 58 | + |
| 59 | + // If the hcpOpenShiftCluster is already deleting, we have to wait for that to finish |
| 60 | + // before trying anything else |
| 61 | + if hcpOpenShiftCluster.Status.Properties != nil && |
| 62 | + hcpOpenShiftCluster.Status.Properties.ProvisioningState != nil && |
| 63 | + strings.EqualFold(*hcpOpenShiftCluster.Status.Properties.ProvisioningState, "Deleting") { |
| 64 | + return extensions.BlockReconcile("reconcile blocked while hcpOpenShiftCluster is at status deleting"), nil |
| 65 | + } |
| 66 | + |
| 67 | + return next(ctx, obj, owner, resourceResolver, armClient, log) |
| 68 | +} |
| 69 | + |
| 70 | +var _ genruntime.KubernetesSecretExporter = &HcpOpenShiftClusterExtension{} |
| 71 | + |
| 72 | +const ( |
| 73 | + BackupInstancePollerResumeTokenAnnotation = "serviceoperator.azure.com/bi-poller-resume-token" |
| 74 | +) |
| 75 | + |
| 76 | +func GetPollerResumeToken(obj genruntime.MetaObject, log logr.Logger) (string, bool) { |
| 77 | + log.V(Debug).Info("GetPollerResumeToken") |
| 78 | + token, hasResumeToken := obj.GetAnnotations()[BackupInstancePollerResumeTokenAnnotation] |
| 79 | + return token, hasResumeToken |
| 80 | +} |
| 81 | + |
| 82 | +func SetPollerResumeToken(obj genruntime.MetaObject, token string, log logr.Logger) { |
| 83 | + log.V(Debug).Info("SetPollerResumeToken") |
| 84 | + genruntime.AddAnnotation(obj, BackupInstancePollerResumeTokenAnnotation, token) |
| 85 | +} |
| 86 | + |
| 87 | +// ClearPollerResumeToken clears the poller resume token and ID annotations |
| 88 | +func ClearPollerResumeToken(obj genruntime.MetaObject, log logr.Logger) { |
| 89 | + log.V(Debug).Info("ClearPollerResumeToken") |
| 90 | + genruntime.RemoveAnnotation(obj, BackupInstancePollerResumeTokenAnnotation) |
| 91 | +} |
| 92 | + |
| 93 | +func (ext *HcpOpenShiftClusterExtension) ExportKubernetesSecrets( |
| 94 | + ctx context.Context, |
| 95 | + obj genruntime.MetaObject, |
| 96 | + additionalSecrets set.Set[string], |
| 97 | + armClient *genericarmclient.GenericClient, |
| 98 | + log logr.Logger, |
| 99 | +) (*genruntime.KubernetesSecretExportResult, error) { |
| 100 | + // This has to be the current hub storage version. It will need to be updated |
| 101 | + // if the hub storage version changes. |
| 102 | + typedObj, ok := obj.(*storage.HcpOpenShiftCluster) |
| 103 | + if !ok { |
| 104 | + return nil, eris.Errorf("cannot run on unknown resource type %T, expected *storage.HcpOpenShiftCluster", obj) |
| 105 | + } |
| 106 | + |
| 107 | + // Type assert that we are the hub type. This will fail to compile if |
| 108 | + // the hub type has been changed but this extension has not |
| 109 | + var _ conversion.Hub = typedObj |
| 110 | + |
| 111 | + primarySecrets := secretsSpecifiedHcp(typedObj) |
| 112 | + requestedSecrets := set.Union(primarySecrets, additionalSecrets) |
| 113 | + |
| 114 | + if len(requestedSecrets) == 0 { |
| 115 | + log.V(Debug).Info("No secrets retrieval to perform as operatorSpec is empty") |
| 116 | + return nil, nil |
| 117 | + } |
| 118 | + |
| 119 | + id, err := genruntime.GetAndParseResourceID(typedObj) |
| 120 | + if err != nil { |
| 121 | + return nil, err |
| 122 | + } |
| 123 | + |
| 124 | + subscription := id.SubscriptionID |
| 125 | + // Using armClient.ClientOptions() here ensures we share the same HTTP connection, so this is not opening a new |
| 126 | + // connection each time through |
| 127 | + var clusterClient *armstorage.HcpOpenShiftClustersClient |
| 128 | + clusterClient, err = armstorage.NewHcpOpenShiftClustersClient(subscription, armClient.Creds(), armClient.ClientOptions()) |
| 129 | + if err != nil { |
| 130 | + return nil, eris.Wrapf(err, "failed to create new NewOpenShiftClustersClient") |
| 131 | + } |
| 132 | + |
| 133 | + var adminCredentials string |
| 134 | + if requestedSecrets.Contains(adminCredentialsKey) { |
| 135 | + resumeToken, _ := GetPollerResumeToken(typedObj, log) |
| 136 | + opts := &armstorage.HcpOpenShiftClustersClientBeginRequestAdminCredentialOptions{ResumeToken: resumeToken} |
| 137 | + log.V(Debug).Info("Starting BeginRequestAdminCredential") |
| 138 | + var poller *runtime.Poller[armstorage.HcpOpenShiftClustersClientRequestAdminCredentialResponse] |
| 139 | + poller, err = clusterClient.BeginRequestAdminCredential(ctx, id.ResourceGroupName, typedObj.AzureName(), opts) |
| 140 | + if err != nil { |
| 141 | + return nil, eris.Wrapf(err, "failed creating admin credentials") |
| 142 | + } |
| 143 | + if resumeToken == "" { |
| 144 | + resumeToken, resumeTokenErr := poller.ResumeToken() |
| 145 | + if resumeTokenErr != nil { |
| 146 | + return nil, eris.Wrapf(resumeTokenErr, "couldn't create PUT resume token for resource") |
| 147 | + } else { |
| 148 | + SetPollerResumeToken(obj, resumeToken, log) |
| 149 | + } |
| 150 | + } |
| 151 | + _, pollErr := poller.Poll(ctx) |
| 152 | + if pollErr != nil { |
| 153 | + return nil, eris.Wrapf(pollErr, "couldn't poll with PUT resume token for resource") |
| 154 | + } |
| 155 | + |
| 156 | + if poller.Done() { |
| 157 | + log.V(Debug).Info("Polling is completed") |
| 158 | + ClearPollerResumeToken(obj, log) |
| 159 | + resp, err := poller.Result(ctx) |
| 160 | + if err != nil { |
| 161 | + return nil, eris.Wrapf(err, "couldn't get result with PUT resume token for resource") |
| 162 | + } |
| 163 | + adminCredentials = to.Value(resp.HcpOpenShiftClusterAdminCredential.Kubeconfig) |
| 164 | + } else { |
| 165 | + log.V(Debug).Info("Polling is in-progress") |
| 166 | + } |
| 167 | + } |
| 168 | + |
| 169 | + secretSlice, err := secretsToWriteHcp(typedObj, adminCredentials) |
| 170 | + if err != nil { |
| 171 | + return nil, err |
| 172 | + } |
| 173 | + |
| 174 | + resolvedSecrets := map[string]string{} |
| 175 | + if adminCredentials != "" { |
| 176 | + resolvedSecrets[adminCredentialsKey] = adminCredentials |
| 177 | + } |
| 178 | + return &genruntime.KubernetesSecretExportResult{ |
| 179 | + Objs: secrets.SliceToClientObjectSlice(secretSlice), |
| 180 | + RawSecrets: secrets.SelectSecrets(additionalSecrets, resolvedSecrets), |
| 181 | + }, nil |
| 182 | +} |
| 183 | + |
| 184 | +func secretsSpecifiedHcp(obj *storage.HcpOpenShiftCluster) set.Set[string] { |
| 185 | + if obj.Spec.OperatorSpec == nil || obj.Spec.OperatorSpec.Secrets == nil { |
| 186 | + return nil |
| 187 | + } |
| 188 | + |
| 189 | + operatorSecrets := obj.Spec.OperatorSpec.Secrets |
| 190 | + result := set.Set[string]{} |
| 191 | + if operatorSecrets.AdminCredentials != nil { |
| 192 | + result.Add(adminCredentialsKey) |
| 193 | + } |
| 194 | + |
| 195 | + return result |
| 196 | +} |
| 197 | + |
| 198 | +func secretsToWriteHcp(obj *storage.HcpOpenShiftCluster, adminCredentials string) ([]*v1.Secret, error) { |
| 199 | + operatorSpecSecrets := obj.Spec.OperatorSpec.Secrets |
| 200 | + if operatorSpecSecrets == nil { |
| 201 | + return nil, nil |
| 202 | + } |
| 203 | + |
| 204 | + collector := secrets.NewCollector(obj.Namespace) |
| 205 | + collector.AddValue(operatorSpecSecrets.AdminCredentials, adminCredentials) |
| 206 | + |
| 207 | + return collector.Values() |
| 208 | +} |
0 commit comments