Skip to content

Commit

Permalink
List clusters when getting clusterset
Browse files Browse the repository at this point in the history
Signed-off-by: Jian Qiu <[email protected]>
  • Loading branch information
qiujian16 committed May 10, 2023
1 parent 95f9d75 commit 7d696de
Showing 1 changed file with 40 additions and 1 deletion.
41 changes: 40 additions & 1 deletion pkg/cmd/get/clusterset/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ package clusterset
import (
"context"
"fmt"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/klog/v2"
v1 "open-cluster-management.io/api/cluster/v1"
"strings"

"github.com/spf13/cobra"
Expand Down Expand Up @@ -74,12 +77,19 @@ func (o *Options) convertToTree(obj runtime.Object, tree *printer.TreePrinter) *
bindingMap[binding.Spec.ClusterSet] = append(bindingMap[binding.Spec.ClusterSet], binding.Namespace)
}

getter := &clusterGetter{client: o.Client}

if csList, ok := obj.(*clusterapiv1beta2.ManagedClusterSetList); ok {
for _, clusterset := range csList.Items {
boundNs, status := getFileds(clusterset, bindingMap[clusterset.Name])
clusters, err := getter.listClustersByClusterSet(&clusterset)
if err != nil {
klog.Fatalf("Failed to list cluster in clusterset %s: %v", clusterset.Name, err)
}
mp := make(map[string]interface{})
mp[".BoundNamespace"] = boundNs
mp[".Status"] = status
mp[".Clusters"] = clusters
tree.AddFileds(clusterset.Name, &mp)
}
}
Expand Down Expand Up @@ -130,8 +140,37 @@ func getFileds(clusterset clusterapiv1beta2.ManagedClusterSet, bindings []string

emptyCond := meta.FindStatusCondition(clusterset.Status.Conditions, clusterapiv1beta2.ManagedClusterSetConditionEmpty)
if emptyCond != nil {
status = string(emptyCond.Message)
status = emptyCond.Message
}

return
}

type clusterGetter struct {
client clusterclientset.Interface
}

func (c *clusterGetter) List(selector labels.Selector) ([]*v1.ManagedCluster, error) {
var ret []*v1.ManagedCluster
clusters, err := c.client.ClusterV1().ManagedClusters().List(context.TODO(), metav1.ListOptions{LabelSelector: selector.String()})
if err != nil {
return nil, err
}
for _, cluster := range clusters.Items {
ret = append(ret, cluster.DeepCopy())
}
return ret, nil
}

func (c *clusterGetter) listClustersByClusterSet(clusterset *clusterapiv1beta2.ManagedClusterSet) ([]string, error) {
clusters, err := clusterapiv1beta2.GetClustersFromClusterSet(clusterset, c)
if err != nil {
return nil, err
}

var ret []string
for _, cluster := range clusters {
ret = append(ret, cluster.Name)
}
return ret, nil
}

0 comments on commit 7d696de

Please sign in to comment.