forked from gophercloud/gophercloud
-
Notifications
You must be signed in to change notification settings - Fork 1
/
containerinfra.go
250 lines (200 loc) · 8.23 KB
/
containerinfra.go
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
package v1
import (
"fmt"
"math"
"strings"
"testing"
"time"
"github.com/bizflycloud/gophercloud"
"github.com/bizflycloud/gophercloud/acceptance/clients"
idv3 "github.com/bizflycloud/gophercloud/acceptance/openstack/identity/v3"
"github.com/bizflycloud/gophercloud/acceptance/tools"
"github.com/bizflycloud/gophercloud/openstack/containerinfra/v1/clusters"
"github.com/bizflycloud/gophercloud/openstack/containerinfra/v1/clustertemplates"
"github.com/bizflycloud/gophercloud/openstack/containerinfra/v1/quotas"
th "github.com/bizflycloud/gophercloud/testhelper"
)
// CreateClusterTemplateCOE will create a random cluster template for the specified orchestration engine.
// An error will be returned if the cluster template could not be created.
func CreateClusterTemplateCOE(t *testing.T, client *gophercloud.ServiceClient, coe string) (*clustertemplates.ClusterTemplate, error) {
choices, err := clients.AcceptanceTestChoicesFromEnv()
if err != nil {
return nil, err
}
name := tools.RandomString("TESTACC-", 8)
t.Logf("Attempting to create %s cluster template: %s", coe, name)
boolFalse := false
createOpts := clustertemplates.CreateOpts{
COE: coe,
DNSNameServer: "8.8.8.8",
DockerStorageDriver: "overlay2",
ExternalNetworkID: choices.ExternalNetworkID,
FlavorID: choices.FlavorID,
FloatingIPEnabled: &boolFalse,
ImageID: choices.MagnumImageID,
MasterFlavorID: choices.FlavorID,
MasterLBEnabled: &boolFalse,
Name: name,
Public: &boolFalse,
RegistryEnabled: &boolFalse,
ServerType: "vm",
}
res := clustertemplates.Create(client, createOpts)
if res.Err != nil {
return nil, res.Err
}
requestID := res.Header.Get("X-OpenStack-Request-Id")
th.AssertEquals(t, true, requestID != "")
t.Logf("Cluster Template %s request ID: %s", name, requestID)
clusterTemplate, err := res.Extract()
if err != nil {
return nil, err
}
t.Logf("Successfully created cluster template: %s", clusterTemplate.Name)
tools.PrintResource(t, clusterTemplate)
tools.PrintResource(t, clusterTemplate.CreatedAt)
th.AssertEquals(t, name, clusterTemplate.Name)
th.AssertEquals(t, choices.ExternalNetworkID, clusterTemplate.ExternalNetworkID)
th.AssertEquals(t, choices.MagnumImageID, clusterTemplate.ImageID)
return clusterTemplate, nil
}
// CreateClusterTemplate will create a random swarm cluster template.
// An error will be returned if the cluster template could not be created.
func CreateClusterTemplate(t *testing.T, client *gophercloud.ServiceClient) (*clustertemplates.ClusterTemplate, error) {
return CreateClusterTemplateCOE(t, client, "swarm")
}
// CreateKubernetesClusterTemplate will create a random kubernetes cluster template.
// An error will be returned if the cluster template could not be created.
func CreateKubernetesClusterTemplate(t *testing.T, client *gophercloud.ServiceClient) (*clustertemplates.ClusterTemplate, error) {
return CreateClusterTemplateCOE(t, client, "kubernetes")
}
// DeleteClusterTemplate will delete a given cluster-template. A fatal error will occur if the
// cluster-template could not be deleted. This works best as a deferred function.
func DeleteClusterTemplate(t *testing.T, client *gophercloud.ServiceClient, id string) {
t.Logf("Attempting to delete cluster-template: %s", id)
err := clustertemplates.Delete(client, id).ExtractErr()
if err != nil {
t.Fatalf("Error deleting cluster-template %s: %s:", id, err)
}
t.Logf("Successfully deleted cluster-template: %s", id)
return
}
// CreateClusterTimeout will create a random cluster and wait for it to reach CREATE_COMPLETE status
// within the given timeout duration. An error will be returned if the cluster could not be created.
func CreateClusterTimeout(t *testing.T, client *gophercloud.ServiceClient, clusterTemplateID string, timeout time.Duration) (string, error) {
clusterName := tools.RandomString("TESTACC-", 8)
t.Logf("Attempting to create cluster: %s using template %s", clusterName, clusterTemplateID)
choices, err := clients.AcceptanceTestChoicesFromEnv()
if err != nil {
return "", err
}
masterCount := 1
nodeCount := 1
// createTimeout is the creation timeout on the magnum side in minutes
createTimeout := int(math.Ceil(timeout.Minutes()))
createOpts := clusters.CreateOpts{
ClusterTemplateID: clusterTemplateID,
CreateTimeout: &createTimeout,
FlavorID: choices.FlavorID,
Keypair: choices.MagnumKeypair,
Labels: map[string]string{},
MasterCount: &masterCount,
MasterFlavorID: choices.FlavorID,
Name: clusterName,
NodeCount: &nodeCount,
}
createResult := clusters.Create(client, createOpts)
th.AssertNoErr(t, createResult.Err)
if len(createResult.Header["X-Openstack-Request-Id"]) > 0 {
t.Logf("Cluster Create Request ID: %s", createResult.Header["X-Openstack-Request-Id"][0])
}
clusterID, err := createResult.Extract()
if err != nil {
return "", err
}
t.Logf("Cluster created: %+v", clusterID)
err = WaitForCluster(client, clusterID, "CREATE_COMPLETE", timeout)
if err != nil {
return clusterID, err
}
t.Logf("Successfully created cluster: %s id: %s", clusterName, clusterID)
return clusterID, nil
}
// CreateCluster will create a random cluster. An error will be returned if the
// cluster could not be created. Has a timeout of 300 seconds.
func CreateCluster(t *testing.T, client *gophercloud.ServiceClient, clusterTemplateID string) (string, error) {
return CreateClusterTimeout(t, client, clusterTemplateID, 300*time.Second)
}
// CreateKubernetesCluster is the same as CreateCluster with a longer timeout necessary for creating a kubernetes cluster
func CreateKubernetesCluster(t *testing.T, client *gophercloud.ServiceClient, clusterTemplateID string) (string, error) {
return CreateClusterTimeout(t, client, clusterTemplateID, 900*time.Second)
}
func DeleteCluster(t *testing.T, client *gophercloud.ServiceClient, id string) {
t.Logf("Attempting to delete cluster: %s", id)
r := clusters.Delete(client, id)
err := clusters.Delete(client, id).ExtractErr()
deleteRequestID := ""
idKey := "X-Openstack-Request-Id"
if len(r.Header[idKey]) > 0 {
deleteRequestID = r.Header[idKey][0]
}
if err != nil {
t.Fatalf("Error deleting cluster. requestID=%s clusterID=%s: err%s:", deleteRequestID, id, err)
}
err = WaitForCluster(client, id, "DELETE_COMPLETE", 300*time.Second)
if err != nil {
t.Fatalf("Error deleting cluster %s: %s:", id, err)
}
t.Logf("Successfully deleted cluster: %s", id)
return
}
func WaitForCluster(client *gophercloud.ServiceClient, clusterID string, status string, timeout time.Duration) error {
return tools.WaitForTimeout(func() (bool, error) {
cluster, err := clusters.Get(client, clusterID).Extract()
if err != nil {
if _, ok := err.(gophercloud.ErrDefault404); ok && status == "DELETE_COMPLETE" {
return true, nil
}
return false, err
}
if cluster.Status == status {
return true, nil
}
if strings.Contains(cluster.Status, "FAILED") {
return false, fmt.Errorf("Cluster %s FAILED. Status=%s StatusReason=%s", clusterID, cluster.Status, cluster.StatusReason)
}
return false, nil
}, timeout)
}
// CreateQuota will create a random quota. An error will be returned if the
// quota could not be created.
func CreateQuota(t *testing.T, client *gophercloud.ServiceClient) (*quotas.Quotas, error) {
name := tools.RandomString("TESTACC-", 8)
t.Logf("Attempting to create quota: %s", name)
idClient, err := clients.NewIdentityV3Client()
th.AssertNoErr(t, err)
project, err := idv3.CreateProject(t, idClient, nil)
th.AssertNoErr(t, err)
defer idv3.DeleteProject(t, idClient, project.ID)
createOpts := quotas.CreateOpts{
Resource: "Cluster",
ProjectID: project.ID,
HardLimit: 10,
}
res := quotas.Create(client, createOpts)
if res.Err != nil {
return nil, res.Err
}
requestID := res.Header.Get("X-OpenStack-Request-Id")
th.AssertEquals(t, true, requestID != "")
t.Logf("Quota %s request ID: %s", name, requestID)
quota, err := res.Extract()
if err == nil {
t.Logf("Successfully created quota: %s", quota.ProjectID)
tools.PrintResource(t, quota)
th.AssertEquals(t, project.ID, quota.ProjectID)
th.AssertEquals(t, "Cluster", quota.Resource)
th.AssertEquals(t, 10, quota.HardLimit)
}
return quota, err
}