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

     1  /*
     2  Copyright 2020 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  	"sigs.k8s.io/cluster-api/cmd/clusterctl/client/tree"
    23  )
    24  
    25  // DescribeClusterOptions carries the options supported by DescribeCluster.
    26  type DescribeClusterOptions struct {
    27  	// Kubeconfig defines the kubeconfig to use for accessing the management cluster. If empty,
    28  	// default rules for kubeconfig discovery will be used.
    29  	Kubeconfig Kubeconfig
    30  
    31  	// Namespace where the workload cluster is located. If unspecified, the current namespace will be used.
    32  	Namespace string
    33  
    34  	// ClusterName to be used for the workload cluster.
    35  	ClusterName string
    36  
    37  	// ShowOtherConditions is a list of comma separated kind or kind/name for which we should add the ShowObjectConditionsAnnotation
    38  	// to signal to the presentation layer to show all the conditions for the objects.
    39  	ShowOtherConditions string
    40  
    41  	// ShowMachineSets instructs the discovery process to include machine sets in the ObjectTree.
    42  	ShowMachineSets bool
    43  
    44  	// ShowClusterResourceSets instructs the discovery process to include cluster resource sets in the ObjectTree.
    45  	ShowClusterResourceSets bool
    46  
    47  	// ShowTemplates instructs the discovery process to include infrastructure and bootstrap config templates in the ObjectTree.
    48  	ShowTemplates bool
    49  
    50  	// AddTemplateVirtualNode instructs the discovery process to group template under a virtual node.
    51  	AddTemplateVirtualNode bool
    52  
    53  	// Echo displays MachineInfrastructure or BootstrapConfig objects if the object's ready condition is true
    54  	// or it has the same Status, Severity and Reason of the parent's object ready condition (it is an echo)
    55  	Echo bool
    56  
    57  	// Grouping groups machines objects in case the ready conditions
    58  	// have the same Status, Severity and Reason.
    59  	Grouping bool
    60  }
    61  
    62  // DescribeCluster returns the object tree representing the status of a Cluster API cluster.
    63  func (c *clusterctlClient) DescribeCluster(ctx context.Context, options DescribeClusterOptions) (*tree.ObjectTree, error) {
    64  	// gets access to the management cluster
    65  	cluster, err := c.clusterClientFactory(ClusterClientFactoryInput{Kubeconfig: options.Kubeconfig})
    66  	if err != nil {
    67  		return nil, err
    68  	}
    69  
    70  	// Ensure this command only runs against management clusters with the current Cluster API contract.
    71  	if err := cluster.ProviderInventory().CheckCAPIContract(ctx); err != nil {
    72  		return nil, err
    73  	}
    74  
    75  	// If the option specifying the Namespace is empty, try to detect it.
    76  	if options.Namespace == "" {
    77  		currentNamespace, err := cluster.Proxy().CurrentNamespace()
    78  		if err != nil {
    79  			return nil, err
    80  		}
    81  		options.Namespace = currentNamespace
    82  	}
    83  
    84  	// Fetch the Cluster client.
    85  	client, err := cluster.Proxy().NewClient(ctx)
    86  	if err != nil {
    87  		return nil, err
    88  	}
    89  
    90  	// Gets the object tree representing the status of a Cluster API cluster.
    91  	return tree.Discovery(ctx, client, options.Namespace, options.ClusterName, tree.DiscoverOptions{
    92  		ShowOtherConditions:     options.ShowOtherConditions,
    93  		ShowMachineSets:         options.ShowMachineSets,
    94  		ShowClusterResourceSets: options.ShowClusterResourceSets,
    95  		ShowTemplates:           options.ShowTemplates,
    96  		AddTemplateVirtualNode:  options.AddTemplateVirtualNode,
    97  		Echo:                    options.Echo,
    98  		Grouping:                options.Grouping,
    99  	})
   100  }