Skip to content

Commit

Permalink
Apply the bootstraprbac stack directly
Browse files Browse the repository at this point in the history
Don't write the manifests to disk, just use them directly in the Init
method as a standalone stack. Flag the stack name as ignored in the
applier manager.

Signed-off-by: Tom Wieczorek <[email protected]>
  • Loading branch information
twz123 committed Jan 31, 2025
1 parent 1c0f7e1 commit 7b1c25b
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 19 deletions.
5 changes: 3 additions & 2 deletions cmd/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,7 @@ func (c *command) start(ctx context.Context) error {
KubeClientFactory: adminClientFactory,
IgnoredStacks: []string{
controller.ClusterConfigStackName,
controller.SystemRBACStackName,
},
LeaderElector: leaderElector,
})
Expand Down Expand Up @@ -533,8 +534,8 @@ func (c *command) start(ctx context.Context) error {
clusterComponents.Add(ctx, reconciler)
}

if !slices.Contains(c.DisableComponents, constant.SystemRbacComponentName) {
clusterComponents.Add(ctx, controller.NewSystemRBAC(c.K0sVars.ManifestsDir))
if !slices.Contains(c.DisableComponents, constant.SystemRBACComponentName) {
clusterComponents.Add(ctx, &controller.SystemRBAC{Clients: adminClientFactory})
}

if !slices.Contains(c.DisableComponents, constant.NodeRoleComponentName) {
Expand Down
68 changes: 53 additions & 15 deletions pkg/component/controller/systemrbac.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,37 +17,75 @@ limitations under the License.
package controller

import (
"bytes"
"cmp"
"context"
_ "embed"
"path"
"path/filepath"
"fmt"

"github.com/k0sproject/k0s/internal/pkg/dir"
"github.com/k0sproject/k0s/internal/pkg/file"
"github.com/k0sproject/k0s/pkg/applier"
"github.com/k0sproject/k0s/pkg/component/manager"
"github.com/k0sproject/k0s/pkg/constant"
"github.com/k0sproject/k0s/pkg/kubernetes"

"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/cli-runtime/pkg/resource"

"github.com/avast/retry-go"
"github.com/sirupsen/logrus"
)

const SystemRBACStackName = "bootstraprbac"

// SystemRBAC implements system RBAC reconciler
type SystemRBAC struct {
manifestDir string
Clients kubernetes.ClientFactoryInterface
}

var _ manager.Component = (*SystemRBAC)(nil)

// NewSystemRBAC creates new system level RBAC reconciler
func NewSystemRBAC(manifestDir string) *SystemRBAC {
return &SystemRBAC{manifestDir}
}

// Writes the bootstrap RBAC manifests into the manifests folder.
func (s *SystemRBAC) Init(context.Context) error {
rbacDir := path.Join(s.manifestDir, "bootstraprbac")
if err := dir.Init(rbacDir, constant.ManifestsDirMode); err != nil {
// Applies the system RBAC manifests to the cluster.
func (s *SystemRBAC) Init(ctx context.Context) error {
infos, err := resource.NewLocalBuilder().
Unstructured().
Stream(bytes.NewReader(systemRBAC), SystemRBACStackName).
Flatten().
Do().
Infos()
if err != nil {
return err
}

return file.WriteContentAtomically(filepath.Join(rbacDir, "bootstrap-rbac.yaml"), systemRBAC, 0644)
resources := make([]*unstructured.Unstructured, len(infos))
for i := range infos {
resources[i] = infos[i].Object.(*unstructured.Unstructured)
}

var lastErr error
if err := retry.Do(
func() error {
stack := applier.Stack{
Name: SystemRBACStackName,
Resources: resources,
Clients: s.Clients,
}
lastErr := stack.Apply(ctx, true)
return lastErr
},
retry.Context(ctx),
retry.LastErrorOnly(true),
retry.OnRetry(func(attempt uint, err error) {
logrus.WithFields(logrus.Fields{
"component": constant.SystemRBACComponentName,
"stack": SystemRBACStackName,
"attempt": attempt + 1,
}).WithError(err).Debug("Failed to apply stack, retrying after backoff")
}),
); err != nil {
return fmt.Errorf("failed to apply system RBAC stack: %w", cmp.Or(lastErr, err))
}

return nil
}

func (s *SystemRBAC) Start(context.Context) error { return nil }
Expand Down
2 changes: 1 addition & 1 deletion pkg/config/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ var availableComponents = []string{
constant.MetricsServerComponentName,
constant.NetworkProviderComponentName,
constant.NodeRoleComponentName,
constant.SystemRbacComponentName,
constant.SystemRBACComponentName,
constant.WindowsNodeComponentName,
constant.WorkerConfigComponentName,
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/constant/constant.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ const (
WorkerConfigComponentName = "worker-config"
MetricsServerComponentName = "metrics-server"
NetworkProviderComponentName = "network-provider"
SystemRbacComponentName = "system-rbac"
SystemRBACComponentName = "system-rbac"
NodeRoleComponentName = "node-role"
WindowsNodeComponentName = "windows-node"
AutopilotComponentName = "autopilot"
Expand Down

0 comments on commit 7b1c25b

Please sign in to comment.