Skip to content

Commit

Permalink
Support flux v2.2.0
Browse files Browse the repository at this point in the history
This adds the latest source-controller v1.2.2 as a marker for Flux
v2.1.2.

Dependencies are updated to allow us to pull in the latest Flux code.

And we now pull the internal Helm release storage version from the
history, rather than the latest version field.

If there's no history, we retain the old version.
  • Loading branch information
bigkevmcd committed Dec 13, 2023
1 parent 8821d9b commit df0361c
Show file tree
Hide file tree
Showing 28 changed files with 1,698 additions and 411 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ download-test-crds:
curl -sL "https://raw.githubusercontent.com/fluxcd/source-controller/v1.0.0/config/crd/bases/$${group}.toolkit.fluxcd.io_$${resource}.yaml" -o "tools/testcrds/$${group}.toolkit.fluxcd.io_$${resource}.yaml"; \
done
curl -sL "https://raw.githubusercontent.com/fluxcd/kustomize-controller/v1.0.0/config/crd/bases/kustomize.toolkit.fluxcd.io_kustomizations.yaml" -o "tools/testcrds/kustomize.toolkit.fluxcd.io_kustomizations.yaml"
curl -sL "https://raw.githubusercontent.com/fluxcd/helm-controller/v0.35.0/config/crd/bases/helm.toolkit.fluxcd.io_helmreleases.yaml" -o "tools/testcrds/helm.toolkit.fluxcd.io_helmreleases.yaml"
curl -sL "https://raw.githubusercontent.com/fluxcd/helm-controller/v0.37.0/config/crd/bases/helm.toolkit.fluxcd.io_helmreleases.yaml" -o "tools/testcrds/helm.toolkit.fluxcd.io_helmreleases.yaml"

.PHONY: help
# Thanks to https://www.thapaliya.com/en/writings/well-documented-makefiles/
Expand Down
7 changes: 1 addition & 6 deletions cmd/gitops/create/terraform/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,9 @@ gitops create terraform -n default my-resource --source GitRepository/my-project
kubeConfigArgs.Namespace = &namespace
kubeConfigArgs.Context = &context

cfg, err := kubeConfigArgs.ToRESTConfig()
if err != nil {
return err
}

v := viper.New()
v.Set("namespace", namespace)
if err := app.Init(cfg, v); err != nil {
if err := app.Init(kubeConfigArgs, v); err != nil {
return err
}

Expand Down
10 changes: 3 additions & 7 deletions cmd/gitops/delete/terraform/cmd.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package terraform

import (
"os"

"github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/weaveworks/tf-controller/tfctl"
"github.com/weaveworks/weave-gitops/cmd/gitops/config"
"github.com/weaveworks/weave-gitops/pkg/run"
"k8s.io/cli-runtime/pkg/genericclioptions"
"os"
)

var kubeConfigArgs *genericclioptions.ConfigFlags
Expand Down Expand Up @@ -38,14 +39,9 @@ gitops delete terraform -n default my-resource
kubeConfigArgs.Namespace = &namespace
kubeConfigArgs.Context = &context

cfg, err := kubeConfigArgs.ToRESTConfig()
if err != nil {
return err
}

v := viper.New()
v.Set("namespace", namespace)
if err := app.Init(cfg, v); err != nil {
if err := app.Init(kubeConfigArgs, v); err != nil {
return err
}

Expand Down
10 changes: 3 additions & 7 deletions cmd/gitops/replan/terraform/cmd.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package terraform

import (
"os"

"github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/weaveworks/tf-controller/tfctl"
"github.com/weaveworks/weave-gitops/cmd/gitops/config"
"github.com/weaveworks/weave-gitops/pkg/run"
"k8s.io/cli-runtime/pkg/genericclioptions"
"os"
)

var kubeConfigArgs *genericclioptions.ConfigFlags
Expand Down Expand Up @@ -38,14 +39,9 @@ gitops replan terraform --namespace flux-system my-resource
kubeConfigArgs.Namespace = &namespace
kubeConfigArgs.Context = &context

cfg, err := kubeConfigArgs.ToRESTConfig()
if err != nil {
return err
}

v := viper.New()
v.Set("namespace", namespace)
if err := app.Init(cfg, v); err != nil {
if err := app.Init(kubeConfigArgs, v); err != nil {
return err
}

Expand Down
10 changes: 3 additions & 7 deletions cmd/gitops/resume/terraform/cmd.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package terraform

import (
"os"

"github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/weaveworks/tf-controller/tfctl"
"github.com/weaveworks/weave-gitops/cmd/gitops/config"
"github.com/weaveworks/weave-gitops/pkg/run"
"k8s.io/cli-runtime/pkg/genericclioptions"
"os"
)

var kubeConfigArgs *genericclioptions.ConfigFlags
Expand Down Expand Up @@ -38,14 +39,9 @@ gitops resume terraform --namespace flux-system my-resource
kubeConfigArgs.Namespace = &namespace
kubeConfigArgs.Context = &context

cfg, err := kubeConfigArgs.ToRESTConfig()
if err != nil {
return err
}

v := viper.New()
v.Set("namespace", namespace)
if err := app.Init(cfg, v); err != nil {
if err := app.Init(kubeConfigArgs, v); err != nil {
return err
}

Expand Down
10 changes: 3 additions & 7 deletions cmd/gitops/suspend/terraform/cmd.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package terraform

import (
"os"

"github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/weaveworks/tf-controller/tfctl"
"github.com/weaveworks/weave-gitops/cmd/gitops/config"
"github.com/weaveworks/weave-gitops/pkg/run"
"k8s.io/cli-runtime/pkg/genericclioptions"
"os"
)

var kubeConfigArgs *genericclioptions.ConfigFlags
Expand Down Expand Up @@ -38,14 +39,9 @@ gitops suspend terraform --namespace flux-system my-resource
kubeConfigArgs.Namespace = &namespace
kubeConfigArgs.Context = &context

cfg, err := kubeConfigArgs.ToRESTConfig()
if err != nil {
return err
}

v := viper.New()
v.Set("namespace", namespace)
if err := app.Init(cfg, v); err != nil {
if err := app.Init(kubeConfigArgs, v); err != nil {
return err
}

Expand Down
2 changes: 1 addition & 1 deletion core/fluxsync/adapters.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package fluxsync

import (
helmv2 "github.com/fluxcd/helm-controller/api/v2beta1"
helmv2 "github.com/fluxcd/helm-controller/api/v2beta2"
imgautomationv1 "github.com/fluxcd/image-automation-controller/api/v1beta1"
reflectorv1 "github.com/fluxcd/image-reflector-controller/api/v1beta2"
kustomizev1 "github.com/fluxcd/kustomize-controller/api/v1"
Expand Down
2 changes: 1 addition & 1 deletion core/server/events_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (

"testing"

helmv2 "github.com/fluxcd/helm-controller/api/v2beta1"
helmv2 "github.com/fluxcd/helm-controller/api/v2beta2"
kustomizev1 "github.com/fluxcd/kustomize-controller/api/v1"
. "github.com/onsi/gomega"
pb "github.com/weaveworks/weave-gitops/pkg/api/core"
Expand Down
2 changes: 1 addition & 1 deletion core/server/fluxruntime.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (
"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/controller-runtime/pkg/client"

helmv2 "github.com/fluxcd/helm-controller/api/v2beta1"
helmv2 "github.com/fluxcd/helm-controller/api/v2beta2"
kustomizev1 "github.com/fluxcd/kustomize-controller/api/v1"
appsv1 "k8s.io/api/apps/v1"
v1 "k8s.io/api/core/v1"
Expand Down
2 changes: 1 addition & 1 deletion core/server/helm_release.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"fmt"
"strings"

helmv2 "github.com/fluxcd/helm-controller/api/v2beta1"
helmv2 "github.com/fluxcd/helm-controller/api/v2beta2"
"github.com/weaveworks/weave-gitops/core/clustersmngr"
pb "github.com/weaveworks/weave-gitops/pkg/api/core"
)
Expand Down
32 changes: 24 additions & 8 deletions core/server/inventory.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"strings"
"sync"

helmv2 "github.com/fluxcd/helm-controller/api/v2beta1"
helmv2 "github.com/fluxcd/helm-controller/api/v2beta2"
kustomizev1 "github.com/fluxcd/kustomize-controller/api/v1"
"github.com/fluxcd/pkg/ssa"
"github.com/go-logr/logr"
Expand Down Expand Up @@ -70,7 +70,7 @@ func (cs *coreServer) GetInventory(ctx context.Context, msg *pb.GetInventoryRequ
}
}

objsWithChildren, err := GetObjectsWithChildren(ctx, inventoryRefs, client, msg.WithChildren, cs.logger)
objsWithChildren, err := getObjectsWithChildren(ctx, msg.Namespace, inventoryRefs, client, msg.WithChildren, cs.logger)
if err != nil {
return nil, fmt.Errorf("failed getting objects with children: %w", err)
}
Expand Down Expand Up @@ -145,16 +145,15 @@ func (cs *coreServer) getHelmReleaseInventory(ctx context.Context, k8sClient cli

// Returns the list of resources applied in the helm chart.
func getHelmReleaseObjects(ctx context.Context, k8sClient client.Client, helmRelease *helmv2.HelmRelease) ([]*unstructured.Unstructured, error) {
storageNamespace := helmRelease.GetStorageNamespace()

storageName := helmRelease.GetReleaseName()

storageVersion := helmRelease.Status.LastReleaseRevision
storageVersion := storageVersionFromHelmRelease(helmRelease)
if storageVersion < 1 {
// skip release if it failed to install
return nil, nil
}

storageNamespace := helmRelease.GetStorageNamespace()
storageName := helmRelease.GetReleaseName()

storageSecret := &v1.Secret{}
secretName := fmt.Sprintf("sh.helm.release.v1.%s.v%v", storageName, storageVersion)
key := client.ObjectKey{
Expand Down Expand Up @@ -258,7 +257,7 @@ func unstructuredToInventoryEntry(clusterName string, objWithChildren ObjectWith
// GetObjectsWithChildren returns objects with their children populated if withChildren is true.
// Objects are retrieved in parallel.
// Children are retrieved recusively, e.g. Deployment -> ReplicaSet -> Pod
func GetObjectsWithChildren(ctx context.Context, objects []*unstructured.Unstructured, k8sClient client.Client, withChildren bool, logger logr.Logger) ([]*ObjectWithChildren, error) {
func getObjectsWithChildren(ctx context.Context, defaultNS string, objects []*unstructured.Unstructured, k8sClient client.Client, withChildren bool, logger logr.Logger) ([]*ObjectWithChildren, error) {

Check failure on line 260 in core/server/inventory.go

View workflow job for this annotation

GitHub Actions / CI Check Static Checks (1.20.X, 16.X)

getObjectsWithChildren - result 1 (error) is always nil (unparam)
result := []*ObjectWithChildren{}
resultMu := sync.Mutex{}

Expand All @@ -270,6 +269,10 @@ func GetObjectsWithChildren(ctx context.Context, objects []*unstructured.Unstruc
go func(obj unstructured.Unstructured) {
defer wg.Done()

if obj.GetNamespace() == "" {
obj.SetNamespace(defaultNS)
}

if err := k8sClient.Get(ctx, client.ObjectKeyFromObject(&obj), &obj); err != nil {
logger.Error(err, "failed to get object", "entry", obj)
return
Expand Down Expand Up @@ -446,3 +449,16 @@ func parseInventoryFromUnstructured(obj *unstructured.Unstructured) ([]*unstruct

return objects, nil
}

func storageVersionFromHelmRelease(helmRelease *helmv2.HelmRelease) int {
storageVersion := helmRelease.Status.LastReleaseRevision

if storageVersion == 0 && helmRelease.Status.History != nil {
latest := helmRelease.Status.History.Latest()
if latest != nil {
storageVersion = latest.Version
}
}

return storageVersion
}
2 changes: 1 addition & 1 deletion core/server/inventory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"fmt"
"testing"

helmv2 "github.com/fluxcd/helm-controller/api/v2beta1"
helmv2 "github.com/fluxcd/helm-controller/api/v2beta2"
kustomizev1 "github.com/fluxcd/kustomize-controller/api/v1"
"github.com/fluxcd/pkg/apis/meta"
sourcev1 "github.com/fluxcd/source-controller/api/v1"
Expand Down
10 changes: 5 additions & 5 deletions core/server/objects.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"errors"
"fmt"

"github.com/fluxcd/helm-controller/api/v2beta1"
helmv2 "github.com/fluxcd/helm-controller/api/v2beta2"
kustomizev1 "github.com/fluxcd/kustomize-controller/api/v1"
"github.com/hashicorp/go-multierror"
"github.com/weaveworks/weave-gitops/core/clustersmngr"
Expand All @@ -25,7 +25,7 @@ const (
)

func getUnstructuredHelmReleaseInventory(ctx context.Context, obj unstructured.Unstructured, c clustersmngr.Client, cluster string) ([]*pb.GroupVersionKind, error) {
var release v2beta1.HelmRelease
var release helmv2.HelmRelease

err := runtime.DefaultUnstructuredConverter.FromUnstructured(obj.UnstructuredContent(), &release)
if err != nil {
Expand Down Expand Up @@ -116,7 +116,7 @@ func (cs *coreServer) ListObjects(ctx context.Context, msg *pb.ListObjectsReques
respErrors = append(respErrors, &pb.ListError{ClusterName: clusterName, Message: fmt.Sprintf("error sanitizing secrets: %v", err)})
continue
}
case v2beta1.HelmReleaseKind:
case helmv2.HelmReleaseKind:
inventory, err = getUnstructuredHelmReleaseInventory(ctx, unstructuredObj, clustersClient, clusterName)
if err != nil {
respErrors = append(respErrors, &pb.ListError{ClusterName: clusterName, Message: err.Error()})
Expand Down Expand Up @@ -175,7 +175,7 @@ func parseSessionInfo(unstructuredObj unstructured.Unstructured) (string, string
if annotations["run.weave.works/automation-kind"] == "ks" {
kind = kustomizev1.KustomizationKind
} else {
kind = v2beta1.HelmReleaseKind
kind = helmv2.HelmReleaseKind
}

ns := annotations["run.weave.works/namespace"]
Expand Down Expand Up @@ -253,7 +253,7 @@ func (cs *coreServer) GetObject(ctx context.Context, msg *pb.GetObjectRequest) (
if err != nil {
return nil, fmt.Errorf("error sanitizing secrets: %w", err)
}
case v2beta1.HelmReleaseKind:
case helmv2.HelmReleaseKind:
inventory, err = getUnstructuredHelmReleaseInventory(ctx, unstructuredObj, clustersClient, msg.ClusterName)
if err != nil {
inventory = nil // We can still display most things without inventory
Expand Down
Loading

0 comments on commit df0361c

Please sign in to comment.