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