/
describe.go
196 lines (159 loc) · 5.35 KB
/
describe.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
package cmd
import (
"context"
"fmt"
"os"
"strconv"
"github.com/d-kuro/kubectl-fuzzy/pkg/fuzzyfinder"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"k8s.io/apimachinery/pkg/api/meta"
"k8s.io/cli-runtime/pkg/genericclioptions"
"k8s.io/cli-runtime/pkg/printers"
"k8s.io/cli-runtime/pkg/resource"
"k8s.io/kubectl/pkg/describe"
)
const (
exampleDescribe = `
# Selecting an object with the fuzzy finder and view the log and show details
kubectl fuzzy describe TYPE [flags]
`
)
// NewCmdDescribe provides a cobra command wrapping DescribeOptions.
func NewCmdDescribe(config *genericclioptions.ConfigFlags, streams genericclioptions.IOStreams) *cobra.Command {
o := NewDescribeOptions(config, streams)
cmd := &cobra.Command{
Use: "describe",
Short: "Selecting an object with the fuzzy finder and show details",
Example: exampleDescribe,
SilenceUsage: true,
SilenceErrors: true,
RunE: func(c *cobra.Command, args []string) error {
if err := o.Complete(c, args); err != nil {
return err
}
if err := o.Validate(); err != nil {
return err
}
return o.Run(c.Context(), args)
},
}
o.AddFlags(cmd.Flags())
return cmd
}
// DescribeOptions provides information required to update
// the current context on a user's KUBECONFIG.
type DescribeOptions struct {
configFlags *genericclioptions.ConfigFlags
printFlags *genericclioptions.JSONYamlPrintFlags
genericclioptions.IOStreams
describerSettings *describe.DescriberSettings
builder *resource.Builder
describer func(*meta.RESTMapping) (describe.ResourceDescriber, error)
allNamespaces bool
namespace string
selector string
builderArgs []string
preview bool
previewFormat string
rawPreview bool
}
// AddFlags adds a flag to the flag set.
func (o *DescribeOptions) AddFlags(flags *pflag.FlagSet) {
// kubectl flags
flags.BoolVarP(&o.allNamespaces, "all-namespaces", "A", false,
"If present, list the requested object(s) across all namespaces. "+
"Namespace in current context is ignored even if specified with --namespace.")
flags.StringVarP(&o.selector, "selector", "l", "",
"Selector (label query) to filter on, supports '=', '==', and '!='.(e.g. -l key1=value1,key2=value2)")
flags.BoolVar(&o.describerSettings.ShowEvents, "show-events", true,
"If true, display events related to the described object.")
// original flags
flags.BoolVarP(&o.preview, "preview", "P", false,
"If true, display the object YAML|JSON by preview window for fuzzy finder selector.")
flags.StringVar(&o.previewFormat, "preview-format", "yaml",
"Preview window output format. One of json|yaml.")
flags.BoolVar(&o.rawPreview, "raw-preview", false,
"If true, display the unsimplified object in the preview window. (default is simplified)")
}
// NewDescribeOptions provides an instance of DescribeOptions with default values.
func NewDescribeOptions(config *genericclioptions.ConfigFlags, streams genericclioptions.IOStreams) *DescribeOptions {
return &DescribeOptions{
configFlags: config,
printFlags: genericclioptions.NewJSONYamlPrintFlags(),
IOStreams: streams,
describerSettings: &describe.DescriberSettings{
ShowEvents: true,
},
}
}
// Complete sets all information required for show details.
func (o *DescribeOptions) Complete(cmd *cobra.Command, args []string) error {
o.builder = resource.NewBuilder(o.configFlags)
o.describer = func(mapping *meta.RESTMapping) (describe.ResourceDescriber, error) {
return describe.DescriberFn(o.configFlags, mapping)
}
o.builderArgs = args
if !o.preview {
o.preview, _ = strconv.ParseBool(os.Getenv(previewEnabledEnvVar))
}
if !o.allNamespaces {
kubeConfig := o.configFlags.ToRawKubeConfigLoader()
namespace, _, err := kubeConfig.Namespace()
if err != nil {
return fmt.Errorf("faild to get namespace from kube config: %w", err)
}
o.namespace = namespace
}
return nil
}
// Validate ensures that all required arguments and flag values are provided.
func (DescribeOptions) Validate() error {
return nil
}
// Run execute fizzy finder and show details.
func (o *DescribeOptions) Run(ctx context.Context, args []string) error {
r := o.builder.
Unstructured().
ContinueOnError().
NamespaceParam(o.namespace).DefaultNamespace().AllNamespaces(o.allNamespaces).
LabelSelectorParam(o.selector).
ResourceTypeOrNameArgs(true, o.builderArgs...).
Flatten().
Do()
if err := r.Err(); err != nil {
return fmt.Errorf("failed to request: %w", err)
}
infos, err := r.Infos()
if err != nil {
return fmt.Errorf("failed to get infos: %w", err)
}
if len(infos) == 0 {
return fmt.Errorf("resource not found")
}
var printer printers.ResourcePrinter
if o.preview {
printer, err = o.printFlags.ToPrinter(o.previewFormat)
if err != nil {
return fmt.Errorf("failed to get printer: %w", err)
}
}
info, err := fuzzyfinder.Infos(infos,
fuzzyfinder.WithAllNamespaces(o.allNamespaces),
fuzzyfinder.WithPreview(printer),
fuzzyfinder.WithRawPreview(o.rawPreview))
if err != nil {
return fmt.Errorf("failed to fuzzyfinder execute: %w", err)
}
mapping := info.ResourceMapping()
describer, err := o.describer(mapping)
if err != nil {
return fmt.Errorf("failed to get describer: %w", err)
}
s, err := describer.Describe(info.Namespace, info.Name, *o.describerSettings)
if err != nil {
return fmt.Errorf("failed to generates output: %w", err)
}
fmt.Fprintf(o.Out, "%s", s)
return nil
}