Skip to content

Commit

Permalink
fix/making tofu controller optional (#126)
Browse files Browse the repository at this point in the history
* fix: making tofu-controller optional

* Reducing log spam
  • Loading branch information
laszlocph authored Oct 2, 2024
1 parent 9e3ad3e commit db1b82c
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 31 deletions.
2 changes: 2 additions & 0 deletions deploy/k8s/rbac.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ rules:
- source.toolkit.fluxcd.io
- kustomize.toolkit.fluxcd.io
- helm.toolkit.fluxcd.io
- infra.contrib.fluxcd.io
resources:
- gitrepositories
- ocirepositories
Expand All @@ -38,6 +39,7 @@ rules:
- helmcharts
- kustomizations
- helmreleases
- terraforms
verbs:
- get
- watch
Expand Down
90 changes: 59 additions & 31 deletions pkg/flux/flux.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,12 @@ var (
}

helmReleaseGVR = schema.GroupVersionResource{
Group: "helm.toolkit.fluxcd.io",
Version: "v2",
Resource: "helmreleases",
}

helmReleaseGVRV2beta2 = schema.GroupVersionResource{
Group: "helm.toolkit.fluxcd.io",
Version: "v2beta2",
Resource: "helmreleases",
Expand All @@ -75,12 +81,24 @@ var (
}

helmRepositoryGVR = schema.GroupVersionResource{
Group: "source.toolkit.fluxcd.io",
Version: "v1",
Resource: "helmrepositories",
}

helmRepositoryGVRv1beta2 = schema.GroupVersionResource{
Group: "source.toolkit.fluxcd.io",
Version: "v1beta2",
Resource: "helmrepositories",
}

helmChartGVR = schema.GroupVersionResource{
Group: "source.toolkit.fluxcd.io",
Version: "v1",
Resource: "helmcharts",
}

helmChartGVRv1beta2 = schema.GroupVersionResource{
Group: "source.toolkit.fluxcd.io",
Version: "v1beta2",
Resource: "helmcharts",
Expand Down Expand Up @@ -284,14 +302,24 @@ func helmReleases(dc *dynamic.DynamicClient) ([]helmv2beta2.HelmRelease, error)
List(context.TODO(), metav1.ListOptions{})
if err != nil {
if strings.Contains(err.Error(), "the server could not find the requested resource") {
// let's try the deprecated v2beta1
helmReleases, err = dc.Resource(helmReleaseGVRV2beta1).
// let's try the deprecated v2beta2
helmReleases, err = dc.Resource(helmReleaseGVRV2beta2).
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
// 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
}
Expand Down Expand Up @@ -405,7 +433,12 @@ func State(c *kubernetes.Clientset, dc *dynamic.DynamicClient) (*FluxState, erro
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") {
// tofu-controller is not mandatory, ignore error
tfResources = &unstructured.UnstructuredList{}
} else {
return nil, err
}
}
for _, t := range tfResources.Items {
unstructured := t.UnstructuredContent()
Expand Down Expand Up @@ -449,43 +482,28 @@ func State(c *kubernetes.Clientset, dc *dynamic.DynamicClient) (*FluxState, erro
fluxState.Kustomizations = append(fluxState.Kustomizations, kustomization)
}

helmReleases, err := dc.Resource(helmReleaseGVR).
helmReleases, err := helmReleases(dc)
if err != nil {
return nil, err
}
fluxState.HelmReleases = helmReleases

helmRepositories, err := dc.Resource(helmRepositoryGVR).
Namespace("").
List(context.TODO(), metav1.ListOptions{})
if err != nil {
if strings.Contains(err.Error(), "the server could not find the requested resource") {
// let's try the deprecated v2beta1
helmReleases, err = dc.Resource(helmReleaseGVRV2beta1).
// let's try the deprecated v1beta2
helmRepositories, err = dc.Resource(helmRepositoryGVRv1beta2).
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{}
}
return nil, err
}
} else {
return nil, err
}
}
for _, h := range helmReleases.Items {
unstructured := h.UnstructuredContent()
var helmRelease helmv2beta2.HelmRelease
err = runtime.DefaultUnstructuredConverter.FromUnstructured(unstructured, &helmRelease)
if err != nil {
return nil, err
}
fluxState.HelmReleases = append(fluxState.HelmReleases, helmRelease)
}

helmRepositories, err := dc.Resource(helmRepositoryGVR).
Namespace("").
List(context.TODO(), metav1.ListOptions{})
if err != nil {
return nil, err
}
for _, h := range helmRepositories.Items {
unstructured := h.UnstructuredContent()
var helmRepository sourcev1beta2.HelmRepository
Expand All @@ -500,7 +518,17 @@ func State(c *kubernetes.Clientset, dc *dynamic.DynamicClient) (*FluxState, erro
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 v1beta2
helmCharts, err = dc.Resource(helmChartGVRv1beta2).
Namespace("").
List(context.TODO(), metav1.ListOptions{})
if err != nil {
return nil, err
}
} else {
return nil, err
}
}
for _, h := range helmCharts.Items {
unstructured := h.UnstructuredContent()
Expand Down

0 comments on commit db1b82c

Please sign in to comment.