This repository has been archived by the owner on Oct 12, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 83
/
main.go
252 lines (221 loc) · 6.25 KB
/
main.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
package main
import (
"bytes"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"os"
"os/exec"
"strings"
"github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/plugin"
"github.com/hashicorp/terraform/terraform"
)
type config struct {
kubeconfig string
kubeconfigContent string
kubeconfigContext string
}
func main() {
plugin.Serve(&plugin.ServeOpts{
ProviderFunc: func() terraform.ResourceProvider {
return &schema.Provider{
Schema: map[string]*schema.Schema{
"kubeconfig": &schema.Schema{
Type: schema.TypeString,
Optional: true,
},
"kubeconfig_content": &schema.Schema{
Type: schema.TypeString,
Optional: true,
},
"kubeconfig_context": &schema.Schema{
Type: schema.TypeString,
Optional: true,
},
},
ResourcesMap: map[string]*schema.Resource{
"k8s_manifest": resourceManifest(),
},
ConfigureFunc: func(d *schema.ResourceData) (interface{}, error) {
return &config{
kubeconfig: d.Get("kubeconfig").(string),
kubeconfigContent: d.Get("kubeconfig_content").(string),
kubeconfigContext: d.Get("kubeconfig_context").(string),
}, nil
},
}
},
})
}
func resourceManifest() *schema.Resource {
return &schema.Resource{
Create: resourceManifestCreate,
Read: resourceManifestRead,
Update: resourceManifestUpdate,
Delete: resourceManifestDelete,
Schema: map[string]*schema.Schema{
"content": &schema.Schema{
Type: schema.TypeString,
Required: true,
Sensitive: true,
},
},
}
}
func run(cmd *exec.Cmd) error {
stderr := &bytes.Buffer{}
cmd.Stderr = stderr
if err := cmd.Run(); err != nil {
cmdStr := cmd.Path + " " + strings.Join(cmd.Args, " ")
if stderr.Len() == 0 {
return fmt.Errorf("%s: %v", cmdStr, err)
}
return fmt.Errorf("%s %v: %s", cmdStr, err, stderr.Bytes())
}
return nil
}
func kubeconfigPath(m interface{}) (string, func(), error) {
kubeconfig := m.(*config).kubeconfig
kubeconfigContent := m.(*config).kubeconfigContent
var cleanupFunc = func() {}
if kubeconfig != "" && kubeconfigContent != "" {
return kubeconfig, cleanupFunc, fmt.Errorf("both kubeconfig and kubeconfig_content are defined, " +
"please use only one of the paramters")
} else if kubeconfigContent != "" {
tmpfile, err := ioutil.TempFile("", "kubeconfig_")
if err != nil {
defer cleanupFunc()
return "", cleanupFunc, fmt.Errorf("creating a kubeconfig file: %v", err)
}
cleanupFunc = func() { os.Remove(tmpfile.Name()) }
if _, err = io.WriteString(tmpfile, kubeconfigContent); err != nil {
defer cleanupFunc()
return "", cleanupFunc, fmt.Errorf("writing kubeconfig to file: %v", err)
}
if err = tmpfile.Close(); err != nil {
defer cleanupFunc()
return "", cleanupFunc, fmt.Errorf("completion of write to kubeconfig file: %v", err)
}
kubeconfig = tmpfile.Name()
}
if kubeconfig != "" {
return kubeconfig, cleanupFunc, nil
}
return "", cleanupFunc, nil
}
func kubectl(m interface{}, kubeconfig string, args ...string) *exec.Cmd {
if kubeconfig != "" {
args = append([]string{"--kubeconfig", kubeconfig}, args...)
}
context := m.(*config).kubeconfigContext
if context != "" {
args = append([]string{"--context", context}, args...)
}
return exec.Command("kubectl", args...)
}
func resourceManifestCreate(d *schema.ResourceData, m interface{}) error {
kubeconfig, cleanup, err := kubeconfigPath(m)
if err != nil {
return fmt.Errorf("determining kubeconfig: %v", err)
}
defer cleanup()
cmd := kubectl(m, kubeconfig, "apply", "-f", "-")
cmd.Stdin = strings.NewReader(d.Get("content").(string))
if err := run(cmd); err != nil {
return err
}
stdout := &bytes.Buffer{}
cmd = kubectl(m, kubeconfig, "get", "-f", "-", "-o", "json")
cmd.Stdin = strings.NewReader(d.Get("content").(string))
cmd.Stdout = stdout
if err := run(cmd); err != nil {
return err
}
var data struct {
Items []struct {
Metadata struct {
Selflink string `json:"selflink"`
} `json:"metadata"`
} `json:"items"`
}
if err := json.Unmarshal(stdout.Bytes(), &data); err != nil {
return fmt.Errorf("decoding response: %v", err)
}
if len(data.Items) != 1 {
return fmt.Errorf("expected to create 1 resource, got %d", len(data.Items))
}
selflink := data.Items[0].Metadata.Selflink
if selflink == "" {
return fmt.Errorf("could not parse self-link from response %s", stdout.String())
}
d.SetId(selflink)
return nil
}
func resourceManifestUpdate(d *schema.ResourceData, m interface{}) error {
kubeconfig, cleanup, err := kubeconfigPath(m)
if err != nil {
return fmt.Errorf("determining kubeconfig: %v", err)
}
defer cleanup()
cmd := kubectl(m, kubeconfig, "apply", "-f", "-")
cmd.Stdin = strings.NewReader(d.Get("content").(string))
return run(cmd)
}
func resourceFromSelflink(s string) (resource, namespace string, ok bool) {
parts := strings.Split(s, "/")
if len(parts) < 2 {
return "", "", false
}
resource = parts[len(parts)-2] + "/" + parts[len(parts)-1]
for i, part := range parts {
if part == "namespaces" && len(parts) > i+1 {
namespace = parts[i+1]
break
}
}
return resource, namespace, true
}
func resourceManifestDelete(d *schema.ResourceData, m interface{}) error {
resource, namespace, ok := resourceFromSelflink(d.Id())
if !ok {
return fmt.Errorf("invalid resource id: %s", d.Id())
}
args := []string{"delete", resource}
if namespace != "" {
args = append(args, "-n", namespace)
}
kubeconfig, cleanup, err := kubeconfigPath(m)
if err != nil {
return fmt.Errorf("determining kubeconfig: %v", err)
}
defer cleanup()
cmd := kubectl(m, kubeconfig, args...)
return run(cmd)
}
func resourceManifestRead(d *schema.ResourceData, m interface{}) error {
resource, namespace, ok := resourceFromSelflink(d.Id())
if !ok {
return fmt.Errorf("invalid resource id: %s", d.Id())
}
args := []string{"get", "--ignore-not-found", resource}
if namespace != "" {
args = append(args, "-n", namespace)
}
stdout := &bytes.Buffer{}
kubeconfig, cleanup, err := kubeconfigPath(m)
if err != nil {
return fmt.Errorf("determining kubeconfig: %v", err)
}
defer cleanup()
cmd := kubectl(m, kubeconfig, args...)
cmd.Stdout = stdout
if err := run(cmd); err != nil {
return err
}
if strings.TrimSpace(stdout.String()) == "" {
d.SetId("")
}
return nil
}