Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
gitpod-io
GitHub Repository: gitpod-io/gitpod
Path: blob/main/dev/blowtorch/cmd/root.go
2497 views
1
// Copyright (c) 2020 Gitpod GmbH. All rights reserved.
2
// Licensed under the GNU Affero General Public License (AGPL).
3
// See License.AGPL.txt in the project root for license information.
4
5
package cmd
6
7
import (
8
"fmt"
9
"os"
10
"path/filepath"
11
12
log "github.com/sirupsen/logrus"
13
"github.com/spf13/cobra"
14
"k8s.io/client-go/rest"
15
"k8s.io/client-go/tools/clientcmd"
16
)
17
18
// rootCmd represents the base command when called without any subcommands
19
var rootCmd = &cobra.Command{
20
Use: "blowtorch",
21
Short: "blowtorch helps using toxiproxy to create network chaos in your k8s application",
22
}
23
24
// Execute adds all child commands to the root command and sets flags appropriately.
25
// This is called by main.main(). It only needs to happen once to the rootCmd.
26
func Execute() {
27
if err := rootCmd.Execute(); err != nil {
28
fmt.Println(err)
29
os.Exit(1)
30
}
31
}
32
33
func init() {
34
hd, err := os.UserHomeDir()
35
if err != nil {
36
log.WithError(err).Warn("cannot determine user home dir")
37
}
38
rootCmd.PersistentFlags().String("kubeconfig", filepath.Join(hd, ".kube", "config"), "path to the kubeconfig file (defaults to $HOME/.kube/config)")
39
40
rootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
41
}
42
43
func getKubeconfig() (res *rest.Config, namespace string, err error) {
44
kubeconfig, err := rootCmd.PersistentFlags().GetString("kubeconfig")
45
if err != nil {
46
return nil, "", err
47
}
48
49
if kubeconfig == "$HOME/.kube/config" {
50
home, err := os.UserHomeDir()
51
if err != nil {
52
return nil, "", err
53
}
54
kubeconfig = filepath.Join(home, ".kube", "config")
55
}
56
57
cfg := clientcmd.NewNonInteractiveDeferredLoadingClientConfig(
58
&clientcmd.ClientConfigLoadingRules{ExplicitPath: kubeconfig},
59
&clientcmd.ConfigOverrides{},
60
)
61
namespace, _, err = cfg.Namespace()
62
if err != nil {
63
return nil, "", err
64
}
65
66
res, err = clientcmd.BuildConfigFromFlags("", kubeconfig)
67
if err != nil {
68
return nil, namespace, err
69
}
70
71
return res, namespace, nil
72
}
73
74