Skip to content

Commit dfa843a

Browse files
committed
fix: Storage container names are unique in a PC Cluster
1 parent 66f1b71 commit dfa843a

File tree

2 files changed

+34
-15
lines changed

2 files changed

+34
-15
lines changed

pkg/webhook/preflight/nutanix/storagecontainer.go

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ func (c *storageContainerCheck) Run(ctx context.Context) preflight.CheckResult {
6161
continue
6262
}
6363

64-
storageContainer, ok := storageClassConfig.Parameters[csiParameterKeyStorageContainer]
64+
storageContainerName, ok := storageClassConfig.Parameters[csiParameterKeyStorageContainer]
6565
if !ok {
6666
continue
6767
}
@@ -73,7 +73,7 @@ func (c *storageContainerCheck) Run(ctx context.Context) preflight.CheckResult {
7373
result.Causes = append(result.Causes, preflight.Cause{
7474
Message: fmt.Sprintf(
7575
"Failed to check if storage container %q exists: failed to get cluster %q: %s. This is usually a temporary error. Please retry.", ///nolint:lll // Message is long.
76-
storageContainer,
76+
storageContainerName,
7777
clusterIdentifier,
7878
err,
7979
),
@@ -86,7 +86,7 @@ func (c *storageContainerCheck) Run(ctx context.Context) preflight.CheckResult {
8686
result.Allowed = false
8787
result.Causes = append(result.Causes, preflight.Cause{
8888
Message: fmt.Sprintf(
89-
"Found %d Clusters (Prism Elements) in Prism Central that match identifier %q. There must be exactly 1 Cluster that matches this identifier. Make the Cluster identifiers unique, and then retry.", ///nolint:lll // Message is long.
89+
"Found %d Clusters (Prism Elements) in Prism Central that match identifier %q. There must be exactly 1 Cluster that matches this identifier. Use a unique Cluster name, or identify the Cluster by its UUID, then retry.", ///nolint:lll // Message is long.
9090
len(clusters),
9191
clusterIdentifier,
9292
),
@@ -98,14 +98,14 @@ func (c *storageContainerCheck) Run(ctx context.Context) preflight.CheckResult {
9898
// Found exactly one cluster.
9999
cluster := &clusters[0]
100100

101-
containers, err := getStorageContainers(c.nclient, *cluster.ExtId, storageContainer)
101+
containers, err := getStorageContainers(c.nclient, *cluster.ExtId, storageContainerName)
102102
if err != nil {
103103
result.Allowed = false
104104
result.InternalError = true
105105
result.Causes = append(result.Causes, preflight.Cause{
106106
Message: fmt.Sprintf(
107107
"Failed to check if Storage Container %q exists in cluster %q: %s. This is usually a temporary error. Please retry.", ///nolint:lll // Message is long.
108-
storageContainer,
108+
storageContainerName,
109109
clusterIdentifier,
110110
err,
111111
),
@@ -114,18 +114,37 @@ func (c *storageContainerCheck) Run(ctx context.Context) preflight.CheckResult {
114114
continue
115115
}
116116

117-
if len(containers) != 1 {
117+
// Because Storage Container names are unique within a Cluster, we will either find exactly one
118+
// Storage Container with the given name, or none at all.
119+
switch len(containers) {
120+
case 0:
118121
result.Allowed = false
119122
result.Causes = append(result.Causes, preflight.Cause{
120123
Message: fmt.Sprintf(
121-
"Found %d Storage Containers that match identifier %q on Cluster %q. There must be exactly 1 Storage Container that matches this identifier. Make the Storage Container identifiers unique, or use a different Storage Container, and then retry.", ///nolint:lll // Message is long.
122-
len(containers),
123-
storageContainer,
124+
"Found no Storage Containers with name %q on Cluster %q. Create a Storage Container with this name on Cluster %q, and then retry.", ///nolint:lll // Message is long.
125+
storageContainerName,
126+
clusterIdentifier,
124127
clusterIdentifier,
125128
),
126129
Field: c.field,
127130
})
131+
case 1:
128132
continue
133+
default: // 2 or more Storage Containers with the same name found on the same Cluster.
134+
// This is an unexpected situation, as Storage Container names should be unique within a Cluster.
135+
// We log this as an internal error, as it indicates a potential issue with the Nutanix API or the
136+
// underlying data.
137+
result.Allowed = false
138+
result.InternalError = true
139+
result.Causes = append(result.Causes, preflight.Cause{
140+
Message: fmt.Sprintf(
141+
"Found %d Storage Containers with name %q on Cluster %q. This should not happen under normal circumstances. Please report.", ///nolint:lll // Message is long.
142+
len(containers),
143+
storageContainerName,
144+
clusterIdentifier,
145+
),
146+
Field: c.field,
147+
})
129148
}
130149
}
131150

pkg/webhook/preflight/nutanix/storagecontainer_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -384,10 +384,10 @@ func TestStorageContainerCheck(t *testing.T) {
384384
},
385385
expectedAllowed: false,
386386
expectedError: false,
387-
expectedCauseMessage: "Found 0 Storage Containers that match identifier \"missing-container\" on Cluster \"test-cluster\". There must be exactly 1 Storage Container that matches this identifier. Make the Storage Container identifiers unique, or use a different Storage Container, and then retry.", //nolint:lll // Message is long.
387+
expectedCauseMessage: "Found no Storage Containers with name \"missing-container\" on Cluster \"test-cluster\". Create a Storage Container with this name on Cluster \"test-cluster\", and then retry.", //nolint:lll // Message is long.
388388
},
389389
{
390-
name: "multiple storage containers found",
390+
name: "multiple storage containers with same name in same cluster found",
391391
machineSpec: &carenv1.NutanixMachineDetails{
392392
Cluster: capxv1.NutanixResourceIdentifier{
393393
Type: capxv1.NutanixIdentifierName,
@@ -458,8 +458,8 @@ func TestStorageContainerCheck(t *testing.T) {
458458
},
459459
},
460460
expectedAllowed: false,
461-
expectedError: false,
462-
expectedCauseMessage: "Found 2 Storage Containers that match identifier \"duplicate-container\" on Cluster \"test-cluster\". There must be exactly 1 Storage Container that matches this identifier. Make the Storage Container identifiers unique, or use a different Storage Container, and then retry.", //nolint:lll // The message is long.
461+
expectedError: true,
462+
expectedCauseMessage: "Found 2 Storage Containers with name \"duplicate-container\" on Cluster \"test-cluster\". This should not happen under normal circumstances. Please report.", //nolint:lll // The message is long.
463463
},
464464
{
465465
name: "successful storage container check",
@@ -581,7 +581,7 @@ func TestStorageContainerCheck(t *testing.T) {
581581
},
582582
expectedAllowed: false,
583583
expectedError: false,
584-
expectedCauseMessage: "Found 2 Clusters (Prism Elements) in Prism Central that match identifier \"test-cluster\". There must be exactly 1 Cluster that matches this identifier. Make the Cluster identifiers unique, and then retry.", //nolint:lll // The message is long.
584+
expectedCauseMessage: "Found 2 Clusters (Prism Elements) in Prism Central that match identifier \"test-cluster\". There must be exactly 1 Cluster that matches this identifier. Use a unique Cluster name, or identify the Cluster by its UUID, then retry.", //nolint:lll // The message is long.
585585
},
586586
{
587587
name: "error getting cluster",
@@ -813,7 +813,7 @@ func TestStorageContainerCheck(t *testing.T) {
813813
},
814814
expectedAllowed: false,
815815
expectedError: false,
816-
expectedCauseMessage: "Found 0 Storage Containers that match identifier \"valid-container\" on Cluster \"test-cluster\". There must be exactly 1 Storage Container that matches this identifier. Make the Storage Container identifiers unique, or use a different Storage Container, and then retry.", //nolint:lll // The message is long.
816+
expectedCauseMessage: "Found no Storage Containers with name \"valid-container\" on Cluster \"test-cluster\". Create a Storage Container with this name on Cluster \"test-cluster\", and then retry.", //nolint:lll // The message is long.
817817
},
818818
{
819819
name: "multiple storage class configs with success",

0 commit comments

Comments
 (0)