Skip to content

Commit

Permalink
Merge pull request #544 from alexander-demicev/indentation
Browse files Browse the repository at this point in the history
Add indentation to generated RKE2 config
  • Loading branch information
Danil-Grigorev authored Jan 10, 2025
2 parents 400f140 + 7268382 commit b9d98de
Show file tree
Hide file tree
Showing 4 changed files with 212 additions and 141 deletions.
1 change: 1 addition & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ linters-settings:
- "5"
- "4"
- "3"
- "2"
goimports:
local-prefixes: github.com/rancher/cluster-api-provider-rke2
gci:
Expand Down
40 changes: 29 additions & 11 deletions bootstrap/internal/controllers/rke2config_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,14 @@ limitations under the License.
package controllers

import (
"bytes"
"context"
"fmt"
"time"

"github.com/go-logr/logr"
"github.com/pkg/errors"
"gopkg.in/yaml.v3"
corev1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand Down Expand Up @@ -430,20 +432,26 @@ func (r *RKE2ConfigReconciler) handleClusterNotInitialized(ctx context.Context,
return ctrl.Result{}, err
}

b, err := kubeyaml.Marshal(configStruct)
var buf bytes.Buffer
yamlEncoder := yaml.NewEncoder(&buf)
yamlEncoder.SetIndent(2)

err = yamlEncoder.Encode(&configStruct)
if err != nil {
return ctrl.Result{}, err
return ctrl.Result{}, fmt.Errorf("unable to marshal config.yaml: %w", err)
}

scope.Logger.Info("Server config marshalled successfully")

initConfigFile := bootstrapv1.File{
Path: rke2.DefaultRKE2ConfigLocation,
Content: string(b),
Content: buf.String(),
Owner: consts.DefaultFileOwner,
Permissions: filePermissions,
}

yamlEncoder.Close()

files, err := r.generateFileListIncludingRegistries(ctx, scope, configFiles)
if err != nil {
return ctrl.Result{}, err
Expand Down Expand Up @@ -639,9 +647,14 @@ func (r *RKE2ConfigReconciler) joinControlplane(ctx context.Context, scope *Scop
return ctrl.Result{}, err
}

b, err := kubeyaml.Marshal(configStruct)
var buf bytes.Buffer
yamlEncoder := yaml.NewEncoder(&buf)
yamlEncoder.SetIndent(2)

scope.Logger.Info("Showing marshalled config.yaml", "config.yaml", string(b))
err = yamlEncoder.Encode(&configStruct)
if err != nil {
return ctrl.Result{}, fmt.Errorf("unable to marshal config.yaml: %w", err)
}

if err != nil {
return ctrl.Result{}, err
Expand All @@ -651,11 +664,13 @@ func (r *RKE2ConfigReconciler) joinControlplane(ctx context.Context, scope *Scop

initConfigFile := bootstrapv1.File{
Path: rke2.DefaultRKE2ConfigLocation,
Content: string(b),
Content: buf.String(),
Owner: consts.DefaultFileOwner,
Permissions: filePermissions,
}

yamlEncoder.Close()

files, err := r.generateFileListIncludingRegistries(ctx, scope, configFiles)
if err != nil {
return ctrl.Result{}, err
Expand Down Expand Up @@ -767,23 +782,26 @@ func (r *RKE2ConfigReconciler) joinWorker(ctx context.Context, scope *Scope) (re
return ctrl.Result{}, err
}

b, err := kubeyaml.Marshal(configStruct)

scope.Logger.V(5).Info("Showing marshalled config.yaml", "config.yaml", string(b))
var buf bytes.Buffer
yamlEncoder := yaml.NewEncoder(&buf)
yamlEncoder.SetIndent(2)

err = yamlEncoder.Encode(&configStruct)
if err != nil {
return ctrl.Result{}, err
return ctrl.Result{}, fmt.Errorf("unable to marshal config.yaml: %w", err)
}

scope.Logger.Info("Joining Worker config marshalled successfully")

wkJoinConfigFile := bootstrapv1.File{
Path: rke2.DefaultRKE2ConfigLocation,
Content: string(b),
Content: buf.String(),
Owner: consts.DefaultFileOwner,
Permissions: filePermissions,
}

yamlEncoder.Close()

files, err := r.generateFileListIncludingRegistries(ctx, scope, configFiles)
if err != nil {
return ctrl.Result{}, err
Expand Down
Loading

0 comments on commit b9d98de

Please sign in to comment.