github.com/oam-dev/kubevela@v1.9.11/references/cli/top/model/resource.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  	"bytes"
    21  	"context"
    22  
    23  	"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
    24  	"k8s.io/apimachinery/pkg/runtime"
    25  	apimachinerytypes "k8s.io/apimachinery/pkg/types"
    26  	"k8s.io/cli-runtime/pkg/printers"
    27  	"sigs.k8s.io/controller-runtime/pkg/client"
    28  
    29  	"github.com/oam-dev/kubevela/apis/core.oam.dev/v1beta1"
    30  	"github.com/oam-dev/kubevela/pkg/multicluster"
    31  	"github.com/oam-dev/kubevela/pkg/velaql/providers/query"
    32  	querytypes "github.com/oam-dev/kubevela/pkg/velaql/providers/query/types"
    33  )
    34  
    35  // ResourceList an abstract kinds of resource list which can convert it to data of view in the form of table
    36  type ResourceList interface {
    37  	// Header generate header of table in resource view
    38  	Header() []string
    39  	// Body generate body of table in resource view
    40  	Body() [][]string
    41  }
    42  
    43  // Resource info used by GVR
    44  type Resource struct {
    45  	Kind      string
    46  	Name      string
    47  	Namespace string
    48  	Cluster   string
    49  }
    50  
    51  // GVR is Group, Version, Resource
    52  type GVR struct {
    53  	GV string
    54  	R  Resource
    55  }
    56  
    57  func collectResource(ctx context.Context, c client.Client, opt query.Option) ([]unstructured.Unstructured, error) {
    58  	app := new(v1beta1.Application)
    59  	appKey := client.ObjectKey{Name: opt.Name, Namespace: opt.Namespace}
    60  	if err := c.Get(context.Background(), appKey, app); err != nil {
    61  		return nil, err
    62  	}
    63  	collector := query.NewAppCollector(c, opt)
    64  	appResList, err := collector.ListApplicationResources(context.Background(), app)
    65  	if err != nil {
    66  		return nil, err
    67  	}
    68  	var resources = make([]unstructured.Unstructured, 0)
    69  	for _, res := range appResList {
    70  		if res.ResourceTree != nil {
    71  			resources = append(resources, sonLeafResource(res.ResourceTree, opt.Filter.Kind, opt.Filter.APIVersion)...)
    72  		}
    73  		if (opt.Filter.Kind == "" && opt.Filter.APIVersion == "") || (res.Kind == opt.Filter.Kind && res.APIVersion == opt.Filter.APIVersion) {
    74  			var object unstructured.Unstructured
    75  			object.SetAPIVersion(opt.Filter.APIVersion)
    76  			object.SetKind(opt.Filter.Kind)
    77  			if err := c.Get(ctx, apimachinerytypes.NamespacedName{Namespace: res.Namespace, Name: res.Name}, &object); err == nil {
    78  				resources = append(resources, object)
    79  			}
    80  		}
    81  	}
    82  	return resources, nil
    83  }
    84  
    85  func sonLeafResource(node *querytypes.ResourceTreeNode, kind string, apiVersion string) []unstructured.Unstructured {
    86  	objects := make([]unstructured.Unstructured, 0)
    87  	if node.LeafNodes != nil {
    88  		for i := 0; i < len(node.LeafNodes); i++ {
    89  			objects = append(objects, sonLeafResource(node.LeafNodes[i], kind, apiVersion)...)
    90  		}
    91  	}
    92  	if (kind == "" && apiVersion == "") || (node.Kind == kind && node.APIVersion == apiVersion) {
    93  		objects = append(objects, node.Object)
    94  	}
    95  	return objects
    96  }
    97  
    98  // GetResourceObject get the resource object refer to the GVR data
    99  func GetResourceObject(c client.Client, gvr *GVR) (runtime.Object, error) {
   100  	obj := new(unstructured.Unstructured)
   101  	obj.SetAPIVersion(gvr.GV)
   102  	obj.SetKind(gvr.R.Kind)
   103  	key := client.ObjectKey{
   104  		Name:      gvr.R.Name,
   105  		Namespace: gvr.R.Namespace,
   106  	}
   107  	ctx := multicluster.ContextWithClusterName(context.Background(), gvr.R.Cluster)
   108  	err := c.Get(ctx, key, obj)
   109  	if err != nil {
   110  		return nil, err
   111  	}
   112  	return obj, nil
   113  }
   114  
   115  // ToYaml load the yaml text of object
   116  func ToYaml(o runtime.Object) (string, error) {
   117  	var (
   118  		buff bytes.Buffer
   119  		p    printers.YAMLPrinter
   120  	)
   121  	err := p.PrintObj(o, &buff)
   122  	if err != nil {
   123  		return "", err
   124  	}
   125  	return buff.String(), nil
   126  }