Skip to content

Commit

Permalink
fix: log spam, v2beta2 is default, but degrade to v2beta1 when necess…
Browse files Browse the repository at this point in the history
…airy
  • Loading branch information
laszlocph committed Mar 18, 2024
1 parent 203623c commit 22ae77d
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 18 deletions.
17 changes: 16 additions & 1 deletion pkg/controllers/helmreleaseController.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package controllers

import (
"context"
"encoding/json"

"github.com/gimlet-io/capacitor/pkg/flux"
Expand All @@ -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",
Expand All @@ -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":
Expand Down
2 changes: 1 addition & 1 deletion pkg/flux/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
58 changes: 44 additions & 14 deletions pkg/flux/flux.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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",
Expand All @@ -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{}
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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{},
}

Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions pkg/flux/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"`
}

Expand Down

0 comments on commit 22ae77d

Please sign in to comment.