-
Notifications
You must be signed in to change notification settings - Fork 6
/
utils.go
405 lines (367 loc) · 13.4 KB
/
utils.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
/*
Copyright (C) 2019 Synopsys, Inc.
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you 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 synopsysctl
import (
"encoding/json"
"fmt"
"io"
"os"
"os/exec"
"path/filepath"
"strings"
alertclientset "github.com/blackducksoftware/synopsysctl/pkg/alert/client/clientset/versioned"
blackduckclientset "github.com/blackducksoftware/synopsysctl/pkg/blackduck/client/clientset/versioned"
"github.com/blackducksoftware/synopsysctl/pkg/globals"
opssightclientset "github.com/blackducksoftware/synopsysctl/pkg/opssight/client/clientset/versioned"
"github.com/blackducksoftware/synopsysctl/pkg/util"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
apiextensions "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1beta1"
apiextensionsclient "k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
clientcmdapi "k8s.io/client-go/tools/clientcmd/api"
)
var restconfig *rest.Config
var kubeClient *kubernetes.Clientset
var apiExtensionClient *apiextensionsclient.Clientset
var alertClient *alertclientset.Clientset
var blackDuckClient *blackduckclientset.Clientset
var opsSightClient *opssightclientset.Clientset
// setSynopsysctlLogLevel sets the binary's log level to the value stored in logLevelCtl
func setSynopsysctlLogLevel() error {
lvl, err := log.ParseLevel(logLevelCtl)
if err != nil {
log.Errorf("ctl-log-Level '%s' is not a valid level: %s", logLevelCtl, err)
return err
}
log.SetLevel(lvl)
return nil
}
// setGlobalKubeConfigPath sets the global variable 'kubeConfigPath' with points to a kubeconfig file for accessing a cluster
// If the kubeconfig flag was set then kubeConfigPath should already have the path.
// If the kubeconfig flag was not set then it will check the environ 'KUBECONFIG' for a path
func setGlobalKubeConfigPath(cmd *cobra.Command) error {
if !cmd.Flags().Lookup("kubeconfig").Changed { // if --kubeconfig flag wasn't set, check the environ
if kubeconfigEnvVal, exists := os.LookupEnv("KUBECONFIG"); exists { // set kubeconfig if environ is set
kubeConfigPath = kubeconfigEnvVal
}
}
// if the kubeConfigPath was set, verify that the file exists
if kubeConfigPath != "" {
if _, err := os.Stat(kubeConfigPath); os.IsNotExist(err) {
return fmt.Errorf("the kubeconfig path '%s' does not point to a file", kubeConfigPath)
}
}
return nil
}
// GetKubeClientFromOutsideCluster returns the rest config of outside cluster
func GetKubeClientFromOutsideCluster(kubeconfigpath string, insecureSkipTLSVerify bool) (*rest.Config, error) {
// Determine Config Paths
if home := homeDir(); len(kubeconfigpath) == 0 && home != "" {
kubeconfigpath = filepath.Join(home, ".kube", "config")
}
kubeConfig, err := clientcmd.NewNonInteractiveDeferredLoadingClientConfig(
&clientcmd.ClientConfigLoadingRules{
ExplicitPath: kubeconfigpath,
},
&clientcmd.ConfigOverrides{
ClusterInfo: clientcmdapi.Cluster{
Server: "",
InsecureSkipTLSVerify: insecureSkipTLSVerify,
},
}).ClientConfig()
if err != nil {
return nil, err
}
return kubeConfig, nil
}
// homeDir determines the user's home directory path
func homeDir() string {
if h := os.Getenv("HOME"); h != "" {
return h
}
return os.Getenv("USERPROFILE") // windows
}
// setGlobalRestConfig sets the global variable 'restconfig' for other commands to use
func setGlobalRestConfig() error {
var err error
restconfig, err = GetKubeClientFromOutsideCluster(kubeConfigPath, insecureSkipTLSVerify)
log.Debugf("rest config: %+v", restconfig)
if err != nil {
return err
}
return nil
}
// setGlobalKubeClient sets the global variable 'kubeClient' for other commands to use
func setGlobalKubeClient() error {
var err error
kubeClient, err = getKubeClient(restconfig)
if err != nil {
return err
}
return nil
}
// setGlobalResourceClients sets the global variables for the Kuberentes rest config
// and the resource clients
func setGlobalResourceClients() error {
var err error
apiExtensionClient, err = apiextensionsclient.NewForConfig(restconfig)
if err != nil {
return err
}
alertClient, err = alertclientset.NewForConfig(restconfig)
if err != nil {
log.Errorf("error creating Alert Clientset: %s", err)
}
blackDuckClient, err = blackduckclientset.NewForConfig(restconfig)
if err != nil {
log.Errorf("error creating Black Duck Clientset: %s", err)
}
opsSightClient, err = opssightclientset.NewForConfig(restconfig)
if err != nil {
log.Errorf("error creating OpsSight Clientset: %s", err)
}
return nil
}
// getKubeClient gets the kubernetes client
func getKubeClient(kubeConfig *rest.Config) (*kubernetes.Clientset, error) {
client, err := kubernetes.NewForConfig(kubeConfig)
if err != nil {
return nil, err
}
return client, nil
}
// DetermineClusterClients returns bool values for which client
// to use. They will never both be true
func DetermineClusterClients(restConfig *rest.Config, kubeClient *kubernetes.Clientset) (kube, openshift bool) {
openshift = false
kube = false
kubectlPath := false
ocPath := false
_, exists := exec.LookPath("kubectl")
if exists == nil {
kubectlPath = true
}
_, ocexists := exec.LookPath("oc")
if ocexists == nil {
ocPath = true
}
// Add Openshift rules
openshiftTest := false
if util.IsOpenshift(kubeClient) {
openshiftTest = true
}
if ocPath && openshiftTest { // if oc exists and the cluster is openshift
log.Debugf("oc exists and the cluster is openshift")
return false, true
}
if kubectlPath && !openshiftTest { // if kubectl exists and it isn't openshift
log.Debugf("kubectl exists and it isn't openshift")
return true, false
}
if kubectlPath && !ocPath && openshiftTest { // if kubectl exists, oc doesn't exist, and it is openshift
log.Debugf("kubectl exists, oc doesn't exist, and it is openshift")
return true, false
}
if ocPath && !kubectlPath && !openshiftTest { // If oc exists, kubectl doesn't exist, and it isn't openshift
log.Debugf("oc exists, kubectl doesn't exist, and it isn't openshift")
return false, true
}
return false, false // neither client exists
}
func getKubeExecCmd(restconfig *rest.Config, kubeClient *kubernetes.Clientset, args ...string) (*exec.Cmd, error) {
kube, openshift := DetermineClusterClients(restconfig, kubeClient)
// cluster-info in kube doesnt seem to be in
// some versions of oc, but status is.
// double check this.
if args[0] == "cluster-info" && openshift {
args[0] = "status"
}
// add global flags: insecure-skip-tls-verify and --kubeconfig
if insecureSkipTLSVerify == true {
args = append([]string{fmt.Sprintf("--insecure-skip-tls-verify=%t", insecureSkipTLSVerify)}, args...)
}
if kubeConfigPath != "" {
args = append([]string{fmt.Sprintf("--kubeconfig=%s", kubeConfigPath)}, args...)
}
if openshift {
return exec.Command("oc", args...), nil
} else if kube {
return exec.Command("kubectl", args...), nil
} else {
return nil, fmt.Errorf("couldn't determine if running in Openshift or Kubernetes")
}
}
// RunKubeCmd is a simple wrapper to oc/kubectl exec that captures output.
// TODO consider replacing w/ go api but not crucial for now.
func RunKubeCmd(restconfig *rest.Config, kubeClient *kubernetes.Clientset, args ...string) (string, error) {
cmd2, err := getKubeExecCmd(restconfig, kubeClient, args...)
if err != nil {
return "", err
}
stdoutErr, err := cmd2.CombinedOutput()
if err != nil {
return string(stdoutErr), err
}
return string(stdoutErr), nil
}
// RunKubeCmdWithStdin is a simple wrapper to kubectl exec command with standard input
func RunKubeCmdWithStdin(restconfig *rest.Config, kubeClient *kubernetes.Clientset, stdin string, args ...string) (string, error) {
cmd2, err := getKubeExecCmd(restconfig, kubeClient, args...)
if err != nil {
return "", err
}
stdinPipe, err := cmd2.StdinPipe()
if err != nil {
return "", err
}
go func() {
defer stdinPipe.Close()
io.WriteString(stdinPipe, stdin)
}()
stdoutErr, err := cmd2.CombinedOutput()
if err != nil {
return string(stdoutErr), err
}
return string(stdoutErr), nil
}
// RunKubeEditorCmd is a wrapper for oc/kubectl but redirects
// input/output to the user - ex: let user control text editor
func RunKubeEditorCmd(restConfig *rest.Config, kubeClient *kubernetes.Clientset, args ...string) error {
var cmd *exec.Cmd
kube, openshift := DetermineClusterClients(restconfig, kubeClient)
// cluster-info in kube doesnt seem to be in
// some versions of oc, but status is.
// double check this.
if args[0] == "cluster-info" && openshift {
args[0] = "status"
}
// add global flags: insecure-skip-tls-verify and --kubeconfig
if insecureSkipTLSVerify == true {
args = append([]string{fmt.Sprintf("--insecure-skip-tls-verify=%t", insecureSkipTLSVerify)}, args...)
}
if kubeConfigPath != "" {
args = append([]string{fmt.Sprintf("--kubeconfig=%s", kubeConfigPath)}, args...)
}
if openshift {
cmd = exec.Command("oc", args...)
} else if kube {
cmd = exec.Command("kubectl", args...)
}
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
err := cmd.Run()
if err != nil {
return err
}
//time.Sleep(1 * time.Second) TODO why did Jay put this here???
return nil
}
// KubectlApplyRuntimeObjects creates runtime objects by converting them to bytes
// and passing them through the kubectl command
func KubectlApplyRuntimeObjects(objects map[string]runtime.Object) error {
var content []byte
for _, obj := range objects {
secretBytes, err := json.Marshal(obj)
if err != nil {
return err
}
content = append(content, secretBytes...)
}
out, err := RunKubeCmdWithStdin(restconfig, kubeClient, string(content), "apply", "--validate=false", "-f", "-")
if err != nil {
return fmt.Errorf("failed to deploy Runtime Object: %+v : %+v", out, err)
}
return nil
}
// KubectlDeleteRuntimeObjects deletes runtime objects by converting them to bytes
// and passing them through the kubectl command
func KubectlDeleteRuntimeObjects(objects map[string]runtime.Object) error {
var content []byte
for _, obj := range objects {
secretBytes, err := json.Marshal(obj)
if err != nil {
return err
}
content = append(content, secretBytes...)
}
out, err := RunKubeCmdWithStdin(restconfig, kubeClient, string(content), "delete", "-f", "-")
if err != nil {
return fmt.Errorf("failed to delete Runtime Object: %+v : %+v", out, err)
}
return nil
}
// getInstanceInfo provides the app and crd namespaces as well as the crd scope of the request custom resource instance
func getInstanceInfo(crdName string, appName string, namespace string, name string) (string, string, apiextensions.ResourceScope, error) {
crdScope := apiextensions.ClusterScoped
crd, err := util.GetCustomResourceDefinition(apiExtensionClient, crdName)
if err != nil {
return "", "", "", fmt.Errorf("unable to get Custom Resource Definition '%s' in your cluster due to %+v", crdName, err)
}
crdScope = crd.Spec.Scope
// if the CRD scope is namespaced scope, then the user need to provide the namespace
if crdScope != apiextensions.ClusterScoped && len(namespace) == 0 {
return "", "", crdScope, fmt.Errorf("namespace needs to be provided. please use the 'namespace' option to set it")
}
crdNamespace := namespace
if crdScope == apiextensions.ClusterScoped {
crdNamespace = ""
if len(namespace) == 0 {
namespace = name
// update scenario to find out the namespace in case of cluster scope
if len(appName) > 0 {
ns, err := util.ListNamespaces(kubeClient, fmt.Sprintf("synopsys.com/%s.%s", appName, name))
if err != nil {
return "", "", crdScope, fmt.Errorf("unable to list the '%s' instance '%s' in namespace '%s' due to %+v", appName, name, namespace, err)
}
if len(ns.Items) > 0 {
namespace = ns.Items[0].Name
} else {
return "", "", crdScope, fmt.Errorf("unable to find the namespace of the '%s' instance '%s'", appName, name)
}
}
}
}
return namespace, crdNamespace, crdScope, nil
}
// SetHelmChartLocation uses --app-resources-path and chartVersion to set the value at *chartVariable
func SetHelmChartLocation(flags *pflag.FlagSet, chartName, appVersion string, chartVariable *string) error {
chartLocationFlag := flags.Lookup("app-resources-path")
if chartLocationFlag.Changed {
*chartVariable = chartLocationFlag.Value.String()
} else {
if len(appVersion) > 0 {
chartURL, err := util.GetLatestChartURLForAppVersion(globals.IndexChartURLs, chartName, appVersion)
if err != nil {
return fmt.Errorf("failed to get resources version for '%s': %+v", chartName, err)
}
*chartVariable = chartURL
}
}
return nil
}
func cleanAlertHelmError(errString, releaseName, alertName string) string {
helmName := fmt.Sprintf("release '%s'", releaseName)
instanceName := fmt.Sprintf("instance '%s'", alertName)
cleanErrorMsg := strings.Replace(errString, helmName, instanceName, 1)
return cleanErrorMsg
}