Skip to content

Commit

Permalink
Merge pull request #2038 from zjs/topic/avoid-orphans
Browse files Browse the repository at this point in the history
🐛 Ensure MachineSet controller won't orphan resources
  • Loading branch information
k8s-ci-robot authored Jan 10, 2020
2 parents 1ec1e28 + aa3eebf commit e73d01a
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
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

0 comments on commit e73d01a

Please sign in to comment.