Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use statically initialized runtime schemes #4537

Merged
merged 1 commit into from
Jun 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 1 addition & 6 deletions pkg/autopilot/controller/root_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ import (
aproot "github.com/k0sproject/k0s/pkg/autopilot/controller/root"
apsig "github.com/k0sproject/k0s/pkg/autopilot/controller/signal"
apupdate "github.com/k0sproject/k0s/pkg/autopilot/controller/updates"
apscheme "github.com/k0sproject/k0s/pkg/client/clientset/scheme"
"github.com/k0sproject/k0s/pkg/kubernetes"

"github.com/sirupsen/logrus"
Expand Down Expand Up @@ -148,6 +147,7 @@ func (c *rootController) Run(ctx context.Context) error {
// and starts it in a goroutine.
func (c *rootController) startSubControllerRoutine(ctx context.Context, logger *logrus.Entry, event LeaseEventStatus) error {
managerOpts := crman.Options{
Scheme: scheme,
WebhookServer: crwebhook.NewServer(crwebhook.Options{
Port: c.cfg.ManagerPort,
}),
Expand All @@ -163,11 +163,6 @@ func (c *rootController) startSubControllerRoutine(ctx context.Context, logger *
return err
}

if err := apscheme.AddToScheme(mgr.GetScheme()); err != nil {
logger.WithError(err).Error("unable to register autopilot scheme")
return err
}

if err := RegisterIndexers(ctx, mgr, "controller"); err != nil {
logger.WithError(err).Error("unable to register indexers")
return err
Expand Down
6 changes: 1 addition & 5 deletions pkg/autopilot/controller/root_worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import (
apdel "github.com/k0sproject/k0s/pkg/autopilot/controller/delegate"
aproot "github.com/k0sproject/k0s/pkg/autopilot/controller/root"
apsig "github.com/k0sproject/k0s/pkg/autopilot/controller/signal"
apscheme "github.com/k0sproject/k0s/pkg/client/clientset/scheme"

v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/wait"
Expand Down Expand Up @@ -60,6 +59,7 @@ func (w *rootWorker) Run(ctx context.Context) error {
logger := w.log

managerOpts := crman.Options{
Scheme: scheme,
WebhookServer: crwebhook.NewServer(crwebhook.Options{
Port: w.cfg.ManagerPort,
}),
Expand All @@ -85,10 +85,6 @@ func (w *rootWorker) Run(ctx context.Context) error {
logger.WithError(err).Fatal("unable to start controller manager")
}

if err := apscheme.AddToScheme(mgr.GetScheme()); err != nil {
logger.WithError(err).Fatal("unable to register autopilot scheme")
}

// In some cases, we need to wait on the worker side until controller deploys all autopilot CRDs
return k8sretry.OnError(wait.Backoff{
Steps: 120,
Expand Down
31 changes: 31 additions & 0 deletions pkg/autopilot/controller/scheme.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
Copyright 2024 k0s authors

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package controller

import (
k0sscheme "github.com/k0sproject/k0s/pkg/client/clientset/scheme"
"k8s.io/apimachinery/pkg/runtime"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
k8sscheme "k8s.io/client-go/kubernetes/scheme"
)

var scheme = runtime.NewScheme()

func init() {
utilruntime.Must(k8sscheme.AddToScheme(scheme))
utilruntime.Must(k0sscheme.AddToScheme(scheme))
}
10 changes: 4 additions & 6 deletions pkg/component/controller/extensions_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import (
helmapi "github.com/k0sproject/k0s/pkg/apis/helm"
"github.com/k0sproject/k0s/pkg/apis/helm/v1beta1"
k0sAPI "github.com/k0sproject/k0s/pkg/apis/k0s/v1beta1"
k0sscheme "github.com/k0sproject/k0s/pkg/client/clientset/scheme"
"github.com/k0sproject/k0s/pkg/component/controller/leaderelector"
"github.com/k0sproject/k0s/pkg/component/manager"
"github.com/k0sproject/k0s/pkg/config"
Expand All @@ -46,6 +47,7 @@ import (
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/tools/clientcmd"
apiretry "k8s.io/client-go/util/retry"
controllerruntime "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/builder"
"sigs.k8s.io/controller-runtime/pkg/client"
ctrlconfig "sigs.k8s.io/controller-runtime/pkg/config"
Expand Down Expand Up @@ -417,7 +419,8 @@ func (ec *ExtensionsController) Start(ctx context.Context) error {
Kind: "Chart",
}

mgr, err := crman.New(clientConfig, crman.Options{
mgr, err := controllerruntime.NewManager(clientConfig, crman.Options{
Scheme: k0sscheme.Scheme,
Metrics: metricsserver.Options{
BindAddress: "0",
},
Expand All @@ -438,11 +441,6 @@ func (ec *ExtensionsController) Start(ctx context.Context) error {
}, retry.Context(ctx)); err != nil {
return fmt.Errorf("can't start ExtensionsReconciler, helm CRD is not registred, check CRD registration reconciler: %w", err)
}
// examples say to not use GetScheme in production, but it is unclear at the moment
// which scheme should be in use
if err := v1beta1.AddToScheme(mgr.GetScheme()); err != nil {
return fmt.Errorf("can't register Chart crd: %w", err)
}

if err := builder.
ControllerManagedBy(mgr).
Expand Down
Loading