Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion controllers/external/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (

"github.com/pkg/errors"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"sigs.k8s.io/controller-runtime/pkg/client"
)
Expand All @@ -42,6 +43,12 @@ func Get(c client.Client, ref *corev1.ObjectReference, namespace string) (*unstr

// CloneTemplate uses the client and the reference to create a new object from the template.
func CloneTemplate(c client.Client, ref *corev1.ObjectReference, namespace string) (*unstructured.Unstructured, error) {
return CloneTemplateWithOwner(c, ref, namespace, nil)
}

// CloneTemplateWithOwner uses the client and the reference to create a new object, owned by the
// indicated resource, from the supplied template.
func CloneTemplateWithOwner(c client.Client, ref *corev1.ObjectReference, namespace string, owner *metav1.OwnerReference) (*unstructured.Unstructured, error) {
from, err := Get(c, ref, namespace)
if err != nil {
return nil, err
Expand All @@ -56,14 +63,17 @@ func CloneTemplate(c client.Client, ref *corev1.ObjectReference, namespace strin
// Create the unstructured object from the template.
to := &unstructured.Unstructured{Object: template}
to.SetResourceVersion("")
to.SetOwnerReferences(nil)
to.SetFinalizers(nil)
to.SetUID("")
to.SetSelfLink("")
to.SetName("")
to.SetGenerateName(fmt.Sprintf("%s-", from.GetName()))
to.SetNamespace(namespace)

if owner != nil {
to.SetOwnerReferences([]metav1.OwnerReference{*owner})
}

// Set the object APIVersion.
if to.GetAPIVersion() == "" {
to.SetAPIVersion(ref.APIVersion)
Expand Down
4 changes: 2 additions & 2 deletions controllers/machineset_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ func (r *MachineSetReconciler) syncReplicas(ms *clusterv1.MachineSet, machines [
err error
)

infraConfig, err = external.CloneTemplate(r.Client, &machine.Spec.InfrastructureRef, machine.Namespace)
infraConfig, err = external.CloneTemplateWithOwner(r.Client, &machine.Spec.InfrastructureRef, machine.Namespace, metav1.NewControllerRef(ms, machineSetKind))
if err != nil {
return errors.Wrapf(err, "failed to clone infrastructure configuration for MachineSet %q in namespace %q", ms.Name, ms.Namespace)
}
Expand All @@ -271,7 +271,7 @@ func (r *MachineSetReconciler) syncReplicas(ms *clusterv1.MachineSet, machines [
}

if machine.Spec.Bootstrap.ConfigRef != nil {
bootstrapConfig, err = external.CloneTemplate(r.Client, machine.Spec.Bootstrap.ConfigRef, machine.Namespace)
bootstrapConfig, err = external.CloneTemplateWithOwner(r.Client, machine.Spec.Bootstrap.ConfigRef, machine.Namespace, metav1.NewControllerRef(ms, machineSetKind))
if err != nil {
return errors.Wrapf(err, "failed to clone bootstrap configuration for MachineSet %q in namespace %q", ms.Name, ms.Namespace)
}
Expand Down