Skip to content

fix: ensure flux check returns non-zero exit code on failure #5267

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

h3nryc0ding
Copy link
Contributor

Fixes #5220 by returning a non-zero exist code for failed checks.

for _, id := range identifiers {
rs := coll.ResourceStatuses[id]
switch rs.Status {
case status.CurrentStatus:
sc.logger.Successf("%s: %s ready", rs.Identifier.Name, strings.ToLower(rs.Identifier.GroupKind.Kind))
case status.NotFoundStatus:
sc.logger.Failuref("%s: %s not found", rs.Identifier.Name, strings.ToLower(rs.Identifier.GroupKind.Kind))
errMsg := fmt.Sprintf("%s: %s not found", rs.Identifier.Name, strings.ToLower(rs.Identifier.GroupKind.Kind))
sc.logger.Failuref(errMsg)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure what's gonna happen now that we both log and return the error, would we see the error printed twice? Can you please test that? Maybe we need to remove the log.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

StatusChecker.Assess is used in three places with inconsistent error handling:

  1. check.go

    flux2/cmd/flux/check.go

    Lines 215 to 217 in f0fecf7

    if err := statusChecker.Assess(ref...); err != nil {
    ok = false
    }

    • Does not use the error
  2. install.go

    flux2/cmd/flux/install.go

    Lines 279 to 281 in f0fecf7

    if err := statusChecker.Assess(componentRefs...); err != nil {
    return fmt.Errorf("install failed")
    }

    • Uses the error
  3. bootstrap_plain_git.go

    if err := checker.Assess(identifiers...); err != nil {
    return err
    }

    • Uses the error

This means either:

  • At least one of the locations logs the error twice, or
  • check.go is missing necessary logging or needs refactoring

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah maybe we need some refactoring here... We're busy preparing for KubeCon EU next week, can you please ping us again after KubeCon? Thanks 🙏

@h3nryc0ding
Copy link
Contributor Author

Hey @matheuscscp 👋

I've done some refactoring to separate business and presentation logic. Let me know what you think.

Some examples logs:

  1. Success complete
$ flux check
► Checking flux pre-requisites
✔ flux 0.0.0-dev.0 is a development build
► Checking kubernetes pre-requisites
✔ kubernetes 1.32.0>=1.30.0-0
► Checking flux installation status
✔ distribution: flux-v2.4.0
✔ bootstrapped: true
► Checking flux controllers
✔ ghcr.io/fluxcd/helm-controller:v1.1.0
✔ ghcr.io/fluxcd/kustomize-controller:v1.4.0
✔ ghcr.io/fluxcd/notification-controller:v1.4.0
✔ ghcr.io/fluxcd/source-controller:v1.4.1
► Checking flux crds
✔ alerts.notification.toolkit.fluxcd.io/v1beta3
✔ buckets.source.toolkit.fluxcd.io/v1
✔ gitrepositories.source.toolkit.fluxcd.io/v1
✔ helmcharts.source.toolkit.fluxcd.io/v1
✔ helmreleases.helm.toolkit.fluxcd.io/v2
✔ helmrepositories.source.toolkit.fluxcd.io/v1
✔ kustomizations.kustomize.toolkit.fluxcd.io/v1
✔ ocirepositories.source.toolkit.fluxcd.io/v1beta2
✔ providers.notification.toolkit.fluxcd.io/v1beta3
✔ receivers.notification.toolkit.fluxcd.io/v1
► All checks passed
  1. Success pre
$ flux check --pre
► Checking flux pre-requisites
✔ flux 0.0.0-dev.0 is a development build
► Checking kubernetes pre-requisites
✔ kubernetes 1.32.0>=1.30.0-0
► All pre-installation checks passed
  1. Failure complete
$ flux check
► Checking flux pre-requisites
✔ flux 0.0.0-dev.0 is a development build
► Checking kubernetes pre-requisites
✔ kubernetes 1.30.7+orb1>=1.30.0-0
► Checking flux installation status
✗ error getting cluster info: customresourcedefinitions.apiextensions.k8s.io "gitrepositories.source.toolkit.fluxcd.io" not found
► Checking flux controllers
✗ no controllers found in the 'flux-system' namespace with the label selector 'app.kubernetes.io/part-of=flux'
► Checking flux crds
✗ no crds found with the label selector 'app.kubernetes.io/part-of=flux'
✗ checks failed
  1. Failure pre
► Checking flux pre-requisites
✔ flux 0.0.0-dev.0 is a development build
► Checking kubernetes pre-requisites
✔ kubernetes 1.30.7+orb1>=1.30.0-0
► All pre-installation checks passed

Signed-off-by: h3nryc0ding <[email protected]>
@h3nryc0ding h3nryc0ding marked this pull request as ready for review April 12, 2025 16:38
@h3nryc0ding h3nryc0ding requested a review from matheuscscp April 12, 2025 16:42
@matheuscscp
Copy link
Member

Hi @h3nryc0ding, was the 4th output a mistake on copy-paste?

  1. Failure pre
► Checking flux pre-requisites
✔ flux 0.0.0-dev.0 is a development build
► Checking kubernetes pre-requisites
✔ kubernetes 1.30.7+orb1>=1.30.0-0
► All pre-installation checks passed

if !kubernetesCheck(cfg, kubernetesConstraints) {
checkFailed = true
if !runPreChecks(ctx, cfg) {
return errors.New("pre-installation checks failed")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If --pre was not passed, we should continue with the other checks and not exit here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

flux check always returns 0
3 participants