From 6fac8b4f2a273e0913e0cf0baa77aef1202cbc94 Mon Sep 17 00:00:00 2001 From: Rohan CJ Date: Fri, 11 Oct 2024 07:51:48 +0530 Subject: [PATCH] fix: configure k0s with current user Signed-off-by: Rohan CJ --- pkg/controllers/deploy/deploy.go | 2 +- pkg/k0s/k0s.go | 24 ++++++++++++++++++++++-- 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/pkg/controllers/deploy/deploy.go b/pkg/controllers/deploy/deploy.go index d1a689ecd9..ad8270f287 100644 --- a/pkg/controllers/deploy/deploy.go +++ b/pkg/controllers/deploy/deploy.go @@ -184,7 +184,7 @@ func (r *Deployer) ProcessInitManifests(ctx context.Context, vConfig *config.Vir var err error manifests := vConfig.Experimental.Deploy.VCluster.Manifests if vConfig.Experimental.Deploy.VCluster.ManifestsTemplate != "" { - templatedManifests, err := k0s.ExecTemplate(vConfig.Experimental.Deploy.VCluster.ManifestsTemplate, vConfig.Name, vConfig.WorkloadTargetNamespace, &vConfig.Config) + templatedManifests, err := k0s.ExecTemplate(vConfig.Experimental.Deploy.VCluster.ManifestsTemplate, vConfig.Name, vConfig.WorkloadTargetNamespace, &vConfig.Config, map[string]interface{}{}) if err != nil { return false, fmt.Errorf("exec manifests template: %w", err) } diff --git a/pkg/k0s/k0s.go b/pkg/k0s/k0s.go index 1fb755402b..d2b58507c2 100644 --- a/pkg/k0s/k0s.go +++ b/pkg/k0s/k0s.go @@ -7,6 +7,7 @@ import ( "fmt" "os" "os/exec" + "os/user" "path/filepath" "strings" "text/template" @@ -26,6 +27,13 @@ kind: Cluster metadata: name: k0s spec: + installConfig: + users: + etcdUser: {{ .Values.user }} + kineUser: {{ .Values.user }} + konnectivityUser: {{ .Values.user }} + kubeAPIserverUser: {{ .Values.user }} + kubeSchedulerUser: {{ .Values.user }} api: port: 6443 k0sApiPort: 9443 @@ -161,8 +169,16 @@ func WriteK0sConfig( configTemplate = vConfig.Config.ControlPlane.Distro.K0S.Config } + // get user for k0s config + user, err := user.Current() + if err != nil { + return fmt.Errorf("failed to get current user: %w", err) + } + + extraValues := map[string]interface{}{"user": user} + // exec template - outBytes, err := ExecTemplate(configTemplate, vConfig.Name, "", &vConfig.Config) + outBytes, err := ExecTemplate(configTemplate, vConfig.Name, "", &vConfig.Config, extraValues) if err != nil { return fmt.Errorf("exec k0s config template: %w", err) } @@ -180,7 +196,7 @@ func WriteK0sConfig( return nil } -func ExecTemplate(templateContents string, name, namespace string, values *vclusterconfig.Config) ([]byte, error) { +func ExecTemplate(templateContents string, name, namespace string, values *vclusterconfig.Config, extraValues map[string]interface{}) ([]byte, error) { out, err := json.Marshal(values) if err != nil { return nil, err @@ -192,6 +208,10 @@ func ExecTemplate(templateContents string, name, namespace string, values *vclus return nil, err } + for key, value := range extraValues { + rawValues[key] = value + } + t, err := template.New("").Parse(templateContents) if err != nil { return nil, err