Skip to content

Commit

Permalink
Merge pull request #3374 from ncdc/no-gvk
Browse files Browse the repository at this point in the history
🌱 Only look at Group and Kind for GetOwnerCluster
  • Loading branch information
k8s-ci-robot authored Jul 21, 2020
2 parents 337b406 + 4f09531 commit 682ecea
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
9 changes: 8 additions & 1 deletion util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,14 @@ func GetClusterFromMetadata(ctx context.Context, c client.Client, obj metav1.Obj
// GetOwnerCluster returns the Cluster object owning the current resource.
func GetOwnerCluster(ctx context.Context, c client.Client, obj metav1.ObjectMeta) (*clusterv1.Cluster, error) {
for _, ref := range obj.OwnerReferences {
if ref.Kind == "Cluster" && ref.APIVersion == clusterv1.GroupVersion.String() {
if ref.Kind != "Cluster" {
continue
}
gv, err := schema.ParseGroupVersion(ref.APIVersion)
if err != nil {
return nil, errors.WithStack(err)
}
if gv.Group == clusterv1.GroupVersion.Group {
return GetClusterByName(ctx, c, obj.Namespace, ref.Name)
}
}
Expand Down
6 changes: 6 additions & 0 deletions util/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -454,6 +454,12 @@ func TestGetOwnerClusterSuccessByName(t *testing.T) {
cluster, err := GetOwnerCluster(context.TODO(), c, objm)
g.Expect(err).NotTo(HaveOccurred())
g.Expect(cluster).NotTo(BeNil())

// Make sure API version does not matter
objm.OwnerReferences[0].APIVersion = "cluster.x-k8s.io/v1alpha1234"
cluster, err = GetOwnerCluster(context.TODO(), c, objm)
g.Expect(err).NotTo(HaveOccurred())
g.Expect(cluster).NotTo(BeNil())
}

func TestGetOwnerMachineSuccessByName(t *testing.T) {
Expand Down

0 comments on commit 682ecea

Please sign in to comment.