Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
gitpod-io
GitHub Repository: gitpod-io/gitpod
Path: blob/main/install/installer/cmd/config_cluster.go
2498 views
1
// Copyright (c) 2022 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
"context"
9
"path/filepath"
10
11
"github.com/gitpod-io/gitpod/common-go/log"
12
"github.com/spf13/cobra"
13
v1 "k8s.io/api/core/v1"
14
"k8s.io/apimachinery/pkg/api/errors"
15
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
16
"k8s.io/client-go/kubernetes"
17
"k8s.io/client-go/rest"
18
"k8s.io/client-go/tools/clientcmd"
19
"k8s.io/client-go/util/homedir"
20
)
21
22
var configClusterOpts struct {
23
Kube kubeConfig
24
Namespace string
25
EnsureNamespace bool
26
}
27
28
// configClusterCmd represents the validate command
29
var configClusterCmd = &cobra.Command{
30
Use: "cluster",
31
Short: "Perform configuration tasks against the cluster",
32
Long: `Perform configuration tasks against the cluster
33
34
These will be deployed and run as Kubernetes resources.`,
35
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
36
log.Debugf("EnsureNamespace: %t", configClusterOpts.EnsureNamespace)
37
38
if !configClusterOpts.EnsureNamespace {
39
return nil
40
}
41
42
log.Infof("Ensuring namespace exists %s", configClusterOpts.Namespace)
43
_, clientset, err := authClusterOrKubeconfig(configClusterOpts.Kube.Config)
44
if err != nil {
45
return err
46
}
47
48
_, err = clientset.CoreV1().Namespaces().Create(
49
context.TODO(),
50
&v1.Namespace{
51
ObjectMeta: metav1.ObjectMeta{
52
Name: configClusterOpts.Namespace,
53
},
54
},
55
metav1.CreateOptions{},
56
)
57
58
if errors.IsAlreadyExists(err) {
59
log.Debug("Namespace already exists")
60
return nil
61
}
62
63
return err
64
},
65
}
66
67
func init() {
68
configCmd.AddCommand(configClusterCmd)
69
70
configClusterCmd.Flags().StringVar(&configClusterOpts.Kube.Config, "kubeconfig", filepath.Join(homedir.HomeDir(), ".kube", "config"), "path to the kubeconfig file")
71
configClusterCmd.PersistentFlags().StringVarP(&configClusterOpts.Namespace, "namespace", "n", getEnvvar("NAMESPACE", "default"), "namespace to deploy to")
72
configClusterCmd.PersistentFlags().BoolVar(&configClusterOpts.EnsureNamespace, "ensure-namespace", true, "ensure that the namespace exists")
73
}
74
75
func authClusterOrKubeconfig(kubeconfig string) (*rest.Config, *kubernetes.Clientset, error) {
76
// Try authenticating in-cluster with serviceaccount
77
log.Debug("Attempting to authenticate with ServiceAccount")
78
config, err := rest.InClusterConfig()
79
if err != nil {
80
// Try authenticating out-of-cluster with kubeconfig
81
log.Debug("ServiceAccount failed - using KubeConfig")
82
config, err = clientcmd.BuildConfigFromFlags("", kubeconfig)
83
if err != nil {
84
return nil, nil, err
85
}
86
}
87
88
clientset, err := kubernetes.NewForConfig(config)
89
if err != nil {
90
return nil, nil, err
91
}
92
93
return config, clientset, nil
94
}
95
96