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

Set the node as the owner reference of the enactmant #808

Merged
merged 1 commit into from
Aug 24, 2021
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 3 additions & 3 deletions api/v1beta1/nodenetworkconfigurationenactment_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,12 @@ type NodeNetworkConfigurationEnactment struct {
Status shared.NodeNetworkConfigurationEnactmentStatus `json:"status,omitempty"`
}

func NewEnactment(nodeName string, policy NodeNetworkConfigurationPolicy) NodeNetworkConfigurationEnactment {
func NewEnactment(node *corev1.Node, policy NodeNetworkConfigurationPolicy) NodeNetworkConfigurationEnactment {
enactment := NodeNetworkConfigurationEnactment{
ObjectMeta: metav1.ObjectMeta{
Name: shared.EnactmentKey(nodeName, policy.Name).Name,
Name: shared.EnactmentKey(node.Name, policy.Name).Name,
OwnerReferences: []metav1.OwnerReference{
{Name: policy.Name, Kind: policy.TypeMeta.Kind, APIVersion: policy.TypeMeta.APIVersion, UID: policy.UID},
{Name: node.Name, Kind: "Node", APIVersion: "v1", UID: node.UID},
},
// Associate policy with the enactment using labels
Labels: names.IncludeRelationshipLabels(map[string]string{shared.EnactmentPolicyLabel: policy.Name}),
Expand Down
41 changes: 41 additions & 0 deletions api/v1beta1/nodenetworkconfigurationenactment_types_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package v1beta1

import (
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"

corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

var _ = Describe("NodeNetworkEnactment", func() {
var (
nncp = NodeNetworkConfigurationPolicy{
TypeMeta: metav1.TypeMeta{
APIVersion: "nmstate.io/v1beta1",
Kind: "NodeNetworkConfigurationPolicy",
},
ObjectMeta: metav1.ObjectMeta{
Name: "policy1",
UID: "12345",
},
}
node = corev1.Node{
ObjectMeta: metav1.ObjectMeta{
Name: "node1",
UID: "54321",
},
}
)

Context("NewEnactment", func() {
It("should have the node as the owner reference of the created enactment", func() {
nnce := NewEnactment(&node, nncp)
desiredOnwerRefs := []metav1.OwnerReference{
{Name: node.Name, Kind: "Node", APIVersion: "v1", UID: node.UID},
}
Expect(nnce.OwnerReferences).To(Equal(desiredOnwerRefs))
})
})

})
33 changes: 19 additions & 14 deletions controllers/nodenetworkconfigurationpolicy_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,14 @@ import (
)

var (
nodeName string
nodeRunningUpdateRetryTime = 5 * time.Second
onCreateOrUpdateWithDifferentGeneration = predicate.Funcs{
nodeName string
nodeRunningUpdateRetryTime = 5 * time.Second
onCreateOrUpdateWithDifferentGenerationOrDelete = predicate.Funcs{
CreateFunc: func(createEvent event.CreateEvent) bool {
return true
},
DeleteFunc: func(deleteEvent event.DeleteEvent) bool {
return false
return true
},
UpdateFunc: func(updateEvent event.UpdateEvent) bool {
// [1] https://blog.openshift.com/kubernetes-operators-best-practices/
Expand Down Expand Up @@ -125,10 +125,9 @@ func (r *NodeNetworkConfigurationPolicyReconciler) Reconcile(ctx context.Context
err := r.Client.Get(context.TODO(), request.NamespacedName, instance)
if err != nil {
if apierrors.IsNotFound(err) {
// Request object not found, could have been deleted after reconcile request.
// Owned objects are automatically garbage collected. For additional cleanup logic use finalizers.
// Return and don't requeue
return ctrl.Result{}, nil
log.Info("Policy is not found, removing previous enactment if any")
err = r.deleteEnactmentForPolicy(request.NamespacedName.Name)
return ctrl.Result{}, err
}
log.Error(err, "Error retrieving policy")
// Error reading the object - requeue the request.
Expand All @@ -150,8 +149,8 @@ func (r *NodeNetworkConfigurationPolicyReconciler) Reconcile(ctx context.Context
}

if len(unmatchingNodeLabels) > 0 {
log.Info("Policy node selectors does not match node, removing previous enactments if any")
err = r.deleteEnactmentForPolicy(instance)
log.Info("Policy node selectors does not match node, removing previous enactment if any")
err = r.deleteEnactmentForPolicy(request.NamespacedName.Name)
return ctrl.Result{}, err
}

Expand Down Expand Up @@ -227,7 +226,7 @@ func (r *NodeNetworkConfigurationPolicyReconciler) SetupWithManager(mgr ctrl.Man
// Reconcile NNCP if they are created or updated
err := ctrl.NewControllerManagedBy(mgr).
For(&nmstatev1beta1.NodeNetworkConfigurationPolicy{}).
WithEventFilter(onCreateOrUpdateWithDifferentGeneration).
WithEventFilter(onCreateOrUpdateWithDifferentGenerationOrDelete).
Complete(r)
if err != nil {
return errors.Wrap(err, "failed to add controller to NNCP Reconciler listening NNCP events")
Expand Down Expand Up @@ -256,7 +255,13 @@ func (r *NodeNetworkConfigurationPolicyReconciler) initializeEnactment(policy nm
}
if err != nil && apierrors.IsNotFound(err) {
log.Info("creating enactment")
enactment = nmstatev1beta1.NewEnactment(nodeName, policy)
// Fetch the Node instance
nodeInstance := &corev1.Node{}
err = r.APIClient.Get(context.TODO(), types.NamespacedName{Name: nodeName}, nodeInstance)
if err != nil {
return nil, errors.Wrap(err, "failed getting node")
}
enactment = nmstatev1beta1.NewEnactment(nodeInstance, policy)
err = r.APIClient.Create(context.TODO(), &enactment)
if err != nil {
return nil, errors.Wrapf(err, "error creating NodeNetworkConfigurationEnactment: %+v", enactment)
Expand Down Expand Up @@ -294,8 +299,8 @@ func (r *NodeNetworkConfigurationPolicyReconciler) waitEnactmentCreated(enactmen
return pollErr
}

func (r *NodeNetworkConfigurationPolicyReconciler) deleteEnactmentForPolicy(policy *nmstatev1beta1.NodeNetworkConfigurationPolicy) error {
enactmentKey := nmstateapi.EnactmentKey(nodeName, policy.Name)
func (r *NodeNetworkConfigurationPolicyReconciler) deleteEnactmentForPolicy(policyName string) error {
enactmentKey := nmstateapi.EnactmentKey(nodeName, policyName)
enactment := nmstatev1beta1.NodeNetworkConfigurationEnactment{
ObjectMeta: metav1.ObjectMeta{
Name: enactmentKey.Name,
Expand Down
11 changes: 4 additions & 7 deletions controllers/nodenetworkconfigurationpolicy_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ var _ = Describe("NodeNetworkConfigurationPolicy controller predicates", func()
type predicateCase struct {
GenerationOld int64
GenerationNew int64
ReconcileCreate bool
ReconcileUpdate bool
}
DescribeTable("testing predicates",
Expand All @@ -40,34 +39,32 @@ var _ = Describe("NodeNetworkConfigurationPolicy controller predicates", func()
},
}

predicate := onCreateOrUpdateWithDifferentGeneration
predicate := onCreateOrUpdateWithDifferentGenerationOrDelete

Expect(predicate.
CreateFunc(event.CreateEvent{
Object: &newNNCP,
})).To(Equal(c.ReconcileCreate))
})).To(BeTrue())
Expect(predicate.
UpdateFunc(event.UpdateEvent{
ObjectOld: &oldNNCP,
ObjectNew: &newNNCP,
})).To(Equal(c.ReconcileUpdate))
Expect(predicate.
DeleteFunc(event.DeleteEvent{
Object: &newNNCP,
})).To(BeFalse())
Object: &oldNNCP,
})).To(BeTrue())
},
Entry("generation remains the same",
predicateCase{
GenerationOld: 1,
GenerationNew: 1,
ReconcileCreate: true,
ReconcileUpdate: false,
}),
Entry("generation is different",
predicateCase{
GenerationOld: 1,
GenerationNew: 2,
ReconcileCreate: true,
ReconcileUpdate: true,
}),
)
Expand Down