Skip to content

Commit

Permalink
Merge pull request #4657 from twz123/backport-4601-to-release-1.27
Browse files Browse the repository at this point in the history
[Backport release-1.27] Use statically initialized runtime schemes
  • Loading branch information
twz123 authored Jun 19, 2024
2 parents 0aa9179 + 26918ac commit ab50185
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 17 deletions.
7 changes: 1 addition & 6 deletions pkg/autopilot/controller/root_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
"time"

apv1beta2 "github.com/k0sproject/k0s/pkg/apis/autopilot.k0sproject.io/v1beta2"
apscheme "github.com/k0sproject/k0s/pkg/apis/autopilot.k0sproject.io/v1beta2/clientset/scheme"
apcli "github.com/k0sproject/k0s/pkg/autopilot/client"
apconst "github.com/k0sproject/k0s/pkg/autopilot/constant"
apdel "github.com/k0sproject/k0s/pkg/autopilot/controller/delegate"
Expand Down Expand Up @@ -143,6 +142,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,
Port: c.cfg.ManagerPort,
MetricsBindAddress: c.cfg.MetricsBindAddr,
HealthProbeBindAddress: c.cfg.HealthProbeBindAddr,
Expand All @@ -154,11 +154,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 @@ -19,7 +19,6 @@ import (
"fmt"
"time"

apscheme "github.com/k0sproject/k0s/pkg/apis/autopilot.k0sproject.io/v1beta2/clientset/scheme"
apcli "github.com/k0sproject/k0s/pkg/autopilot/client"
apdel "github.com/k0sproject/k0s/pkg/autopilot/controller/delegate"
aproot "github.com/k0sproject/k0s/pkg/autopilot/controller/root"
Expand Down Expand Up @@ -58,6 +57,7 @@ func (w *rootWorker) Run(ctx context.Context) error {
logger := w.log

managerOpts := crman.Options{
Scheme: scheme,
Port: w.cfg.ManagerPort,
MetricsBindAddress: w.cfg.MetricsBindAddr,
HealthProbeBindAddress: w.cfg.HealthProbeBindAddr,
Expand All @@ -79,10 +79,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
33 changes: 33 additions & 0 deletions pkg/autopilot/controller/scheme.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
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 (
autopilotv1beta2sscheme "github.com/k0sproject/k0s/pkg/apis/autopilot.k0sproject.io/v1beta2/clientset/scheme"
k0sscheme "github.com/k0sproject/k0s/pkg/apis/k0s.k0sproject.io/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))
utilruntime.Must(autopilotv1beta2sscheme.AddToScheme(scheme))
}
15 changes: 9 additions & 6 deletions pkg/component/controller/extensions_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,12 @@ import (
"github.com/sirupsen/logrus"
"helm.sh/helm/v3/pkg/release"
"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"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"
"sigs.k8s.io/controller-runtime/pkg/config"
Expand Down Expand Up @@ -370,7 +372,13 @@ func (ec *ExtensionsController) Start(ctx context.Context) error {
Kind: "Chart",
}

mgr, err := ctrlManager.New(clientConfig, ctrlManager.Options{
scheme := runtime.NewScheme()
if err := v1beta1.AddToScheme(scheme); err != nil {
return err
}

mgr, err := controllerruntime.NewManager(clientConfig, ctrlManager.Options{
Scheme: scheme,
MetricsBindAddress: "0",
Logger: logrusr.New(ec.L),
Controller: config.Controller{},
Expand All @@ -389,11 +397,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

0 comments on commit ab50185

Please sign in to comment.