-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathclient.go
More file actions
304 lines (246 loc) · 9.67 KB
/
client.go
File metadata and controls
304 lines (246 loc) · 9.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
package api
import (
"context"
"fmt"
"testing"
"time"
"github.com/rancher/shepherd/clients/rancher"
"github.com/rancher/shepherd/extensions/defaults"
"github.com/rancher/shepherd/pkg/namegenerator"
"github.com/rancher/tests/actions/charts"
"github.com/rancher/tests/interoperability/longhorn"
kwait "k8s.io/apimachinery/pkg/util/wait"
)
const (
longhornNodeType = "longhorn.io.node"
longhornSettingType = "longhorn.io.setting"
longhornVolumeType = "longhorn.io.volume"
)
// LonghornClient represents a client for interacting with Longhorn resources via Rancher API
type LonghornClient struct {
Client *rancher.Client
ClusterID string
ServiceURL string
}
// NewLonghornClient creates a new Longhorn client that uses Rancher Steve API
func NewLonghornClient(client *rancher.Client, clusterID, serviceURL string) (*LonghornClient, error) {
longhornClient := &LonghornClient{
Client: client,
ClusterID: clusterID,
ServiceURL: serviceURL,
}
return longhornClient, nil
}
// getReplicaCount determines an appropriate replica count for a Longhorn volume
// based on the number of available Longhorn nodes. It caps the replica count
// at 3 to preserve the previous default behavior on larger clusters, while
// ensuring it does not exceed the number of nodes on smaller clusters.
func getReplicaCount(t *testing.T, lc *LonghornClient) (int, error) {
steveClient, err := lc.Client.Steve.ProxyDownstream(lc.ClusterID)
if err != nil {
return 0, fmt.Errorf("failed to get downstream client for replica count: %w", err)
}
longhornNodes, err := steveClient.SteveType(longhornNodeType).NamespacedSteveClient(charts.LonghornNamespace).List(nil)
if err != nil {
return 0, fmt.Errorf("failed to list Longhorn nodes: %w", err)
}
nodeCount := len(longhornNodes.Data)
if nodeCount <= 0 {
t.Logf("No Longhorn nodes found; defaulting replica count to 1")
return 1, nil
}
// Do not exceed the number of nodes, and cap at 3 to match previous behavior.
if nodeCount >= 3 {
return 3, nil
}
return nodeCount, nil
}
// CreateVolume creates a new Longhorn volume via the Rancher Steve API
func CreateVolume(t *testing.T, lc *LonghornClient) (string, error) {
volumeName := namegenerator.AppendRandomString("test-lh-vol")
replicaCount, err := getReplicaCount(t, lc)
if err != nil {
return "", err
}
steveClient, err := lc.Client.Steve.ProxyDownstream(lc.ClusterID)
if err != nil {
return "", fmt.Errorf("failed to get downstream client: %w", err)
}
// Create volume spec
volumeSpec := map[string]interface{}{
"type": longhornVolumeType,
"metadata": map[string]interface{}{
"name": volumeName,
"namespace": charts.LonghornNamespace,
},
"spec": map[string]interface{}{
"numberOfReplicas": replicaCount,
"size": "1073741824", // 1Gi in bytes
"frontend": "blockdev", // Required for data engine v1
},
}
t.Logf("Creating Longhorn volume: %s with %d replicas", volumeName, replicaCount)
_, err = steveClient.SteveType(longhornVolumeType).Create(volumeSpec)
if err != nil {
return "", fmt.Errorf("failed to create volume: %w", err)
}
t.Logf("Successfully created volume: %s", volumeName)
return volumeName, nil
}
// ValidateVolumeActive validates that a volume is in an active/detached state and ready to use
func ValidateVolumeActive(t *testing.T, lc *LonghornClient, volumeName string) error {
t.Logf("Validating volume %s is active", volumeName)
steveClient, err := lc.Client.Steve.ProxyDownstream(lc.ClusterID)
if err != nil {
return fmt.Errorf("failed to get downstream client: %w", err)
}
err = kwait.PollUntilContextTimeout(context.TODO(), 5*time.Second, defaults.FiveMinuteTimeout, true, func(ctx context.Context) (done bool, err error) {
volumeID := fmt.Sprintf("%s/%s", charts.LonghornNamespace, volumeName)
volume, err := steveClient.SteveType(longhornVolumeType).ByID(volumeID)
if err != nil {
return false, nil
}
// Extract status from the volume
if volume.Status == nil {
return false, nil
}
statusMap, ok := volume.Status.(map[string]interface{})
if !ok {
return false, nil
}
state, _ := statusMap["state"].(string)
robustness, _ := statusMap["robustness"].(string)
t.Logf("Volume %s state: %s, robustness: %s", volumeName, state, robustness)
// Volume is ready when it's in detached state with valid robustness
// "unknown" robustness is expected for detached volumes with no replicas scheduled
if state == "detached" && (robustness == "healthy" || robustness == "unknown") {
return true, nil
}
return false, nil
})
if err != nil {
return fmt.Errorf("volume %s did not become active: %w", volumeName, err)
}
t.Logf("Volume %s is active and ready to use", volumeName)
return nil
}
// DeleteVolume deletes a Longhorn volume
func DeleteVolume(t *testing.T, lc *LonghornClient, volumeName string) error {
steveClient, err := lc.Client.Steve.ProxyDownstream(lc.ClusterID)
if err != nil {
return fmt.Errorf("failed to get downstream client: %w", err)
}
volumeID := fmt.Sprintf("%s/%s", charts.LonghornNamespace, volumeName)
volume, err := steveClient.SteveType(longhornVolumeType).ByID(volumeID)
if err != nil {
return fmt.Errorf("failed to get volume %s: %w", volumeName, err)
}
t.Logf("Deleting volume: %s", volumeName)
err = steveClient.SteveType(longhornVolumeType).Delete(volume)
if err != nil {
return fmt.Errorf("failed to delete volume %s: %w", volumeName, err)
}
return nil
}
// ValidateNodes validates that all Longhorn nodes are in a valid state
func ValidateNodes(lc *LonghornClient) error {
steveClient, err := lc.Client.Steve.ProxyDownstream(lc.ClusterID)
if err != nil {
return fmt.Errorf("failed to get downstream client: %w", err)
}
nodes, err := steveClient.SteveType(longhornNodeType).NamespacedSteveClient(charts.LonghornNamespace).List(nil)
if err != nil {
return fmt.Errorf("failed to list nodes: %w", err)
}
if len(nodes.Data) == 0 {
return fmt.Errorf("no Longhorn nodes found")
}
// Validate each node has valid conditions
for _, node := range nodes.Data {
if node.Status == nil {
return fmt.Errorf("node %s has no status", node.Name)
}
}
return nil
}
// ValidateSettings validates that Longhorn settings are properly configured
func ValidateSettings(lc *LonghornClient) error {
steveClient, err := lc.Client.Steve.ProxyDownstream(lc.ClusterID)
if err != nil {
return fmt.Errorf("failed to get downstream client: %w", err)
}
settings, err := steveClient.SteveType(longhornSettingType).NamespacedSteveClient(charts.LonghornNamespace).List(nil)
if err != nil {
return fmt.Errorf("failed to list settings: %w", err)
}
if len(settings.Data) == 0 {
return fmt.Errorf("no Longhorn settings found")
}
return nil
}
// ValidateVolumeInRancherAPI validates that the volume is accessible and in a ready state through Rancher API
func ValidateVolumeInRancherAPI(t *testing.T, lc *LonghornClient, volumeName string) error {
t.Logf("Validating volume %s is accessible through Rancher API", volumeName)
steveClient, err := lc.Client.Steve.ProxyDownstream(lc.ClusterID)
if err != nil {
return fmt.Errorf("failed to get downstream client: %w", err)
}
// Get the volume using the Rancher API path
volumeID := fmt.Sprintf("%s/%s", charts.LonghornNamespace, volumeName)
volume, err := steveClient.SteveType(longhornVolumeType).ByID(volumeID)
if err != nil {
return fmt.Errorf("failed to get volume %s through Rancher API: %w", volumeName, err)
}
// Validate volume has status
if volume.Status == nil {
return fmt.Errorf("volume %s has no status in Rancher API", volumeName)
}
statusMap, ok := volume.Status.(map[string]interface{})
if !ok {
return fmt.Errorf("volume %s status is not in expected format", volumeName)
}
state, _ := statusMap["state"].(string)
robustness, _ := statusMap["robustness"].(string)
t.Logf("Volume %s in Rancher API - state: %s, robustness: %s", volumeName, state, robustness)
// Verify volume is in a ready state
if state != "detached" {
return fmt.Errorf("volume %s is not in detached state through Rancher API, current state: %s", volumeName, state)
}
if robustness != "healthy" && robustness != "unknown" {
return fmt.Errorf("volume %s has invalid robustness through Rancher API: %s", volumeName, robustness)
}
t.Logf("Volume %s validated successfully through Rancher API", volumeName)
return nil
}
// ValidateDynamicConfiguration validates Longhorn configuration based on user-provided test config
func ValidateDynamicConfiguration(t *testing.T, lc *LonghornClient, config longhorn.TestConfig) error {
steveClient, err := lc.Client.Steve.ProxyDownstream(lc.ClusterID)
if err != nil {
return fmt.Errorf("failed to get downstream client for dynamic validation: %w", err)
}
// Validate that the configured storage class exists
t.Logf("Validating configured storage class: %s", config.LonghornTestStorageClass)
storageClasses, err := steveClient.SteveType("storage.k8s.io.storageclass").List(nil)
if err != nil {
return fmt.Errorf("failed to list storage classes: %w", err)
}
found := false
for _, sc := range storageClasses.Data {
if sc.Name == config.LonghornTestStorageClass {
found = true
t.Logf("Found configured storage class: %s", config.LonghornTestStorageClass)
break
}
}
if !found {
return fmt.Errorf("configured storage class %s not found", config.LonghornTestStorageClass)
}
// Validate settings exist
settings, err := steveClient.SteveType(longhornSettingType).NamespacedSteveClient(charts.LonghornNamespace).List(nil)
if err != nil {
return fmt.Errorf("failed to list settings: %w", err)
}
t.Logf("Successfully validated Longhorn configuration with %d settings", len(settings.Data))
t.Logf("Validated storage class: %s from test configuration", config.LonghornTestStorageClass)
return nil
}