Skip to content

Commit

Permalink
set namespace ownership
Browse files Browse the repository at this point in the history
  • Loading branch information
jpinsonneau committed Sep 9, 2024
1 parent e8bfcb0 commit 30261af
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 8 deletions.
5 changes: 3 additions & 2 deletions controllers/monitoring/monitoring_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ func (r *Reconciler) Reconcile(ctx context.Context, _ ctrl.Request) (ctrl.Result

func (r *Reconciler) reconcile(ctx context.Context, clh *helper.Client, desired *flowslatest.FlowCollector) error {
log := log.FromContext(ctx)
cl := helper.NewClientHelper(desired, r.Client)
ns := helper.GetNamespace(&desired.Spec)
r.currentNamespace = ns

Expand All @@ -98,12 +99,12 @@ func (r *Reconciler) reconcile(ctx context.Context, clh *helper.Client, desired
}
desiredNs := buildNamespace(ns, r.mgr.Config.DownstreamDeployment)
if nsExist == nil {
err = r.Create(ctx, desiredNs)
err = cl.CreateOwned(ctx, desiredNs)
if err != nil {
return err
}
} else if !helper.IsSubSet(nsExist.ObjectMeta.Labels, desiredNs.ObjectMeta.Labels) {
err = r.Update(ctx, desiredNs)
err = cl.UpdateIfOwned(ctx, nsExist, desiredNs)
if err != nil {
return err
}
Expand Down
17 changes: 11 additions & 6 deletions pkg/helper/client_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,22 @@ func UnmanagedClient(cl client.Client) Client {
}
}

func NewFlowCollectorClientHelper(ctx context.Context, c client.Client) (*Client, *flowslatest.FlowCollector, error) {
fc, err := getFlowCollector(ctx, c)
if err != nil || fc == nil {
return nil, fc, err
}
func NewClientHelper(fc *flowslatest.FlowCollector, c client.Client) *Client {
return &Client{
Client: c,
SetControllerReference: func(obj client.Object) error {
return controllerutil.SetControllerReference(fc, obj, c.Scheme())
},
}, fc, nil
}
}

func NewFlowCollectorClientHelper(ctx context.Context, c client.Client) (*Client, *flowslatest.FlowCollector, error) {
fc, err := getFlowCollector(ctx, c)
if err != nil || fc == nil {
return nil, fc, err
}
client := NewClientHelper(fc, c)
return client, fc, nil
}

// CreateOwned is an helper function that creates an object, sets owner reference and writes info & errors logs
Expand Down

0 comments on commit 30261af

Please sign in to comment.