Skip to content

Commit

Permalink
Add logic to generate labels for release drift with kustomize API
Browse files Browse the repository at this point in the history
  • Loading branch information
dmvolod committed Jan 13, 2025
1 parent 1173f6d commit b743d00
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 4 deletions.
5 changes: 3 additions & 2 deletions controllers/helmreleasedrift/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ import (
)

const (
InstanceLabelKey = "app.kubernetes.io/instance"
InstanceLabelKey = "app.kubernetes.io/instance"
ManagedByLabelValue = "Helm"
)

var (
Expand Down Expand Up @@ -77,7 +78,7 @@ func Add(ctx context.Context, restConfig *rest.Config, helmReleaseProxy *addonsv
HealthProbeBindAddress: "0",
Cache: cache.Options{
DefaultLabelSelector: labels.SelectorFromSet(map[string]string{
konfig.ManagedbyLabelKey: "Helm",
konfig.ManagedbyLabelKey: ManagedByLabelValue,
InstanceLabelKey: helmReleaseProxy.Spec.ReleaseName,
}),
},
Expand Down
9 changes: 9 additions & 0 deletions internal/data/kustomization.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Labels to add to all resources.
labels:
- pairs:
app.kubernetes.io/managed-by: Helm
app.kubernetes.io/instance: $RELEASE_NAME
includeSelectors: false

resources:
- rendered-manifests.yaml
45 changes: 44 additions & 1 deletion internal/helm_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,15 @@ limitations under the License.
package internal

import (
"bytes"
"context"
_ "embed"
"fmt"
"io"
"net/url"
"os"
"path"
"strings"

"github.com/databus23/helm-diff/v3/diff"
"github.com/databus23/helm-diff/v3/manifest"
Expand All @@ -35,6 +38,7 @@ import (
helmCli "helm.sh/helm/v3/pkg/cli"
helmVals "helm.sh/helm/v3/pkg/cli/values"
helmGetter "helm.sh/helm/v3/pkg/getter"
"helm.sh/helm/v3/pkg/postrender"
"helm.sh/helm/v3/pkg/registry"
helmRelease "helm.sh/helm/v3/pkg/release"
helmDriver "helm.sh/helm/v3/pkg/storage/driver"
Expand All @@ -44,6 +48,8 @@ import (
"k8s.io/utils/ptr"
addonsv1alpha1 "sigs.k8s.io/cluster-api-addon-provider-helm/api/v1alpha1"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/kustomize/api/krusty"
"sigs.k8s.io/kustomize/kyaml/filesys"
)

type Client interface {
Expand All @@ -52,7 +58,13 @@ type Client interface {
UninstallHelmRelease(ctx context.Context, restConfig *rest.Config, spec addonsv1alpha1.HelmReleaseProxySpec) (*helmRelease.UninstallReleaseResponse, error)
}

var _ Client = (*HelmClient)(nil)
var (
_ Client = (*HelmClient)(nil)
_ postrender.PostRenderer = (*releaseDriftPostRenderer)(nil)
)

//go:embed data/kustomization.yaml
var releaseDriftKustomization string

type HelmClient struct{}

Expand All @@ -63,6 +75,10 @@ type HelmInstallOverride struct {
IsUpgrade bool
}

type releaseDriftPostRenderer struct {
releaseName string
}

// GetActionConfig returns a new Helm action configuration.
func GetActionConfig(ctx context.Context, namespace string, config *rest.Config) (*helmAction.Configuration, error) {
log := ctrl.LoggerFrom(ctx)
Expand Down Expand Up @@ -216,6 +232,9 @@ func (c *HelmClient) InstallHelmRelease(ctx context.Context, restConfig *rest.Co
installClient.RepoURL = repoURL
installClient.Version = spec.Version
installClient.Namespace = spec.ReleaseNamespace
if spec.ReleaseDrift {
installClient.PostRenderer = releaseDriftPostRenderer{releaseName: spec.ReleaseName}
}

if spec.ReleaseName == "" {
installClient.GenerateName = true
Expand Down Expand Up @@ -348,6 +367,9 @@ func (c *HelmClient) UpgradeHelmReleaseIfChanged(ctx context.Context, restConfig
upgradeClient.RepoURL = repoURL
upgradeClient.Version = spec.Version
upgradeClient.Namespace = spec.ReleaseNamespace
if spec.ReleaseDrift {
upgradeClient.PostRenderer = releaseDriftPostRenderer{releaseName: spec.ReleaseName}
}

log.V(2).Info("Locating chart...")
cp, err := upgradeClient.ChartPathOptions.LocateChart(chartName, settings)
Expand Down Expand Up @@ -565,3 +587,24 @@ func (c *HelmClient) RollbackHelmRelease(ctx context.Context, restConfig *rest.C

return rollbackClient.Run(spec.ReleaseName)
}

func (r releaseDriftPostRenderer) Run(renderedManifests *bytes.Buffer) (modifiedManifests *bytes.Buffer, err error) {
fSys := filesys.MakeFsInMemory()
if err := fSys.WriteFile("kustomization.yaml", []byte(strings.Replace(releaseDriftKustomization, "$RELEASE_NAME", r.releaseName, 1))); err != nil {
return nil, err
}
if err := fSys.WriteFile("rendered-manifests.yaml", renderedManifests.Bytes()); err != nil {
return nil, err
}

kustomizer := krusty.MakeKustomizer(krusty.MakeDefaultOptions())
m, err := kustomizer.Run(fSys, ".")
if err != nil {
return nil, err
}
yml, err := m.AsYaml()
if err != nil {
return nil, err
}
return bytes.NewBuffer(yml), nil
}
2 changes: 1 addition & 1 deletion test/e2e/helm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ var _ = Describe("Workload cluster creation", func() {
})
})

FIt("Install and manage Helm chart with ReleaseDrift option enabled", func() {
It("Install and manage Helm chart with ReleaseDrift option enabled", func() {
clusterName = fmt.Sprintf("%s-%s", specName, util.RandomString(6))
clusterctl.ApplyClusterTemplateAndWait(ctx, createApplyClusterTemplateInput(
specName,
Expand Down

0 comments on commit b743d00

Please sign in to comment.