-
Notifications
You must be signed in to change notification settings - Fork 16
/
apply.go
279 lines (254 loc) · 8.22 KB
/
apply.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
package cli
import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"io"
"log"
"net/http"
"os"
"path/filepath"
"strings"
apiclient "github.com/flightctl/flightctl/internal/api/client"
"github.com/flightctl/flightctl/internal/client"
"github.com/spf13/cobra"
yamlutil "k8s.io/apimachinery/pkg/util/yaml"
)
var (
fileExtensions = []string{".json", ".yaml", ".yml"}
inputExtensions = append(fileExtensions, "stdin")
)
type ApplyOptions struct {
Filenames []string
DryRun bool
Recursive bool
}
func NewCmdApply() *cobra.Command {
o := &ApplyOptions{Filenames: []string{}, DryRun: false, Recursive: false}
cmd := &cobra.Command{
Use: "apply -f FILENAME",
Short: "Apply a configuration to a resource by file name or stdin.",
RunE: func(cmd *cobra.Command, args []string) error {
if err := o.Complete(cmd, args); err != nil {
return err
}
if err := o.Validate(args); err != nil {
return err
}
return o.Run(cmd.Context(), args)
},
SilenceUsage: true,
}
flags := cmd.Flags()
flags.StringSliceVarP(&o.Filenames, "filename", "f", o.Filenames, "The files or directory that contain the resources to apply.")
annotations := make([]string, 0, len(fileExtensions))
for _, ext := range fileExtensions {
annotations = append(annotations, strings.TrimLeft(ext, "."))
}
err := flags.SetAnnotation("filename", cobra.BashCompFilenameExt, annotations)
if err != nil {
log.Fatalf("setting filename flag annotation: %v", err)
}
flags.BoolVarP(&o.DryRun, "dry-run", "", o.DryRun, "Only print the object that would be sent, without sending it.")
flags.BoolVarP(&o.Recursive, "recursive", "R", o.Recursive, "Process the directory used in -f, --filename recursively.")
return cmd
}
func (o *ApplyOptions) Complete(cmd *cobra.Command, args []string) error {
return nil
}
func (o *ApplyOptions) Validate(args []string) error {
if len(o.Filenames) == 0 {
return fmt.Errorf("must specify -f FILENAME")
}
if len(args) > 0 {
return fmt.Errorf("unexpected arguments: %v (did you forget to quote wildcards?)", args)
}
return nil
}
func (o *ApplyOptions) Run(ctx context.Context, args []string) error {
client, err := client.NewFromConfigFile(defaultClientConfigFile)
if err != nil {
return fmt.Errorf("creating client: %w", err)
}
errs := make([]error, 0)
for _, filename := range o.Filenames {
switch {
case filename == "-":
errs = append(errs, applyFromReader(ctx, client, "<stdin>", os.Stdin, o.DryRun)...)
default:
expandedFilenames, err := expandIfFilePattern(filename)
if err != nil {
errs = append(errs, err)
continue
}
for _, filename := range expandedFilenames {
_, err := os.Stat(filename)
if os.IsNotExist(err) {
errs = append(errs, fmt.Errorf("the path %q does not exist", filename))
continue
}
if err != nil {
errs = append(errs, fmt.Errorf("the path %q cannot be accessed: %w", filename, err))
continue
}
err = filepath.Walk(filename, func(path string, fi os.FileInfo, err error) error {
if err != nil {
return err
}
if fi.IsDir() {
if path != filename && !o.Recursive {
return filepath.SkipDir
}
return nil
}
// Don't check extension if the filepath was passed explicitly
if path != filename && ignoreFile(path, inputExtensions) {
return nil
}
r, err := os.Open(path)
if err != nil {
return nil
}
defer r.Close()
errs = append(errs, applyFromReader(ctx, client, path, r, o.DryRun)...)
return nil
})
if err != nil {
errs = append(errs, fmt.Errorf("error walking %q: %w", filename, err))
}
}
}
}
return errors.Join(errs...)
}
type genericResource map[string]interface{}
func applyFromReader(ctx context.Context, client *apiclient.ClientWithResponses, filename string, r io.Reader, dryRun bool) []error {
decoder := yamlutil.NewYAMLOrJSONDecoder(r, 100)
resources := []genericResource{}
var err error
for {
var resource genericResource
err = decoder.Decode(&resource)
if err != nil {
break
}
resources = append(resources, resource)
}
if !errors.Is(err, io.EOF) {
return []error{err}
}
errs := make([]error, 0)
for _, resource := range resources {
kind, ok := resource["kind"].(string)
if !ok {
errs = append(errs, fmt.Errorf("%s: skipping resource of unspecified kind: %v", filename, resource))
continue
}
metadata, ok := resource["metadata"].(map[string]interface{})
if !ok {
errs = append(errs, fmt.Errorf("%s: skipping resource of unspecified metadata: %v", filename, resource))
continue
}
resourceName, ok := metadata["name"].(string)
if !ok {
errs = append(errs, fmt.Errorf("%s: skipping resource of unspecified resource name: %v", filename, resource))
continue
}
if dryRun {
fmt.Printf("%s: applying %s/%s (dry run only)\n", strings.ToLower(kind), filename, resourceName)
continue
}
fmt.Printf("%s: applying %s/%s: ", strings.ToLower(kind), filename, resourceName)
buf, err := json.Marshal(resource)
if err != nil {
errs = append(errs, fmt.Errorf("%s: skipping resource of kind %q: %w", filename, kind, err))
}
var httpResponse *http.Response
var message string
switch strings.ToLower(kind) {
case DeviceKind:
var response *apiclient.ReplaceDeviceResponse
response, err = client.ReplaceDeviceWithBodyWithResponse(ctx, resourceName, "application/json", bytes.NewReader(buf))
if response != nil {
httpResponse = response.HTTPResponse
message = string(response.Body)
}
case EnrollmentRequestKind:
var response *apiclient.ReplaceEnrollmentRequestResponse
response, err = client.ReplaceEnrollmentRequestWithBodyWithResponse(ctx, resourceName, "application/json", bytes.NewReader(buf))
if response != nil {
httpResponse = response.HTTPResponse
message = string(response.Body)
}
case FleetKind:
var response *apiclient.ReplaceFleetResponse
response, err = client.ReplaceFleetWithBodyWithResponse(ctx, resourceName, "application/json", bytes.NewReader(buf))
if response != nil {
httpResponse = response.HTTPResponse
message = string(response.Body)
}
case TemplateVersionKind:
var response *apiclient.CreateTemplateVersionResponse
response, err = client.CreateTemplateVersionWithBodyWithResponse(ctx, "application/json", bytes.NewReader(buf))
if response != nil {
httpResponse = response.HTTPResponse
message = string(response.Body)
}
case RepositoryKind:
var response *apiclient.ReplaceRepositoryResponse
response, err = client.ReplaceRepositoryWithBodyWithResponse(ctx, resourceName, "application/json", bytes.NewReader(buf))
if response != nil {
httpResponse = response.HTTPResponse
message = string(response.Body)
}
case ResourceSyncKind:
var response *apiclient.ReplaceResourceSyncResponse
response, err = client.ReplaceResourceSyncWithBodyWithResponse(ctx, resourceName, "application/json", bytes.NewReader(buf))
if response != nil {
httpResponse = response.HTTPResponse
message = string(response.Body)
}
default:
err = fmt.Errorf("%s: skipping resource of unknown kind %q: %v", filename, kind, resource)
}
if err != nil {
errs = append(errs, err)
}
if httpResponse != nil {
fmt.Printf("%s\n", httpResponse.Status)
// bad HTTP Responses don't generate an error on the OpenAPI client, we need to check the status code manually
if httpResponse.StatusCode != http.StatusOK && httpResponse.StatusCode != http.StatusCreated {
errs = append(errs, fmt.Errorf("%s: failed to apply %s/%s: %s", strings.ToLower(kind), filename, resourceName, httpResponse.Status))
fmt.Printf("%s\n", message)
}
}
}
return errs
}
func expandIfFilePattern(pattern string) ([]string, error) {
if _, err := os.Stat(pattern); os.IsNotExist(err) {
matches, err := filepath.Glob(pattern)
if err == nil && len(matches) == 0 {
return nil, fmt.Errorf("the path %q does not exist", pattern)
}
if err == filepath.ErrBadPattern {
return nil, fmt.Errorf("pattern %q is not valid: %w", pattern, err)
}
return matches, err
}
return []string{pattern}, nil
}
func ignoreFile(path string, extensions []string) bool {
if len(extensions) == 0 {
return false
}
ext := filepath.Ext(path)
for _, s := range extensions {
if s == ext {
return false
}
}
return true
}