Skip to content

Commit

Permalink
fix: configure k0s with current user
Browse files Browse the repository at this point in the history
Signed-off-by: Rohan CJ <[email protected]>
  • Loading branch information
rohantmp committed Oct 11, 2024
1 parent c87f61e commit 6fac8b4
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
2 changes: 1 addition & 1 deletion pkg/controllers/deploy/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
24 changes: 22 additions & 2 deletions pkg/k0s/k0s.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"fmt"
"os"
"os/exec"
"os/user"
"path/filepath"
"strings"
"text/template"
Expand All @@ -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
Expand Down Expand Up @@ -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)
}
Expand All @@ -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
Expand All @@ -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
Expand Down

0 comments on commit 6fac8b4

Please sign in to comment.