Skip to content

Commit

Permalink
Merge pull request #3942 from fabriziopandini/clusterctl-tree
Browse files Browse the repository at this point in the history
🌱 Add clusterctl describe cluster command
  • Loading branch information
k8s-ci-robot authored Jan 12, 2021
2 parents 52794b5 + 6be9dba commit daba8fe
Show file tree
Hide file tree
Showing 17 changed files with 2,712 additions and 0 deletions.
4 changes: 4 additions & 0 deletions cmd/clusterctl/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"sigs.k8s.io/cluster-api/cmd/clusterctl/client/cluster"
"sigs.k8s.io/cluster-api/cmd/clusterctl/client/config"
"sigs.k8s.io/cluster-api/cmd/clusterctl/client/repository"
"sigs.k8s.io/cluster-api/cmd/clusterctl/client/tree"
)

// Client is exposes the clusterctl high-level client library.
Expand Down Expand Up @@ -67,6 +68,9 @@ type Client interface {
// variables.
ProcessYAML(options ProcessYAMLOptions) (YamlPrinter, error)

// DescribeCluster returns the object tree representing the status of a Cluster API cluster.
DescribeCluster(options DescribeClusterOptions) (*tree.ObjectTree, error)

// Interface for alpha features in clusterctl
AlphaClient
}
Expand Down
5 changes: 5 additions & 0 deletions cmd/clusterctl/client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
"sigs.k8s.io/cluster-api/cmd/clusterctl/client/cluster"
"sigs.k8s.io/cluster-api/cmd/clusterctl/client/config"
"sigs.k8s.io/cluster-api/cmd/clusterctl/client/repository"
"sigs.k8s.io/cluster-api/cmd/clusterctl/client/tree"
yaml "sigs.k8s.io/cluster-api/cmd/clusterctl/client/yamlprocessor"
"sigs.k8s.io/cluster-api/cmd/clusterctl/internal/scheme"
"sigs.k8s.io/cluster-api/cmd/clusterctl/internal/test"
Expand Down Expand Up @@ -122,6 +123,10 @@ func (f fakeClient) RolloutRestart(options RolloutRestartOptions) error {
return f.internalClient.RolloutRestart(options)
}

func (f fakeClient) DescribeCluster(options DescribeClusterOptions) (*tree.ObjectTree, error) {
return f.internalClient.DescribeCluster(options)
}

// newFakeClient returns a clusterctl client that allows to execute tests on a set of fake config, fake repositories and fake clusters.
// you can use WithCluster and WithRepository to prepare for the test case.
func newFakeClient(configClient config.Client) *fakeClient {
Expand Down
79 changes: 79 additions & 0 deletions cmd/clusterctl/client/describe.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/*
Copyright 2020 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package client

import (
"context"

"sigs.k8s.io/cluster-api/cmd/clusterctl/client/tree"
)

// DescribeClusterOptions carries the options supported by DescribeCluster.
type DescribeClusterOptions struct {
// Kubeconfig defines the kubeconfig to use for accessing the management cluster. If empty,
// default rules for kubeconfig discovery will be used.
Kubeconfig Kubeconfig

// Namespace where the workload cluster is located. If unspecified, the current namespace will be used.
Namespace string

// ClusterName to be used for the workload cluster.
ClusterName string

// ShowOtherConditions is a list of comma separated kind or kind/name for which we should add the ShowObjectConditionsAnnotation
// to signal to the presentation layer to show all the conditions for the objects.
ShowOtherConditions string

// DisableNoEcho disable hiding MachineInfrastructure or BootstrapConfig objects if the object's ready condition is true
// or it has the same Status, Severity and Reason of the parent's object ready condition (it is an echo)
DisableNoEcho bool

// DisableGrouping disable grouping machines objects in case the ready condition
// has the same Status, Severity and Reason
DisableGrouping bool
}

// DescribeCluster returns the object tree representing the status of a Cluster API cluster.
func (c *clusterctlClient) DescribeCluster(options DescribeClusterOptions) (*tree.ObjectTree, error) {
// gets access to the management cluster
cluster, err := c.clusterClientFactory(ClusterClientFactoryInput{Kubeconfig: options.Kubeconfig})
if err != nil {
return nil, err
}

// If the option specifying the Namespace is empty, try to detect it.
if options.Namespace == "" {
currentNamespace, err := cluster.Proxy().CurrentNamespace()
if err != nil {
return nil, err
}
options.Namespace = currentNamespace
}

// Fetch the Cluster client.
client, err := cluster.Proxy().NewClient()
if err != nil {
return nil, err
}

// Gets the object tree representing the status of a Cluster API cluster.
return tree.Discovery(context.TODO(), client, options.Namespace, options.ClusterName, tree.DiscoverOptions{
ShowOtherConditions: options.ShowOtherConditions,
DisableNoEcho: options.DisableNoEcho,
DisableGrouping: options.DisableGrouping,
})
}
131 changes: 131 additions & 0 deletions cmd/clusterctl/client/tree/annotations.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
/*
Copyright 2020 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package tree

import (
"strconv"

"sigs.k8s.io/controller-runtime/pkg/client"
)

const (
// ShowObjectConditionsAnnotation documents that the presentation layer should show all the conditions for the object.
ShowObjectConditionsAnnotation = "tree.cluster.x-k8s.io.io/show-conditions"

// ObjectMetaNameAnnotation contains the meta name that should be used for the object in the presentation layer,
// e.g. control plane for KCP.
ObjectMetaNameAnnotation = "tree.cluster.x-k8s.io.io/meta-name"

// VirtualObjectAnnotation documents that the object does not correspond to any real object, but instead is
// a virtual object introduced to provide a better representation of the cluster status, e.g. workers.
VirtualObjectAnnotation = "tree.cluster.x-k8s.io.io/virtual-object"

// GroupingObjectAnnotation is an annotation that should be applied to a node in order to trigger the grouping action
// when adding the node's children. e.g. if you have a control-plane node, and you apply this annotation, then
// the control-plane machines added as a children of this node will be grouped in case the ready condition
// has the same Status, Severity and Reason.
GroupingObjectAnnotation = "tree.cluster.x-k8s.io.io/grouping-object"

// GroupObjectAnnotation is an annotation that documents that a node is the result of a grouping operation, and
// thus the node is representing group of sibling nodes, e.g. a group of machines.
GroupObjectAnnotation = "tree.cluster.x-k8s.io.io/group-object"

// GroupItemsAnnotation contains the list of names for the objects included in a group object.
GroupItemsAnnotation = "tree.cluster.x-k8s.io.io/group-items"

// GroupItemsSeparator is the separator used in the GroupItemsAnnotation
GroupItemsSeparator = ", "
)

// GetMetaName returns the object meta name that should be used for the object in the presentation layer, if defined.
func GetMetaName(obj client.Object) string {
if val, ok := getAnnotation(obj, ObjectMetaNameAnnotation); ok {
return val
}
return ""
}

// IsGroupingObject returns true in case the object is responsible to trigger the grouping action
// when adding the object's children. e.g. A control-plane object, could be responsible of grouping
// the control-plane machines while added as a children objects.
func IsGroupingObject(obj client.Object) bool {
if val, ok := getBoolAnnotation(obj, GroupingObjectAnnotation); ok {
return val
}
return false
}

// IsGroupObject return true if the object is the result of a grouping operation, and
// thus the object is representing group of sibling object, e.g. a group of machines.
func IsGroupObject(obj client.Object) bool {
if val, ok := getBoolAnnotation(obj, GroupObjectAnnotation); ok {
return val
}
return false
}

// GetGroupItems return the list of names for the objects included in a group object.
func GetGroupItems(obj client.Object) string {
if val, ok := getAnnotation(obj, GroupItemsAnnotation); ok {
return val
}
return ""
}

// IsVirtualObject return true if the object does not correspond to any real object, but instead it is
// a virtual object introduced to provide a better representation of the cluster status.
func IsVirtualObject(obj client.Object) bool {
if val, ok := getBoolAnnotation(obj, VirtualObjectAnnotation); ok {
return val
}
return false
}

// IsShowConditionsObject returns true if the presentation layer should show all the conditions for the object.
func IsShowConditionsObject(obj client.Object) bool {
if val, ok := getBoolAnnotation(obj, ShowObjectConditionsAnnotation); ok {
return val
}
return false
}

func getAnnotation(obj client.Object, annotation string) (string, bool) {
if obj == nil {
return "", false
}
val, ok := obj.GetAnnotations()[annotation]
return val, ok
}

func getBoolAnnotation(obj client.Object, annotation string) (bool, bool) {
val, ok := getAnnotation(obj, annotation)
if ok {
if boolVal, err := strconv.ParseBool(val); err == nil {
return boolVal, true
}
}
return false, false
}

func addAnnotation(obj client.Object, annotation, value string) {
annotations := obj.GetAnnotations()
if annotations == nil {
annotations = map[string]string{}
}
annotations[annotation] = value
obj.SetAnnotations(annotations)
}
Loading

0 comments on commit daba8fe

Please sign in to comment.