Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
gitpod-io
GitHub Repository: gitpod-io/gitpod
Path: blob/main/install/installer/cmd/validate_cluster.go
2498 views
1
// Copyright (c) 2021 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
"fmt"
10
"os"
11
"path/filepath"
12
13
"github.com/gitpod-io/gitpod/common-go/log"
14
"github.com/gitpod-io/gitpod/installer/pkg/cluster"
15
"github.com/gitpod-io/gitpod/installer/pkg/common"
16
"github.com/gitpod-io/gitpod/installer/pkg/config"
17
"github.com/spf13/cobra"
18
"k8s.io/client-go/rest"
19
"k8s.io/client-go/tools/clientcmd"
20
)
21
22
var validateClusterOpts struct {
23
Kube kubeConfig
24
Namespace string
25
Config string
26
}
27
28
// validateClusterCmd represents the cluster command
29
var validateClusterCmd = &cobra.Command{
30
Use: "cluster",
31
Short: "Validate the cluster setup",
32
RunE: func(cmd *cobra.Command, args []string) error {
33
if err := checkKubeConfig(&validateClusterOpts.Kube); err != nil {
34
return err
35
}
36
37
clientcfg := clientcmd.NewNonInteractiveDeferredLoadingClientConfig(
38
&clientcmd.ClientConfigLoadingRules{ExplicitPath: validateClusterOpts.Kube.Config},
39
&clientcmd.ConfigOverrides{},
40
)
41
res, err := clientcfg.ClientConfig()
42
if err != nil {
43
return err
44
}
45
46
result, err := cluster.ClusterChecks.Validate(context.Background(), res, validateClusterOpts.Namespace)
47
if err != nil {
48
return err
49
}
50
51
if validateClusterOpts.Config != "" {
52
res, err := runClusterConfigValidation(context.Background(), res, validateClusterOpts.Namespace)
53
if err != nil {
54
return err
55
}
56
57
// Update the status
58
switch res.Status {
59
case cluster.ValidationStatusError:
60
// Always change the status if error
61
result.Status = cluster.ValidationStatusError
62
case cluster.ValidationStatusWarning:
63
// Only put to warning if status is ok
64
if result.Status == cluster.ValidationStatusOk {
65
result.Status = cluster.ValidationStatusWarning
66
}
67
}
68
69
result.Items = append(result.Items, res.Items...)
70
}
71
72
jsonOut, err := common.ToJSONString(result)
73
if err != nil {
74
return err
75
}
76
out := fmt.Sprintf("%s\n", string(jsonOut))
77
78
if result.Status == cluster.ValidationStatusError {
79
// Warnings are treated as valid
80
_, err := fmt.Fprintln(os.Stderr, out)
81
if err != nil {
82
return err
83
}
84
os.Exit(1)
85
}
86
87
fmt.Print(out)
88
return nil
89
},
90
}
91
92
func runClusterConfigValidation(ctx context.Context, restConfig *rest.Config, namespace string) (*cluster.ValidationResult, error) {
93
_, version, cfg, err := loadConfig(validateClusterOpts.Config)
94
if err != nil {
95
return nil, err
96
}
97
apiVersion, err := config.LoadConfigVersion(version)
98
if err != nil {
99
return nil, err
100
}
101
return apiVersion.ClusterValidation(cfg).Validate(ctx, restConfig, namespace)
102
}
103
104
func init() {
105
validateCmd.AddCommand(validateClusterCmd)
106
107
dir, err := os.Getwd()
108
if err != nil {
109
log.WithError(err).Fatal("Failed to get working directory")
110
}
111
112
validateClusterCmd.PersistentFlags().StringVar(&validateClusterOpts.Kube.Config, "kubeconfig", "", "path to the kubeconfig file")
113
validateClusterCmd.PersistentFlags().StringVarP(&validateClusterOpts.Config, "config", "c", getEnvvar("GITPOD_INSTALLER_CONFIG", filepath.Join(dir, "gitpod.config.yaml")), "path to the config file")
114
validateClusterCmd.PersistentFlags().StringVarP(&validateClusterOpts.Namespace, "namespace", "n", getEnvvar("NAMESPACE", "default"), "namespace to deploy to")
115
}
116
117