Skip to content

Commit

Permalink
Simplify writing config files
Browse files Browse the repository at this point in the history
Signed-off-by: Atanas Dinov <[email protected]>
  • Loading branch information
atanasdinov committed Jan 25, 2024
1 parent 193be35 commit 2a61e49
Showing 1 changed file with 36 additions and 25 deletions.
61 changes: 36 additions & 25 deletions pkg/combustion/kubernetes.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,13 @@ import (
)

const (
k8sComponentName = "kubernetes"
k8sDir = "kubernetes"
k8sConfigDir = "config"
k8sServerConfigFile = "server.yaml"
k8sAgentConfigFile = "agent.yaml"
rke2InstallScript = "15-rke2-install.sh"
k8sComponentName = "kubernetes"
k8sDir = "kubernetes"
k8sConfigDir = "config"
k8sInitServerConfigFIle = "init_server.yaml"
k8sServerConfigFile = "server.yaml"
k8sAgentConfigFile = "agent.yaml"
rke2InstallScript = "15-rke2-install.sh"
)

var (
Expand Down Expand Up @@ -106,8 +107,8 @@ func configureRKE2(ctx *image.Context) (string, error) {
return "", fmt.Errorf("initialising kubernetes cluster config: %w", err)
}

if err = storeKubernetesConfig(ctx, cluster.ServerConfig, k8sServerConfigFile); err != nil {
return "", fmt.Errorf("storing RKE2 server config file: %w", err)
if err = storeKubernetesClusterConfig(cluster, ctx.CombustionDir); err != nil {
return "", fmt.Errorf("storing RKE2 cluster config: %w", err)
}

installPath, imagesPath, err := downloadRKE2Artefacts(ctx, cluster)
Expand Down Expand Up @@ -138,23 +139,14 @@ func configureRKE2(ctx *image.Context) (string, error) {
return storeRKE2Installer(ctx, "single-node-rke2", rke2SingleNodeInstaller, templateValues)
}

if err = storeKubernetesConfig(ctx, cluster.AgentConfig, k8sAgentConfigFile); err != nil {
return "", fmt.Errorf("storing RKE2 agent config file: %w", err)
}

initialiserConfigFile := fmt.Sprintf("init_%s", k8sServerConfigFile)
if err = storeKubernetesConfig(ctx, cluster.InitialiserConfig, initialiserConfigFile); err != nil {
return "", fmt.Errorf("storing RKE2 initialising server config file: %w", err)
}

vipManifest, err := storeRKE2VIPManifest(ctx)
if err != nil {
return "", fmt.Errorf("storing RKE2 VIP manifest: %w", err)
}

templateValues["nodes"] = ctx.ImageDefinition.Kubernetes.Nodes
templateValues["initialiser"] = cluster.Initialiser
templateValues["initialiserConfigFile"] = initialiserConfigFile
templateValues["initialiserConfigFile"] = k8sInitServerConfigFIle
templateValues["vipManifest"] = vipManifest

return storeRKE2Installer(ctx, "multi-node-rke2", rke2MultiNodeInstaller, templateValues)
Expand Down Expand Up @@ -211,17 +203,36 @@ func storeRKE2VIPManifest(ctx *image.Context) (string, error) {
return vipManifest, nil
}

func storeKubernetesConfig(ctx *image.Context, config map[string]any, filename string) error {
data, err := yaml.Marshal(config)
if err != nil {
return fmt.Errorf("serializing kubernetes config: %w", err)
func storeKubernetesClusterConfig(cluster *kubernetes.Cluster, destPath string) error {
serverConfig := filepath.Join(destPath, k8sServerConfigFile)
if err := storeKubernetesConfig(cluster.ServerConfig, serverConfig); err != nil {
return fmt.Errorf("storing kubernetes server config file: %w", err)
}

configPath := filepath.Join(ctx.CombustionDir, filename)
if cluster.InitialiserConfig != nil {
initialiserConfig := filepath.Join(destPath, k8sInitServerConfigFIle)

if err = os.WriteFile(configPath, data, fileio.NonExecutablePerms); err != nil {
return fmt.Errorf("storing kubernetes config file: %w", err)
if err := storeKubernetesConfig(cluster.InitialiserConfig, initialiserConfig); err != nil {
return fmt.Errorf("storing kubernetes init server config file: %w", err)
}
}

if cluster.AgentConfig != nil {
agentConfig := filepath.Join(destPath, k8sAgentConfigFile)

if err := storeKubernetesConfig(cluster.AgentConfig, agentConfig); err != nil {
return fmt.Errorf("storing kubernetes agent config file: %w", err)
}
}

return nil
}

func storeKubernetesConfig(config map[string]any, configPath string) error {
data, err := yaml.Marshal(config)
if err != nil {
return fmt.Errorf("serializing kubernetes config: %w", err)
}

return os.WriteFile(configPath, data, fileio.NonExecutablePerms)
}

0 comments on commit 2a61e49

Please sign in to comment.