Skip to content

Commit

Permalink
Merge pull request #6927 from ykakarap/rename-clonetemplate
Browse files Browse the repository at this point in the history
🌱 rename CloneTemplate to CreateFromTemplate
  • Loading branch information
k8s-ci-robot authored Aug 3, 2022
2 parents a0f3da1 + 80ad385 commit 50d525e
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 8 deletions.
57 changes: 57 additions & 0 deletions controllers/external/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ func Delete(ctx context.Context, c client.Writer, ref *corev1.ObjectReference) e
}

// CloneTemplateInput is the input to CloneTemplate.
// Deprecated: use CreateFromTemplateInput instead. This type will be removed in a future release.
type CloneTemplateInput struct {
// Client is the controller runtime client.
Client client.Client
Expand Down Expand Up @@ -87,6 +88,7 @@ type CloneTemplateInput struct {
}

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

// CreateFromTemplateInput is the input to CreateFromTemplate.
type CreateFromTemplateInput struct {
// Client is the controller runtime client.
Client client.Client

// TemplateRef is a reference to the template that needs to be cloned.
TemplateRef *corev1.ObjectReference

// Namespace is the Kubernetes namespace the cloned object should be created into.
Namespace string

// ClusterName is the cluster this object is linked to.
ClusterName string

// OwnerRef is an optional OwnerReference to attach to the cloned object.
// +optional
OwnerRef *metav1.OwnerReference

// Labels is an optional map of labels to be added to the object.
// +optional
Labels map[string]string

// Annotations is an optional map of annotations to be added to the object.
// +optional
Annotations map[string]string
}

// CreateFromTemplate uses the client and the reference to create a new object from the template.
func CreateFromTemplate(ctx context.Context, in *CreateFromTemplateInput) (*corev1.ObjectReference, error) {
from, err := Get(ctx, in.Client, in.TemplateRef, in.Namespace)
if err != nil {
return nil, err
}
generateTemplateInput := &GenerateTemplateInput{
Template: from,
TemplateRef: in.TemplateRef,
Namespace: in.Namespace,
ClusterName: in.ClusterName,
OwnerRef: in.OwnerRef,
Labels: in.Labels,
Annotations: in.Annotations,
}
to, err := GenerateTemplate(generateTemplateInput)
if err != nil {
return nil, err
}

// Create the external clone.
if err := in.Client.Create(ctx, to); err != nil {
return nil, err
}

return GetObjectReference(to), nil
}

// GenerateTemplateInput is the input needed to generate a new template.
type GenerateTemplateInput struct {
// Template is the TemplateRef turned into an unstructured.
Expand Down
8 changes: 4 additions & 4 deletions controllers/external/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ func TestCloneTemplateResourceNotFound(t *testing.T) {
}

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

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

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

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

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

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

_, err := CloneTemplate(ctx, &CloneTemplateInput{
_, err := CreateFromTemplate(ctx, &CreateFromTemplateInput{
Client: fakeClient,
TemplateRef: templateRef,
Namespace: metav1.NamespaceDefault,
Expand Down
2 changes: 1 addition & 1 deletion controlplane/kubeadm/internal/controllers/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ func (r *KubeadmControlPlaneReconciler) cloneConfigsAndGenerateMachine(ctx conte
}

// Clone the infrastructure template
infraRef, err := external.CloneTemplate(ctx, &external.CloneTemplateInput{
infraRef, err := external.CreateFromTemplate(ctx, &external.CreateFromTemplateInput{
Client: r.Client,
TemplateRef: &kcp.Spec.MachineTemplate.InfrastructureRef,
Namespace: kcp.Namespace,
Expand Down
2 changes: 1 addition & 1 deletion docs/book/src/developer/providers/v1.2-to-v1.3.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ in Cluster API are kept in sync with the versions used by `sigs.k8s.io/controlle

### Deprecation

-
- `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.

### Removals

Expand Down
4 changes: 2 additions & 2 deletions internal/controllers/machineset/machineset_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,7 @@ func (r *Reconciler) syncReplicas(ctx context.Context, ms *clusterv1.MachineSet,
)

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

infraRef, err = external.CloneTemplate(ctx, &external.CloneTemplateInput{
infraRef, err = external.CreateFromTemplate(ctx, &external.CreateFromTemplateInput{
Client: r.Client,
TemplateRef: &machine.Spec.InfrastructureRef,
Namespace: machine.Namespace,
Expand Down

0 comments on commit 50d525e

Please sign in to comment.