Skip to content

Commit

Permalink
Inline manifestsSaver into the Windows stack component
Browse files Browse the repository at this point in the history
The manifestSaver abstraction was not serving any special purpose.
Inlining it makes the code more straightforward.

Signed-off-by: Tom Wieczorek <twieczorek@mirantis.com>
  • Loading branch information
twz123 committed Oct 2, 2024
1 parent f55ea19 commit 7d002a6
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 14 deletions.
6 changes: 1 addition & 5 deletions cmd/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -464,13 +464,9 @@ func (c *command) start(ctx context.Context) error {
if err != nil {
return fmt.Errorf("failed to create calico_init manifests saver: %w", err)
}
windowsStackSaver, err := controller.NewManifestsSaver("windows", c.K0sVars.DataDir)
if err != nil {
return fmt.Errorf("failed to create windows manifests saver: %w", err)
}
clusterComponents.Add(ctx, controller.NewCalico(c.K0sVars, calicoInitSaver, calicoSaver))
if !slices.Contains(c.DisableComponents, constant.WindowsNodeComponentName) {
clusterComponents.Add(ctx, controller.NewWindowsStackComponent(c.K0sVars, adminClientFactory, windowsStackSaver))
clusterComponents.Add(ctx, controller.NewWindowsStackComponent(c.K0sVars, adminClientFactory))
}
clusterComponents.Add(ctx, controller.NewKubeRouter(c.K0sVars))
}
Expand Down
23 changes: 14 additions & 9 deletions pkg/component/controller/windowsstackcomponent.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ import (
"strings"
"time"

"github.com/k0sproject/k0s/internal/pkg/dir"
"github.com/k0sproject/k0s/internal/pkg/file"
"github.com/k0sproject/k0s/internal/pkg/templatewriter"
"github.com/k0sproject/k0s/static"
"github.com/sirupsen/logrus"
Expand All @@ -46,7 +48,6 @@ type WindowsStackComponent struct {

kubeClientFactory k8sutil.ClientFactoryInterface
k0sVars *config.CfgVars
saver manifestsSaver
prevRenderingContext windowsStackRenderingContext
}

Expand All @@ -65,21 +66,21 @@ type windowsStackRenderingContext struct {
}

// NewWindowsStackComponent creates new WindowsStackComponent reconciler
func NewWindowsStackComponent(k0sVars *config.CfgVars, clientFactory k8sutil.ClientFactoryInterface, saver manifestsSaver) *WindowsStackComponent {
func NewWindowsStackComponent(k0sVars *config.CfgVars, clientFactory k8sutil.ClientFactoryInterface) *WindowsStackComponent {
return &WindowsStackComponent{
log: logrus.WithFields(logrus.Fields{"component": "WindowsNodeController"}),
saver: saver,
kubeClientFactory: clientFactory,
k0sVars: k0sVars,
}
}

// Init no-op
func (n *WindowsStackComponent) Init(_ context.Context) error {
return nil
// Init implements [manager.Component].
func (n *WindowsStackComponent) Init(context.Context) error {
return dir.Init(filepath.Join(n.k0sVars.ManifestsDir, "windows"), constant.ManifestsDirMode)
}

// Run checks and adds labels
// Start implements [manager.Component].
// Runs checks and adds labels.
func (n *WindowsStackComponent) Start(ctx context.Context) error {

go func() {
Expand Down Expand Up @@ -177,7 +178,7 @@ func (n *WindowsStackComponent) makeRenderingContext(cfg *v1beta1.ClusterConfig)
return config, nil
}

// Stop no-op
// Stop implements [manager.Component].
func (n *WindowsStackComponent) Stop() error {
return nil
}
Expand All @@ -189,6 +190,8 @@ func (n *WindowsStackComponent) createWindowsStack(newConfig windowsStackRenderi
if err != nil {
return fmt.Errorf("error retrieving manifests: %w", err)
}

targetDir := filepath.Join(n.k0sVars.ManifestsDir, "windows")
for _, entry := range manifestDirectories {
dir := entry.Name()
manifestPaths, err := fs.ReadDir(static.WindowsManifests, dir)
Expand Down Expand Up @@ -218,7 +221,9 @@ func (n *WindowsStackComponent) createWindowsStack(newConfig windowsStackRenderi
Data: newConfig,
}
tryAndLog(manifestName, tw.WriteToBuffer(output))
tryAndLog(manifestName, n.saver.Save(manifestName, output.Bytes()))
tryAndLog(manifestName, file.AtomicWithTarget(filepath.Join(targetDir, manifestName)).
WithPermissions(constant.CertMode).
Write(output.Bytes()))
}
}
return nil
Expand Down

0 comments on commit 7d002a6

Please sign in to comment.