From 890b5c5202e3f7a97e5bf516b1b14877fc17a26f Mon Sep 17 00:00:00 2001 From: Hidde Beydals Date: Mon, 15 Feb 2021 16:34:55 +0100 Subject: [PATCH] Use label selector to check components Signed-off-by: Hidde Beydals --- cmd/flux/check.go | 36 ++++++++++++++++++++++-------------- 1 file changed, 22 insertions(+), 14 deletions(-) diff --git a/cmd/flux/check.go b/cmd/flux/check.go index 3404c396..fad6a6cb 100644 --- a/cmd/flux/check.go +++ b/cmd/flux/check.go @@ -21,14 +21,16 @@ import ( "encoding/json" "os" "os/exec" - "strings" "time" "github.com/blang/semver/v4" - "github.com/fluxcd/flux2/internal/utils" "github.com/spf13/cobra" + v1 "k8s.io/api/apps/v1" apimachineryversion "k8s.io/apimachinery/pkg/version" "k8s.io/client-go/kubernetes" + "sigs.k8s.io/controller-runtime/pkg/client" + + "github.com/fluxcd/flux2/internal/utils" ) var checkCmd = &cobra.Command{ @@ -176,23 +178,29 @@ func componentsCheck() bool { ctx, cancel := context.WithTimeout(context.Background(), rootArgs.timeout) defer cancel() - statusChecker, err := NewStatusChecker(time.Second, 30*time.Second) + kubeClient, err := utils.KubeClient(rootArgs.kubeconfig, rootArgs.kubecontext) if err != nil { return false } - ok := true - deployments := append(checkArgs.components, checkArgs.extraComponents...) - for _, deployment := range deployments { - if err := statusChecker.Assess(deployment); err != nil { - ok = false - } else { - logger.Successf("%s: healthy", deployment) - } + statusChecker, err := NewStatusChecker(time.Second, rootArgs.timeout) + if err != nil { + return false + } - kubectlArgs := []string{"-n", rootArgs.namespace, "get", "deployment", deployment, "-o", "jsonpath=\"{..image}\""} - if output, err := utils.ExecKubectlCommand(ctx, utils.ModeCapture, rootArgs.kubeconfig, rootArgs.kubecontext, kubectlArgs...); err == nil { - logger.Actionf(strings.TrimPrefix(strings.TrimSuffix(output, "\""), "\"")) + ok := true + selector := client.MatchingLabels{"app.kubernetes.io/instance": rootArgs.namespace} + var list v1.DeploymentList + if err := kubeClient.List(ctx, &list, client.InNamespace(rootArgs.namespace), selector); err == nil { + for _, d := range list.Items { + if err := statusChecker.Assess(d.Name); err != nil { + ok = false + } else { + logger.Successf("%s: healthy", d.Name) + } + for _, c := range d.Spec.Template.Spec.Containers { + logger.Actionf(c.Image) + } } } return ok