This repository has been archived by the owner on Mar 1, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
groot.go
141 lines (111 loc) · 3.18 KB
/
groot.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
/*
Copyright © 2019 Amey Deshmukh
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 (
"fmt"
"os"
"github.com/spf13/cobra"
"os/user"
_ "k8s.io/client-go/plugin/pkg/client/auth"
"github.com/ameydev/groot/kmap"
homedir "github.com/mitchellh/go-homedir"
"github.com/spf13/viper"
appsv1 "k8s.io/api/apps/v1"
v1 "k8s.io/api/core/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/clientcmd"
)
var indentationCount int = 1
var indentation string
var pods v1.PodList
var deployments appsv1.DeploymentList
var services v1.ServiceList
type configs struct {
Namespace string
KubeConfig string
}
func Execute() error {
return groot().Execute()
}
func groot() *cobra.Command {
c := &configs{
Namespace: "default",
KubeConfig: getKubeConfig(),
}
cmd := &cobra.Command{
Use: "groot",
Version: "\nI am baby Groot - v0.0.1",
Short: "groot is a k8s helper CLI utility tool.",
Long: `This tool is used to find k8s resourses and their mappings with other k8s reources. For example:
groot -n $namespace.`,
PreRunE: func(cobracmd *cobra.Command, _ []string) error {
// load current kube-config
return initConfig(c)
},
RunE: func(_ *cobra.Command, _ []string) error {
return getOverView(c)
},
}
flags := cmd.Flags()
flags.StringVar(&c.Namespace, "namespace", c.Namespace, "namespace in which we need to map k8s resources..")
flags.StringVar(&c.KubeConfig, "kubeconfig", c.KubeConfig, "Any external kube config we want to use")
return cmd
}
// initConfig reads in config file and ENV variables if set.
func initConfig(c *configs) error {
if c.KubeConfig != "" {
// Use config file from the flag.
viper.SetConfigFile(c.KubeConfig)
} else {
// Find home directory.
home, err := homedir.Dir()
if err != nil {
fmt.Println(err)
os.Exit(1)
}
// Search config in home directory with name ".groot" (without extension).
viper.AddConfigPath(home)
viper.SetConfigName(".groot")
}
viper.AutomaticEnv() // read in environment variables that match
// If a config file is found, read it in.
if err := viper.ReadInConfig(); err == nil {
fmt.Println("Using config file:", viper.ConfigFileUsed())
}
return nil
}
func getOverView(c *configs) error {
config, err := clientcmd.BuildConfigFromFlags("", c.KubeConfig)
if err != nil {
return err
}
clientset, err := kubernetes.NewForConfig(config)
if err != nil {
return err
}
kmap.FindThemAll(clientset, &c.Namespace)
return nil
}
func getKubeConfig() string {
var kubeconfig string
if envVar := os.Getenv("KUBECONFIG"); len(envVar) > 0 {
kubeconfig = envVar
} else {
usr, err := user.Current()
if err != nil {
fmt.Println(err)
}
kubeconfig = usr.HomeDir + "/.kube/config"
}
return kubeconfig
}