-
Notifications
You must be signed in to change notification settings - Fork 3
/
resource_taikun_kubernetes_profile.go
284 lines (256 loc) · 9.87 KB
/
resource_taikun_kubernetes_profile.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
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
package taikun
import (
"context"
tk "github.com/itera-io/taikungoclient"
tkcore "github.com/itera-io/taikungoclient/client"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
)
func resourceTaikunKubernetesProfileSchema() map[string]*schema.Schema {
return map[string]*schema.Schema{
"bastion_proxy": {
Description: "Whether to expose the Service on each Node's IP at a static port, the NodePort. You'll be able to contact the NodePort Service, from outside the cluster, by requesting `<NodeIP>:<NodePort>`.",
Type: schema.TypeBool,
Optional: true,
Default: false,
ForceNew: true,
},
"cni": {
Description: "Container Network Interface (CNI) of the Kubernetes profile.",
Type: schema.TypeString,
Computed: true,
},
"created_by": {
Description: "The creator of the Kubernetes profile.",
Type: schema.TypeString,
Computed: true,
},
"id": {
Description: "The ID of the Kubernetes profile.",
Type: schema.TypeString,
Computed: true,
},
"last_modified": {
Description: "Time and date of last modification.",
Type: schema.TypeString,
Computed: true,
},
"last_modified_by": {
Description: "The last user to have modified the Kubernetes profile.",
Type: schema.TypeString,
Computed: true,
},
"load_balancing_solution": {
Description: "The load-balancing solution: `None`, `Octavia` or `Taikun`. `Octavia` and `Taikun` are only available for OpenStack cloud.",
Type: schema.TypeString,
Optional: true,
Default: "Octavia",
ForceNew: true,
ValidateFunc: validation.StringInSlice([]string{"None", "Octavia", "Taikun"}, false),
},
"lock": {
Description: "Indicates whether to lock the Kubernetes profile.",
Type: schema.TypeBool,
Optional: true,
Default: false,
},
"name": {
Description: "The name of the Kubernetes profile.",
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validation.StringLenBetween(3, 30),
},
"organization_id": {
Description: "The ID of the organization which owns the Kubernetes profile.",
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
ValidateDiagFunc: stringIsInt,
},
"organization_name": {
Description: "The name of the organization which owns the Kubernetes profile.",
Type: schema.TypeString,
Computed: true,
},
"schedule_on_master": {
Description: "When enabled, the workload will also run on master nodes (not recommended).",
Type: schema.TypeBool,
Optional: true,
Default: false,
ForceNew: true,
},
"unique_cluster_name": {
Description: "If not enabled, the cluster name will be cluster.local.",
Type: schema.TypeBool,
Optional: true,
Default: false,
ForceNew: true,
},
"nvidia_gpu_operator": {
Description: "When enabled, the Kubernetes will have NVIDIA GPU support.",
Type: schema.TypeBool,
Optional: true,
Default: false,
ForceNew: true,
},
"wasm": {
Description: "When enabled, the Kubernetes will have WASM support.",
Type: schema.TypeBool,
Optional: true,
Default: false,
ForceNew: true,
},
"proxmox_storage": {
Description: "Choose the default Proxmox storage.",
Type: schema.TypeString,
Optional: true,
ForceNew: true,
DiffSuppressFunc: ignoreChangeFromEmpty,
},
}
}
func resourceTaikunKubernetesProfile() *schema.Resource {
return &schema.Resource{
Description: "Taikun Kubernetes Profile",
CreateContext: resourceTaikunKubernetesProfileCreate,
ReadContext: generateResourceTaikunKubernetesProfileReadWithoutRetries(),
UpdateContext: resourceTaikunKubernetesProfileUpdate,
DeleteContext: resourceTaikunKubernetesProfileDelete,
Schema: resourceTaikunKubernetesProfileSchema(),
Importer: &schema.ResourceImporter{
StateContext: schema.ImportStatePassthroughContext,
},
}
}
func resourceTaikunKubernetesProfileCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
apiClient := meta.(*tk.Client)
octaviaEnabled, taikunLBEnabled := parseLoadBalancingSolution(d.Get("load_balancing_solution").(string))
body := tkcore.CreateKubernetesProfileCommand{}
body.SetName(d.Get("name").(string))
body.SetAllowSchedulingOnMaster(d.Get("schedule_on_master").(bool))
body.SetTaikunLBEnabled(taikunLBEnabled)
body.SetOctaviaEnabled(octaviaEnabled)
body.SetExposeNodePortOnBastion(d.Get("bastion_proxy").(bool))
body.SetNvidiaGpuOperatorEnabled(d.Get("nvidia_gpu_operator").(bool))
body.SetUniqueClusterName(d.Get("unique_cluster_name").(bool))
body.SetWasmEnabled(d.Get("wasm").(bool))
proxmoxStorage, err := tkcore.NewProxmoxStorageFromValue(d.Get("proxmox_storage").(string))
if err == nil {
body.SetProxmoxStorage(*proxmoxStorage) // User input is not empty and valid.
} else if d.Get("proxmox_storage").(string) != "" {
return diag.FromErr(err) // User input is not empty and not valid.
}
organizationIDData, organizationIDIsSet := d.GetOk("organization_id")
if organizationIDIsSet {
organizationId, err := atoi32(organizationIDData.(string))
if err != nil {
return diag.Errorf("organization_id isn't valid: %s", d.Get("organization_id").(string))
}
body.SetOrganizationId(organizationId)
}
createResult, res, err := apiClient.Client.KubernetesProfilesAPI.KubernetesprofilesCreate(context.TODO()).CreateKubernetesProfileCommand(body).Execute()
if err != nil {
return diag.FromErr(tk.CreateError(res, err))
}
id, err := atoi32(createResult.GetId())
if err != nil {
return diag.FromErr(err)
}
d.SetId(createResult.GetId())
if d.Get("lock").(bool) {
if err := resourceTaikunKubernetesProfileLock(id, true, apiClient); err != nil {
return diag.FromErr(err)
}
}
return readAfterCreateWithRetries(generateResourceTaikunKubernetesProfileReadWithRetries(), ctx, d, meta)
}
func generateResourceTaikunKubernetesProfileReadWithRetries() schema.ReadContextFunc {
return generateResourceTaikunKubernetesProfileRead(true)
}
func generateResourceTaikunKubernetesProfileReadWithoutRetries() schema.ReadContextFunc {
return generateResourceTaikunKubernetesProfileRead(false)
}
func generateResourceTaikunKubernetesProfileRead(withRetries bool) schema.ReadContextFunc {
return func(_ context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
apiClient := meta.(*tk.Client)
id, err := atoi32(d.Id())
d.SetId("")
if err != nil {
return diag.FromErr(err)
}
response, res, err := apiClient.Client.KubernetesProfilesAPI.KubernetesprofilesList(context.TODO()).Id(id).Execute()
if err != nil {
return diag.FromErr(tk.CreateError(res, err))
}
if len(response.GetData()) != 1 {
if withRetries {
d.SetId(i32toa(id))
return diag.Errorf(notFoundAfterCreateOrUpdateError)
}
return nil
}
rawKubernetesProfile := response.GetData()[0]
err = setResourceDataFromMap(d, flattenTaikunKubernetesProfile(&rawKubernetesProfile))
if err != nil {
return diag.FromErr(err)
}
d.SetId(i32toa(id))
return nil
}
}
func resourceTaikunKubernetesProfileUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
apiClient := meta.(*tk.Client)
id, err := atoi32(d.Id())
if err != nil {
return diag.FromErr(err)
}
if d.HasChange("lock") {
if err := resourceTaikunKubernetesProfileLock(id, d.Get("lock").(bool), apiClient); err != nil {
return diag.FromErr(err)
}
}
return readAfterUpdateWithRetries(generateResourceTaikunKubernetesProfileReadWithRetries(), ctx, d, meta)
}
func resourceTaikunKubernetesProfileDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
apiClient := meta.(*tk.Client)
id, err := atoi32(d.Id())
if err != nil {
return diag.FromErr(err)
}
res, err := apiClient.Client.KubernetesProfilesAPI.KubernetesprofilesDelete(ctx, id).Execute()
if err != nil {
return diag.FromErr(tk.CreateError(res, err))
}
d.SetId("")
return nil
}
func flattenTaikunKubernetesProfile(rawKubernetesProfile *tkcore.KubernetesProfilesListDto) map[string]interface{} {
return map[string]interface{}{
"bastion_proxy": rawKubernetesProfile.GetExposeNodePortOnBastion(),
"created_by": rawKubernetesProfile.GetCreatedBy(),
"cni": rawKubernetesProfile.GetCni(),
"id": i32toa(rawKubernetesProfile.GetId()),
"lock": rawKubernetesProfile.GetIsLocked(),
"last_modified": rawKubernetesProfile.GetLastModified(),
"last_modified_by": rawKubernetesProfile.GetLastModifiedBy(),
"load_balancing_solution": getLoadBalancingSolution(rawKubernetesProfile.GetOctaviaEnabled(), rawKubernetesProfile.GetTaikunLBEnabled()),
"name": rawKubernetesProfile.GetName(),
"organization_id": i32toa(rawKubernetesProfile.GetOrganizationId()),
"organization_name": rawKubernetesProfile.GetOrganizationName(),
"schedule_on_master": rawKubernetesProfile.GetAllowSchedulingOnMaster(),
"unique_cluster_name": rawKubernetesProfile.GetUniqueClusterName(),
"nvidia_gpu_operator": rawKubernetesProfile.GetNvidiaGpuOperatorEnabled(),
"wasm": rawKubernetesProfile.GetWasmEnabled(),
"proxmox_storage": rawKubernetesProfile.GetProxmoxStorage(),
}
}
func resourceTaikunKubernetesProfileLock(id int32, lock bool, apiClient *tk.Client) error {
body := tkcore.KubernetesProfilesLockManagerCommand{}
body.SetId(id)
body.SetMode(getLockMode(lock))
res, err := apiClient.Client.KubernetesProfilesAPI.KubernetesprofilesLockManager(context.TODO()).KubernetesProfilesLockManagerCommand(body).Execute()
return tk.CreateError(res, err)
}