diff --git a/pkg/controllers/helmreleaseController.go b/pkg/controllers/helmreleaseController.go index ed49450..213da31 100644 --- a/pkg/controllers/helmreleaseController.go +++ b/pkg/controllers/helmreleaseController.go @@ -1,6 +1,7 @@ package controllers import ( + "context" "encoding/json" "github.com/gimlet-io/capacitor/pkg/flux" @@ -13,6 +14,12 @@ import ( ) var helmReleaseResource = schema.GroupVersionResource{ + Group: "helm.toolkit.fluxcd.io", + Version: "v2beta2", + Resource: "helmreleases", +} + +var helmReleaseResourceV2beta1 = schema.GroupVersionResource{ Group: "helm.toolkit.fluxcd.io", Version: "v2beta1", Resource: "helmreleases", @@ -23,10 +30,18 @@ func HelmReleaseController( dynamicClient *dynamic.DynamicClient, clientHub *streaming.ClientHub, ) (*Controller, error) { + resource := helmReleaseResource + // check if v2beta2 is supported + _, err := dynamicClient.Resource(resource).Namespace("").List(context.TODO(), metav1.ListOptions{}) + if err != nil { + // try and possibly fail (helm-controller is not mandatory) with v2beta1 + resource = helmReleaseResourceV2beta1 + } + return NewDynamicController( "helmreleases.helm.toolkit.fluxcd.io", dynamicClient, - helmReleaseResource, + resource, func(informerEvent Event, objectMeta metav1.ObjectMeta, obj interface{}) error { switch informerEvent.eventType { case "create": diff --git a/pkg/flux/events.go b/pkg/flux/events.go index 1b1dd23..ebec0e6 100644 --- a/pkg/flux/events.go +++ b/pkg/flux/events.go @@ -25,7 +25,7 @@ import ( corev1 "k8s.io/api/core/v1" "k8s.io/apimachinery/pkg/runtime/schema" - helmv2 "github.com/fluxcd/helm-controller/api/v2beta1" + helmv2 "github.com/fluxcd/helm-controller/api/v2beta2" autov1 "github.com/fluxcd/image-automation-controller/api/v1beta1" imagev1 "github.com/fluxcd/image-reflector-controller/api/v1beta2" kustomizev1 "github.com/fluxcd/kustomize-controller/api/v1" diff --git a/pkg/flux/flux.go b/pkg/flux/flux.go index 07c60eb..259c08c 100644 --- a/pkg/flux/flux.go +++ b/pkg/flux/flux.go @@ -8,7 +8,7 @@ import ( "sort" "strings" - helmv2beta1 "github.com/fluxcd/helm-controller/api/v2beta1" + helmv2beta2 "github.com/fluxcd/helm-controller/api/v2beta2" kustomizationv1 "github.com/fluxcd/kustomize-controller/api/v1" sourcev1 "github.com/fluxcd/source-controller/api/v1" sourcev1beta2 "github.com/fluxcd/source-controller/api/v1beta2" @@ -56,6 +56,12 @@ var ( } helmReleaseGVR = schema.GroupVersionResource{ + Group: "helm.toolkit.fluxcd.io", + Version: "v2beta2", + Resource: "helmreleases", + } + + helmReleaseGVRV2beta1 = schema.GroupVersionResource{ Group: "helm.toolkit.fluxcd.io", Version: "v2beta1", Resource: "helmreleases", @@ -65,10 +71,7 @@ var ( func helmServices(dc *dynamic.DynamicClient) ([]Service, error) { helmReleases, err := helmReleases(dc) if err != nil { - if !strings.Contains(err.Error(), "the server could not find the requested resource") { - return nil, err - } - return []Service{}, nil + return nil, err } services := []Service{} @@ -254,19 +257,34 @@ func inventory(dc *dynamic.DynamicClient) ([]object.ObjMetadata, error) { return inventory, nil } -func helmReleases(dc *dynamic.DynamicClient) ([]helmv2beta1.HelmRelease, error) { - releases := []helmv2beta1.HelmRelease{} +func helmReleases(dc *dynamic.DynamicClient) ([]helmv2beta2.HelmRelease, error) { + releases := []helmv2beta2.HelmRelease{} helmReleases, err := dc.Resource(helmReleaseGVR). Namespace(""). List(context.TODO(), metav1.ListOptions{}) if err != nil { - return nil, err + if strings.Contains(err.Error(), "the server could not find the requested resource") { + // let's try the deprecated v2beta1 + helmReleases, err = dc.Resource(helmReleaseGVRV2beta1). + Namespace(""). + List(context.TODO(), metav1.ListOptions{}) + if err != nil { + if strings.Contains(err.Error(), "the server could not find the requested resource") { + // helm-controller is not mandatory, ignore error + return releases, nil + } else { + return nil, err + } + } + } else { + return nil, err + } } for _, h := range helmReleases.Items { unstructured := h.UnstructuredContent() - var helmRelease helmv2beta1.HelmRelease + var helmRelease helmv2beta2.HelmRelease err = runtime.DefaultUnstructuredConverter.FromUnstructured(unstructured, &helmRelease) if err != nil { return nil, err @@ -280,7 +298,7 @@ func helmReleases(dc *dynamic.DynamicClient) ([]helmv2beta1.HelmRelease, error) func helmStatusWithResources( releases []*rspb.Release, - hr helmv2beta1.HelmRelease, + hr helmv2beta2.HelmRelease, ) (kube.ResourceList, error) { var release *rspb.Release version := -1 @@ -322,7 +340,7 @@ func State(c *kubernetes.Clientset, dc *dynamic.DynamicClient) (*FluxState, erro OCIRepositories: []sourcev1beta2.OCIRepository{}, Buckets: []sourcev1beta2.Bucket{}, Kustomizations: []kustomizationv1.Kustomization{}, - HelmReleases: []helmv2beta1.HelmRelease{}, + HelmReleases: []helmv2beta2.HelmRelease{}, FluxServices: []Service{}, } @@ -397,14 +415,26 @@ func State(c *kubernetes.Clientset, dc *dynamic.DynamicClient) (*FluxState, erro Namespace(""). List(context.TODO(), metav1.ListOptions{}) if err != nil { - helmReleases = &unstructured.UnstructuredList{} - if !strings.Contains(err.Error(), "the server could not find the requested resource") { + if strings.Contains(err.Error(), "the server could not find the requested resource") { + // let's try the deprecated v2beta1 + helmReleases, err = dc.Resource(helmReleaseGVRV2beta1). + Namespace(""). + List(context.TODO(), metav1.ListOptions{}) + if err != nil { + if !strings.Contains(err.Error(), "the server could not find the requested resource") { + return nil, err + } else { + // helm-controller is not mandatory, ignore error + helmReleases = &unstructured.UnstructuredList{} + } + } + } else { return nil, err } } for _, h := range helmReleases.Items { unstructured := h.UnstructuredContent() - var helmRelease helmv2beta1.HelmRelease + var helmRelease helmv2beta2.HelmRelease err = runtime.DefaultUnstructuredConverter.FromUnstructured(unstructured, &helmRelease) if err != nil { return nil, err diff --git a/pkg/flux/types.go b/pkg/flux/types.go index f7a0b72..8149cbd 100644 --- a/pkg/flux/types.go +++ b/pkg/flux/types.go @@ -3,7 +3,7 @@ package flux import ( "time" - helmv2beta1 "github.com/fluxcd/helm-controller/api/v2beta1" + helmv2beta2 "github.com/fluxcd/helm-controller/api/v2beta2" kustomizationv1 "github.com/fluxcd/kustomize-controller/api/v1" sourcev1 "github.com/fluxcd/source-controller/api/v1" sourcev1beta2 "github.com/fluxcd/source-controller/api/v1beta2" @@ -17,7 +17,7 @@ type FluxState struct { OCIRepositories []sourcev1beta2.OCIRepository `json:"ociRepositories"` Buckets []sourcev1beta2.Bucket `json:"buckets"` Kustomizations []kustomizationv1.Kustomization `json:"kustomizations"` - HelmReleases []helmv2beta1.HelmRelease `json:"helmReleases"` + HelmReleases []helmv2beta2.HelmRelease `json:"helmReleases"` FluxServices []Service `json:"fluxServices"` }