Skip to content

Commit

Permalink
Allow disabling injection of cluster config into HelmCharts
Browse files Browse the repository at this point in the history
Signed-off-by: Brad Davidson <[email protected]>
  • Loading branch information
brandond committed May 25, 2024
1 parent 1dcd76d commit 9cd9c02
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
1 change: 1 addition & 0 deletions charts/build-chart.sh
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ metadata:
namespace: "${CHART_NAMESPACE:="kube-system"}"
annotations:
helm.cattle.io/chart-url: "${CHART_URL}"
rke2.cattle.io/inject-cluster-config: "true"
spec:
bootstrap: ${CHART_BOOTSTRAP:=false}
chartContent: $(base64 -w0 < "${CHART_TMP}")
Expand Down
31 changes: 29 additions & 2 deletions pkg/bootstrap/bootstrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"path/filepath"
"regexp"
"runtime"
"strconv"
"strings"

"github.com/google/go-containerregistry/pkg/authn"
Expand All @@ -20,6 +21,7 @@ import (
"github.com/k3s-io/k3s/pkg/daemons/agent"
daemonconfig "github.com/k3s-io/k3s/pkg/daemons/config"
"github.com/k3s-io/k3s/pkg/util"
"github.com/k3s-io/k3s/pkg/version"
"github.com/pkg/errors"
"github.com/rancher/rke2/pkg/images"
"github.com/rancher/wharfie/pkg/credentialprovider/plugin"
Expand All @@ -33,8 +35,11 @@ import (
)

var (
releasePattern = regexp.MustCompile("^v[0-9]")
helmChartGVK = helmv1.SchemeGroupVersion.WithKind("HelmChart")
releasePattern = regexp.MustCompile("^v[0-9]")
helmChartGVK = helmv1.SchemeGroupVersion.WithKind("HelmChart")
injectAnnotationKey = version.Program + ".cattle.io/inject-cluster-config"
injectEnvKey = version.ProgramUpper + "_INJECT_CLUSTER_CONFIG"
injectDefault = true
)

// binDirForDigest returns the path to dataDir/data/refDigest/bin.
Expand Down Expand Up @@ -373,6 +378,11 @@ OBJECTS:
continue
}

// Ignore object if injection is disabled via annotation or default setting
if !isInjectEnabled(unst) {
continue
}

var contentChanged bool
content := unst.UnstructuredContent()

Expand Down Expand Up @@ -429,3 +439,20 @@ OBJECTS:
logrus.Infof("Updated manifest %s to set cluster configuration values", fileName)
return nil
}

func isInjectEnabled(obj *unstructured.Unstructured) bool {
annotations := obj.GetAnnotations()
if v, ok := annotations[injectAnnotationKey]; ok {
if b, err := strconv.ParseBool(v); err == nil {
return b
}
}
return getInjectDefault()
}

func getInjectDefault() bool {
if b, err := strconv.ParseBool(os.Getenv(injectEnvKey)); err == nil {
return b
}
return injectDefault
}

0 comments on commit 9cd9c02

Please sign in to comment.