Skip to content

Commit

Permalink
Adopt Reconcile() method signature change
Browse files Browse the repository at this point in the history
Adopt MapOjbect removing change
Adopt event handling object changes
Adopt ToRequestsFunc to MapFunc change
Adopt EnqueueRequestsFromMapFunc change
Adopt runerable interface change
Adopt using admission review version v1

Signed-off-by: RainbowMango <renhongcai@huawei.com>
  • Loading branch information
RainbowMango committed May 8, 2021
1 parent 5d9b8a5 commit 28afae3
Show file tree
Hide file tree
Showing 19 changed files with 85 additions and 87 deletions.
14 changes: 7 additions & 7 deletions artifacts/deploy/webhook-configuration.yaml
Expand Up @@ -17,7 +17,7 @@ webhooks:
caBundle: {{caBundle}}
failurePolicy: Fail
sideEffects: None
admissionReviewVersions: ["v1beta1"]
admissionReviewVersions: ["v1"]
timeoutSeconds: 3
- name: clusterpropagationpolicy.karmada.io
rules:
Expand All @@ -31,7 +31,7 @@ webhooks:
caBundle: {{caBundle}}
failurePolicy: Fail
sideEffects: None
admissionReviewVersions: ["v1beta1"]
admissionReviewVersions: ["v1"]
timeoutSeconds: 3
- name: overridepolicy.karmada.io
rules:
Expand All @@ -45,7 +45,7 @@ webhooks:
caBundle: {{caBundle}}
failurePolicy: Fail
sideEffects: None
admissionReviewVersions: ["v1beta1"]
admissionReviewVersions: ["v1"]
timeoutSeconds: 3
- name: work.karmada.io
rules:
Expand All @@ -59,7 +59,7 @@ webhooks:
caBundle: {{caBundle}}
failurePolicy: Fail
sideEffects: None
admissionReviewVersions: ["v1beta1"]
admissionReviewVersions: ["v1"]
timeoutSeconds: 3
---
apiVersion: admissionregistration.k8s.io/v1
Expand All @@ -81,7 +81,7 @@ webhooks:
caBundle: {{caBundle}}
failurePolicy: Fail
sideEffects: None
admissionReviewVersions: ["v1beta1"]
admissionReviewVersions: ["v1"]
timeoutSeconds: 3
- name: propagationpolicy.karmada.io
rules:
Expand All @@ -95,7 +95,7 @@ webhooks:
caBundle: {{caBundle}}
failurePolicy: Fail
sideEffects: None
admissionReviewVersions: ["v1beta1"]
admissionReviewVersions: ["v1"]
timeoutSeconds: 3
- name: clusterpropagationpolicy.karmada.io
rules:
Expand All @@ -109,5 +109,5 @@ webhooks:
caBundle: {{caBundle}}
failurePolicy: Fail
sideEffects: None
admissionReviewVersions: ["v1beta1"]
admissionReviewVersions: ["v1"]
timeoutSeconds: 3
17 changes: 9 additions & 8 deletions cmd/agent/app/agent.go
@@ -1,6 +1,7 @@
package app

import (
"context"
"flag"
"fmt"
"os"
Expand All @@ -27,14 +28,14 @@ import (
)

// NewAgentCommand creates a *cobra.Command object with default parameters
func NewAgentCommand(stopChan <-chan struct{}) *cobra.Command {
func NewAgentCommand(ctx context.Context) *cobra.Command {
opts := options.NewOptions()

cmd := &cobra.Command{
Use: "karmada-agent",
Long: `The karmada agent runs the cluster registration agent`,
Run: func(cmd *cobra.Command, args []string) {
if err := run(opts, stopChan); err != nil {
if err := run(ctx, opts); err != nil {
fmt.Fprintf(os.Stderr, "%v\n", err)
os.Exit(1)
}
Expand All @@ -46,7 +47,7 @@ func NewAgentCommand(stopChan <-chan struct{}) *cobra.Command {
return cmd
}

func run(opts *options.Options, stopChan <-chan struct{}) error {
func run(ctx context.Context, opts *options.Options) error {
controlPlaneRestConfig, err := clientcmd.BuildConfigFromFlags("", opts.KarmadaKubeConfig)
if err != nil {
return fmt.Errorf("error building kubeconfig of karmada control plane: %s", err.Error())
Expand Down Expand Up @@ -74,10 +75,10 @@ func run(opts *options.Options, stopChan <-chan struct{}) error {
return err
}

setupControllers(controllerManager, opts, stopChan)
setupControllers(controllerManager, opts, ctx.Done())

// blocks until the stop channel is closed.
if err := controllerManager.Start(stopChan); err != nil {
if err := controllerManager.Start(ctx); err != nil {
klog.Errorf("controller manager exits unexpectedly: %v", err)
return err
}
Expand All @@ -88,13 +89,13 @@ func run(opts *options.Options, stopChan <-chan struct{}) error {
func setupControllers(mgr controllerruntime.Manager, opts *options.Options, stopChan <-chan struct{}) {
predicateFun := predicate.Funcs{
CreateFunc: func(createEvent event.CreateEvent) bool {
return createEvent.Meta.GetName() == opts.ClusterName
return createEvent.Object.GetName() == opts.ClusterName
},
UpdateFunc: func(updateEvent event.UpdateEvent) bool {
return updateEvent.MetaOld.GetName() == opts.ClusterName
return updateEvent.ObjectOld.GetName() == opts.ClusterName
},
DeleteFunc: func(deleteEvent event.DeleteEvent) bool {
return deleteEvent.Meta.GetName() == opts.ClusterName
return deleteEvent.Object.GetName() == opts.ClusterName
},
GenericFunc: func(genericEvent event.GenericEvent) bool {
return false
Expand Down
4 changes: 2 additions & 2 deletions cmd/agent/main.go
Expand Up @@ -12,9 +12,9 @@ func main() {
logs.InitLogs()
defer logs.FlushLogs()

stopChan := apiserver.SetupSignalHandler()
ctx := apiserver.SetupSignalContext()

if err := app.NewAgentCommand(stopChan).Execute(); err != nil {
if err := app.NewAgentCommand(ctx).Execute(); err != nil {
klog.Fatal(err.Error())
}
}
11 changes: 6 additions & 5 deletions cmd/controller-manager/app/controllermanager.go
@@ -1,6 +1,7 @@
package app

import (
"context"
"flag"
"fmt"
"os"
Expand Down Expand Up @@ -36,15 +37,15 @@ import (
)

// NewControllerManagerCommand creates a *cobra.Command object with default parameters
func NewControllerManagerCommand(stopChan <-chan struct{}) *cobra.Command {
func NewControllerManagerCommand(ctx context.Context) *cobra.Command {
opts := options.NewOptions()

cmd := &cobra.Command{
Use: "controller-manager",
Long: `The controller manager runs a bunch of controllers`,
Run: func(cmd *cobra.Command, args []string) {
opts.Complete()
if err := Run(opts, stopChan); err != nil {
if err := Run(ctx, opts); err != nil {
fmt.Fprintf(os.Stderr, "%v\n", err)
os.Exit(1)
}
Expand All @@ -57,7 +58,7 @@ func NewControllerManagerCommand(stopChan <-chan struct{}) *cobra.Command {
}

// Run runs the controller-manager with options. This should never exit.
func Run(opts *options.Options, stopChan <-chan struct{}) error {
func Run(ctx context.Context, opts *options.Options) error {
logs.InitLogs()
defer logs.FlushLogs()

Expand All @@ -82,10 +83,10 @@ func Run(opts *options.Options, stopChan <-chan struct{}) error {
return err
}

setupControllers(controllerManager, opts, stopChan)
setupControllers(controllerManager, opts, ctx.Done())

// blocks until the stop channel is closed.
if err := controllerManager.Start(stopChan); err != nil {
if err := controllerManager.Start(ctx); err != nil {
klog.Errorf("controller manager exits unexpectedly: %v", err)
return err
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/controller-manager/controller-manager.go
Expand Up @@ -15,9 +15,9 @@ func main() {
logs.InitLogs()
defer logs.FlushLogs()

stopChan := apiserver.SetupSignalHandler()
ctx := apiserver.SetupSignalContext()

if err := app.NewControllerManagerCommand(stopChan).Execute(); err != nil {
if err := app.NewControllerManagerCommand(ctx).Execute(); err != nil {
fmt.Fprintf(os.Stderr, "%v\n", err)
os.Exit(1)
}
Expand Down
9 changes: 5 additions & 4 deletions cmd/webhook/app/webhook.go
@@ -1,6 +1,7 @@
package app

import (
"context"
"flag"
"fmt"
"net/http"
Expand All @@ -23,15 +24,15 @@ import (
)

// NewWebhookCommand creates a *cobra.Command object with default parameters
func NewWebhookCommand(stopChan <-chan struct{}) *cobra.Command {
func NewWebhookCommand(ctx context.Context) *cobra.Command {
opts := options.NewOptions()

cmd := &cobra.Command{
Use: "webhook",
Long: `Start a webhook server`,
Run: func(cmd *cobra.Command, args []string) {
opts.Complete()
if err := Run(opts, stopChan); err != nil {
if err := Run(ctx, opts); err != nil {
fmt.Fprintf(os.Stderr, "%v\n", err)
os.Exit(1)
}
Expand All @@ -45,7 +46,7 @@ func NewWebhookCommand(stopChan <-chan struct{}) *cobra.Command {
}

// Run runs the webhook server with options. This should never exit.
func Run(opts *options.Options, stopChan <-chan struct{}) error {
func Run(ctx context.Context, opts *options.Options) error {
logs.InitLogs()
defer logs.FlushLogs()

Expand Down Expand Up @@ -78,7 +79,7 @@ func Run(opts *options.Options, stopChan <-chan struct{}) error {
hookServer.WebhookMux.Handle("/readyz/", http.StripPrefix("/readyz/", &healthz.Handler{}))

// blocks until the stop channel is closed.
if err := hookManager.Start(stopChan); err != nil {
if err := hookManager.Start(ctx); err != nil {
klog.Errorf("webhook server exits unexpectedly: %v", err)
return err
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/webhook/main.go
Expand Up @@ -14,9 +14,9 @@ func main() {
logs.InitLogs()
defer logs.FlushLogs()

stopChan := apiserver.SetupSignalHandler()
ctx := apiserver.SetupSignalContext()

if err := app.NewWebhookCommand(stopChan).Execute(); err != nil {
if err := app.NewWebhookCommand(ctx).Execute(); err != nil {
fmt.Fprintf(os.Stderr, "%v\n", err)
os.Exit(1)
}
Expand Down
32 changes: 16 additions & 16 deletions pkg/controllers/binding/binding_controller.go
Expand Up @@ -38,7 +38,7 @@ type ResourceBindingController struct {
// Reconcile performs a full reconciliation for the object referred to by the Request.
// The Controller will requeue the Request to be processed again if an error is non-nil or
// Result.Requeue is true, otherwise upon completion it will remove the work from the queue.
func (c *ResourceBindingController) Reconcile(req controllerruntime.Request) (controllerruntime.Result, error) {
func (c *ResourceBindingController) Reconcile(ctx context.Context, req controllerruntime.Request) (controllerruntime.Result, error) {
klog.V(4).Infof("Reconciling ResourceBinding %s.", req.NamespacedName.String())

binding := &workv1alpha1.ResourceBinding{}
Expand Down Expand Up @@ -109,11 +109,11 @@ func (c *ResourceBindingController) syncBinding(binding *workv1alpha1.ResourceBi

// SetupWithManager creates a controller and register to controller manager.
func (c *ResourceBindingController) SetupWithManager(mgr controllerruntime.Manager) error {
workFn := handler.ToRequestsFunc(
func(a handler.MapObject) []reconcile.Request {
workFn := handler.MapFunc(
func(a client.Object) []reconcile.Request {
var requests []reconcile.Request

labels := a.Meta.GetLabels()
labels := a.GetLabels()
resourcebindingNamespace, namespaceExist := labels[util.ResourceBindingNamespaceLabel]
resourcebindingName, nameExist := labels[util.ResourceBindingNameLabel]
if !namespaceExist || !nameExist {
Expand All @@ -129,17 +129,17 @@ func (c *ResourceBindingController) SetupWithManager(mgr controllerruntime.Manag
})

return controllerruntime.NewControllerManagedBy(mgr).For(&workv1alpha1.ResourceBinding{}).
Watches(&source.Kind{Type: &workv1alpha1.Work{}}, &handler.EnqueueRequestsFromMapFunc{ToRequests: workFn}, workPredicateFn).
Watches(&source.Kind{Type: &policyv1alpha1.OverridePolicy{}}, &handler.EnqueueRequestsFromMapFunc{ToRequests: c.newOverridePolicyFunc()}).
Watches(&source.Kind{Type: &policyv1alpha1.ClusterOverridePolicy{}}, &handler.EnqueueRequestsFromMapFunc{ToRequests: c.newOverridePolicyFunc()}).
Watches(&source.Kind{Type: &policyv1alpha1.ReplicaSchedulingPolicy{}}, &handler.EnqueueRequestsFromMapFunc{ToRequests: c.newReplicaSchedulingPolicyFunc()}).
Watches(&source.Kind{Type: &workv1alpha1.Work{}}, handler.EnqueueRequestsFromMapFunc(workFn), workPredicateFn).
Watches(&source.Kind{Type: &policyv1alpha1.OverridePolicy{}}, handler.EnqueueRequestsFromMapFunc(c.newOverridePolicyFunc())).
Watches(&source.Kind{Type: &policyv1alpha1.ClusterOverridePolicy{}}, handler.EnqueueRequestsFromMapFunc(c.newOverridePolicyFunc())).
Watches(&source.Kind{Type: &policyv1alpha1.ReplicaSchedulingPolicy{}}, handler.EnqueueRequestsFromMapFunc(c.newReplicaSchedulingPolicyFunc())).
Complete(c)
}

func (c *ResourceBindingController) newOverridePolicyFunc() handler.ToRequestsFunc {
return func(a handler.MapObject) []reconcile.Request {
func (c *ResourceBindingController) newOverridePolicyFunc() handler.MapFunc {
return func(a client.Object) []reconcile.Request {
var overrideRS []policyv1alpha1.ResourceSelector
switch t := a.Object.(type) {
switch t := a.(type) {
case *policyv1alpha1.ClusterOverridePolicy:
overrideRS = t.Spec.ResourceSelectors
case *policyv1alpha1.OverridePolicy:
Expand All @@ -164,7 +164,7 @@ func (c *ResourceBindingController) newOverridePolicyFunc() handler.ToRequestsFu

for _, rs := range overrideRS {
if util.ResourceMatches(workload, rs) {
klog.V(2).Infof("Enqueue ResourceBinding(%s/%s) as override policy(%s/%s) changes.", binding.Namespace, binding.Name, a.Meta.GetNamespace(), a.Meta.GetName())
klog.V(2).Infof("Enqueue ResourceBinding(%s/%s) as override policy(%s/%s) changes.", binding.Namespace, binding.Name, a.GetNamespace(), a.GetName())
requests = append(requests, reconcile.Request{NamespacedName: types.NamespacedName{Namespace: binding.Namespace, Name: binding.Name}})
break
}
Expand All @@ -174,9 +174,9 @@ func (c *ResourceBindingController) newOverridePolicyFunc() handler.ToRequestsFu
}
}

func (c *ResourceBindingController) newReplicaSchedulingPolicyFunc() handler.ToRequestsFunc {
return func(a handler.MapObject) []reconcile.Request {
rspResourceSelectors := a.Object.(*policyv1alpha1.ReplicaSchedulingPolicy).Spec.ResourceSelectors
func (c *ResourceBindingController) newReplicaSchedulingPolicyFunc() handler.MapFunc {
return func(a client.Object) []reconcile.Request {
rspResourceSelectors := a.(*policyv1alpha1.ReplicaSchedulingPolicy).Spec.ResourceSelectors
bindingList := &workv1alpha1.ResourceBindingList{}
if err := c.Client.List(context.TODO(), bindingList); err != nil {
klog.Errorf("Failed to list resourceBindings, error: %v", err)
Expand All @@ -193,7 +193,7 @@ func (c *ResourceBindingController) newReplicaSchedulingPolicyFunc() handler.ToR

for _, rs := range rspResourceSelectors {
if util.ResourceMatches(workload, rs) {
klog.V(2).Infof("Enqueue ResourceBinding(%s/%s) as replica scheduling policy(%s/%s) changes.", binding.Namespace, binding.Name, a.Meta.GetNamespace(), a.Meta.GetName())
klog.V(2).Infof("Enqueue ResourceBinding(%s/%s) as replica scheduling policy(%s/%s) changes.", binding.Namespace, binding.Name, a.GetNamespace(), a.GetName())
requests = append(requests, reconcile.Request{NamespacedName: types.NamespacedName{Namespace: binding.Namespace, Name: binding.Name}})
break
}
Expand Down

0 comments on commit 28afae3

Please sign in to comment.