-
Notifications
You must be signed in to change notification settings - Fork 64
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Validate: add support for kubernetes deployment validation with `src …
…validate kube` subcommand (#926) * Refactor out shared vars * Add basic kube validation * Add pod validation * Add skeleton for service and connection validation * Add connection validations * Add go modules files * Fix --kubeconfig flag * Fix line output * Add usage output to cli * Fix path in example * Add quiet flag to suppress output and return exit status only * Fix exit status to be consistent if there are failures * Remove TODOs * Update CHANGELOG.md
- Loading branch information
1 parent
270b736
commit 8779a73
Showing
11 changed files
with
1,014 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"flag" | ||
"fmt" | ||
"path/filepath" | ||
|
||
"k8s.io/client-go/kubernetes" | ||
"k8s.io/client-go/tools/clientcmd" | ||
"k8s.io/client-go/util/homedir" | ||
|
||
"github.com/sourcegraph/src-cli/internal/validate/kube" | ||
|
||
"github.com/sourcegraph/sourcegraph/lib/errors" | ||
) | ||
|
||
func init() { | ||
usage := `'src validate kube' is a tool that validates a Kubernetes based Sourcegraph deployment | ||
Examples: | ||
Run default deployment validation: | ||
$ src validate kube | ||
Specify Kubernetes namespace: | ||
$ src validate kube --namespace sourcegraph | ||
Specify the kubeconfig file location: | ||
$ src validate kube --kubeconfig ~/.kube/config | ||
Suppress output (useful for CI/CD pipelines) | ||
$ src validate kube --quiet | ||
` | ||
|
||
flagSet := flag.NewFlagSet("kube", flag.ExitOnError) | ||
usageFunc := func() { | ||
fmt.Fprintf(flag.CommandLine.Output(), "Usage of 'src validate %s':\n", flagSet.Name()) | ||
flagSet.PrintDefaults() | ||
fmt.Println(usage) | ||
} | ||
|
||
var ( | ||
kubeConfig *string | ||
namespace = flagSet.String("namespace", "", "(optional) specify the kubernetes namespace to use") | ||
quiet = flagSet.Bool("quiet", false, "(optional) suppress output and return exit status only") | ||
) | ||
|
||
if home := homedir.HomeDir(); home != "" { | ||
kubeConfig = flagSet.String("kubeconfig", filepath.Join(home, ".kube", "config"), "(optional) absolute path to the kubeconfig file") | ||
} else { | ||
kubeConfig = flagSet.String("kubeconfig", "", "absolute path to the kubeconfig file") | ||
} | ||
|
||
handler := func(args []string) error { | ||
if err := flagSet.Parse(args); err != nil { | ||
return err | ||
} | ||
|
||
// use the current context in kubeConfig | ||
config, err := clientcmd.BuildConfigFromFlags("", *kubeConfig) | ||
if err != nil { | ||
return errors.Wrap(err, "failed to load kubernetes config") | ||
} | ||
|
||
clientSet, err := kubernetes.NewForConfig(config) | ||
if err != nil { | ||
return errors.Wrap(err, "failed to create kubernetes client") | ||
} | ||
|
||
// parse through flag options | ||
var options []kube.Option | ||
|
||
if *namespace != "" { | ||
options = append(options, kube.WithNamespace(*namespace)) | ||
} | ||
|
||
if *quiet { | ||
options = append(options, kube.Quiet()) | ||
} | ||
|
||
return kube.Validate(context.Background(), clientSet, config, options...) | ||
} | ||
|
||
validateCommands = append(validateCommands, &command{ | ||
flagSet: flagSet, | ||
handler: handler, | ||
usageFunc: usageFunc, | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.