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