forked from kubernetes-retired/contrib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
provider.go
429 lines (365 loc) · 12.2 KB
/
provider.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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
/*
Copyright 2016 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package provider
import (
"fmt"
"log"
"os"
"time"
"github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/terraform"
"k8s.io/kubernetes/pkg/api"
"k8s.io/kubernetes/pkg/client/clientset_generated/release_1_4"
"k8s.io/kubernetes/pkg/client/restclient"
"k8s.io/kubernetes/pkg/client/unversioned/clientcmd"
clientcmdapi "k8s.io/kubernetes/pkg/client/unversioned/clientcmd/api"
)
const (
userAgent = "terraform-kubernetes"
pollInterval = 10 * time.Second
pollTimeout = 10 * time.Minute
configPollInterval = 100 * time.Millisecond
configPollTimeout = 2 * time.Minute
resourceShutdownInterval = 1 * time.Minute
)
// Provider returns an implementation of the Kubernetes provider.
func Provider() terraform.ResourceProvider {
return &schema.Provider{
ResourcesMap: map[string]*schema.Resource{
"kubernetes_kubeconfig": resourceKubeconfig(),
"kubernetes_cluster": resourceCluster(),
},
ConfigureFunc: providerConfig,
}
}
type configFunc func(*schema.ResourceData) (*config, error)
type config struct {
pollInterval time.Duration
pollTimeout time.Duration
configPollInterval time.Duration
ConfigPollTimeout time.Duration
resourceShutdownInterval time.Duration
kubeConfig *clientcmdapi.Config
clientset release_1_4.Interface
}
func providerConfig(d *schema.ResourceData) (interface{}, error) {
var f configFunc = func(d *schema.ResourceData) (*config, error) {
server := d.Get("server").(string)
configGetter := kubeConfigGetter(d)
clientConfig, err := clientcmd.BuildConfigFromKubeconfigGetter(server, configGetter)
if err != nil {
return nil, fmt.Errorf("couldn't parse the supplied config: %v", err)
}
clientset, err := release_1_4.NewForConfig(restclient.AddUserAgent(clientConfig, userAgent))
if err != nil {
return nil, fmt.Errorf("failed to initialize the cluster client: %v", err)
}
kubeConfig, err := configGetter()
if err != nil {
return nil, fmt.Errorf("couldn't parse the supplied config: %v", err)
}
return &config{
pollInterval: pollInterval,
pollTimeout: pollTimeout,
configPollInterval: configPollInterval,
ConfigPollTimeout: configPollTimeout,
resourceShutdownInterval: resourceShutdownInterval,
kubeConfig: kubeConfig,
clientset: clientset,
}, nil
}
return f, nil
}
func resourceKubeconfig() *schema.Resource {
return &schema.Resource{
Create: createKubeconfig,
Delete: deleteKubeconfig,
Read: readKubeconfig,
Schema: map[string]*schema.Schema{
"server": {
Type: schema.TypeString,
Required: true,
Description: "Domain name or IP address of the API server",
ForceNew: true,
},
"configdata": {
Type: schema.TypeString,
Required: true,
Description: "kubeconfig in the serialized JSON format",
ForceNew: true,
},
"path": {
Type: schema.TypeString,
Optional: true,
Description: "path to the kubeconfig file",
ForceNew: true,
},
},
}
}
func createKubeconfig(d *schema.ResourceData, meta interface{}) error {
configF := meta.(configFunc)
cfg, err := configF(d)
if err != nil {
return fmt.Errorf("failed to initialize the cluster client: %v", err)
}
if len(cfg.kubeConfig.Clusters) != 1 || len(cfg.kubeConfig.AuthInfos) != 1 || len(cfg.kubeConfig.Contexts) != 1 {
return fmt.Errorf("config must supplied for exactly one cluster - number of clusters: %d, number of users: %d, number of contexts: %d", len(cfg.kubeConfig.Clusters), len(cfg.kubeConfig.AuthInfos), len(cfg.kubeConfig.Contexts))
}
log.Printf("[DEBUG] checking for cluster components' health")
if !poll(cfg.pollInterval, cfg.pollTimeout, allComponentsHealthy(cfg.clientset)) {
return fmt.Errorf("cluster components never turned healthy")
}
po := clientcmd.NewDefaultPathOptions()
if path, ok := d.GetOk("path"); ok {
po.LoadingRules.ExplicitPath = path.(string)
}
// Retry until modifyConfig succeeds or times out.
log.Printf("[DEBUG] updating kubeconfig")
if !poll(cfg.configPollInterval, cfg.ConfigPollTimeout, updateConfig(po, cfg.kubeConfig)) {
return fmt.Errorf("couldn't update kubeconfig")
}
// Store the ID now
d.SetId(d.Get("server").(string))
return nil
}
func deleteKubeconfig(d *schema.ResourceData, meta interface{}) error {
d.SetId("")
return nil
}
func readKubeconfig(d *schema.ResourceData, meta interface{}) error {
return nil
}
func resourceCluster() *schema.Resource {
return &schema.Resource{
Create: createCluster,
Delete: deleteCluster,
Read: readCluster,
Schema: map[string]*schema.Schema{
"server": {
Type: schema.TypeString,
Required: true,
Description: "Domain name or IP address of the API server",
ForceNew: true,
},
"configdata": {
Type: schema.TypeString,
Required: true,
Description: "kubeconfig in the serialized JSON format",
ForceNew: true,
},
},
}
}
func createCluster(d *schema.ResourceData, meta interface{}) error {
configF := meta.(configFunc)
cfg, err := configF(d)
if err != nil {
return fmt.Errorf("failed to initialize the cluster client: %v", err)
}
log.Printf("[DEBUG] checking for cluster components' health")
if !poll(cfg.pollInterval, cfg.pollTimeout, allComponentsHealthy(cfg.clientset)) {
return fmt.Errorf("cluster components never turned healthy")
}
// Store the ID now
d.SetId(d.Get("server").(string))
return nil
}
func deleteCluster(d *schema.ResourceData, meta interface{}) error {
configF := meta.(configFunc)
cfg, err := configF(d)
if err != nil {
return fmt.Errorf("failed to initialize the cluster client: %v", err)
}
if err := cfg.clientset.Core().Nodes().DeleteCollection(&api.DeleteOptions{}, api.ListOptions{}); err != nil {
return fmt.Errorf("failed to delete the nodes: %v", err)
}
// Block for some time to give the controllers sufficient time to
// delete the cloud provider resources they might have acquired.
// Only resources we are considering right now are routes installed
// by the route controller.
// TODO: Enumerate the resources we should wait for before returning.
time.Sleep(cfg.resourceShutdownInterval)
d.SetId("")
return nil
}
func readCluster(d *schema.ResourceData, meta interface{}) error {
return nil
}
func poll(pollInterval, pollTimeout time.Duration, cond func() (bool, error)) bool {
interval := time.NewTicker(pollInterval)
defer interval.Stop()
timeout := time.NewTimer(pollTimeout)
defer timeout.Stop()
// Try the first time before waiting.
if ok, err := cond(); ok {
log.Printf("[DEBUG] condition succeeded, error: %v", err)
return true
} else if err != nil {
log.Printf("[DEBUG] condition error: %v", err)
return false
} else {
log.Printf("[DEBUG] condition has failed, retrying...")
}
for {
select {
case <-interval.C:
if ok, err := cond(); ok {
log.Printf("[DEBUG] condition succeeded, error: %v", err)
return true
} else if err != nil {
log.Printf("[DEBUG] condition error: %v", err)
return false
} else {
log.Printf("[DEBUG] condition has failed, retrying...")
}
case <-timeout.C:
return false
}
}
// Something went wrong
log.Printf("[DEBUG] something went wrong while polling, that's all we know")
return false
}
func allComponentsHealthy(clientset release_1_4.Interface) func() (bool, error) {
return func() (bool, error) {
csList, err := clientset.Core().ComponentStatuses().List(api.ListOptions{})
if err != nil || len(csList.Items) <= 0 {
log.Printf("[DEBUG] Listing components failed %s", err)
return false, nil
}
for _, cs := range csList.Items {
if !(len(cs.Conditions) > 0 && cs.Conditions[0].Type == "Healthy" && cs.Conditions[0].Status == "True") {
log.Printf("[DEBUG] %s isn't healthy. Conditions: %+v", cs.Name, cs.Conditions)
return false, nil
}
}
return true, nil
}
}
func kubeConfigGetter(d *schema.ResourceData) clientcmd.KubeconfigGetter {
return func() (*clientcmdapi.Config, error) {
kubeConfigStr := d.Get("configdata").(string)
return clientcmd.Load([]byte(kubeConfigStr))
}
}
func updateConfig(configAccess clientcmd.ConfigAccess, suppliedConfig *clientcmdapi.Config) func() (bool, error) {
return func() (bool, error) {
err := modifyConfig(configAccess, suppliedConfig)
if err != nil {
// TODO: We are relying too much on the fact that this error is going
// to be an *os.PathError returned by file locking mechanism. This is
// dangerous. Try to introduce a specific error for this in
// "k8s.io/kubernetes/pkg/client/unversioned/clientcmd" package.
if os.IsExist(err) {
return false, nil
}
return false, fmt.Errorf("couldn't update kubeconfig: %v", err)
}
return true, nil
}
}
func modifyConfig(configAccess clientcmd.ConfigAccess, suppliedConfig *clientcmdapi.Config) error {
config, err := configAccess.GetStartingConfig()
if err != nil {
return err
}
for name, authInfo := range suppliedConfig.AuthInfos {
initial, ok := config.AuthInfos[name]
if !ok {
initial = clientcmdapi.NewAuthInfo()
}
modifiedAuthInfo := *initial
var setToken, setBasic bool
if len(authInfo.ClientCertificate) > 0 {
modifiedAuthInfo.ClientCertificate = authInfo.ClientCertificate
}
if len(authInfo.ClientCertificateData) > 0 {
modifiedAuthInfo.ClientCertificateData = authInfo.ClientCertificateData
}
if len(authInfo.ClientKey) > 0 {
modifiedAuthInfo.ClientKey = authInfo.ClientKey
}
if len(authInfo.ClientKeyData) > 0 {
modifiedAuthInfo.ClientKeyData = authInfo.ClientKeyData
}
if len(authInfo.Token) > 0 {
modifiedAuthInfo.Token = authInfo.Token
setToken = len(modifiedAuthInfo.Token) > 0
}
if len(authInfo.Username) > 0 {
modifiedAuthInfo.Username = authInfo.Username
setBasic = setBasic || len(modifiedAuthInfo.Username) > 0
}
if len(authInfo.Password) > 0 {
modifiedAuthInfo.Password = authInfo.Password
setBasic = setBasic || len(modifiedAuthInfo.Password) > 0
}
// If any auth info was set, make sure any other existing auth types are cleared
if setToken || setBasic {
if !setToken {
modifiedAuthInfo.Token = ""
}
if !setBasic {
modifiedAuthInfo.Username = ""
modifiedAuthInfo.Password = ""
}
}
config.AuthInfos[name] = &modifiedAuthInfo
}
for name, cluster := range suppliedConfig.Clusters {
initial, ok := config.Clusters[name]
if !ok {
initial = clientcmdapi.NewCluster()
}
modifiedCluster := *initial
if len(cluster.Server) > 0 {
modifiedCluster.Server = cluster.Server
}
if cluster.InsecureSkipTLSVerify {
modifiedCluster.InsecureSkipTLSVerify = cluster.InsecureSkipTLSVerify
// Specifying insecure mode clears any certificate authority
if modifiedCluster.InsecureSkipTLSVerify {
modifiedCluster.CertificateAuthority = ""
modifiedCluster.CertificateAuthorityData = nil
}
}
if len(cluster.CertificateAuthorityData) > 0 {
modifiedCluster.CertificateAuthorityData = cluster.CertificateAuthorityData
modifiedCluster.InsecureSkipTLSVerify = false
}
if len(cluster.CertificateAuthority) > 0 {
modifiedCluster.CertificateAuthority = cluster.CertificateAuthority
modifiedCluster.InsecureSkipTLSVerify = false
}
config.Clusters[name] = &modifiedCluster
}
for name, context := range suppliedConfig.Contexts {
initial, ok := config.Contexts[name]
if !ok {
initial = clientcmdapi.NewContext()
}
modifiedContext := *initial
if len(context.Cluster) > 0 {
modifiedContext.Cluster = context.Cluster
}
if len(context.AuthInfo) > 0 {
modifiedContext.AuthInfo = context.AuthInfo
}
if len(context.Namespace) > 0 {
modifiedContext.Namespace = context.Namespace
}
config.Contexts[name] = &modifiedContext
}
return clientcmd.ModifyConfig(configAccess, *config, true)
}