Skip to content

Commit

Permalink
use error to check if flux is installed
Browse files Browse the repository at this point in the history
Signed-off-by: Somtochi Onyekwere <[email protected]>
  • Loading branch information
somtochiama committed Oct 27, 2023
1 parent 7ce9d90 commit b84aa02
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 46 deletions.
19 changes: 13 additions & 6 deletions cmd/flux/bootstrap_bitbucket_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"github.com/fluxcd/pkg/git/gogit"
"github.com/manifoldco/promptui"
"github.com/spf13/cobra"
"k8s.io/apimachinery/pkg/api/errors"

"github.com/fluxcd/flux2/v2/internal/flags"
"github.com/fluxcd/flux2/v2/internal/utils"
Expand Down Expand Up @@ -126,17 +127,23 @@ func bootstrapBServerCmdRun(cmd *cobra.Command, args []string) error {
}

if !bootstrapArgs.force {
installed := true
info, err := getFluxClusterInfo(ctx, kubeClient)
if err != nil {
return fmt.Errorf("cluster info unavailable: %w", err)
if !errors.IsNotFound(err) {
return fmt.Errorf("cluster info unavailable: %w", err)
}
installed = false
}

err = confirmFluxInstallOverride(info)
if err != nil {
if err == promptui.ErrAbort {
return fmt.Errorf("bootstrap cancelled")
if installed {
err = confirmFluxInstallOverride(info)
if err != nil {
if err == promptui.ErrAbort {
return fmt.Errorf("bootstrap cancelled")
}
return err
}
return err
}
}

Expand Down
19 changes: 13 additions & 6 deletions cmd/flux/bootstrap_git.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"github.com/manifoldco/promptui"
"github.com/spf13/cobra"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"

"github.com/fluxcd/flux2/v2/internal/flags"
"github.com/fluxcd/flux2/v2/internal/utils"
Expand Down Expand Up @@ -147,17 +148,23 @@ func bootstrapGitCmdRun(cmd *cobra.Command, args []string) error {
}

if !bootstrapArgs.force {
installed := true
info, err := getFluxClusterInfo(ctx, kubeClient)
if err != nil {
return fmt.Errorf("cluster info unavailable: %w", err)
if !errors.IsNotFound(err) {
return fmt.Errorf("cluster info unavailable: %w", err)
}
installed = false
}

err = confirmFluxInstallOverride(info)
if err != nil {
if err == promptui.ErrAbort {
return fmt.Errorf("bootstrap cancelled")
if installed {
err = confirmFluxInstallOverride(info)
if err != nil {
if err == promptui.ErrAbort {
return fmt.Errorf("bootstrap cancelled")
}
return err
}
return err
}
}

Expand Down
20 changes: 14 additions & 6 deletions cmd/flux/bootstrap_github.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"github.com/fluxcd/pkg/git/gogit"
"github.com/manifoldco/promptui"
"github.com/spf13/cobra"
"k8s.io/apimachinery/pkg/api/errors"

"github.com/fluxcd/flux2/v2/internal/flags"
"github.com/fluxcd/flux2/v2/internal/utils"
Expand Down Expand Up @@ -130,16 +131,23 @@ func bootstrapGitHubCmdRun(cmd *cobra.Command, args []string) error {
}

if !bootstrapArgs.force {
installed := true
info, err := getFluxClusterInfo(ctx, kubeClient)
if err != nil {
return fmt.Errorf("cluster info unavailable: %w", err)
if !errors.IsNotFound(err) {
return fmt.Errorf("cluster info unavailable: %w", err)
}
installed = false
}
err = confirmFluxInstallOverride(info)
if err != nil {
if err == promptui.ErrAbort {
return fmt.Errorf("bootstrap cancelled")

if installed {
err = confirmFluxInstallOverride(info)
if err != nil {
if err == promptui.ErrAbort {
return fmt.Errorf("bootstrap cancelled")
}
return err
}
return err
}
}

Expand Down
19 changes: 13 additions & 6 deletions cmd/flux/bootstrap_gitlab.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
"github.com/fluxcd/pkg/git/gogit"
"github.com/manifoldco/promptui"
"github.com/spf13/cobra"
"k8s.io/apimachinery/pkg/api/errors"

"github.com/fluxcd/flux2/v2/internal/flags"
"github.com/fluxcd/flux2/v2/internal/utils"
Expand Down Expand Up @@ -147,17 +148,23 @@ func bootstrapGitLabCmdRun(cmd *cobra.Command, args []string) error {
}

if !bootstrapArgs.force {
installed := true
info, err := getFluxClusterInfo(ctx, kubeClient)
if err != nil {
return fmt.Errorf("cluster info unavailable: %w", err)
if !errors.IsNotFound(err) {
return fmt.Errorf("cluster info unavailable: %w", err)
}
installed = false
}

err = confirmFluxInstallOverride(info)
if err != nil {
if err == promptui.ErrAbort {
return fmt.Errorf("bootstrap cancelled")
if installed {
err = confirmFluxInstallOverride(info)
if err != nil {
if err == promptui.ErrAbort {
return fmt.Errorf("bootstrap cancelled")
}
return err
}
return err
}
}

Expand Down
13 changes: 5 additions & 8 deletions cmd/flux/cluster_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (

"github.com/manifoldco/promptui"
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/controller-runtime/pkg/client"

Expand All @@ -39,8 +38,6 @@ var bootstrapLabels = []string{

// fluxClusterInfo contains information about an existing flux installation on a cluster.
type fluxClusterInfo struct {
// installed indicates that Flux is installed on the cluster.
installed bool
// bootstrapped indicates that Flux was installed using the `flux bootstrap` command.
bootstrapped bool
// managedBy is the name of the tool being used to manage the installation of Flux.
Expand All @@ -54,6 +51,8 @@ type fluxClusterInfo struct {
//
// This function retrieves the GitRepository CRD from the cluster and checks it
// for a set of labels used to determine the Flux version and how Flux was installed.
// It returns the NotFound error from the underlying library if it was unable to find
// the GitRepository CRD and this can be used to check if Flux is installed.
func getFluxClusterInfo(ctx context.Context, c client.Client) (fluxClusterInfo, error) {
var info fluxClusterInfo
crdMetadata := &metav1.PartialObjectMetadata{
Expand All @@ -66,13 +65,9 @@ func getFluxClusterInfo(ctx context.Context, c client.Client) (fluxClusterInfo,
},
}
if err := c.Get(ctx, client.ObjectKeyFromObject(crdMetadata), crdMetadata); err != nil {
if errors.IsNotFound(err) {
return info, nil
}
return info, err
}

info.installed = true
info.version = crdMetadata.Labels["app.kubernetes.io/version"]

var present bool
Expand All @@ -83,6 +78,8 @@ func getFluxClusterInfo(ctx context.Context, c client.Client) (fluxClusterInfo,
info.bootstrapped = true
}

// the `app.kubernetes.io` label is not set by flux but might be set by other
// tools used to install Flux e.g Helm.
if manager, ok := crdMetadata.Labels["app.kubernetes.io/managed-by"]; ok {
info.managedBy = manager
}
Expand All @@ -95,7 +92,7 @@ func getFluxClusterInfo(ctx context.Context, c client.Client) (fluxClusterInfo,
func confirmFluxInstallOverride(info fluxClusterInfo) error {
// no need to display prompt if flux is not installed or installation is
// managed by Flux
if !info.installed || info.managedBy == "" || info.managedBy == "flux" {
if info.managedBy == "" || info.managedBy == "flux" {
return nil
}

Expand Down
19 changes: 8 additions & 11 deletions cmd/flux/cluster_info_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (

. "github.com/onsi/gomega"
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/runtime"
"sigs.k8s.io/controller-runtime/pkg/client/fake"

Expand All @@ -47,7 +48,8 @@ func Test_getFluxClusterInfo(t *testing.T) {
wantInfo fluxClusterInfo
}{
{
name: "no git repository CRD present",
name: "no git repository CRD present",
wantErr: true,
},
{
name: "CRD with kustomize-controller labels",
Expand All @@ -59,7 +61,6 @@ func Test_getFluxClusterInfo(t *testing.T) {
wantInfo: fluxClusterInfo{
version: "v2.1.0",
bootstrapped: true,
installed: true,
},
},
{
Expand All @@ -73,7 +74,6 @@ func Test_getFluxClusterInfo(t *testing.T) {
wantInfo: fluxClusterInfo{
version: "v2.1.0",
bootstrapped: true,
installed: true,
managedBy: "flux",
},
},
Expand All @@ -86,24 +86,20 @@ func Test_getFluxClusterInfo(t *testing.T) {
wantInfo: fluxClusterInfo{
version: "v2.1.0",
managedBy: "helm",
installed: true,
},
},
{
name: "CRD with no labels",
labels: map[string]string{},
wantInfo: fluxClusterInfo{
installed: true,
},
name: "CRD with no labels",
labels: map[string]string{},
wantInfo: fluxClusterInfo{},
},
{
name: "CRD with only version label",
labels: map[string]string{
"app.kubernetes.io/version": "v2.1.0",
},
wantInfo: fluxClusterInfo{
version: "v2.1.0",
installed: true,
version: "v2.1.0",
},
},
}
Expand All @@ -123,6 +119,7 @@ func Test_getFluxClusterInfo(t *testing.T) {
info, err := getFluxClusterInfo(context.Background(), client)
if tt.wantErr {
g.Expect(err).To(HaveOccurred())
g.Expect(errors.IsNotFound(err)).To(BeTrue())
} else {
g.Expect(err).To(Not(HaveOccurred()))
}
Expand Down
11 changes: 8 additions & 3 deletions cmd/flux/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (

"github.com/manifoldco/promptui"
"github.com/spf13/cobra"
"k8s.io/apimachinery/pkg/api/errors"

"github.com/fluxcd/flux2/v2/internal/flags"
"github.com/fluxcd/flux2/v2/internal/utils"
Expand Down Expand Up @@ -191,17 +192,21 @@ func installCmdRun(cmd *cobra.Command, args []string) error {
return err
}

installed := true
info, err := getFluxClusterInfo(ctx, kubeClient)
if err != nil {
return fmt.Errorf("cluster info unavailable: %w", err)
if !errors.IsNotFound(err) {
return fmt.Errorf("cluster info unavailable: %w", err)
}
installed = false
}

if info.installed && info.bootstrapped {
if info.bootstrapped {
return fmt.Errorf("this cluster has already been bootstrapped with Flux %s! Please use 'flux bootstrap' to upgrade",
info.version)
}

if !installArgs.force {
if installed && !installArgs.force {
err := confirmFluxInstallOverride(info)
if err != nil {
if err == promptui.ErrAbort {
Expand Down

0 comments on commit b84aa02

Please sign in to comment.