sigs.k8s.io/cluster-api@v1.7.1/cmd/clusterctl/client/topology.go (about)

     1  /*
     2  Copyright 2022 The Kubernetes Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package client
    18  
    19  import (
    20  	"context"
    21  
    22  	"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
    23  
    24  	"sigs.k8s.io/cluster-api/cmd/clusterctl/client/cluster"
    25  )
    26  
    27  // TopologyPlanOptions define options for TopologyPlan.
    28  type TopologyPlanOptions struct {
    29  	// Kubeconfig defines the kubeconfig to use for accessing the management cluster. If empty,
    30  	// default rules for kubeconfig discovery will be used.
    31  	Kubeconfig Kubeconfig
    32  
    33  	// Objs is the list of objects that are input to the topology plan (dry run) operation.
    34  	// The objects can be among new/modified clusters, new/modifed ClusterClasses and new/modified templates.
    35  	Objs []*unstructured.Unstructured
    36  
    37  	// Cluster is the name of the cluster to dryrun reconcile if multiple clusters are affected by the input.
    38  	Cluster string
    39  
    40  	// Namespace is the target namespace for the operation.
    41  	// This namespace is used as default for objects with missing namespaces.
    42  	// If the namespace of any of the input objects conflicts with Namespace an error is returned.
    43  	Namespace string
    44  }
    45  
    46  // TopologyPlanOutput defines the output of the topology plan operation.
    47  type TopologyPlanOutput = cluster.TopologyPlanOutput
    48  
    49  // TopologyPlan performs a dry run execution of the topology reconciler using the given inputs.
    50  // It returns a summary of the changes observed during the execution.
    51  func (c *clusterctlClient) TopologyPlan(ctx context.Context, options TopologyPlanOptions) (*TopologyPlanOutput, error) {
    52  	clusterClient, err := c.clusterClientFactory(ClusterClientFactoryInput{Kubeconfig: options.Kubeconfig})
    53  	if err != nil {
    54  		return nil, err
    55  	}
    56  
    57  	out, err := clusterClient.Topology().Plan(ctx, &cluster.TopologyPlanInput{
    58  		Objs:              options.Objs,
    59  		TargetClusterName: options.Cluster,
    60  		TargetNamespace:   options.Namespace,
    61  	})
    62  
    63  	return out, err
    64  }