Skip to content

Commit 2b4cdb8

Browse files
authored
Merge pull request #708 from ellistarn/nn
fix: Use controllerruntime's namespaced name correctly
2 parents e762514 + 65f6b11 commit 2b4cdb8

File tree

2 files changed

+12
-17
lines changed

2 files changed

+12
-17
lines changed

pkg/controller/instance/controller.go

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ package instance
1717
import (
1818
"context"
1919
"fmt"
20-
"strings"
2120
"time"
2221

2322
"github.com/go-logr/logr"
@@ -109,11 +108,9 @@ func NewController(
109108

110109
// Reconcile is a handler function that reconciles the instance and its sub-resources.
111110
func (c *Controller) Reconcile(ctx context.Context, req ctrl.Request) error {
112-
namespace, name := getNamespaceName(req)
111+
log := c.log.WithValues("namespace", req.Namespace, "name", req.Name)
113112

114-
log := c.log.WithValues("namespace", namespace, "name", name)
115-
116-
instance, err := c.clientSet.Dynamic().Resource(c.gvr).Namespace(namespace).Get(ctx, name, metav1.GetOptions{})
113+
instance, err := c.clientSet.Dynamic().Resource(c.gvr).Namespace(req.Namespace).Get(ctx, req.Name, metav1.GetOptions{})
117114
if err != nil {
118115
if apierrors.IsNotFound(err) {
119116
log.Info("Instance not found, it may have been deleted")
@@ -152,14 +149,3 @@ func (c *Controller) Reconcile(ctx context.Context, req ctrl.Request) error {
152149
}
153150
return instanceGraphReconciler.reconcile(ctx)
154151
}
155-
156-
// getNamespaceName extracts the namespace and name from the request.
157-
func getNamespaceName(req ctrl.Request) (string, string) {
158-
parts := strings.Split(req.Name, "/")
159-
name := parts[len(parts)-1]
160-
namespace := parts[0]
161-
if namespace == "" {
162-
namespace = metav1.NamespaceDefault
163-
}
164-
return namespace, name
165-
}

pkg/dynamiccontroller/dynamic_controller.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ package dynamiccontroller
5959
import (
6060
"context"
6161
"fmt"
62+
"strings"
6263
"sync"
6364
"time"
6465

@@ -333,7 +334,15 @@ func (dc *DynamicController) syncFunc(ctx context.Context, oi ObjectIdentifiers)
333334
if !ok {
334335
return fmt.Errorf("invalid handler type for GVR: %s", gvrKey)
335336
}
336-
err := handlerFunc(ctx, ctrl.Request{NamespacedName: types.NamespacedName{Name: oi.NamespacedKey}})
337+
338+
nn := types.NamespacedName{}
339+
if parts := strings.Split(oi.NamespacedKey, "/"); len(parts) == 1 {
340+
nn.Name = parts[0]
341+
} else {
342+
nn.Namespace = parts[0]
343+
nn.Name = parts[1]
344+
}
345+
err := handlerFunc(ctx, ctrl.Request{NamespacedName: nn})
337346
if err != nil {
338347
handlerErrorsTotal.WithLabelValues(gvrKey).Inc()
339348
}

0 commit comments

Comments
 (0)