diff --git a/pkg/controllers/helmChartController.go b/pkg/controllers/helmChartController.go index 0e8e9d1..a4456ad 100644 --- a/pkg/controllers/helmChartController.go +++ b/pkg/controllers/helmChartController.go @@ -1,6 +1,7 @@ package controllers import ( + "context" "encoding/json" "github.com/gimlet-io/capacitor/pkg/flux" @@ -13,6 +14,12 @@ import ( ) var helmChartResource = schema.GroupVersionResource{ + Group: "source.toolkit.fluxcd.io", + Version: "v1", + Resource: "helmcharts", +} + +var helmChartResourceV1beta2 = schema.GroupVersionResource{ Group: "source.toolkit.fluxcd.io", Version: "v1beta2", Resource: "helmcharts", @@ -23,10 +30,17 @@ func HelmChartController( dynamicClient *dynamic.DynamicClient, clientHub *streaming.ClientHub, ) (*Controller, error) { + resource := helmChartResource + // check if v1 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 v1beta2 + resource = helmChartResourceV1beta2 + } return NewDynamicController( "helmcharts.source.toolkit.fluxcd.io", dynamicClient, - helmChartResource, + resource, func(informerEvent Event, objectMeta metav1.ObjectMeta, obj interface{}) error { switch informerEvent.eventType { case "create": diff --git a/pkg/controllers/helmRepositoryController.go b/pkg/controllers/helmRepositoryController.go index a6d92bd..fefce0c 100644 --- a/pkg/controllers/helmRepositoryController.go +++ b/pkg/controllers/helmRepositoryController.go @@ -1,6 +1,7 @@ package controllers import ( + "context" "encoding/json" "github.com/gimlet-io/capacitor/pkg/flux" @@ -13,6 +14,12 @@ import ( ) var helmRepositoryResource = schema.GroupVersionResource{ + Group: "source.toolkit.fluxcd.io", + Version: "v1", + Resource: "helmrepositories", +} + +var helmRepositoryResourceV1beta2 = schema.GroupVersionResource{ Group: "source.toolkit.fluxcd.io", Version: "v1beta2", Resource: "helmrepositories", @@ -23,10 +30,17 @@ func HelmRepositoryController( dynamicClient *dynamic.DynamicClient, clientHub *streaming.ClientHub, ) (*Controller, error) { + resource := helmRepositoryResource + // check if v1 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 v1beta2 + resource = helmRepositoryResourceV1beta2 + } return NewDynamicController( "helmrepositories.source.toolkit.fluxcd.io", dynamicClient, - helmRepositoryResource, + resource, func(informerEvent Event, objectMeta metav1.ObjectMeta, obj interface{}) error { switch informerEvent.eventType { case "create": diff --git a/pkg/controllers/helmreleaseController.go b/pkg/controllers/helmreleaseController.go index 213da31..1ddb45e 100644 --- a/pkg/controllers/helmreleaseController.go +++ b/pkg/controllers/helmreleaseController.go @@ -14,6 +14,12 @@ import ( ) var helmReleaseResource = schema.GroupVersionResource{ + Group: "helm.toolkit.fluxcd.io", + Version: "v2", + Resource: "helmreleases", +} + +var helmReleaseResourceV2beta2 = schema.GroupVersionResource{ Group: "helm.toolkit.fluxcd.io", Version: "v2beta2", Resource: "helmreleases", @@ -31,8 +37,14 @@ func HelmReleaseController( clientHub *streaming.ClientHub, ) (*Controller, error) { resource := helmReleaseResource - // check if v2beta2 is supported + // check if v2 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 v2beta2 + resource = helmReleaseResourceV2beta2 + } + + _, 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