-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy pathconfig_cluster.go
94 lines (78 loc) · 2.73 KB
/
config_cluster.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
// Copyright (c) 2022 Gitpod GmbH. All rights reserved.
/// Licensed under the GNU Affero General Public License (AGPL).
// See License.AGPL.txt in the project root for license information.
package cmd
import (
"context"
"path/filepath"
"github.com/gitpod-io/gitpod/common-go/log"
"github.com/spf13/cobra"
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
"k8s.io/client-go/util/homedir"
)
var configClusterOpts struct {
Kube kubeConfig
Namespace string
EnsureNamespace bool
}
// configClusterCmd represents the validate command
var configClusterCmd = &cobra.Command{
Use: "cluster",
Short: "Perform configuration tasks against the cluster",
Long: `Perform configuration tasks against the cluster
These will be deployed and run as Kubernetes resources.`,
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
log.Debugf("EnsureNamespace: %t", configClusterOpts.EnsureNamespace)
if !configClusterOpts.EnsureNamespace {
return nil
}
log.Infof("Ensuring namespace exists %s", configClusterOpts.Namespace)
_, clientset, err := authClusterOrKubeconfig(configClusterOpts.Kube.Config)
if err != nil {
return err
}
_, err = clientset.CoreV1().Namespaces().Create(
context.TODO(),
&v1.Namespace{
ObjectMeta: metav1.ObjectMeta{
Name: configClusterOpts.Namespace,
},
},
metav1.CreateOptions{},
)
if errors.IsAlreadyExists(err) {
log.Debug("Namespace already exists")
return nil
}
return err
},
}
func init() {
configCmd.AddCommand(configClusterCmd)
configClusterCmd.Flags().StringVar(&configClusterOpts.Kube.Config, "kubeconfig", filepath.Join(homedir.HomeDir(), ".kube", "config"), "path to the kubeconfig file")
configClusterCmd.PersistentFlags().StringVarP(&configClusterOpts.Namespace, "namespace", "n", getEnvvar("NAMESPACE", "default"), "namespace to deploy to")
configClusterCmd.PersistentFlags().BoolVar(&configClusterOpts.EnsureNamespace, "ensure-namespace", true, "ensure that the namespace exists")
}
func authClusterOrKubeconfig(kubeconfig string) (*rest.Config, *kubernetes.Clientset, error) {
// Try authenticating in-cluster with serviceaccount
log.Debug("Attempting to authenticate with ServiceAccount")
config, err := rest.InClusterConfig()
if err != nil {
// Try authenticating out-of-cluster with kubeconfig
log.Debug("ServiceAccount failed - using KubeConfig")
config, err = clientcmd.BuildConfigFromFlags("", kubeconfig)
if err != nil {
return nil, nil, err
}
}
clientset, err := kubernetes.NewForConfig(config)
if err != nil {
return nil, nil, err
}
return config, clientset, nil
}