github.com/oam-dev/kubevela@v1.9.11/references/cli/top/model/cluster.go (about)

     1  /*
     2  Copyright 2022 The KubeVela 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 model
    18  
    19  import (
    20  	"context"
    21  	"strings"
    22  
    23  	"github.com/oam-dev/cluster-gateway/pkg/config"
    24  	"sigs.k8s.io/controller-runtime/pkg/client"
    25  
    26  	"github.com/oam-dev/kubevela/pkg/multicluster"
    27  )
    28  
    29  // Cluster is cluster resource struct
    30  type Cluster struct {
    31  	name        string
    32  	alias       string
    33  	clusterType string
    34  	endpoint    string
    35  	labels      string
    36  }
    37  
    38  // ClusterList is cluster resource list
    39  type ClusterList []Cluster
    40  
    41  // ListClusters list clusters where application deploys resource
    42  func ListClusters(ctx context.Context, c client.Client) (ClusterList, error) {
    43  	name := ctx.Value(&CtxKeyAppName).(string)
    44  	ns := ctx.Value(&CtxKeyNamespace).(string)
    45  	app, err := LoadApplication(c, name, ns)
    46  	if err != nil {
    47  		return ClusterList{}, err
    48  	}
    49  	clusterSet := make(map[string]interface{})
    50  
    51  	for _, svc := range app.Status.AppliedResources {
    52  		if svc.Cluster == "" {
    53  			clusterSet[multicluster.ClusterLocalName] = struct{}{}
    54  		} else {
    55  			clusterSet[svc.Cluster] = struct{}{}
    56  		}
    57  	}
    58  
    59  	list := make(ClusterList, 0)
    60  
    61  	for key := range clusterSet {
    62  		clusterInfo := Cluster{
    63  			name: key,
    64  		}
    65  		cluster, err := multicluster.NewClusterClient(c).Get(context.Background(), key)
    66  		if err != nil {
    67  			continue
    68  		}
    69  		clusterInfo.alias = cluster.Spec.Alias
    70  		clusterInfo.clusterType = string(cluster.Spec.CredentialType)
    71  		clusterInfo.endpoint = cluster.Spec.Endpoint
    72  		var labels []string
    73  		for k, v := range cluster.Labels {
    74  			if !strings.HasPrefix(k, config.MetaApiGroupName) {
    75  				labels = append(labels, "[blue::]"+k+"="+"[green::]"+v)
    76  			}
    77  		}
    78  		clusterInfo.labels = strings.Join(labels, ",")
    79  
    80  		list = append(list, clusterInfo)
    81  	}
    82  	return list, nil
    83  }
    84  
    85  // ToTableBody generate body of table in cluster view
    86  func (l ClusterList) ToTableBody() [][]string {
    87  	data := make([][]string, len(l)+1)
    88  	data[0] = []string{AllCluster, "*", "*", "*", "*"}
    89  	for index, cluster := range l {
    90  		data[index+1] = []string{cluster.name, cluster.alias, cluster.clusterType, cluster.endpoint, cluster.labels}
    91  	}
    92  	return data
    93  }