Skip to content

Commit aa3eebf

Browse files
committed
Ensure MachineSet controller won't orphan resources
To ensure that the MachineSet controller won't orphan infrastructure config or bootstrap config resources as the result of an incomplete reconcile followed by cluster deletion, add an OwnerReference during creation of these resources by the MachineSet controller.
1 parent 1ec1e28 commit aa3eebf

File tree

2 files changed

+13
-3
lines changed

2 files changed

+13
-3
lines changed

controllers/external/util.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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.
4445
func 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)

controllers/machineset_controller.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,7 @@ func (r *MachineSetReconciler) syncReplicas(ms *clusterv1.MachineSet, machines [
259259
err error
260260
)
261261

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

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

0 commit comments

Comments
 (0)