@@ -6,9 +6,9 @@ package nutanix
6
6
import (
7
7
"context"
8
8
"fmt"
9
+ "sync"
9
10
10
11
clustermgmtv4 "github.com/nutanix/ntnx-api-golang-clients/clustermgmt-go-client/v4/models/clustermgmt/v4/config"
11
- "k8s.io/utils/ptr"
12
12
13
13
"github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/api/external/github.com/nutanix-cloud-native/cluster-api-provider-nutanix/api/v1beta1"
14
14
carenv1 "github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/api/v1alpha1"
@@ -62,6 +62,14 @@ func (c *storageContainerCheck) Run(ctx context.Context) preflight.CheckResult {
62
62
return result
63
63
}
64
64
65
+ clusterIdentifier := & c .nodeSpec .MachineDetails .Cluster
66
+
67
+ // We wait to get the cluster until we know we need to.
68
+ // We should only get the cluster once.
69
+ getClusterOnce := sync .OnceValues (func () (* clustermgmtv4.Cluster , error ) {
70
+ return getCluster (c .nclient , clusterIdentifier )
71
+ })
72
+
65
73
for _ , storageClassConfig := range c .csiSpec .StorageClassConfigs {
66
74
if storageClassConfig .Parameters == nil {
67
75
continue
@@ -72,19 +80,62 @@ func (c *storageContainerCheck) Run(ctx context.Context) preflight.CheckResult {
72
80
continue
73
81
}
74
82
75
- if _ , err := getStorageContainer (c .nclient , c .nodeSpec , storageContainer ); err != nil {
83
+ cluster , err := getClusterOnce ()
84
+ if err != nil {
85
+ result .Allowed = false
86
+ result .Error = true
87
+ result .Causes = append (result .Causes , preflight.Cause {
88
+ Message : fmt .Sprintf (
89
+ "failed to check if storage container %q exists: failed to get cluster %q: %s" ,
90
+ storageContainer ,
91
+ clusterIdentifier ,
92
+ err ,
93
+ ),
94
+ Field : c .field ,
95
+ })
96
+ continue
97
+ }
98
+
99
+ containers , err := getStorageContainers (c .nclient , * cluster .ExtId , storageContainer )
100
+ if err != nil {
76
101
result .Allowed = false
77
102
result .Error = true
78
103
result .Causes = append (result .Causes , preflight.Cause {
79
104
Message : fmt .Sprintf (
80
- "failed to check if storage container named %q exists: %s" ,
105
+ "failed to check if storage container %q exists in cluster %q : %s" ,
81
106
storageContainer ,
107
+ clusterIdentifier ,
82
108
err ,
83
109
),
84
110
Field : c .field ,
85
111
})
112
+ continue
113
+ }
114
+
115
+ if len (containers ) == 0 {
116
+ result .Allowed = false
117
+ result .Causes = append (result .Causes , preflight.Cause {
118
+ Message : fmt .Sprintf (
119
+ "storage container %q not found on cluster %q" ,
120
+ storageContainer ,
121
+ clusterIdentifier ,
122
+ ),
123
+ Field : c .field ,
124
+ })
125
+ continue
126
+ }
86
127
87
- return result
128
+ if len (containers ) > 1 {
129
+ result .Allowed = false
130
+ result .Causes = append (result .Causes , preflight.Cause {
131
+ Message : fmt .Sprintf (
132
+ "multiple storage containers named %q found on cluster %q" ,
133
+ storageContainer ,
134
+ clusterIdentifier ,
135
+ ),
136
+ Field : c .field ,
137
+ })
138
+ continue
88
139
}
89
140
}
90
141
@@ -137,44 +188,25 @@ func newStorageContainerChecks(cd *checkDependencies) []preflight.Check {
137
188
return checks
138
189
}
139
190
140
- func getStorageContainer (
191
+ func getStorageContainers (
141
192
client client ,
142
- nodeSpec * carenv1. NutanixNodeSpec ,
193
+ clusterUUID string ,
143
194
storageContainerName string ,
144
- ) (* clustermgmtv4.StorageContainer , error ) {
145
- cluster , err := getCluster (client , & nodeSpec .MachineDetails .Cluster )
146
- if err != nil {
147
- return nil , fmt .Errorf ("failed to get cluster: %w" , err )
148
- }
149
-
150
- fltr := fmt .Sprintf ("name eq '%s' and clusterExtId eq '%s'" , storageContainerName , * cluster .ExtId )
195
+ ) ([]clustermgmtv4.StorageContainer , error ) {
196
+ fltr := fmt .Sprintf ("name eq '%s' and clusterExtId eq '%s'" , storageContainerName , clusterUUID )
151
197
resp , err := client .ListStorageContainers (nil , nil , & fltr , nil , nil )
152
198
if err != nil {
153
- return nil , fmt .Errorf ("failed to list storage containers: %w" , err )
199
+ return nil , err
200
+ }
201
+ if resp == nil || resp .GetData () == nil {
202
+ // No images were returned.
203
+ return []clustermgmtv4.StorageContainer {}, nil
154
204
}
155
-
156
205
containers , ok := resp .GetData ().([]clustermgmtv4.StorageContainer )
157
206
if ! ok {
158
207
return nil , fmt .Errorf ("failed to get data returned by ListStorageContainers(filter=%q)" , fltr )
159
208
}
160
-
161
- if len (containers ) == 0 {
162
- return nil , fmt .Errorf (
163
- "no storage container named %q found on cluster named %q" ,
164
- storageContainerName ,
165
- * cluster .Name ,
166
- )
167
- }
168
-
169
- if len (containers ) > 1 {
170
- return nil , fmt .Errorf (
171
- "multiple storage containers found with name %q on cluster %q" ,
172
- storageContainerName ,
173
- * cluster .Name ,
174
- )
175
- }
176
-
177
- return ptr .To (containers [0 ]), nil
209
+ return containers , nil
178
210
}
179
211
180
212
func getCluster (
0 commit comments