-
Notifications
You must be signed in to change notification settings - Fork 95
/
Copy pathresource.go
205 lines (189 loc) · 5.17 KB
/
resource.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
/*
Copyright 2016 Skippbox, Ltd.
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 (
"github.com/bitnami-labs/kubewatch/config"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)
// resourceConfigCmd represents the resource subcommand
var resourceConfigCmd = &cobra.Command{
Use: "resource",
Short: "manage resources to be watched",
Long: `
manage resources to be watched`,
Run: func(cmd *cobra.Command, args []string) {
// warn for too few arguments
if len(args) < 2 {
logrus.Warn("Too few arguments to Command \"resource\".\nMinimum 2 arguments required: subcommand, resource flags")
}
// display help
cmd.Help()
},
}
// resourceConfigAddCmd represents the resource add subcommand
var resourceConfigAddCmd = &cobra.Command{
Use: "add",
Short: "adds specific resources to be watched",
Long: `
adds specific resources to be watched`,
Run: func(cmd *cobra.Command, args []string) {
conf, err := config.New()
if err != nil {
logrus.Fatal(err)
}
// add resource to config
configureResource("add", cmd, conf)
},
}
// resourceConfigRemoveCmd represents the resource remove subcommand
var resourceConfigRemoveCmd = &cobra.Command{
Use: "remove",
Short: "remove specific resources being watched",
Long: `
remove specific resources being watched`,
Run: func(cmd *cobra.Command, args []string) {
conf, err := config.New()
if err != nil {
logrus.Fatal(err)
}
// remove resource from config
configureResource("remove", cmd, conf)
},
}
// configures resource in config based on operation add/remove
func configureResource(operation string, cmd *cobra.Command, conf *config.Config) {
// flags struct
flags := []struct {
resourceStr string
resourceToWatch *bool
}{
{
"svc",
&conf.Resource.Services,
},
{
"deploy",
&conf.Resource.Deployment,
},
{
"po",
&conf.Resource.Pod,
},
{
"rs",
&conf.Resource.ReplicaSet,
},
{
"rc",
&conf.Resource.ReplicationController,
},
{
"ns",
&conf.Resource.Namespace,
},
{
"job",
&conf.Resource.Job,
},
{
"pv",
&conf.Resource.PersistentVolume,
},
{
"ds",
&conf.Resource.DaemonSet,
},
{
"secret",
&conf.Resource.Secret,
},
{
"cm",
&conf.Resource.ConfigMap,
},
{
"ing",
&conf.Resource.Ingress,
},
{
"node",
&conf.Resource.Node,
},
{
"clusterrole",
&conf.Resource.ClusterRole,
},
{
"clusterrolebinding",
&conf.Resource.ClusterRoleBinding,
},
{
"sa",
&conf.Resource.ServiceAccount,
},
{
"statefulset",
&conf.Resource.StatefulSet,
},
{
"coreevent",
&conf.Resource.CoreEvent,
},
}
for _, flag := range flags {
b, err := cmd.Flags().GetBool(flag.resourceStr)
if err == nil {
if b {
switch operation {
case "add":
*flag.resourceToWatch = true
logrus.Infof("resource %s configured", flag.resourceStr)
case "remove":
*flag.resourceToWatch = false
logrus.Infof("resource %s removed", flag.resourceStr)
}
}
} else {
logrus.Fatal(flag.resourceStr, err)
}
}
if err := conf.Write(); err != nil {
logrus.Fatal(err)
}
}
func init() {
RootCmd.AddCommand(resourceConfigCmd)
resourceConfigCmd.AddCommand(
resourceConfigAddCmd,
resourceConfigRemoveCmd,
)
// Add resource object flags as PersistentFlags to resourceConfigCmd
resourceConfigCmd.PersistentFlags().Bool("svc", false, "watch for services")
resourceConfigCmd.PersistentFlags().Bool("deploy", false, "watch for deployments")
resourceConfigCmd.PersistentFlags().Bool("po", false, "watch for pods")
resourceConfigCmd.PersistentFlags().Bool("rc", false, "watch for replication controllers")
resourceConfigCmd.PersistentFlags().Bool("rs", false, "watch for replicasets")
resourceConfigCmd.PersistentFlags().Bool("ns", false, "watch for namespaces")
resourceConfigCmd.PersistentFlags().Bool("pv", false, "watch for persistent volumes")
resourceConfigCmd.PersistentFlags().Bool("job", false, "watch for jobs")
resourceConfigCmd.PersistentFlags().Bool("ds", false, "watch for daemonsets")
resourceConfigCmd.PersistentFlags().Bool("secret", false, "watch for plain secrets")
resourceConfigCmd.PersistentFlags().Bool("cm", false, "watch for plain configmaps")
resourceConfigCmd.PersistentFlags().Bool("ing", false, "watch for ingresses")
resourceConfigCmd.PersistentFlags().Bool("node", false, "watch for Nodes")
resourceConfigCmd.PersistentFlags().Bool("clusterrole", false, "watch for cluster roles")
resourceConfigCmd.PersistentFlags().Bool("clusterrolebinding", false, "watch for cluster roles binding")
resourceConfigCmd.PersistentFlags().Bool("sa", false, "watch for service accounts")
resourceConfigCmd.PersistentFlags().Bool("coreevent", false, "watch for events (old events object)")
}