-
Notifications
You must be signed in to change notification settings - Fork 1.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
✨Add support to MachinePool to reference template objects #4112
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,21 +23,21 @@ import ( | |
"strings" | ||
"time" | ||
|
||
"sigs.k8s.io/cluster-api/util" | ||
ctrl "sigs.k8s.io/controller-runtime" | ||
|
||
"github.com/pkg/errors" | ||
corev1 "k8s.io/api/core/v1" | ||
apierrors "k8s.io/apimachinery/pkg/api/errors" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" | ||
"k8s.io/utils/pointer" | ||
clusterv1 "sigs.k8s.io/cluster-api/api/v1alpha4" | ||
"sigs.k8s.io/cluster-api/controllers/external" | ||
capierrors "sigs.k8s.io/cluster-api/errors" | ||
expv1 "sigs.k8s.io/cluster-api/exp/api/v1alpha4" | ||
"sigs.k8s.io/cluster-api/util" | ||
"sigs.k8s.io/cluster-api/util/annotations" | ||
"sigs.k8s.io/cluster-api/util/conditions" | ||
"sigs.k8s.io/cluster-api/util/patch" | ||
ctrl "sigs.k8s.io/controller-runtime" | ||
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil" | ||
"sigs.k8s.io/controller-runtime/pkg/handler" | ||
"sigs.k8s.io/controller-runtime/pkg/source" | ||
|
@@ -108,6 +108,33 @@ func (r *MachinePoolReconciler) reconcileExternal(ctx context.Context, cluster * | |
return external.ReconcileOutput{Paused: true}, nil | ||
} | ||
|
||
if strings.HasSuffix(ref.Kind, external.TemplateSuffix) { | ||
owner := &metav1.OwnerReference{ | ||
APIVersion: expv1.GroupVersion.String(), | ||
Kind: "MachinePool", | ||
Name: m.Name, | ||
UID: m.UID, | ||
} | ||
ref, err = external.CloneTemplate(ctx, &external.CloneTemplateInput{ | ||
Client: r.Client, | ||
TemplateRef: ref, | ||
Namespace: m.Namespace, | ||
ClusterName: cluster.Name, | ||
OwnerRef: owner, | ||
}) | ||
Comment on lines
+118
to
+124
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. /hold Wouldn't this code clone the reference every time it reconciles? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In my point of view no because reference is replaced by concrete object reference, so in next reconciliation the code will do not pass inside if block There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. line 128 is getting the ref object and this object returned by function There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To clarify my earlier statement, the When I initially authored the cluster yamls for However much confusion I had previously, I feel better about about the distinction today since, as @CecileRobertMichon pointed out, I don't feel strongly one way or another. Just thought I'd share my early experiences in case it can provide some color. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What @devigned described was exactly what I went through as well. I don't have a use case for this yet, only those pains that @devigned pointed out. Perhaps we could put in the developer documentation how and when to use template objects. This is a little confusing for those who are starting to understand the project and looking at MachineDeployment and MachinePool There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Got it. In that case, how can we make this subtle distinction very explicit so it's not so confusing for newcomers? I don't think allowing templates and making MachinePool more like MachineDeployments is the answer here, but we should make it clear why and how they are different. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe pictures would tell the story our words have failed to tell. Documentation showing a visual representation comparing a @felipeweb do you think that would have helped you? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @devigned sure! |
||
if err != nil { | ||
return external.ReconcileOutput{}, errors.Wrap(err, "failed to clone template") | ||
} | ||
obj, err = external.Get(ctx, r.Client, ref, m.Namespace) | ||
if err != nil { | ||
if apierrors.IsNotFound(errors.Cause(err)) { | ||
return external.ReconcileOutput{}, errors.Wrapf(err, "could not find %v %q for MachinePool %q in namespace %q, requeuing", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should this be could not find the MachinePool template? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @fabriziopandini I may be wrong because I don't have a deep knowledge of the codebase of the cluster-api, but by my understanding the object to be reconciled must be the concrete object and not the template so I do Get after the clone and if I can't get the concrete object I can't continue the reconciliation |
||
ref.GroupVersionKind(), ref.Name, m.Name, m.Namespace) | ||
} | ||
return external.ReconcileOutput{}, err | ||
} | ||
} | ||
|
||
// Initialize the patch helper. | ||
patchHelper, err := patch.NewHelper(obj, r.Client) | ||
if err != nil { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Isn't the owner reference already set below?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't t think so
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Current line 118, there is a call that sets it
controllerutil.SetControllerReference(m, obj, r.Client.Scheme())
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I use this owner in line 123 passing owner as input in ColneTemplate func so i can't remove it.