@@ -23,6 +23,7 @@ import (
2323
2424 "github.com/pkg/errors"
2525 corev1 "k8s.io/api/core/v1"
26+ metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2627 "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
2728 "sigs.k8s.io/controller-runtime/pkg/client"
2829)
@@ -42,6 +43,12 @@ func Get(c client.Client, ref *corev1.ObjectReference, namespace string) (*unstr
4243
4344// CloneTemplate uses the client and the reference to create a new object from the template.
4445func CloneTemplate (c client.Client , ref * corev1.ObjectReference , namespace string ) (* unstructured.Unstructured , error ) {
46+ return CloneTemplateWithOwner (c , ref , namespace , nil )
47+ }
48+
49+ // CloneTemplateWithOwner uses the client and the reference to create a new object, owned by the
50+ // indicated resource, from the supplied template.
51+ func CloneTemplateWithOwner (c client.Client , ref * corev1.ObjectReference , namespace string , owner * metav1.OwnerReference ) (* unstructured.Unstructured , error ) {
4552 from , err := Get (c , ref , namespace )
4653 if err != nil {
4754 return nil , err
@@ -56,14 +63,17 @@ func CloneTemplate(c client.Client, ref *corev1.ObjectReference, namespace strin
5663 // Create the unstructured object from the template.
5764 to := & unstructured.Unstructured {Object : template }
5865 to .SetResourceVersion ("" )
59- to .SetOwnerReferences (nil )
6066 to .SetFinalizers (nil )
6167 to .SetUID ("" )
6268 to .SetSelfLink ("" )
6369 to .SetName ("" )
6470 to .SetGenerateName (fmt .Sprintf ("%s-" , from .GetName ()))
6571 to .SetNamespace (namespace )
6672
73+ if owner != nil {
74+ to .SetOwnerReferences ([]metav1.OwnerReference {* owner })
75+ }
76+
6777 // Set the object APIVersion.
6878 if to .GetAPIVersion () == "" {
6979 to .SetAPIVersion (ref .APIVersion )
0 commit comments