Skip to content

Commit 4776e71

Browse files
committed
fix: make findOwners handle custom types better
This change stops the controller from bubbling up an error when it encounters a Kind that is has not registered in its scheme or an object that it does not have access to. In these situations, the controller will now stop climbing the ownership tree instead. The new behaviour is desireable when an unsupported type (such as one from a company's in-house operator) is marked as the controller of the resource that controls the pods themselves.
1 parent ea796df commit 4776e71

File tree

1 file changed

+10
-1
lines changed

1 file changed

+10
-1
lines changed

components/migrations.go

+10-1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import (
3030
kerrors "k8s.io/apimachinery/pkg/api/errors"
3131
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
3232
"k8s.io/apimachinery/pkg/labels"
33+
"k8s.io/apimachinery/pkg/runtime"
3334
"k8s.io/apimachinery/pkg/runtime/schema"
3435
"k8s.io/apimachinery/pkg/types"
3536
ctrl "sigs.k8s.io/controller-runtime"
@@ -311,11 +312,19 @@ func (_ *migrationsComponent) findOwners(ctx *cu.Context, obj cu.Object) ([]cu.O
311312
gvk := schema.FromAPIVersionAndKind(ref.APIVersion, ref.Kind)
312313
ownerObj, err := ctx.Scheme.New(gvk)
313314
if err != nil {
315+
// Gracefully handle kinds that we haven't registered. Useful when a Rollout or Deployment is
316+
// owned by someone's in-house operator
317+
if runtime.IsNotRegisteredError(err) {
318+
break
319+
}
314320
return nil, errors.Wrapf(err, "error finding object type for owner reference %v", ref)
315321
}
316322
err = ctx.Client.Get(ctx, types.NamespacedName{Name: ref.Name, Namespace: namespace}, ownerObj)
317323
if err != nil {
318-
// TODO IMPORTANT If this is a 403, don't bubble up the error. Probably a custom type we don't have access to, just pretend it's not there.
324+
// Gracefully handle objects we don't have access to
325+
if kerrors.IsForbidden(err) {
326+
break
327+
}
319328
return nil, errors.Wrapf(err, "error finding object type for owner reference %v", ref)
320329
}
321330
obj = ownerObj.(cu.Object)

0 commit comments

Comments
 (0)