-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrootcmd.go
More file actions
265 lines (232 loc) · 7.02 KB
/
rootcmd.go
File metadata and controls
265 lines (232 loc) · 7.02 KB
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
/*
Copyright © 2019 NAME HERE <EMAIL ADDRESS>
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 main
import (
"flag"
"fmt"
"io"
"os"
"strconv"
"strings"
"github.com/fatih/color"
"github.com/pkg/errors"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"helm.sh/helm/v3/pkg/action"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/util/yaml"
"k8s.io/cli-runtime/pkg/genericclioptions"
"k8s.io/client-go/dynamic"
_ "k8s.io/client-go/plugin/pkg/client/auth" // combined authprovider import
"k8s.io/client-go/rest"
"k8s.io/klog"
)
const (
allNamespacesFlag = "all-namespaces"
colorFlag = "color"
)
var cf *genericclioptions.ConfigFlags
// This variable is populated by goreleaser
var version string
// rootCmd represents the base command when called without any subcommands
var rootCmd = &cobra.Command{
Use: "tree RELEASE",
SilenceUsage: true, // for when RunE returns an error
Short: "Show sub-resources of a helm release",
Example: " helm tree my-release\n" +
" helm tree -n kube-public my-release",
Args: cobra.ExactArgs(1),
RunE: run,
Version: versionString(),
}
// versionString returns the version prefixed by 'v'
// or an empty string if no version has been populated by goreleaser.
// In this case, the --version flag will not be added by cobra.
func versionString() string {
if len(version) == 0 {
return ""
}
return "v" + version
}
func atoi(a string, dv int) int {
i, err := strconv.ParseInt(a, 10, 32)
if err != nil {
return dv
}
return int(i)
}
func atof32(a string, dv float32) float32 {
f64, err := strconv.ParseFloat(a, 32)
if err != nil {
return dv
}
return float32(f64)
}
func SetPtrAsEnvvarIfNil(ptr *string, env string) {
if ptr == nil || *ptr == "" {
*ptr = os.Getenv(env)
}
}
func run(command *cobra.Command, args []string) error {
allNs, err := command.Flags().GetBool(allNamespacesFlag)
if err != nil {
allNs = false
}
colorArg, err := command.Flags().GetString(colorFlag)
if err != nil {
return err
}
if colorArg == "always" {
color.NoColor = false
} else if colorArg == "never" {
color.NoColor = true
} else if colorArg != "auto" {
return errors.Errorf("invalid value for --%s", colorFlag)
}
// parse helm envvars to ConfigFlags
SetPtrAsEnvvarIfNil(cf.APIServer, "HELM_KUBEAPISERVER")
SetPtrAsEnvvarIfNil(cf.Impersonate, "HELM_KUBEASUSER")
SetPtrAsEnvvarIfNil(cf.CAFile, "HELM_KUBECAFILE")
SetPtrAsEnvvarIfNil(cf.Context, "HELM_KUBECONTEXT")
*cf.Insecure = os.Getenv("HELM_KUBEINSECURE_SKIP_TLS_VERIFY") == "true"
SetPtrAsEnvvarIfNil(cf.TLSServerName, "HELM_KUBETLS_SERVER_NAME")
SetPtrAsEnvvarIfNil(cf.BearerToken, "HELM_KUBETOKEN")
SetPtrAsEnvvarIfNil(cf.KubeConfig, "KUBECONFIG")
SetPtrAsEnvvarIfNil(cf.Namespace, "HELM_NAMESPACE")
restConfig, err := cf.ToRESTConfig()
if err != nil {
return err
}
restConfig.WarningHandler = rest.NoWarnings{}
restConfig.QPS = atof32(os.Getenv("HELM_QPS"), 1000)
restConfig.Burst = atoi(os.Getenv("HELM_BURST_LIMIT"), 1000)
if restConfig.QPS < 1 {
restConfig.QPS = min(100, float32(restConfig.Burst))
}
dyn, err := dynamic.NewForConfig(restConfig)
if err != nil {
return fmt.Errorf("failed to construct dynamic client: %w", err)
}
dc, err := cf.ToDiscoveryClient()
if err != nil {
return err
}
apis, err := findAPIs(dc)
if err != nil {
return err
}
klog.V(3).Info("completed querying APIs list")
releaseName, err := figureOutName(args)
if err != nil {
return err
}
klog.V(3).Infof("parsed releaseName=%v", releaseName)
ns := getNamespace()
klog.V(2).Infof("namespace=%s allNamespaces=%v", ns, allNs)
// init helm client
actionConfig := new(action.Configuration)
if err = actionConfig.Init(cf, ns, "", klog.Infof); err != nil {
return err
}
cmdGet := action.NewGet(actionConfig)
release, err := cmdGet.Run(releaseName)
if err != nil {
return err
}
var manifests []unstructured.Unstructured
decoder := yaml.NewYAMLOrJSONDecoder(strings.NewReader(release.Manifest), 4096)
for {
var obj unstructured.Unstructured
derr := decoder.Decode(&obj)
if derr == io.EOF {
break
}
if derr != nil {
continue
}
if obj.Object == nil {
continue
}
manifests = append(manifests, obj)
}
klog.V(2).Infof("querying all api objects")
apiObjects, err := getAllResources(dyn, apis.resources(), allNs)
if err != nil {
return fmt.Errorf("error while querying api objects: %w", err)
}
klog.V(2).Infof("found total %d api objects", len(apiObjects))
objDir := newObjectDirectory(apiObjects)
var objs []*unstructured.Unstructured
for _, obj := range manifests {
if obj.GetNamespace() == "" {
kind := obj.GetKind()
apiResults := apis.lookup(kind)
if len(apiResults) == 0 {
return fmt.Errorf("could not find api kind %q", kind)
} else if len(apiResults) > 1 {
names := make([]string, 0, len(apiResults))
for _, a := range apiResults {
names = append(names, fullAPIName(a))
}
return fmt.Errorf("ambiguous kind %q. use one of these as the KIND disambiguate: [%s]", kind,
strings.Join(names, ", "))
}
api := apiResults[0]
if api.r.Namespaced && obj.GetNamespace() == "" {
obj.SetNamespace(ns)
}
}
real := setRealObject(apiObjects, obj)
if real != nil {
objs = append(objs, real)
} else {
objs = append(objs, &obj)
}
}
treeView(os.Stderr, objDir, objs)
klog.V(2).Infof("done printing tree view")
return nil
}
func setRealObject(apiObjects []unstructured.Unstructured, obj unstructured.Unstructured) *unstructured.Unstructured {
for _, real := range apiObjects {
if real.GetKind() == obj.GetKind() && real.GetNamespace() == obj.GetNamespace() && real.GetName() == obj.GetName() {
real := real
return &real
}
}
return nil
}
func init() {
klog.InitFlags(nil)
pflag.CommandLine.AddGoFlagSet(flag.CommandLine)
// hide all glog flags except for -v
flag.CommandLine.VisitAll(func(f *flag.Flag) {
if f.Name != "v" {
pflag.Lookup(f.Name).Hidden = true
}
})
cf = genericclioptions.NewConfigFlags(true)
rootCmd.Flags().BoolP(allNamespacesFlag, "A", false, "query all objects in all API groups, both namespaced and non-namespaced")
rootCmd.Flags().StringP(colorFlag, "c", "auto", "Enable or disable color output. This can be 'always', 'never', or 'auto' (default = use color only if using tty). The flag is overridden by the NO_COLOR env variable if set.")
// cf.AddFlags(rootCmd.Flags())
if err := flag.Set("logtostderr", "true"); err != nil {
fmt.Fprintf(os.Stderr, "failed to set logtostderr flag: %v\n", err)
os.Exit(1)
}
}
func main() {
defer klog.Flush()
if err := rootCmd.Execute(); err != nil {
os.Exit(1)
}
}