/
resource_vcluster.go
289 lines (250 loc) · 7.18 KB
/
resource_vcluster.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
285
286
287
288
289
package vcluster
import (
"context"
"encoding/json"
"fmt"
"os/exec"
"strings"
"time"
"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"
)
var distroKinds = []string{"k0s", "k8s", "k3s"}
const LoftChartRepo = "https://charts.loft.sh"
func resourceVCluster() *schema.Resource {
return &schema.Resource{
CreateContext: resourceVClusterCreate,
ReadContext: resourceVClusterRead,
UpdateContext: resourceVClusterUpdate,
DeleteContext: resourceVClusterDelete,
Importer: &schema.ResourceImporter{
StateContext: schema.ImportStatePassthroughContext,
},
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Description: "The name of the vcluster",
Required: true,
ForceNew: true,
},
"distro": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: validation.StringInSlice(distroKinds, true),
ForceNew: true,
},
"extra_values": {
Type: schema.TypeList,
Optional: true,
Description: "List of values in raw yaml format to pass to vcluster.",
Elem: &schema.Schema{Type: schema.TypeString},
},
"chart": {
Type: schema.TypeString,
Optional: true,
Description: "The virtual cluster chart name to use",
},
"chart_version": {
Type: schema.TypeString,
Optional: true,
Description: "The virtual cluster chart version to use (e.g. v0.9.1)",
},
"chart_repo": {
Type: schema.TypeString,
Optional: true,
Description: "The virtual cluster chart repo to use",
},
"local_chart_dir": {
Type: schema.TypeString,
ConflictsWith: []string{"chart", "chart_version", "chart_repo"},
Optional: true,
Description: "The virtual cluster local chart dir to use",
},
"kubernetes_version": {
Type: schema.TypeString,
Description: "The kubernetes version to use (e.g. v1.20). Patch versions are not supported",
Optional: true,
},
"create_namespace": {
Type: schema.TypeBool,
Description: "If true the namespace will be created if it does not exist",
Optional: true,
},
"disable_ingress_sync": {
Type: schema.TypeBool,
Description: "If true the virtual cluster will not sync any ingresses",
Optional: true,
},
// UpdateCurrent?
"expose": {
Type: schema.TypeBool,
Description: "If true will create a load balancer service to expose the vcluster endpoint",
Optional: true,
},
"expose_local": {
Type: schema.TypeBool,
Description: "If true and a local Kubernetes distro is detected, will deploy vcluster with a NodePort service",
Optional: true,
},
"isolate": {
Type: schema.TypeBool,
Description: "If true vcluster and its workloads will run in an isolated environment",
Optional: true,
},
"context": {
Type: schema.TypeString,
Optional: true,
Description: "The kubernetes config context to use",
},
"namespace": {
Type: schema.TypeString,
Optional: true,
Description: "The kubernetes namespace to use",
},
"status": {
Type: schema.TypeString,
Computed: true,
},
"created": {
Type: schema.TypeString,
Computed: true,
},
},
}
}
func vclusterBaseArgs(d *schema.ResourceData, args []string) []string {
if namespace := d.Get("namespace"); namespace != nil && namespace.(string) != "" {
args = append(args, "--namespace", namespace.(string))
}
if context := d.Get("context"); context != nil && context.(string) != "" {
args = append(args, "--context", context.(string))
}
return args
}
func resourceVClusterCreate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
provider := m.(*Meta)
_ = provider
vClusterName := d.Get("name").(string)
args := vclusterBaseArgs(d, []string{
"create",
vClusterName,
"--connect=false",
})
if distro := d.Get("distro"); distro != nil && distro.(string) != "" {
args = append(args, fmt.Sprintf("--distro=%s", distro.(string)))
}
if isolate := d.Get("isolate"); isolate != nil {
args = append(args, fmt.Sprintf("--isolate=%v", isolate.(bool)))
}
if expose := d.Get("expose"); expose != nil {
args = append(args, fmt.Sprintf("--expose=%v", expose.(bool)))
}
if exposeLocal := d.Get("expose_local"); exposeLocal != nil {
args = append(args, fmt.Sprintf("--expose-local=%v", exposeLocal.(bool)))
}
if disableIngressSync := d.Get("disable_ingress_sync"); disableIngressSync != nil {
args = append(args, fmt.Sprintf("--disable-ingress-sync=%v", disableIngressSync.(bool)))
}
if createNamespace := d.Get("create_namespace"); createNamespace != nil {
args = append(args, fmt.Sprintf("--create-namespace=%v", createNamespace.(bool)))
}
if kubernetesVersion := d.Get("kubernetes_version"); kubernetesVersion != nil && kubernetesVersion.(string) != "" {
args = append(args, fmt.Sprintf("--kubernetes-version=%s", kubernetesVersion.(string)))
}
// extra_values
// chart
// chart_version
// chart_repo
// local_chart_dir
cmd := exec.Command(
"vcluster",
args...,
)
output, err := cmd.CombinedOutput()
if err != nil {
return diag.Diagnostics{
{
Severity: diag.Error,
Summary: fmt.Sprint("vcluster ", strings.Join(args, " ")),
Detail: string(output),
},
}
}
_ = output
d.SetId(vClusterName)
d.Set("name", vClusterName)
return nil
}
// ListEntry is a struct matching the results of the vcluster list operation's json output.
type ListEntry struct {
Name string
Namespace string
Status string
Created time.Time // "2022-12-09T03:12:10Z",
Context string
}
func resourceVClusterRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
args := vclusterBaseArgs(d, []string{
"list",
"--output", "json",
})
cmd := exec.Command(
"vcluster",
args...,
)
output, err := cmd.CombinedOutput()
if err != nil {
return diag.Diagnostics{
{
Severity: diag.Error,
Summary: fmt.Sprint("vcluster ", strings.Join(args, " ")),
Detail: string(output),
},
}
}
var entries []ListEntry
err = json.Unmarshal(output, &entries)
if err != nil {
return diag.FromErr(err)
}
var resourceEntry ListEntry
for _, entry := range entries {
if entry.Name == d.Id() {
resourceEntry = entry
}
}
if resourceEntry == (ListEntry{}) {
d.SetId("")
return nil
}
d.Set("name", resourceEntry.Name)
d.Set("status", resourceEntry.Status)
d.Set("created", resourceEntry.Created)
return nil
}
func resourceVClusterUpdate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
return nil
}
func resourceVClusterDelete(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
args := vclusterBaseArgs(d, []string{
"delete",
d.Get("name").(string),
})
cmd := exec.Command(
"vcluster",
args...,
)
output, err := cmd.CombinedOutput()
if err != nil {
return diag.Diagnostics{
{
Severity: diag.Error,
Summary: fmt.Sprint("vcluster ", strings.Join(args, " ")),
Detail: string(output),
},
}
}
_ = output
return nil
}