Skip to content

Commit e48b179

Browse files
committed
Add helper function for adding OwnerReferences to metav1.Objects
1 parent e205778 commit e48b179

File tree

4 files changed

+133
-0
lines changed

4 files changed

+133
-0
lines changed
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
Copyright 2018 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package controllerutil
18+
19+
import (
20+
"fmt"
21+
22+
"k8s.io/apimachinery/pkg/apis/meta/v1"
23+
"k8s.io/apimachinery/pkg/runtime"
24+
"k8s.io/apimachinery/pkg/runtime/schema"
25+
"sigs.k8s.io/controller-runtime/pkg/client/apiutil"
26+
)
27+
28+
// SetControllerReference sets owner as a Controller OwnerReference on owned.
29+
// This is used for garbage collection of the owned object and for
30+
// reconciling the owner object on changes to owned (with a Watch + EnqueueOwner).
31+
func SetControllerReference(owner, object v1.Object, scheme *runtime.Scheme) error {
32+
ro, ok := owner.(runtime.Object)
33+
if !ok {
34+
return fmt.Errorf("is not a %T a runtime.Object, cannot call SetControllerReference", owner)
35+
}
36+
37+
gvk, err := apiutil.GVKForObject(ro, scheme)
38+
if err != nil {
39+
return err
40+
}
41+
42+
// Create a new ref
43+
ref := *v1.NewControllerRef(owner, schema.GroupVersionKind{Group: gvk.Group, Version: gvk.Version, Kind: gvk.Kind})
44+
45+
// Add it to the child
46+
object.SetOwnerReferences(append(object.GetOwnerReferences(), ref))
47+
return nil
48+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package controllerutil_test
2+
3+
import (
4+
"testing"
5+
6+
. "github.com/onsi/ginkgo"
7+
. "github.com/onsi/gomega"
8+
)
9+
10+
func TestControllerutil(t *testing.T) {
11+
RegisterFailHandler(Fail)
12+
RunSpecs(t, "Controllerutil Suite")
13+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package controllerutil
2+
3+
import (
4+
. "github.com/onsi/ginkgo"
5+
. "github.com/onsi/gomega"
6+
appsv1 "k8s.io/api/apps/v1"
7+
extensionsv1beta1 "k8s.io/api/extensions/v1beta1"
8+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
9+
"k8s.io/apimachinery/pkg/runtime"
10+
"k8s.io/client-go/kubernetes/scheme"
11+
)
12+
13+
var _ = Describe("Controllerutil", func() {
14+
Describe("SetControllerReference", func() {
15+
It("should set the OwnerReference if it can find the group version kind", func() {
16+
rs := &appsv1.ReplicaSet{}
17+
dep := &extensionsv1beta1.Deployment{
18+
ObjectMeta: metav1.ObjectMeta{Name: "foo", UID: "foo-uid"},
19+
}
20+
21+
Expect(SetControllerReference(dep, rs, scheme.Scheme)).NotTo(HaveOccurred())
22+
t := true
23+
Expect(rs.OwnerReferences).To(ConsistOf(metav1.OwnerReference{
24+
Name: "foo",
25+
Kind: "Deployment",
26+
APIVersion: "extensions/v1beta1",
27+
UID: "foo-uid",
28+
Controller: &t,
29+
BlockOwnerDeletion: &t,
30+
}))
31+
})
32+
33+
It("should return an error if it can't find the group version kind of the owner", func() {
34+
rs := &appsv1.ReplicaSet{}
35+
dep := &extensionsv1beta1.Deployment{
36+
ObjectMeta: metav1.ObjectMeta{Name: "foo"},
37+
}
38+
Expect(SetControllerReference(dep, rs, runtime.NewScheme())).To(HaveOccurred())
39+
})
40+
41+
It("should return an error if the owner isn't a runtime.Object", func() {
42+
rs := &appsv1.ReplicaSet{}
43+
Expect(SetControllerReference(&errMetaObj{}, rs, scheme.Scheme)).To(HaveOccurred())
44+
})
45+
})
46+
})
47+
48+
var _ metav1.Object = &errMetaObj{}
49+
50+
type errMetaObj struct {
51+
metav1.ObjectMeta
52+
}

pkg/controller/controllerutil/doc.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/*
2+
Copyright 2018 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
/*
18+
Package controllerutil contains utility functions for working with and implementing Controllers.
19+
*/
20+
package controllerutil

0 commit comments

Comments
 (0)