Skip to content

Commit

Permalink
Adapt code to new operator-sdk and kaw versions
Browse files Browse the repository at this point in the history
After bumping operator-sdk to 1.4.2 the Reconcile function expects a
context.Context and the Meta at events is included in the client.Object
passed to it.

Signed-off-by: Quique Llorente <[email protected]>
  • Loading branch information
qinqon committed Mar 4, 2021
1 parent 8f0d00a commit dffed33
Show file tree
Hide file tree
Showing 23 changed files with 168 additions and 92 deletions.
2 changes: 1 addition & 1 deletion controllers/nmstate_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ type NMStateReconciler struct {
// +kubebuilder:rbac:groups=apps,resources=deployments;daemonsets;replicasets;statefulsets,verbs="*"
// +kubebuilder:rbac:groups="",resources=serviceaccounts;configmaps;namespaces;statefulsets,verbs="*"

func (r *NMStateReconciler) Reconcile(req ctrl.Request) (ctrl.Result, error) {
func (r *NMStateReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
_ = context.Background()
_ = r.Log.WithValues("nmstate", req.NamespacedName)

Expand Down
8 changes: 4 additions & 4 deletions controllers/nmstate_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ var _ = Describe("NMState controller reconcile", func() {
request.Name = "not-present-node"
})
It("should return empty result", func() {
result, err := reconciler.Reconcile(request)
result, err := reconciler.Reconcile(context.Background(), request)
Expect(err).ToNot(HaveOccurred())
Expect(result).To(Equal(ctrl.Result{}))
})
Expand All @@ -92,7 +92,7 @@ var _ = Describe("NMState controller reconcile", func() {
request.Name = existingNMStateName
})
It("should return a Result", func() {
result, err := reconciler.Reconcile(request)
result, err := reconciler.Reconcile(context.Background(), request)
Expect(err).ToNot(HaveOccurred())
Expect(result).To(Equal(ctrl.Result{}))
})
Expand All @@ -111,7 +111,7 @@ var _ = Describe("NMState controller reconcile", func() {
}
})
It("should return error", func() {
_, err := reconciler.Reconcile(request)
_, err := reconciler.Reconcile(context.Background(), request)
Expect(err).To(HaveOccurred())
})
})
Expand All @@ -131,7 +131,7 @@ var _ = Describe("NMState controller reconcile", func() {
cl = fake.NewFakeClientWithScheme(s, objs...)
reconciler.Client = cl
request.Name = existingNMStateName
result, err := reconciler.Reconcile(request)
result, err := reconciler.Reconcile(context.Background(), request)
Expect(err).ToNot(HaveOccurred())
Expect(result).To(Equal(ctrl.Result{}))
})
Expand Down
4 changes: 2 additions & 2 deletions controllers/node_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ type NodeReconciler struct {
// Note:
// The Controller will requeue the Request to be processed again if the returned error is non-nil or
// Result.Requeue is true, otherwise upon completion it will remove the work from the queue.
func (r *NodeReconciler) Reconcile(request ctrl.Request) (ctrl.Result, error) {
func (r *NodeReconciler) Reconcile(ctx context.Context, request ctrl.Request) (ctrl.Result, error) {
currentStateRaw, err := r.nmstatectlShow()
if err != nil {
// We cannot call nmstatectl show let's reconcile again
Expand Down Expand Up @@ -110,7 +110,7 @@ func (r *NodeReconciler) SetupWithManager(mgr ctrl.Manager) error {
// but we only want to watch create/delete for current node.
onCreationForThisNode := predicate.Funcs{
CreateFunc: func(createEvent event.CreateEvent) bool {
return nmstate.EventIsForThisNode(createEvent.Meta)
return nmstate.EventIsForThisNode(createEvent.Object)
},
DeleteFunc: func(event.DeleteEvent) bool {
return false
Expand Down
12 changes: 6 additions & 6 deletions controllers/node_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ routes:
}
})
It("should return the error from nmstatectl", func() {
_, err := reconciler.Reconcile(request)
_, err := reconciler.Reconcile(context.Background(), request)
Expect(err).To(MatchError("forced failure at unit test"))
})
})
Expand All @@ -110,7 +110,7 @@ routes:
}
})
It("should not call nmstateUpdater and return a Result with RequeueAfter set", func() {
result, err := reconciler.Reconcile(request)
result, err := reconciler.Reconcile(context.Background(), request)
Expect(err).ToNot(HaveOccurred())
expectRequeueAfterIsSetWithNetworkStateRefresh(result)
})
Expand All @@ -123,7 +123,7 @@ routes:
request.Name = "not-present-node"
})
It("should returns empty result", func() {
result, err := reconciler.Reconcile(request)
result, err := reconciler.Reconcile(context.Background(), request)
Expect(err).ToNot(HaveOccurred())
Expect(result).To(Equal(reconcile.Result{}))
})
Expand Down Expand Up @@ -162,7 +162,7 @@ routes:

})
It("should call nmstateUpdater and return a Result with RequeueAfter set (trigger re-reconciliation)", func() {
result, err := reconciler.Reconcile(request)
result, err := reconciler.Reconcile(context.Background(), request)
Expect(err).ToNot(HaveOccurred())
expectRequeueAfterIsSetWithNetworkStateRefresh(result)
obtainedNNS := nmstatev1beta1.NodeNetworkState{}
Expand All @@ -180,7 +180,7 @@ routes:
Expect(err).ToNot(HaveOccurred())
})
It("should create a new nodenetworkstate with node as owner reference, making sure the nodenetworkstate will be removed when the node is deleted", func() {
_, err := reconciler.Reconcile(request)
_, err := reconciler.Reconcile(context.Background(), request)
Expect(err).ToNot(HaveOccurred())

obtainedNNS := nmstatev1beta1.NodeNetworkState{}
Expand All @@ -194,7 +194,7 @@ routes:
))
})
It("should return a Result with RequeueAfter set (trigger re-reconciliation)", func() {
result, err := reconciler.Reconcile(request)
result, err := reconciler.Reconcile(context.Background(), request)
Expect(err).ToNot(HaveOccurred())
expectRequeueAfterIsSetWithNetworkStateRefresh(result)
})
Expand Down
14 changes: 7 additions & 7 deletions controllers/nodenetworkconfigurationpolicy_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ var (
},
UpdateFunc: func(updateEvent event.UpdateEvent) bool {
// [1] https://blog.openshift.com/kubernetes-operators-best-practices/
generationIsDifferent := updateEvent.MetaNew.GetGeneration() != updateEvent.MetaOld.GetGeneration()
generationIsDifferent := updateEvent.ObjectNew.GetGeneration() != updateEvent.ObjectOld.GetGeneration()
return generationIsDifferent
},
}
Expand All @@ -78,8 +78,8 @@ var (
return false
},
UpdateFunc: func(updateEvent event.UpdateEvent) bool {
labelsChanged := !reflect.DeepEqual(updateEvent.MetaOld.GetLabels(), updateEvent.MetaNew.GetLabels())
return labelsChanged && nmstate.EventIsForThisNode(updateEvent.MetaNew)
labelsChanged := !reflect.DeepEqual(updateEvent.ObjectOld.GetLabels(), updateEvent.ObjectNew.GetLabels())
return labelsChanged && nmstate.EventIsForThisNode(updateEvent.ObjectNew)
},
GenericFunc: func(event.GenericEvent) bool {
return false
Expand Down Expand Up @@ -199,7 +199,7 @@ func (r *NodeNetworkConfigurationPolicyReconciler) releaseNodeRunningUpdate(poli
// Note:
// The Controller will requeue the Request to be processed again if the returned error is non-nil or
// Result.Requeue is true, otherwise upon completion it will remove the work from the queue.
func (r *NodeNetworkConfigurationPolicyReconciler) Reconcile(request ctrl.Request) (ctrl.Result, error) {
func (r *NodeNetworkConfigurationPolicyReconciler) Reconcile(ctx context.Context, request ctrl.Request) (ctrl.Result, error) {
_ = context.Background()
log := r.Log.WithValues("nodenetworkconfigurationpolicy", request.NamespacedName)

Expand Down Expand Up @@ -288,8 +288,8 @@ func (r *NodeNetworkConfigurationPolicyReconciler) Reconcile(request ctrl.Reques

func (r *NodeNetworkConfigurationPolicyReconciler) SetupWithManager(mgr ctrl.Manager) error {

allPolicies := handler.ToRequestsFunc(
func(handler.MapObject) []reconcile.Request {
allPolicies := handler.MapFunc(
func(client.Object) []reconcile.Request {
log := r.Log.WithName("allPolicies")
allPoliciesAsRequest := []reconcile.Request{}
policyList := nmstatev1beta1.NodeNetworkConfigurationPolicyList{}
Expand Down Expand Up @@ -320,7 +320,7 @@ func (r *NodeNetworkConfigurationPolicyReconciler) SetupWithManager(mgr ctrl.Man
// is not needed since all NNCPs are going to be Reconcile at node startup.
err = ctrl.NewControllerManagedBy(mgr).
For(&corev1.Node{}).
Watches(&source.Kind{Type: &corev1.Node{}}, &handler.EnqueueRequestsFromMapFunc{ToRequests: allPolicies}, builder.WithPredicates(onLabelsUpdatedForThisNode)).
Watches(&source.Kind{Type: &corev1.Node{}}, handler.EnqueueRequestsFromMapFunc(allPolicies), builder.WithPredicates(onLabelsUpdatedForThisNode)).
Complete(r)
if err != nil {
return errors.Wrap(err, "failed to add controller to NNCP Reconciler listening Node events")
Expand Down
27 changes: 12 additions & 15 deletions controllers/nodenetworkconfigurationpolicy_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,34 +20,31 @@ var _ = Describe("NodeNetworkConfigurationPolicy controller predicates", func()
}
DescribeTable("testing predicates",
func(c predicateCase) {
oldNodeNetworkConfigurationPolicyMeta := metav1.ObjectMeta{
Generation: c.GenerationOld,
oldNNCP := nmstatev1beta1.NodeNetworkConfigurationPolicy{
ObjectMeta: metav1.ObjectMeta{
Generation: c.GenerationOld,
},
}

newNodeNetworkConfigurationPolicyMeta := metav1.ObjectMeta{
Generation: c.GenerationNew,
newNNCP := nmstatev1beta1.NodeNetworkConfigurationPolicy{
ObjectMeta: metav1.ObjectMeta{
Generation: c.GenerationNew,
},
}

nodeNetworkConfigurationPolicy := nmstatev1beta1.NodeNetworkConfigurationPolicy{}

predicate := onCreateOrUpdateWithDifferentGeneration

Expect(predicate.
CreateFunc(event.CreateEvent{
Meta: &newNodeNetworkConfigurationPolicyMeta,
Object: &nodeNetworkConfigurationPolicy,
Object: &newNNCP,
})).To(Equal(c.ReconcileCreate))
Expect(predicate.
UpdateFunc(event.UpdateEvent{
MetaOld: &oldNodeNetworkConfigurationPolicyMeta,
ObjectOld: &nodeNetworkConfigurationPolicy,
MetaNew: &newNodeNetworkConfigurationPolicyMeta,
ObjectNew: &nodeNetworkConfigurationPolicy,
ObjectOld: &oldNNCP,
ObjectNew: &newNNCP,
})).To(Equal(c.ReconcileUpdate))
Expect(predicate.
DeleteFunc(event.DeleteEvent{
Meta: &newNodeNetworkConfigurationPolicyMeta,
Object: &nodeNetworkConfigurationPolicy,
Object: &newNNCP,
})).To(BeFalse())
},
Entry("generation remains the same",
Expand Down
10 changes: 5 additions & 5 deletions controllers/nodenetworkstate_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ type NodeNetworkStateReconciler struct {
// Note:
// The Controller will requeue the Request to be processed again if the returned error is non-nil or
// Result.Requeue is true, otherwise upon completion it will remove the work from the queue.
func (r *NodeNetworkStateReconciler) Reconcile(request ctrl.Request) (ctrl.Result, error) {
func (r *NodeNetworkStateReconciler) Reconcile(ctx context.Context, request ctrl.Request) (ctrl.Result, error) {
// Fetch the Node instance
node := &corev1.Node{}
err := r.Client.Get(context.TODO(), request.NamespacedName, node)
Expand Down Expand Up @@ -94,10 +94,10 @@ func (r *NodeNetworkStateReconciler) SetupWithManager(mgr ctrl.Manager) error {
return false
},
DeleteFunc: func(deleteEvent event.DeleteEvent) bool {
return nmstate.EventIsForThisNode(deleteEvent.Meta)
return nmstate.EventIsForThisNode(deleteEvent.Object)
},
UpdateFunc: func(updateEvent event.UpdateEvent) bool {
return nmstate.EventIsForThisNode(updateEvent.MetaNew) &&
return nmstate.EventIsForThisNode(updateEvent.ObjectNew) &&
shouldForceRefresh(updateEvent)
},
GenericFunc: func(event.GenericEvent) bool {
Expand All @@ -112,11 +112,11 @@ func (r *NodeNetworkStateReconciler) SetupWithManager(mgr ctrl.Manager) error {
}

func shouldForceRefresh(updateEvent event.UpdateEvent) bool {
newForceRefresh, hasForceRefreshNow := updateEvent.MetaNew.GetLabels()[forceRefreshLabel]
newForceRefresh, hasForceRefreshNow := updateEvent.ObjectNew.GetLabels()[forceRefreshLabel]
if !hasForceRefreshNow {
return false
}
oldForceRefresh, hasForceRefreshLabelPreviously := updateEvent.MetaOld.GetLabels()[forceRefreshLabel]
oldForceRefresh, hasForceRefreshLabelPreviously := updateEvent.ObjectOld.GetLabels()[forceRefreshLabel]
if !hasForceRefreshLabelPreviously {
return true
}
Expand Down
16 changes: 12 additions & 4 deletions deploy/crds/nmstate.io_nmstates.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
annotations:
controller-gen.kubebuilder.io/version: v0.4.0
controller-gen.kubebuilder.io/version: v0.4.1
creationTimestamp: null
name: nmstates.nmstate.io
spec:
Expand All @@ -22,10 +22,14 @@ spec:
description: NMState is the Schema for the nmstates API
properties:
apiVersion:
description: 'APIVersion defines the versioned schema of this representation of an object. Servers should convert recognized schemas to the latest internal value, and may reject unrecognized values. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#resources'
description: 'APIVersion defines the versioned schema of this representation
of an object. Servers should convert recognized schemas to the latest
internal value, and may reject unrecognized values. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#resources'
type: string
kind:
description: 'Kind is a string value representing the REST resource this object represents. Servers may infer this from the endpoint the client submits requests to. Cannot be updated. In CamelCase. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kinds'
description: 'Kind is a string value representing the REST resource this
object represents. Servers may infer this from the endpoint the client
submits requests to. Cannot be updated. In CamelCase. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kinds'
type: string
metadata:
type: object
Expand All @@ -35,7 +39,11 @@ spec:
nodeSelector:
additionalProperties:
type: string
description: NodeSelector is an optional selector that will be added to handler DaemonSet manifest for both workers and masters (https://github.com/nmstate/kubernetes-nmstate/blob/master/deploy/handler/operator.yaml). If NodeSelector is specified, the handler will run only on nodes that have each of the indicated key-value pairs as labels applied to the node.
description: NodeSelector is an optional selector that will be added
to handler DaemonSet manifest for both workers and masters (https://github.com/nmstate/kubernetes-nmstate/blob/master/deploy/handler/operator.yaml).
If NodeSelector is specified, the handler will run only on nodes
that have each of the indicated key-value pairs as labels applied
to the node.
type: object
type: object
status:
Expand Down
Loading

0 comments on commit dffed33

Please sign in to comment.