mirror of https://github.com/fluxcd/flux2.git
You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
186 lines
4.6 KiB
Go
186 lines
4.6 KiB
Go
5 years ago
|
/*
|
||
4 years ago
|
Copyright 2020 The Flux authors
|
||
5 years ago
|
|
||
|
Licensed under the Apache License, Version 2.0 (the "License");
|
||
|
you may not use this file except in compliance with the License.
|
||
|
You may obtain a copy of the License at
|
||
|
|
||
|
http://www.apache.org/licenses/LICENSE-2.0
|
||
|
|
||
|
Unless required by applicable law or agreed to in writing, software
|
||
|
distributed under the License is distributed on an "AS IS" BASIS,
|
||
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||
|
See the License for the specific language governing permissions and
|
||
|
limitations under the License.
|
||
|
*/
|
||
|
|
||
5 years ago
|
package main
|
||
|
|
||
|
import (
|
||
5 years ago
|
"context"
|
||
4 years ago
|
"encoding/json"
|
||
5 years ago
|
"os"
|
||
|
"os/exec"
|
||
5 years ago
|
"strings"
|
||
5 years ago
|
|
||
4 years ago
|
"github.com/blang/semver/v4"
|
||
4 years ago
|
"github.com/fluxcd/toolkit/internal/utils"
|
||
5 years ago
|
"github.com/spf13/cobra"
|
||
4 years ago
|
apimachineryversion "k8s.io/apimachinery/pkg/version"
|
||
5 years ago
|
"k8s.io/client-go/kubernetes"
|
||
|
"k8s.io/client-go/tools/clientcmd"
|
||
5 years ago
|
)
|
||
|
|
||
|
var checkCmd = &cobra.Command{
|
||
5 years ago
|
Use: "check",
|
||
5 years ago
|
Short: "Check requirements and installation",
|
||
5 years ago
|
Long: `The check command will perform a series of checks to validate that
|
||
5 years ago
|
the local environment is configured correctly and if the installed components are healthy.`,
|
||
|
Example: ` # Run pre-installation checks
|
||
4 years ago
|
flux check --pre
|
||
5 years ago
|
|
||
|
# Run installation checks
|
||
4 years ago
|
flux check
|
||
5 years ago
|
`,
|
||
|
RunE: runCheckCmd,
|
||
5 years ago
|
}
|
||
|
|
||
|
var (
|
||
5 years ago
|
checkPre bool
|
||
|
checkComponents []string
|
||
5 years ago
|
)
|
||
|
|
||
4 years ago
|
type kubectlVersion struct {
|
||
|
ClientVersion *apimachineryversion.Info `json:"clientVersion"`
|
||
|
}
|
||
|
|
||
5 years ago
|
func init() {
|
||
|
checkCmd.Flags().BoolVarP(&checkPre, "pre", "", false,
|
||
|
"only run pre-installation checks")
|
||
4 years ago
|
checkCmd.Flags().StringSliceVar(&checkComponents, "components", defaults.Components,
|
||
5 years ago
|
"list of components, accepts comma-separated values")
|
||
5 years ago
|
rootCmd.AddCommand(checkCmd)
|
||
|
}
|
||
|
|
||
|
func runCheckCmd(cmd *cobra.Command, args []string) error {
|
||
5 years ago
|
ctx, cancel := context.WithTimeout(context.Background(), timeout)
|
||
|
defer cancel()
|
||
|
|
||
5 years ago
|
logger.Actionf("checking prerequisites")
|
||
5 years ago
|
checkFailed := false
|
||
5 years ago
|
|
||
5 years ago
|
if !kubectlCheck(ctx, ">=1.18.0") {
|
||
5 years ago
|
checkFailed = true
|
||
5 years ago
|
}
|
||
5 years ago
|
|
||
5 years ago
|
if !kubernetesCheck(">=1.16.0") {
|
||
5 years ago
|
checkFailed = true
|
||
|
}
|
||
|
|
||
5 years ago
|
if checkPre {
|
||
5 years ago
|
if checkFailed {
|
||
|
os.Exit(1)
|
||
|
}
|
||
5 years ago
|
logger.Successf("prerequisites checks passed")
|
||
5 years ago
|
return nil
|
||
|
}
|
||
|
|
||
5 years ago
|
logger.Actionf("checking controllers")
|
||
5 years ago
|
if !componentsCheck() {
|
||
5 years ago
|
checkFailed = true
|
||
5 years ago
|
}
|
||
5 years ago
|
if checkFailed {
|
||
|
os.Exit(1)
|
||
|
}
|
||
5 years ago
|
logger.Successf("all checks passed")
|
||
5 years ago
|
return nil
|
||
|
}
|
||
|
|
||
5 years ago
|
func kubectlCheck(ctx context.Context, version string) bool {
|
||
5 years ago
|
_, err := exec.LookPath("kubectl")
|
||
|
if err != nil {
|
||
5 years ago
|
logger.Failuref("kubectl not found")
|
||
5 years ago
|
return false
|
||
|
}
|
||
|
|
||
4 years ago
|
kubectlArgs := []string{"version", "--client", "--output", "json"}
|
||
4 years ago
|
output, err := utils.ExecKubectlCommand(ctx, utils.ModeCapture, kubectlArgs...)
|
||
5 years ago
|
if err != nil {
|
||
5 years ago
|
logger.Failuref("kubectl version can't be determined")
|
||
5 years ago
|
return false
|
||
|
}
|
||
|
|
||
4 years ago
|
kv := &kubectlVersion{}
|
||
|
if err = json.Unmarshal([]byte(output), kv); err != nil {
|
||
|
logger.Failuref("kubectl version output can't be unmarshaled")
|
||
|
return false
|
||
|
}
|
||
|
|
||
|
v, err := semver.ParseTolerant(kv.ClientVersion.GitVersion)
|
||
5 years ago
|
if err != nil {
|
||
5 years ago
|
logger.Failuref("kubectl version can't be parsed")
|
||
5 years ago
|
return false
|
||
|
}
|
||
|
|
||
|
rng, _ := semver.ParseRange(version)
|
||
|
if !rng(v) {
|
||
5 years ago
|
logger.Failuref("kubectl version must be %s", version)
|
||
5 years ago
|
return false
|
||
|
}
|
||
|
|
||
5 years ago
|
logger.Successf("kubectl %s %s", v.String(), version)
|
||
5 years ago
|
return true
|
||
|
}
|
||
|
|
||
|
func kubernetesCheck(version string) bool {
|
||
5 years ago
|
cfg, err := clientcmd.BuildConfigFromFlags("", kubeconfig)
|
||
|
if err != nil {
|
||
5 years ago
|
logger.Failuref("Kubernetes client initialization failed: %s", err.Error())
|
||
5 years ago
|
return false
|
||
|
}
|
||
|
|
||
|
client, err := kubernetes.NewForConfig(cfg)
|
||
5 years ago
|
if err != nil {
|
||
5 years ago
|
logger.Failuref("Kubernetes client initialization failed: %s", err.Error())
|
||
5 years ago
|
return false
|
||
|
}
|
||
|
|
||
|
ver, err := client.Discovery().ServerVersion()
|
||
|
if err != nil {
|
||
5 years ago
|
logger.Failuref("Kubernetes API call failed: %s", err.Error())
|
||
5 years ago
|
return false
|
||
|
}
|
||
|
|
||
5 years ago
|
v, err := semver.ParseTolerant(ver.String())
|
||
|
if err != nil {
|
||
5 years ago
|
logger.Failuref("Kubernetes version can't be determined")
|
||
5 years ago
|
return false
|
||
|
}
|
||
|
|
||
|
rng, _ := semver.ParseRange(version)
|
||
|
if !rng(v) {
|
||
5 years ago
|
logger.Failuref("Kubernetes version must be %s", version)
|
||
5 years ago
|
return false
|
||
|
}
|
||
|
|
||
5 years ago
|
logger.Successf("Kubernetes %s %s", v.String(), version)
|
||
5 years ago
|
return true
|
||
|
}
|
||
5 years ago
|
|
||
|
func componentsCheck() bool {
|
||
|
ctx, cancel := context.WithTimeout(context.Background(), timeout)
|
||
|
defer cancel()
|
||
|
|
||
5 years ago
|
ok := true
|
||
5 years ago
|
for _, deployment := range checkComponents {
|
||
4 years ago
|
kubectlArgs := []string{"-n", namespace, "rollout", "status", "deployment", deployment, "--timeout", timeout.String()}
|
||
4 years ago
|
if output, err := utils.ExecKubectlCommand(ctx, utils.ModeCapture, kubectlArgs...); err != nil {
|
||
5 years ago
|
logger.Failuref("%s: %s", deployment, strings.TrimSuffix(output, "\n"))
|
||
5 years ago
|
ok = false
|
||
5 years ago
|
} else {
|
||
5 years ago
|
logger.Successf("%s is healthy", deployment)
|
||
5 years ago
|
}
|
||
|
}
|
||
5 years ago
|
return ok
|
||
5 years ago
|
}
|