Skip to content

Commit

Permalink
Merge pull request #4764 from twz123/backport-4762-to-release-1.27
Browse files Browse the repository at this point in the history
[Backport release-1.27] Cleanup unknown Helm chart manifest files
  • Loading branch information
twz123 authored Aug 1, 2024
2 parents 125ae2d + e785828 commit 7437d1d
Show file tree
Hide file tree
Showing 6 changed files with 169 additions and 83 deletions.
1 change: 0 additions & 1 deletion cmd/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,6 @@ func (c *command) start(ctx context.Context) error {
}
c.ClusterComponents.Add(ctx, controller.NewCRD(helmSaver, []string{"helm"}))
c.ClusterComponents.Add(ctx, controller.NewExtensionsController(
helmSaver,
c.K0sVars,
adminClientFactory,
leaderElector,
Expand Down
70 changes: 61 additions & 9 deletions inttest/addons/addons_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,22 @@ package addons

import (
"bytes"
"context"
"fmt"
"slices"
"testing"
"time"

"github.com/stretchr/testify/suite"

"github.com/k0sproject/k0s/internal/pkg/templatewriter"
"github.com/k0sproject/k0s/inttest/common"
"github.com/k0sproject/k0s/pkg/apis/helm.k0sproject.io/v1beta1"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
helmv1beta1 "github.com/k0sproject/k0s/pkg/apis/helm.k0sproject.io/v1beta1"
helmclientset "github.com/k0sproject/k0s/pkg/apis/helm.k0sproject.io/v1beta1/clientset"
k0sclientset "github.com/k0sproject/k0s/pkg/apis/k0s.k0sproject.io/clientset"
k0sv1beta1 "github.com/k0sproject/k0s/pkg/apis/k0s.k0sproject.io/v1beta1"
"github.com/k0sproject/k0s/pkg/constant"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/wait"
k8s "k8s.io/client-go/kubernetes"
"k8s.io/client-go/kubernetes/scheme"
Expand All @@ -40,12 +46,14 @@ type AddonsSuite struct {
}

func (as *AddonsSuite) TestHelmBasedAddons() {
ctx := as.Context()

addonName := "test-addon"
ociAddonName := "oci-addon"
fileAddonName := "tgz-addon"
as.PutFile(as.ControllerNode(0), "/tmp/k0s.yaml", fmt.Sprintf(k0sConfigWithAddon, addonName))
as.pullHelmChart(as.ControllerNode(0))
as.Require().NoError(as.InitController(0, "--config=/tmp/k0s.yaml"))
as.Require().NoError(as.InitController(0, "--config=/tmp/k0s.yaml", "--enable-dynamic-config"))
as.NoError(as.RunWorkers())
kc, err := as.KubeClient(as.ControllerNode(0))
as.Require().NoError(err)
Expand All @@ -57,6 +65,8 @@ func (as *AddonsSuite) TestHelmBasedAddons() {

as.AssertSomeKubeSystemPods(kc)

as.Run("Rename chart in Helm extension", func() { as.renameChart(ctx) })

values := map[string]interface{}{
"replicaCount": 2,
"image": map[string]interface{}{
Expand All @@ -83,7 +93,49 @@ func (as *AddonsSuite) pullHelmChart(node string) {
as.Require().NoError(err)
}

func (as *AddonsSuite) deleteRelease(chart *v1beta1.Chart) {
func (as *AddonsSuite) renameChart(ctx context.Context) {
restConfig, err := as.GetKubeConfig(as.ControllerNode(0))
as.Require().NoError(err)
k0sClients, err := k0sclientset.NewForConfig(restConfig)
as.Require().NoError(err)
helmClients, err := helmclientset.NewForConfig(restConfig)
as.Require().NoError(err)

configs := k0sClients.K0sV1beta1().ClusterConfigs(constant.ClusterConfigNamespace)
cfg, err := configs.Get(ctx, constant.ClusterConfigObjectName, metav1.GetOptions{})
as.Require().NoError(err)

i := slices.IndexFunc(cfg.Spec.Extensions.Helm.Charts, func(c k0sv1beta1.Chart) bool {
return c.Name == "tgz-addon"
})
as.Require().GreaterOrEqual(i, 0, "Didn't find tgz-addon in %v", cfg.Spec.Extensions.Helm.Charts)
cfg.Spec.Extensions.Helm.Charts[i].Name = "tgz-renamed-addon"

cfg, err = configs.Update(ctx, cfg, metav1.UpdateOptions{FieldManager: as.T().Name()})
as.Require().NoError(err)
if data, err := yaml.Marshal(cfg); as.NoError(err) {
as.T().Logf("%s", data)
}

as.Require().NoError(wait.PollUntilContextCancel(ctx, 350*time.Millisecond, true, func(ctx context.Context) (bool, error) {
charts, err := helmClients.HelmV1beta1().Charts(constant.ClusterConfigNamespace).List(ctx, metav1.ListOptions{})
if err != nil {
return false, nil
}

hasChart := func(name string) bool {
return slices.IndexFunc(charts.Items, func(c helmv1beta1.Chart) bool {
return c.Name == name
}) >= 0
}

return !hasChart("k0s-addon-chart-tgz-addon") && hasChart("k0s-addon-chart-tgz-renamed-addon"), nil
}), "While waiting for Chart resource to be swapped")

as.waitForTestRelease("tgz-renamed-addon", "0.6.0", "kube-system", 1)
}

func (as *AddonsSuite) deleteRelease(chart *helmv1beta1.Chart) {
as.T().Logf("Deleting chart %s/%s", chart.Namespace, chart.Name)
ssh, err := as.SSH(as.Context(), as.ControllerNode(0))
as.Require().NoError(err)
Expand All @@ -96,7 +148,7 @@ func (as *AddonsSuite) deleteRelease(chart *v1beta1.Chart) {
as.Require().NoError(err)
as.Require().NoError(wait.PollImmediate(time.Second, 5*time.Minute, func() (done bool, err error) {
as.T().Logf("Expecting have no secrets left for release %s/%s", chart.Namespace, chart.Name)
items, err := k8sclient.CoreV1().Secrets("default").List(as.Context(), v1.ListOptions{
items, err := k8sclient.CoreV1().Secrets("default").List(as.Context(), metav1.ListOptions{
LabelSelector: fmt.Sprintf("name=%s", chart.Name),
})
if err != nil {
Expand All @@ -111,18 +163,18 @@ func (as *AddonsSuite) deleteRelease(chart *v1beta1.Chart) {
}))
}

func (as *AddonsSuite) waitForTestRelease(addonName, appVersion string, namespace string, rev int64) *v1beta1.Chart {
func (as *AddonsSuite) waitForTestRelease(addonName, appVersion string, namespace string, rev int64) *helmv1beta1.Chart {
as.T().Logf("waiting to see %s release ready in kube API, generation %d", addonName, rev)

cfg, err := as.GetKubeConfig(as.ControllerNode(0))
as.Require().NoError(err)
err = v1beta1.AddToScheme(scheme.Scheme)
err = helmv1beta1.AddToScheme(scheme.Scheme)
as.Require().NoError(err)
chartClient, err := client.New(cfg, client.Options{
Scheme: scheme.Scheme,
})
as.Require().NoError(err)
var chart v1beta1.Chart
var chart helmv1beta1.Chart
as.Require().NoError(wait.PollImmediate(time.Second, 5*time.Minute, func() (done bool, err error) {
err = chartClient.Get(as.Context(), client.ObjectKey{
Namespace: "kube-system",
Expand Down Expand Up @@ -163,7 +215,7 @@ func (as *AddonsSuite) checkCustomValues(releaseName string) error {
}
return wait.PollImmediate(time.Second, 2*time.Minute, func() (done bool, err error) {
serverDeployment := fmt.Sprintf("%s-echo-server", releaseName)
d, err := kc.AppsV1().Deployments("default").Get(as.Context(), serverDeployment, v1.GetOptions{})
d, err := kc.AppsV1().Deployments("default").Get(as.Context(), serverDeployment, metav1.GetOptions{})
if err != nil {
return false, nil
}
Expand Down
6 changes: 0 additions & 6 deletions pkg/apis/k0s.k0sproject.io/v1beta1/extensions.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ package v1beta1

import (
"errors"
"fmt"
"time"

"helm.sh/helm/v3/pkg/chartutil"
Expand Down Expand Up @@ -99,11 +98,6 @@ type Chart struct {
Order int `json:"order"`
}

// ManifestFileName returns filename to use for the crd manifest
func (c Chart) ManifestFileName() string {
return fmt.Sprintf("%d_helm_extension_%s.yaml", c.Order, c.Name)
}

// Validate performs validation
func (c Chart) Validate() error {
if c.Name == "" {
Expand Down
26 changes: 0 additions & 26 deletions pkg/apis/k0s.k0sproject.io/v1beta1/extenstions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,30 +81,4 @@ func TestValidation(t *testing.T) {
})

})

t.Run("chart_manifest_name", func(t *testing.T) {
chart := Chart{
Name: "release",
ChartName: "k0s/chart",
TargetNS: "default",
}

chart1 := Chart{
Name: "release",
ChartName: "k0s/chart",
TargetNS: "default",
Order: 1,
}

chart2 := Chart{
Name: "release",
ChartName: "k0s/chart",
TargetNS: "default",
Order: 2,
}
assert.Equal(t, chart.ManifestFileName(), "0_helm_extension_release.yaml")
assert.Equal(t, chart1.ManifestFileName(), "1_helm_extension_release.yaml")
assert.Equal(t, chart2.ManifestFileName(), "2_helm_extension_release.yaml")
})

}
Loading

0 comments on commit 7437d1d

Please sign in to comment.