Skip to content

Commit 50d525e

Browse files
authored
Merge pull request #6927 from ykakarap/rename-clonetemplate
🌱 rename CloneTemplate to CreateFromTemplate
2 parents a0f3da1 + 80ad385 commit 50d525e

File tree

5 files changed

+65
-8
lines changed

5 files changed

+65
-8
lines changed

controllers/external/util.go

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ func Delete(ctx context.Context, c client.Writer, ref *corev1.ObjectReference) e
6060
}
6161

6262
// CloneTemplateInput is the input to CloneTemplate.
63+
// Deprecated: use CreateFromTemplateInput instead. This type will be removed in a future release.
6364
type CloneTemplateInput struct {
6465
// Client is the controller runtime client.
6566
Client client.Client
@@ -87,6 +88,7 @@ type CloneTemplateInput struct {
8788
}
8889

8990
// CloneTemplate uses the client and the reference to create a new object from the template.
91+
// Deprecated: use CreateFromTemplate instead. This function will be removed in a future release.
9092
func CloneTemplate(ctx context.Context, in *CloneTemplateInput) (*corev1.ObjectReference, error) {
9193
from, err := Get(ctx, in.Client, in.TemplateRef, in.Namespace)
9294
if err != nil {
@@ -114,6 +116,61 @@ func CloneTemplate(ctx context.Context, in *CloneTemplateInput) (*corev1.ObjectR
114116
return GetObjectReference(to), nil
115117
}
116118

119+
// CreateFromTemplateInput is the input to CreateFromTemplate.
120+
type CreateFromTemplateInput struct {
121+
// Client is the controller runtime client.
122+
Client client.Client
123+
124+
// TemplateRef is a reference to the template that needs to be cloned.
125+
TemplateRef *corev1.ObjectReference
126+
127+
// Namespace is the Kubernetes namespace the cloned object should be created into.
128+
Namespace string
129+
130+
// ClusterName is the cluster this object is linked to.
131+
ClusterName string
132+
133+
// OwnerRef is an optional OwnerReference to attach to the cloned object.
134+
// +optional
135+
OwnerRef *metav1.OwnerReference
136+
137+
// Labels is an optional map of labels to be added to the object.
138+
// +optional
139+
Labels map[string]string
140+
141+
// Annotations is an optional map of annotations to be added to the object.
142+
// +optional
143+
Annotations map[string]string
144+
}
145+
146+
// CreateFromTemplate uses the client and the reference to create a new object from the template.
147+
func CreateFromTemplate(ctx context.Context, in *CreateFromTemplateInput) (*corev1.ObjectReference, error) {
148+
from, err := Get(ctx, in.Client, in.TemplateRef, in.Namespace)
149+
if err != nil {
150+
return nil, err
151+
}
152+
generateTemplateInput := &GenerateTemplateInput{
153+
Template: from,
154+
TemplateRef: in.TemplateRef,
155+
Namespace: in.Namespace,
156+
ClusterName: in.ClusterName,
157+
OwnerRef: in.OwnerRef,
158+
Labels: in.Labels,
159+
Annotations: in.Annotations,
160+
}
161+
to, err := GenerateTemplate(generateTemplateInput)
162+
if err != nil {
163+
return nil, err
164+
}
165+
166+
// Create the external clone.
167+
if err := in.Client.Create(ctx, to); err != nil {
168+
return nil, err
169+
}
170+
171+
return GetObjectReference(to), nil
172+
}
173+
117174
// GenerateTemplateInput is the input needed to generate a new template.
118175
type GenerateTemplateInput struct {
119176
// Template is the TemplateRef turned into an unstructured.

controllers/external/util_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ func TestCloneTemplateResourceNotFound(t *testing.T) {
9797
}
9898

9999
fakeClient := fake.NewClientBuilder().Build()
100-
_, err := CloneTemplate(ctx, &CloneTemplateInput{
100+
_, err := CreateFromTemplate(ctx, &CreateFromTemplateInput{
101101
Client: fakeClient,
102102
TemplateRef: testResourceReference,
103103
Namespace: metav1.NamespaceDefault,
@@ -169,7 +169,7 @@ func TestCloneTemplateResourceFound(t *testing.T) {
169169

170170
fakeClient := fake.NewClientBuilder().WithObjects(template.DeepCopy()).Build()
171171

172-
ref, err := CloneTemplate(ctx, &CloneTemplateInput{
172+
ref, err := CreateFromTemplate(ctx, &CreateFromTemplateInput{
173173
Client: fakeClient,
174174
TemplateRef: templateRef.DeepCopy(),
175175
Namespace: metav1.NamespaceDefault,
@@ -261,7 +261,7 @@ func TestCloneTemplateResourceFoundNoOwner(t *testing.T) {
261261

262262
fakeClient := fake.NewClientBuilder().WithObjects(template.DeepCopy()).Build()
263263

264-
ref, err := CloneTemplate(ctx, &CloneTemplateInput{
264+
ref, err := CreateFromTemplate(ctx, &CreateFromTemplateInput{
265265
Client: fakeClient,
266266
TemplateRef: templateRef,
267267
Namespace: metav1.NamespaceDefault,
@@ -315,7 +315,7 @@ func TestCloneTemplateMissingSpecTemplate(t *testing.T) {
315315

316316
fakeClient := fake.NewClientBuilder().WithObjects(template.DeepCopy()).Build()
317317

318-
_, err := CloneTemplate(ctx, &CloneTemplateInput{
318+
_, err := CreateFromTemplate(ctx, &CreateFromTemplateInput{
319319
Client: fakeClient,
320320
TemplateRef: templateRef,
321321
Namespace: metav1.NamespaceDefault,

controlplane/kubeadm/internal/controllers/helpers.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ func (r *KubeadmControlPlaneReconciler) cloneConfigsAndGenerateMachine(ctx conte
167167
}
168168

169169
// Clone the infrastructure template
170-
infraRef, err := external.CloneTemplate(ctx, &external.CloneTemplateInput{
170+
infraRef, err := external.CreateFromTemplate(ctx, &external.CreateFromTemplateInput{
171171
Client: r.Client,
172172
TemplateRef: &kcp.Spec.MachineTemplate.InfrastructureRef,
173173
Namespace: kcp.Namespace,

docs/book/src/developer/providers/v1.2-to-v1.3.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ in Cluster API are kept in sync with the versions used by `sigs.k8s.io/controlle
1818

1919
### Deprecation
2020

21-
-
21+
- `sigs.k8s.io/cluster-api/controllers/external.CloneTemplate` has been deprecated and will be removed in a future release. Please use `sigs.k8s.io/cluster-api/controllers/external.CreateFromTemplate` instead.
2222

2323
### Removals
2424

internal/controllers/machineset/machineset_controller.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -378,7 +378,7 @@ func (r *Reconciler) syncReplicas(ctx context.Context, ms *clusterv1.MachineSet,
378378
)
379379

380380
if machine.Spec.Bootstrap.ConfigRef != nil {
381-
bootstrapRef, err = external.CloneTemplate(ctx, &external.CloneTemplateInput{
381+
bootstrapRef, err = external.CreateFromTemplate(ctx, &external.CreateFromTemplateInput{
382382
Client: r.Client,
383383
TemplateRef: machine.Spec.Bootstrap.ConfigRef,
384384
Namespace: machine.Namespace,
@@ -399,7 +399,7 @@ func (r *Reconciler) syncReplicas(ctx context.Context, ms *clusterv1.MachineSet,
399399
machine.Spec.Bootstrap.ConfigRef = bootstrapRef
400400
}
401401

402-
infraRef, err = external.CloneTemplate(ctx, &external.CloneTemplateInput{
402+
infraRef, err = external.CreateFromTemplate(ctx, &external.CreateFromTemplateInput{
403403
Client: r.Client,
404404
TemplateRef: &machine.Spec.InfrastructureRef,
405405
Namespace: machine.Namespace,

0 commit comments

Comments
 (0)