Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[installer]: add validate cluster command to the installer #6234

Merged
merged 1 commit into from
Oct 18, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions installer/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ package cmd

import (
"github.com/spf13/cobra"
"os"
"os/user"
"path/filepath"
)

// rootCmd represents the base command when called without any subcommands
Expand All @@ -21,3 +24,23 @@ func Execute() {
func init() {
rootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}

type kubeConfig struct {
Config string
}

// checkKubeConfig performs validation on the Kubernetes struct and fills in default values if necessary
func checkKubeConfig(kube *kubeConfig) error {
if kube.Config == "" {
kube.Config = os.Getenv("KUBECONFIG")
}
if kube.Config == "" {
u, err := user.Current()
if err != nil {
return err
}
kube.Config = filepath.Join(u.HomeDir, ".kube", "config")
}

return nil
}
20 changes: 20 additions & 0 deletions installer/cmd/validate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Copyright (c) 2021 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 (
"github.com/spf13/cobra"
)

// validateCmd represents the validate command
var validateCmd = &cobra.Command{
Use: "validate",
Short: "Performs validation tasks",
Aliases: []string{"verify"},
}

func init() {
rootCmd.AddCommand(validateCmd)
}
65 changes: 65 additions & 0 deletions installer/cmd/validate_cluster.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// Copyright (c) 2021 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 (
"encoding/json"
"fmt"
"github.com/gitpod-io/gitpod/installer/pkg/cluster"
"github.com/spf13/cobra"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/clientcmd"
)

var validateClusterOpts struct {
Kube kubeConfig
}

// validateClusterCmd represents the cluster command
var validateClusterCmd = &cobra.Command{
Use: "cluster",
Short: "Validate the cluster configuration",
RunE: func(cmd *cobra.Command, args []string) error {
if err := checkKubeConfig(&validateClusterOpts.Kube); err != nil {
return err
}

res, err := clientcmd.BuildConfigFromFlags("", validateClusterOpts.Kube.Config)
if err != nil {
return err
}

clientset, err := kubernetes.NewForConfig(res)
if err != nil {
return err
}

result, err := cluster.Validate(clientset, res)
if err != nil {
return err
}

jsonOut, err := json.MarshalIndent(result, "", " ")
if err != nil {
return err
}

out := fmt.Sprintf("%s\n", string(jsonOut))

if result.Status == cluster.ValidationStatusError {
// Warnings are treated as valid
return fmt.Errorf(out)
}

fmt.Printf(out)
return nil
},
}

func init() {
validateCmd.AddCommand(validateClusterCmd)

validateClusterCmd.PersistentFlags().StringVar(&validateClusterOpts.Kube.Config, "kubeconfig", "", "Path to the kubeconfig file")
}
16 changes: 9 additions & 7 deletions installer/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module github.com/gitpod-io/gitpod/installer
go 1.17

require (
github.com/Masterminds/semver v1.5.0
github.com/docker/distribution v2.7.1+incompatible
github.com/gitpod-io/gitpod/agent-smith v0.0.0-00010101000000-000000000000
github.com/gitpod-io/gitpod/blobserve v0.0.0-00010101000000-000000000000
Expand All @@ -24,6 +25,7 @@ require (
helm.sh/helm/v3 v3.6.1-0.20210915004119-9fafb4ad6811
k8s.io/api v0.22.2
k8s.io/apimachinery v0.22.2
k8s.io/client-go v0.22.2
k8s.io/kubectl v0.22.2
k8s.io/utils v0.0.0-20210820185131-d34e5cb4466e
sigs.k8s.io/yaml v1.2.0
Expand All @@ -33,6 +35,12 @@ require (
cloud.google.com/go v0.83.0 // indirect
cloud.google.com/go/storage v1.15.0 // indirect
github.com/Azure/go-ansiterm v0.0.0-20210617225240-d185dfc1b5a1 // indirect
github.com/Azure/go-autorest v14.2.0+incompatible // indirect
github.com/Azure/go-autorest/autorest v0.11.18 // indirect
github.com/Azure/go-autorest/autorest/adal v0.9.13 // indirect
github.com/Azure/go-autorest/autorest/date v0.3.0 // indirect
github.com/Azure/go-autorest/logger v0.2.1 // indirect
github.com/Azure/go-autorest/tracing v0.6.0 // indirect
github.com/BurntSushi/toml v0.3.1 // indirect
github.com/MakeNowJust/heredoc v0.0.0-20170808103936-bb23615498cd // indirect
github.com/Masterminds/goutils v1.1.1 // indirect
Expand All @@ -50,7 +58,6 @@ require (
github.com/cenkalti/backoff v2.2.1+incompatible // indirect
github.com/cespare/xxhash/v2 v2.1.1 // indirect
github.com/chai2010/gettext-go v0.0.0-20160711120539-c6fed771bfd5 // indirect
github.com/cilium/ebpf v0.6.2 // indirect
github.com/containerd/cgroups v1.0.1 // indirect
github.com/containerd/containerd v1.5.5 // indirect
github.com/containerd/continuity v0.1.0 // indirect
Expand All @@ -75,10 +82,9 @@ require (
github.com/fatih/gomodifytags v1.14.0 // indirect
github.com/fatih/structtag v1.2.0 // indirect
github.com/felixge/httpsnoop v1.0.1 // indirect
github.com/fsnotify/fsnotify v1.4.9 // indirect
github.com/form3tech-oss/jwt-go v3.2.3+incompatible // indirect
github.com/fvbommel/sortorder v1.0.1 // indirect
github.com/gitpod-io/gitpod/content-service v0.0.0-00010101000000-000000000000 // indirect
github.com/gitpod-io/gitpod/gitpod-protocol v0.0.0-00010101000000-000000000000 // indirect
github.com/gitpod-io/gitpod/registry-facade v0.0.0-00010101000000-000000000000 // indirect
github.com/go-errors/errors v1.0.1 // indirect
github.com/go-logr/logr v0.4.0 // indirect
Expand All @@ -92,7 +98,6 @@ require (
github.com/gogo/googleapis v1.4.0 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/golang/mock v1.6.0 // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/google/btree v1.0.1 // indirect
github.com/google/gofuzz v1.2.0 // indirect
Expand All @@ -102,7 +107,6 @@ require (
github.com/googleapis/gnostic v0.5.5 // indirect
github.com/gorilla/handlers v1.5.1 // indirect
github.com/gorilla/mux v1.8.0 // indirect
github.com/gorilla/websocket v1.4.2 // indirect
github.com/gosuri/uitable v0.0.4 // indirect
github.com/gregjones/httpcache v0.0.0-20180305231024-9cad4c3443a7 // indirect
github.com/grpc-ecosystem/go-grpc-middleware v1.3.0 // indirect
Expand Down Expand Up @@ -162,7 +166,6 @@ require (
github.com/shirou/gopsutil v2.20.9+incompatible // indirect
github.com/shopspring/decimal v1.2.0 // indirect
github.com/sirupsen/logrus v1.8.1 // indirect
github.com/sourcegraph/jsonrpc2 v0.0.0-20200429184054-15c2290dcb37 // indirect
github.com/spf13/cast v1.3.1 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/stretchr/testify v1.7.0 // indirect
Expand Down Expand Up @@ -197,7 +200,6 @@ require (
k8s.io/apiextensions-apiserver v0.22.2 // indirect
k8s.io/apiserver v0.22.2 // indirect
k8s.io/cli-runtime v0.22.2 // indirect
k8s.io/client-go v0.22.2 // indirect
k8s.io/component-base v0.22.2 // indirect
k8s.io/component-helpers v0.22.2 // indirect
k8s.io/klog/v2 v2.9.0 // indirect
Expand Down
Loading