-
Notifications
You must be signed in to change notification settings - Fork 8
/
edit_status.go
444 lines (397 loc) · 13.7 KB
/
edit_status.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
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
// HO.
/*
Copyright 2020 Alper Rifat Ulucinar
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.
*/
/*
This software is a derivative work based upon:
- https://github.com/mstrzele/helm-edit
[License: LICENSE.helm-edit]
- https://github.com/kubernetes/sample-cli-plugin
[License: LICENSE.sample-cli-plugin]
Modifications:
- Declare EditStatusOptions
- Implement cmd.NewCmdEditStatus, which provides the "edit status" command
*/
/*
Copyright 2018 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 cmd
import (
"context"
"fmt"
"io/ioutil"
"os"
"os/exec"
"strings"
jsonPatch "github.com/evanphx/json-patch"
"github.com/pkg/errors"
"github.com/spf13/cobra"
"go.uber.org/multierr"
"sigs.k8s.io/controller-runtime/pkg/client/apiutil"
"sigs.k8s.io/yaml"
kerrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/runtime/serializer"
"k8s.io/apimachinery/pkg/types"
"k8s.io/cli-runtime/pkg/genericclioptions"
"k8s.io/client-go/discovery"
"k8s.io/client-go/dynamic"
_ "k8s.io/client-go/plugin/pkg/client/auth"
"k8s.io/client-go/rest"
"k8s.io/client-go/restmapper"
)
const (
argEditStatus = "edit-status"
envEditor = "EDITOR"
envKubeEditor = "KUBE_EDITOR"
delimEditorNames = ":"
defaultEditorName = "vi"
defaultNamespaced = true
patternTmpFile = "kubectl-edit-status-"
flagNamespaced = "namespaced"
fmtUsage = ` kubectl %s [flags] <partial resource specification> <resource name>
or,
kubectl %s [flags] <partial resource specification>/<resource name>
`
fmtEditStatusExample = ` # edit the status field of the MyResource CR named "test", which uses status subresource
kubectl %s myresource test
kubectl %s myresource/test
`
)
// EditStatusOptions provides information required to update
// the current context on a user's KUBECONFIG
type EditStatusOptions struct {
configFlags *genericclioptions.ConfigFlags
cmd *cobra.Command
namespaced bool
namespace string
resource string
resourceName string
resourceEditor string
dynamicClient dynamic.Interface
restConfig *rest.Config
discoveryClient discovery.CachedDiscoveryInterface
restMapper meta.RESTMapper
gvk schema.GroupVersionKind
originalJSon []byte
genericclioptions.IOStreams
}
// NewEditStatusOptions provides an instance of EditStatusOptions with default values
func NewEditStatusOptions(streams genericclioptions.IOStreams) *EditStatusOptions {
return &EditStatusOptions{
configFlags: genericclioptions.NewConfigFlags(true),
IOStreams: streams,
}
}
// NewCmdEditStatus provides a cobra command wrapping EditStatusOptions
func NewCmdEditStatus(streams genericclioptions.IOStreams) *cobra.Command {
o := NewEditStatusOptions(streams)
o.cmd = &cobra.Command{
Use: fmt.Sprintf(fmtUsage, argEditStatus, argEditStatus),
Short: "Edit /status subresource",
Example: fmt.Sprintf(fmtEditStatusExample, argEditStatus, argEditStatus),
SilenceUsage: true,
PreRunE: func(cmd *cobra.Command, args []string) error {
if err := o.Init(cmd, args); err != nil {
if usageErr := cmd.Usage(); usageErr != nil {
// log usageError
_, _ = fmt.Fprintf(o.ErrOut, "Error occured while printing command usage: %s", usageErr.Error())
}
return errors.Wrap(err, "cannot initialize")
}
return nil
},
RunE: o.Run,
}
o.cmd.Flags().BoolVar(&o.namespaced, flagNamespaced, defaultNamespaced, "set to false for cluster-scoped resources")
o.cmd.Flags().StringVarP(&o.resourceEditor, "editor", "e",
fmt.Sprintf("${%s}%s${%s}%s%s",
envKubeEditor, delimEditorNames, envEditor, delimEditorNames, defaultEditorName),
fmt.Sprintf("editor to use. Either editor name in PATH or path to the editor executable. "+
"If not specified, first value of %q and then value of %q environment variables are substituted and checked",
envKubeEditor, envEditor))
// add K8s generic client flags
o.configFlags.AddFlags(o.cmd.Flags())
return o.cmd
}
// Init ensures that all required arguments and flag values are provided and fills the EditStatusOptions receiver arg
func (o *EditStatusOptions) Init(_ *cobra.Command, args []string) error {
switch len(args) {
case 2:
o.resource = args[0]
o.resourceName = args[1]
case 1:
parts := strings.Split(args[0], "/")
if len(parts) != 2 {
return errors.New("single command-line argument must be in the following format: <partial resource specification>/<resource name>")
}
o.resource = parts[0]
o.resourceName = parts[1]
default:
return errors.New("invalid number of command-line arguments. Expecting 1 or 2 arguments.")
}
var err error
o.discoveryClient, err = o.configFlags.ToDiscoveryClient()
if err != nil {
return errors.Wrap(err, "cannot get discovery client")
}
o.restConfig, err = o.configFlags.ToRESTConfig()
if err != nil {
return errors.Wrap(err, "cannot get REST config")
}
o.dynamicClient, err = dynamic.NewForConfig(o.restConfig)
if err != nil {
return errors.Wrap(err, "cannot initialize a dynamic client")
}
o.restMapper, err = o.configFlags.ToRESTMapper()
if err != nil {
return errors.Wrap(err, "cannot get a REST mapper")
}
o.namespace, _, err = o.configFlags.ToRawKubeConfigLoader().Namespace()
if err != nil {
return errors.Wrap(err, "cannot get configured namespace")
}
return o.storeEditorPath()
}
func (o *EditStatusOptions) storeEditorPath() error {
editorNames := strings.Split(os.ExpandEnv(o.resourceEditor), delimEditorNames)
o.resourceEditor = ""
for _, e := range editorNames {
trimmedName := strings.TrimSpace(e)
if trimmedName != "" {
o.resourceEditor = trimmedName
break
}
}
if o.resourceEditor == "" {
return errors.New("resource editor not specified")
}
return nil
}
// Run forks an editor for editing the specified CR's status subresource
func (o *EditStatusOptions) Run(_ *cobra.Command, _ []string) error {
tmpEditFile, err := ioutil.TempFile(os.TempDir(), patternTmpFile)
if err != nil {
return errors.Wrap(err, "cannot create temp file")
}
defer func() {
errRemove := os.Remove(tmpEditFile.Name())
if err == nil {
err = errRemove
} else if errRemove != nil {
_, _ = fmt.Fprintf(o.ErrOut, "Error occured while removing temporary file %q: %s",
tmpEditFile.Name(), errRemove.Error())
}
}()
if err := o.storeResource(tmpEditFile); err != nil {
return err
}
if err := o.editResource(tmpEditFile); err != nil {
return err
}
return o.writeResourceStatus(tmpEditFile)
}
func (o *EditStatusOptions) storeResourceWithGVR(f *os.File, gvr schema.GroupVersionResource) error {
restMapper := restmapper.NewDeferredDiscoveryRESTMapper(o.discoveryClient)
gvrs, err := restMapper.ResourcesFor(gvr)
if err != nil {
return errors.Wrap(err, "no GVRs found")
}
namespaced := defaultNamespaced
for _, gvr := range gvrs {
var obj *unstructured.Unstructured
var ri dynamic.ResourceInterface = o.dynamicClient.Resource(gvr)
namespaced, err = o.isNamespaced(gvr)
if err != nil {
return err
}
if namespaced {
ri = ri.(dynamic.NamespaceableResourceInterface).Namespace(o.namespace)
}
if obj, err = ri.Get(context.TODO(),
o.resourceName, metav1.GetOptions{}); kerrors.IsNotFound(err) {
// then resource with the given GVR is not found
continue
}
if err != nil {
return errors.Wrapf(err, "cannot get object: GVR: %s, Name: %s", gvr.String(), o.resourceName)
}
// having read the object, store its YAML manifest into a file for editing
if o.originalJSon, err = obj.MarshalJSON(); err != nil {
return errors.Wrap(err, "cannot marshal object into JSON")
}
buff, err := yaml.JSONToYAML(o.originalJSon)
if err != nil {
return errors.Wrap(err, "cannot convert object JSON to YAML")
}
if _, err = f.Write(buff); err != nil {
return errors.Wrapf(err, "cannot write marshaled YAML to file: %s", f.Name())
}
if err = f.Sync(); err != nil {
return errors.Wrapf(err, "cannot sync file: %s", f.Name())
}
// finally, store the GVK
o.gvk, err = o.restMapper.KindFor(gvr)
return errors.Wrapf(err, "cannot get GVK for GVR: %s", gvr.String())
}
scope := "cluster-scoped"
if namespaced {
scope = fmt.Sprintf("namespaced (in %q)", o.namespace)
}
return errors.Wrapf(err, "%s resource %s %q with GVR %q not found",
scope, o.resource, o.resourceName, gvr.String())
}
func (o *EditStatusOptions) storeResource(f *os.File) error {
resourceParts := strings.Split(o.resource, ".")
searchGVRs := []schema.GroupVersionResource{
{
Resource: resourceParts[0],
Group: strings.Join(resourceParts[1:], "."),
},
}
// extend list of GVRs to be searched by any matching short names
shortNameGVRs, err := o.resourcesForShortName(resourceParts[0])
if err != nil {
return err
}
// short-name matching resources are considered with lower priority
searchGVRs = append(searchGVRs, shortNameGVRs...)
var aggregatedErr error
for _, gvr := range searchGVRs {
err := o.storeResourceWithGVR(f, gvr)
// as long as we have no resource match for
// the partial resource specification or
// no object with the given scope & name
// we will continue searching.
t := &meta.NoResourceMatchError{}
if errors.As(err, &t) || kerrors.IsNotFound(err) {
aggregatedErr = multierr.Append(aggregatedErr, err)
continue
}
if err != nil {
return err
}
return nil
}
return errors.Wrap(aggregatedErr,
"cannot find any GVRs for the partial specification, or no objects have been found")
}
func (o *EditStatusOptions) resourcesForShortName(shortName string) ([]schema.GroupVersionResource, error) {
var result []schema.GroupVersionResource
_, arrResourceList, err := o.discoveryClient.ServerGroupsAndResources()
if err != nil {
return nil, errors.Wrapf(err, "cannot discover all server resources to search for the short name: %s", shortName)
}
for _, resourceList := range arrResourceList {
if resourceList == nil {
continue
}
for _, r := range resourceList.APIResources {
for _, sn := range r.ShortNames {
if sn == shortName {
result = append(result, schema.GroupVersionResource{
Group: r.Group,
Version: r.Version,
Resource: r.Name,
})
}
}
}
}
return result, nil
}
// isNamespaced returns true if the resource is namespaced.
// if "--namespaced" is not explicitly set, then we try to infer
// the resource's scope.
func (o *EditStatusOptions) isNamespaced(gvr schema.GroupVersionResource) (bool, error) {
if o.cmd.Flags().Changed(flagNamespaced) {
return o.namespaced, nil
}
// try to infer resource scope from CRDs
gv := schema.GroupVersion{
Group: gvr.Group,
Version: gvr.Version,
}.String()
resourceList, err := o.discoveryClient.ServerResourcesForGroupVersion(gv)
if err != nil {
return false, errors.Wrapf(err, "cannot discover server resources for GV: %q", gv)
}
for _, r := range resourceList.APIResources {
if r.Name == gvr.Resource {
return r.Namespaced, nil
}
}
return defaultNamespaced, nil
}
func (o *EditStatusOptions) editResource(f *os.File) error {
parts := strings.Fields(o.resourceEditor)
execName := ""
var args []string
switch len(parts) {
case 0:
return errors.Errorf("invalid editor specification: %q", o.resourceEditor)
default:
args = parts[1:]
fallthrough
case 1:
execName = parts[0]
args = append(args, f.Name())
}
cmd := exec.Command(execName, args...)
cmd.Stdin = o.In
cmd.Stdout = o.Out
cmd.Stderr = o.ErrOut
return errors.Wrapf(cmd.Run(), "cannot edit resource using editor. Command-line: %q", cmd.String())
}
func (o *EditStatusOptions) writeResourceStatus(f *os.File) error {
restMapping, err := o.restMapper.RESTMapping(o.gvk.GroupKind(), o.gvk.Version)
if err != nil {
return errors.Wrapf(err, "cannot get REST mapping for GVK: %s", o.gvk.String())
}
editedYaml, err := ioutil.ReadFile(f.Name())
if err != nil {
return errors.Wrapf(err, "cannot read edited object from file: %s", f.Name())
}
editedJSon, err := yaml.YAMLToJSON(editedYaml)
if err != nil {
return errors.Wrapf(err, "cannot convert edited object's YAML to JSON from file: %s", f.Name())
}
patch, err := jsonPatch.CreateMergePatch(o.originalJSon, editedJSon)
if err != nil {
return errors.Wrap(err, "cannot prepare merge patch")
}
restClient, err := apiutil.RESTClientForGVK(o.gvk, true, o.restConfig, serializer.CodecFactory{})
if err != nil {
return errors.Wrapf(err, "cannot get REST client for GVK: %s", o.gvk.String())
}
_, err = restClient.Patch(types.MergePatchType).
NamespaceIfScoped(o.namespace, restMapping.Scope.Name() == meta.RESTScopeNameNamespace).
Resource(restMapping.Resource.Resource).
Name(o.resourceName).
SubResource("status").
VersionedParams(&metav1.PatchOptions{}, metav1.ParameterCodec).
Body(patch).
DoRaw(context.TODO())
return errors.Wrapf(err, "cannot merge patch object: GVK: %s, Name: %s", o.gvk.String(), o.resourceName)
}