Skip to content

Commit

Permalink
set namespace ownership label (#763)
Browse files Browse the repository at this point in the history
  • Loading branch information
jpinsonneau authored Sep 20, 2024
1 parent e36e258 commit 0de6d49
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 2 deletions.
2 changes: 1 addition & 1 deletion controllers/monitoring/monitoring_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ func (r *Reconciler) reconcile(ctx context.Context, clh *helper.Client, desired
if err != nil {
return err
}
} else if !helper.IsSubSet(nsExist.ObjectMeta.Labels, desiredNs.ObjectMeta.Labels) {
} else if !helper.SkipOwnership(nsExist) && !helper.IsSubSet(nsExist.ObjectMeta.Labels, desiredNs.ObjectMeta.Labels) {
err = r.Update(ctx, desiredNs)
if err != nil {
return err
Expand Down
3 changes: 2 additions & 1 deletion pkg/helper/client_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ func (c *Client) CreateOwned(ctx context.Context, obj client.Object) error {
log.Error(err, "Failed to set controller reference")
return err
}
AddOwnedLabel(obj)
kind := reflect.TypeOf(obj).String()
log.Info("CREATING a new "+kind, "Namespace", obj.GetNamespace(), "Name", obj.GetName())
err = c.Create(ctx, obj)
Expand Down Expand Up @@ -86,7 +87,7 @@ func (c *Client) UpdateOwned(ctx context.Context, old, obj client.Object) error
return nil
}

// UpdateIfOwned is an helper function that updates an object if currently owned by the operator
// UpdateIfOwned is an helper function that updates an object if currently owned and managed by the operator
func (c *Client) UpdateIfOwned(ctx context.Context, old, obj client.Object) error {
log := log.FromContext(ctx)

Expand Down
23 changes: 23 additions & 0 deletions pkg/helper/flowcollector.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ import (
"sigs.k8s.io/controller-runtime/pkg/client"
)

const (
netobservManagedLabel = "netobserv-managed"
)

func GetSampling(spec *flowslatest.FlowCollectorSpec) int {
if spec.Agent.EBPF.Sampling == nil {
return 50
Expand Down Expand Up @@ -173,7 +177,26 @@ func PtrInt32(i *int32) int32 {
return *i
}

func AddOwnedLabel(obj client.Object) {
// set netobserv-managed label to true so users can easily switch to false if they want to skip ownership
labels := obj.GetLabels()
if labels == nil {
labels = make(map[string]string)
}
labels[netobservManagedLabel] = "true"
obj.SetLabels(labels)
}

func SkipOwnership(obj client.Object) bool {
// ownership is ignored if netobserv-managed label is explicitly set to false
labels := obj.GetLabels()
return labels != nil && labels[netobservManagedLabel] == "false"
}

func IsOwned(obj client.Object) bool {
if SkipOwnership(obj) {
return false
}
refs := obj.GetOwnerReferences()
return len(refs) > 0 && strings.HasPrefix(refs[0].APIVersion, flowslatest.GroupVersion.Group)
}
Expand Down

0 comments on commit 0de6d49

Please sign in to comment.