|
| 1 | +// Copyright 2025 Nutanix. All rights reserved. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +package nutanix |
| 5 | + |
| 6 | +import ( |
| 7 | + "context" |
| 8 | + "fmt" |
| 9 | + |
| 10 | + clustermgmtv4 "github.com/nutanix/ntnx-api-golang-clients/clustermgmt-go-client/v4/models/clustermgmt/v4/config" |
| 11 | + "k8s.io/utils/ptr" |
| 12 | + |
| 13 | + prismv4 "github.com/nutanix-cloud-native/prism-go-client/v4" |
| 14 | + |
| 15 | + "github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/api/external/github.com/nutanix-cloud-native/cluster-api-provider-nutanix/api/v1beta1" |
| 16 | + carenv1 "github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/api/v1alpha1" |
| 17 | + "github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/pkg/webhook/preflight" |
| 18 | +) |
| 19 | + |
| 20 | +func (n *nutanixChecker) initStorageContainerChecks() []preflight.Check { |
| 21 | + checks := []preflight.Check{} |
| 22 | + |
| 23 | + if n.nutanixClusterConfigSpec != nil && n.nutanixClusterConfigSpec.ControlPlane != nil && |
| 24 | + n.nutanixClusterConfigSpec.ControlPlane.Nutanix != nil { |
| 25 | + checks = append(checks, |
| 26 | + n.storageContainerCheck( |
| 27 | + n.nutanixClusterConfigSpec.ControlPlane.Nutanix, |
| 28 | + "cluster.spec.topology[.name=clusterConfig].value.controlPlane.nutanix", |
| 29 | + &n.nutanixClusterConfigSpec.Addons.CSI.Providers.NutanixCSI, |
| 30 | + ), |
| 31 | + ) |
| 32 | + } |
| 33 | + |
| 34 | + for mdName, nutanixWorkerNodeConfigSpec := range n.nutanixWorkerNodeConfigSpecByMachineDeploymentName { |
| 35 | + if nutanixWorkerNodeConfigSpec.Nutanix != nil { |
| 36 | + checks = append(checks, |
| 37 | + n.storageContainerCheck( |
| 38 | + nutanixWorkerNodeConfigSpec.Nutanix, |
| 39 | + fmt.Sprintf( |
| 40 | + "cluster.spec.topology.workers.machineDeployments[.name=%s]"+ |
| 41 | + ".variables[.name=workerConfig].value.nutanix", |
| 42 | + mdName, |
| 43 | + ), |
| 44 | + &n.nutanixClusterConfigSpec.Addons.CSI.Providers.NutanixCSI, |
| 45 | + ), |
| 46 | + ) |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + return checks |
| 51 | +} |
| 52 | + |
| 53 | +// storageContainerCheck checks if the storage container specified in the CSIProvider's StorageClassConfigs exists. |
| 54 | +// It admits the NodeSpec instead of the MachineDetails because the failure domains will be specified in the NodeSpec |
| 55 | +// and the MachineDetails.Cluster will be nil in that case. |
| 56 | +func (n *nutanixChecker) storageContainerCheck( |
| 57 | + nodeSpec *carenv1.NutanixNodeSpec, |
| 58 | + field string, |
| 59 | + csiSpec *carenv1.CSIProvider, |
| 60 | +) preflight.Check { |
| 61 | + const ( |
| 62 | + csiParameterKeyStorageContainer = "storageContainer" |
| 63 | + ) |
| 64 | + |
| 65 | + return func(ctx context.Context) preflight.CheckResult { |
| 66 | + result := &preflight.CheckResult{} |
| 67 | + if csiSpec == nil { |
| 68 | + result.Allowed = false |
| 69 | + result.Error = true |
| 70 | + result.Causes = append(result.Causes, preflight.Cause{ |
| 71 | + Message: fmt.Sprintf( |
| 72 | + "no storage container found for cluster %q", |
| 73 | + *nodeSpec.MachineDetails.Cluster.Name, |
| 74 | + ), |
| 75 | + Field: field, |
| 76 | + }) |
| 77 | + |
| 78 | + return *result |
| 79 | + } |
| 80 | + |
| 81 | + if csiSpec.StorageClassConfigs == nil { |
| 82 | + result.Allowed = false |
| 83 | + result.Causes = append(result.Causes, preflight.Cause{ |
| 84 | + Message: fmt.Sprintf( |
| 85 | + "no storage class configs found for cluster %q", |
| 86 | + *nodeSpec.MachineDetails.Cluster.Name, |
| 87 | + ), |
| 88 | + Field: field, |
| 89 | + }) |
| 90 | + |
| 91 | + return *result |
| 92 | + } |
| 93 | + |
| 94 | + for _, storageClassConfig := range csiSpec.StorageClassConfigs { |
| 95 | + if storageClassConfig.Parameters == nil { |
| 96 | + continue |
| 97 | + } |
| 98 | + |
| 99 | + storageContainer, ok := storageClassConfig.Parameters[csiParameterKeyStorageContainer] |
| 100 | + if !ok { |
| 101 | + continue |
| 102 | + } |
| 103 | + |
| 104 | + // TODO: check if cluster name is set, if not use uuid. |
| 105 | + // If neither is set, use the cluster name from the NodeSpec failure domain. |
| 106 | + if _, err := getStorageContainer(n.v4client, nodeSpec, storageContainer); err != nil { |
| 107 | + result.Allowed = false |
| 108 | + result.Error = true |
| 109 | + result.Causes = append(result.Causes, preflight.Cause{ |
| 110 | + Message: fmt.Sprintf( |
| 111 | + "failed to check if storage container named %q exists: %s", |
| 112 | + storageContainer, |
| 113 | + err, |
| 114 | + ), |
| 115 | + Field: field, |
| 116 | + }) |
| 117 | + |
| 118 | + return *result |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + return *result |
| 123 | + } |
| 124 | +} |
| 125 | + |
| 126 | +func getStorageContainer( |
| 127 | + client *prismv4.Client, |
| 128 | + nodeSpec *carenv1.NutanixNodeSpec, |
| 129 | + storageContainerName string, |
| 130 | +) (*clustermgmtv4.StorageContainer, error) { |
| 131 | + cluster, err := getCluster(client, &nodeSpec.MachineDetails.Cluster) |
| 132 | + if err != nil { |
| 133 | + return nil, fmt.Errorf("failed to get cluster: %w", err) |
| 134 | + } |
| 135 | + |
| 136 | + fltr := fmt.Sprintf("name eq %q and clusterExtId eq %q", storageContainerName, *cluster.ExtId) |
| 137 | + resp, err := client.StorageContainerAPI.ListStorageContainers(nil, nil, &fltr, nil, nil) |
| 138 | + if err != nil { |
| 139 | + return nil, fmt.Errorf("failed to list storage containers: %w", err) |
| 140 | + } |
| 141 | + |
| 142 | + containers, ok := resp.GetData().([]clustermgmtv4.StorageContainer) |
| 143 | + if !ok { |
| 144 | + return nil, fmt.Errorf("failed to get data returned by ListStorageContainers(filter=%q)", fltr) |
| 145 | + } |
| 146 | + |
| 147 | + if len(containers) == 0 { |
| 148 | + return nil, fmt.Errorf( |
| 149 | + "no storage container named %q found on cluster named %q", |
| 150 | + storageContainerName, |
| 151 | + *cluster.Name, |
| 152 | + ) |
| 153 | + } |
| 154 | + |
| 155 | + if len(containers) > 1 { |
| 156 | + return nil, fmt.Errorf( |
| 157 | + "multiple storage containers found with name %q on cluster %q", |
| 158 | + storageContainerName, |
| 159 | + *cluster.Name, |
| 160 | + ) |
| 161 | + } |
| 162 | + |
| 163 | + return ptr.To(containers[0]), nil |
| 164 | +} |
| 165 | + |
| 166 | +func getCluster( |
| 167 | + client *prismv4.Client, |
| 168 | + clusterIdentifier *v1beta1.NutanixResourceIdentifier, |
| 169 | +) (*clustermgmtv4.Cluster, error) { |
| 170 | + switch clusterIdentifier.Type { |
| 171 | + case v1beta1.NutanixIdentifierUUID: |
| 172 | + resp, err := client.ClustersApiInstance.GetClusterById(clusterIdentifier.UUID) |
| 173 | + if err != nil { |
| 174 | + return nil, err |
| 175 | + } |
| 176 | + |
| 177 | + cluster, ok := resp.GetData().(clustermgmtv4.Cluster) |
| 178 | + if !ok { |
| 179 | + return nil, fmt.Errorf("failed to get data returned by GetClusterById") |
| 180 | + } |
| 181 | + |
| 182 | + return &cluster, nil |
| 183 | + case v1beta1.NutanixIdentifierName: |
| 184 | + filter := fmt.Sprintf("name eq '%s'", *clusterIdentifier.Name) |
| 185 | + resp, err := client.ClustersApiInstance.ListClusters(nil, nil, &filter, nil, nil, nil) |
| 186 | + if err != nil { |
| 187 | + return nil, err |
| 188 | + } |
| 189 | + |
| 190 | + if resp == nil || resp.GetData() == nil { |
| 191 | + return nil, fmt.Errorf("no clusters were returned") |
| 192 | + } |
| 193 | + |
| 194 | + clusters, ok := resp.GetData().([]clustermgmtv4.Cluster) |
| 195 | + if !ok { |
| 196 | + return nil, fmt.Errorf("failed to get data returned by ListClusters") |
| 197 | + } |
| 198 | + |
| 199 | + if len(clusters) == 0 { |
| 200 | + return nil, fmt.Errorf("no clusters found with name %q", *clusterIdentifier.Name) |
| 201 | + } |
| 202 | + |
| 203 | + if len(clusters) > 1 { |
| 204 | + return nil, fmt.Errorf("multiple clusters found with name %q", *clusterIdentifier.Name) |
| 205 | + } |
| 206 | + |
| 207 | + return &clusters[0], nil |
| 208 | + default: |
| 209 | + return nil, fmt.Errorf("cluster identifier is missing both name and uuid") |
| 210 | + } |
| 211 | +} |
0 commit comments