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

update to controller-runtime 0.18 #1786

Merged
merged 1 commit into from
May 31, 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
17 changes: 1 addition & 16 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ require (
github.com/aws/smithy-go v1.20.1
github.com/davecgh/go-spew v1.1.1
github.com/digitalocean/godo v1.110.0
github.com/flatcar/container-linux-config-transpiler v0.9.4
github.com/go-logr/logr v1.4.1
github.com/go-logr/zapr v1.3.0
github.com/go-test/deep v1.1.0
Expand Down Expand Up @@ -61,13 +60,10 @@ require (
k8s.io/client-go v0.30.0
k8s.io/cloud-provider v0.30.0
k8s.io/klog v1.0.0
k8s.io/kubelet v0.30.0
k8s.io/utils v0.0.0-20240310230437-4693a0247e57
kubevirt.io/api v1.2.0
kubevirt.io/containerized-data-importer-api v1.58.1
// Pinned due to a breaking change in k8s.io/client-go/tools/leaderelection in v0.30.0
// TODO: Update to the latest semver version when https://github.com/kubernetes-sigs/controller-runtime/pull/2693 is released
sigs.k8s.io/controller-runtime v0.17.1-0.20240418082203-04706074d2f1
sigs.k8s.io/controller-runtime v0.18.2
sigs.k8s.io/yaml v1.4.0
)

Expand All @@ -87,8 +83,6 @@ require (
github.com/Masterminds/goutils v1.1.1 // indirect
github.com/PaesslerAG/gval v1.2.2 // indirect
github.com/PaesslerAG/jsonpath v0.1.1 // indirect
github.com/ajeddeloh/go-json v0.0.0-20200220154158-5ae607161559 // indirect
github.com/alecthomas/units v0.0.0-20231202071711-9a357b53e9c9 // indirect
github.com/araddon/dateparse v0.0.0-20210429162001-6b43995a97de // indirect
github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.15.3 // indirect
github.com/aws/aws-sdk-go-v2/internal/configsources v1.3.3 // indirect
Expand All @@ -99,18 +93,14 @@ require (
github.com/aws/aws-sdk-go-v2/service/sso v1.20.2 // indirect
github.com/aws/aws-sdk-go-v2/service/ssooidc v1.23.2 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/blang/semver/v4 v4.0.0 // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/coreos/go-semver v0.3.1 // indirect
github.com/coreos/go-systemd v0.0.0-20191104093116-d3cd4ed1dbcf // indirect
github.com/dimchansky/utfbom v1.1.1 // indirect
github.com/distribution/reference v0.5.0 // indirect
github.com/docker/distribution v2.8.3+incompatible // indirect
github.com/emicklei/go-restful/v3 v3.11.1 // indirect
github.com/evanphx/json-patch v5.7.0+incompatible // indirect
github.com/evanphx/json-patch/v5 v5.9.0 // indirect
github.com/felixge/httpsnoop v1.0.4 // indirect
github.com/flatcar/ignition v0.36.2 // indirect
github.com/fsnotify/fsnotify v1.7.0 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/go-openapi/jsonpointer v0.20.2 // indirect
Expand All @@ -135,7 +125,6 @@ require (
github.com/hashicorp/go-version v1.6.0 // indirect
github.com/huandu/xstrings v1.4.0 // indirect
github.com/imdario/mergo v0.3.16 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/jmespath/go-jmespath v0.4.0 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
Expand Down Expand Up @@ -163,8 +152,6 @@ require (
github.com/rogpeppe/go-internal v1.12.0 // indirect
github.com/shopspring/decimal v1.3.1 // indirect
github.com/spf13/cast v1.6.0 // indirect
github.com/spf13/cobra v1.8.0 // indirect
github.com/vincent-petithory/dataurl v1.0.0 // indirect
go.opencensus.io v0.24.0 // indirect
go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.49.0 // indirect
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.49.0 // indirect
Expand All @@ -173,7 +160,6 @@ require (
go.opentelemetry.io/otel/trace v1.24.0 // indirect
go.uber.org/atomic v1.11.0 // indirect
go.uber.org/multierr v1.11.0 // indirect
go4.org v0.0.0-20230225012048-214862532bf5 // indirect
golang.org/x/exp v0.0.0-20240103183307-be819d1f06fc // indirect
golang.org/x/net v0.23.0 // indirect
golang.org/x/sync v0.6.0 // indirect
Expand All @@ -192,7 +178,6 @@ require (
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/warnings.v0 v0.1.2 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
k8s.io/component-base v0.30.0 // indirect
k8s.io/klog/v2 v2.120.1 // indirect
k8s.io/kube-openapi v0.0.0-20240228011516-70dd3763d340 // indirect
kubevirt.io/controller-lifecycle-operator-sdk/api v0.2.4 // indirect
Expand Down
56 changes: 2 additions & 54 deletions go.sum

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion pkg/cloudprovider/provider/anexia/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,10 @@ import (
"github.com/kubermatic/machine-controller/pkg/cloudprovider/instance"
anxtypes "github.com/kubermatic/machine-controller/pkg/cloudprovider/provider/anexia/types"
cloudprovidertypes "github.com/kubermatic/machine-controller/pkg/cloudprovider/types"
cloudproviderutil "github.com/kubermatic/machine-controller/pkg/cloudprovider/util"
"github.com/kubermatic/machine-controller/pkg/providerconfig"
providerconfigtypes "github.com/kubermatic/machine-controller/pkg/providerconfig/types"

cloudproviderutil "github.com/kubermatic/machine-controller/pkg/cloudprovider/util"
"k8s.io/apimachinery/pkg/api/meta"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
Expand Down
1 change: 1 addition & 0 deletions pkg/cloudprovider/provider/edge/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package edge

import (
"context"

"go.uber.org/zap"

clusterv1alpha1 "github.com/kubermatic/machine-controller/pkg/apis/cluster/v1alpha1"
Expand Down
71 changes: 33 additions & 38 deletions pkg/controller/machine/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,14 @@ import (
"k8s.io/client-go/tools/reference"
"k8s.io/client-go/util/retry"
ccmapi "k8s.io/cloud-provider/api"
"sigs.k8s.io/controller-runtime/pkg/builder"
ctrlruntimeclient "sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller"
"sigs.k8s.io/controller-runtime/pkg/event"
"sigs.k8s.io/controller-runtime/pkg/handler"
"sigs.k8s.io/controller-runtime/pkg/manager"
"sigs.k8s.io/controller-runtime/pkg/predicate"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
"sigs.k8s.io/controller-runtime/pkg/source"
)

const (
Expand Down Expand Up @@ -209,50 +209,45 @@ func Add(
reconciler.metrics.Errors.Add(1)
})

c, err := controller.New(ControllerName, mgr, controller.Options{
Reconciler: reconciler,
MaxConcurrentReconciles: numWorkers,
LogConstructor: func(*reconcile.Request) logr.Logger {
// we log ourselves
return zapr.NewLogger(zap.NewNop())
},
})
if err != nil {
return err
}
if err := c.Watch(source.Kind(mgr.GetCache(), &clusterv1alpha1.Machine{}),
&handler.EnqueueRequestForObject{}); err != nil {
return err
}

metrics.Workers.Set(float64(numWorkers))

return c.Watch(
source.Kind(mgr.GetCache(), &corev1.Node{}),
enqueueRequestsForNodes(ctx, log, mgr),
predicate.Funcs{UpdateFunc: func(e event.UpdateEvent) bool {
oldNode := e.ObjectOld.(*corev1.Node)
newNode := e.ObjectNew.(*corev1.Node)
if newNode.ResourceVersion == oldNode.ResourceVersion {
return false
nodePredicate := predicate.Funcs{UpdateFunc: func(e event.UpdateEvent) bool {
oldNode := e.ObjectOld.(*corev1.Node)
newNode := e.ObjectNew.(*corev1.Node)
if newNode.ResourceVersion == oldNode.ResourceVersion {
return false
}
// Don't do anything if the ready condition hasn't changed
for _, newCondition := range newNode.Status.Conditions {
if newCondition.Type != corev1.NodeReady {
continue
}
// Don't do anything if the ready condition hasn't changed
for _, newCondition := range newNode.Status.Conditions {
if newCondition.Type != corev1.NodeReady {
for _, oldCondition := range oldNode.Status.Conditions {
if oldCondition.Type != corev1.NodeReady {
continue
}
for _, oldCondition := range oldNode.Status.Conditions {
if oldCondition.Type != corev1.NodeReady {
continue
}
if newCondition.Status == oldCondition.Status {
return false
}
if newCondition.Status == oldCondition.Status {
return false
}
}
return true
}},
)
}
return true
}}

_, err := builder.ControllerManagedBy(mgr).
Named(ControllerName).
WithOptions(controller.Options{
MaxConcurrentReconciles: numWorkers,
LogConstructor: func(*reconcile.Request) logr.Logger {
// we log ourselves
return zapr.NewLogger(zap.NewNop())
},
}).
For(&clusterv1alpha1.Machine{}).
Watches(&corev1.Node{}, enqueueRequestsForNodes(ctx, log, mgr), builder.WithPredicates(nodePredicate)).
Build(reconciler)

return err
}

func enqueueRequestsForNodes(ctx context.Context, log *zap.SugaredLogger, mgr manager.Manager) handler.EventHandler {
Expand Down
62 changes: 20 additions & 42 deletions pkg/controller/machinedeployment/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,12 @@ import (
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/tools/record"
ctrlruntime "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/controller"
"sigs.k8s.io/controller-runtime/pkg/handler"
"sigs.k8s.io/controller-runtime/pkg/manager"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
"sigs.k8s.io/controller-runtime/pkg/source"
)

// controllerName is the name of this controller.
Expand Down Expand Up @@ -77,47 +77,25 @@ func Add(mgr manager.Manager, log *zap.SugaredLogger) error {

// add adds a new Controller to mgr with r as the reconcile.Reconciler.
func add(mgr manager.Manager, r reconcile.Reconciler, mapFn handler.MapFunc) error {
// Create a new controller.
c, err := controller.New(controllerName, mgr, controller.Options{
Reconciler: r,
LogConstructor: func(*reconcile.Request) logr.Logger {
// we log ourselves
return zapr.NewLogger(zap.NewNop())
},
})
if err != nil {
return err
}

// Watch for changes to MachineDeployment.
err = c.Watch(source.Kind(mgr.GetCache(), &v1alpha1.MachineDeployment{}),
&handler.EnqueueRequestForObject{},
)
if err != nil {
return err
}

// Watch for changes to MachineSet and reconcile the owner MachineDeployment.
err = c.Watch(
source.Kind(mgr.GetCache(), &v1alpha1.MachineSet{}),
handler.EnqueueRequestForOwner(mgr.GetScheme(), mgr.GetRESTMapper(), &v1alpha1.MachineDeployment{}, handler.OnlyControllerOwner()),
)
if err != nil {
return err
}

// Watch for changes to MachineSets using a mapping function to MachineDeployment.
// This watcher is required for use cases like adoption. In case a MachineSet doesn't have
// a controller reference, it'll look for potential matching MachineDeployments to reconcile.
err = c.Watch(
source.Kind(mgr.GetCache(), &v1alpha1.MachineSet{}),
handler.EnqueueRequestsFromMapFunc(mapFn),
)
if err != nil {
return err
}

return nil
_, err := builder.ControllerManagedBy(mgr).
Named(controllerName).
WithOptions(controller.Options{
LogConstructor: func(*reconcile.Request) logr.Logger {
// we log ourselves
return zapr.NewLogger(zap.NewNop())
},
}).
// Watch for changes to MachineDeployment.
For(&v1alpha1.MachineDeployment{}).
// Watch for changes to MachineSet and reconcile the owner MachineDeployment.
Owns(&v1alpha1.MachineSet{}).
// Watch for changes to MachineSets using a mapping function to MachineDeployment.
// This watcher is required for use cases like adoption. In case a MachineSet doesn't have
// a controller reference, it'll look for potential matching MachineDeployments to reconcile.
Watches(&v1alpha1.MachineSet{}, handler.EnqueueRequestsFromMapFunc(mapFn)).
Build(r)

return err
}

// Reconcile reads that state of the cluster for a MachineDeployment object and makes changes based on the state read
Expand Down
58 changes: 20 additions & 38 deletions pkg/controller/machineset/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,12 @@ import (
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/client-go/tools/record"
ctrlruntime "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/controller"
"sigs.k8s.io/controller-runtime/pkg/handler"
"sigs.k8s.io/controller-runtime/pkg/manager"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
"sigs.k8s.io/controller-runtime/pkg/source"
)

// controllerName is the name of this controller.
Expand Down Expand Up @@ -80,43 +80,25 @@ func newReconciler(mgr manager.Manager, log *zap.SugaredLogger) *ReconcileMachin

// add adds a new Controller to mgr with r as the reconcile.Reconciler.
func add(mgr manager.Manager, r reconcile.Reconciler, mapFn handler.MapFunc) error {
// Create a new controller.
c, err := controller.New(controllerName, mgr, controller.Options{
Reconciler: r,
LogConstructor: func(*reconcile.Request) logr.Logger {
// we log ourselves
return zapr.NewLogger(zap.NewNop())
},
})
if err != nil {
return err
}

// Watch for changes to MachineSet.
err = c.Watch(
source.Kind(mgr.GetCache(), &clusterv1alpha1.MachineSet{}),
&handler.EnqueueRequestForObject{},
)
if err != nil {
return err
}

// Watch for changes to Machines and reconcile the owner MachineSet.
err = c.Watch(
source.Kind(mgr.GetCache(), &clusterv1alpha1.Machine{}),
handler.EnqueueRequestForOwner(mgr.GetScheme(), mgr.GetRESTMapper(), &clusterv1alpha1.MachineSet{}, handler.OnlyControllerOwner()),
)
if err != nil {
return err
}

// Watch for changes to Machines using a mapping function to MachineSets.
// This watcher is required for use cases like adoption. In case a Machine doesn't have
// a controller reference, it'll look for potential matching MachineSet to reconcile.
return c.Watch(
source.Kind(mgr.GetCache(), &clusterv1alpha1.Machine{}),
handler.EnqueueRequestsFromMapFunc(mapFn),
)
_, err := builder.ControllerManagedBy(mgr).
Named(controllerName).
WithOptions(controller.Options{
LogConstructor: func(*reconcile.Request) logr.Logger {
// we log ourselves
return zapr.NewLogger(zap.NewNop())
},
}).
// Watch for changes to MachineSet.
For(&clusterv1alpha1.MachineSet{}).
// Watch for changes to Machines and reconcile the owner MachineSet.
Owns(&clusterv1alpha1.Machine{}).
// Watch for changes to Machines using a mapping function to MachineSets.
// This watcher is required for use cases like adoption. In case a Machine doesn't have
// a controller reference, it'll look for potential matching MachineSet to reconcile.
Watches(&clusterv1alpha1.Machine{}, handler.EnqueueRequestsFromMapFunc(mapFn)).
Build(r)

return err
}

// ReconcileMachineSet reconciles a MachineSet object.
Expand Down
Loading