Path: blob/main/install/installer/cmd/validate_cluster.go
2498 views
// Copyright (c) 2021 Gitpod GmbH. All rights reserved.1// Licensed under the GNU Affero General Public License (AGPL).2// See License.AGPL.txt in the project root for license information.34package cmd56import (7"context"8"fmt"9"os"10"path/filepath"1112"github.com/gitpod-io/gitpod/common-go/log"13"github.com/gitpod-io/gitpod/installer/pkg/cluster"14"github.com/gitpod-io/gitpod/installer/pkg/common"15"github.com/gitpod-io/gitpod/installer/pkg/config"16"github.com/spf13/cobra"17"k8s.io/client-go/rest"18"k8s.io/client-go/tools/clientcmd"19)2021var validateClusterOpts struct {22Kube kubeConfig23Namespace string24Config string25}2627// validateClusterCmd represents the cluster command28var validateClusterCmd = &cobra.Command{29Use: "cluster",30Short: "Validate the cluster setup",31RunE: func(cmd *cobra.Command, args []string) error {32if err := checkKubeConfig(&validateClusterOpts.Kube); err != nil {33return err34}3536clientcfg := clientcmd.NewNonInteractiveDeferredLoadingClientConfig(37&clientcmd.ClientConfigLoadingRules{ExplicitPath: validateClusterOpts.Kube.Config},38&clientcmd.ConfigOverrides{},39)40res, err := clientcfg.ClientConfig()41if err != nil {42return err43}4445result, err := cluster.ClusterChecks.Validate(context.Background(), res, validateClusterOpts.Namespace)46if err != nil {47return err48}4950if validateClusterOpts.Config != "" {51res, err := runClusterConfigValidation(context.Background(), res, validateClusterOpts.Namespace)52if err != nil {53return err54}5556// Update the status57switch res.Status {58case cluster.ValidationStatusError:59// Always change the status if error60result.Status = cluster.ValidationStatusError61case cluster.ValidationStatusWarning:62// Only put to warning if status is ok63if result.Status == cluster.ValidationStatusOk {64result.Status = cluster.ValidationStatusWarning65}66}6768result.Items = append(result.Items, res.Items...)69}7071jsonOut, err := common.ToJSONString(result)72if err != nil {73return err74}75out := fmt.Sprintf("%s\n", string(jsonOut))7677if result.Status == cluster.ValidationStatusError {78// Warnings are treated as valid79_, err := fmt.Fprintln(os.Stderr, out)80if err != nil {81return err82}83os.Exit(1)84}8586fmt.Print(out)87return nil88},89}9091func runClusterConfigValidation(ctx context.Context, restConfig *rest.Config, namespace string) (*cluster.ValidationResult, error) {92_, version, cfg, err := loadConfig(validateClusterOpts.Config)93if err != nil {94return nil, err95}96apiVersion, err := config.LoadConfigVersion(version)97if err != nil {98return nil, err99}100return apiVersion.ClusterValidation(cfg).Validate(ctx, restConfig, namespace)101}102103func init() {104validateCmd.AddCommand(validateClusterCmd)105106dir, err := os.Getwd()107if err != nil {108log.WithError(err).Fatal("Failed to get working directory")109}110111validateClusterCmd.PersistentFlags().StringVar(&validateClusterOpts.Kube.Config, "kubeconfig", "", "path to the kubeconfig file")112validateClusterCmd.PersistentFlags().StringVarP(&validateClusterOpts.Config, "config", "c", getEnvvar("GITPOD_INSTALLER_CONFIG", filepath.Join(dir, "gitpod.config.yaml")), "path to the config file")113validateClusterCmd.PersistentFlags().StringVarP(&validateClusterOpts.Namespace, "namespace", "n", getEnvvar("NAMESPACE", "default"), "namespace to deploy to")114}115116117