github.com/oam-dev/kubevela@v1.9.11/references/cli/pods.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 cli
    18  
    19  import (
    20  	"context"
    21  	"fmt"
    22  
    23  	"github.com/gosuri/uitable"
    24  
    25  	"github.com/oam-dev/kubevela/apis/core.oam.dev/v1beta1"
    26  	"github.com/oam-dev/kubevela/pkg/utils/common"
    27  	"github.com/oam-dev/kubevela/references/appfile"
    28  )
    29  
    30  func printAppPods(appName string, namespace string, f Filter, velaC common.Args) error {
    31  	app, err := appfile.LoadApplication(namespace, appName, velaC)
    32  	if err != nil {
    33  		return err
    34  	}
    35  	var podArgs = PodArgs{
    36  		Args:      velaC,
    37  		Namespace: namespace,
    38  		Filter:    f,
    39  		App:       app,
    40  	}
    41  
    42  	table, err := podArgs.listPods(context.Background())
    43  	if err != nil {
    44  		return err
    45  	}
    46  	fmt.Println(table.String())
    47  	return nil
    48  }
    49  
    50  // PodArgs creates arguments for `pods` command
    51  type PodArgs struct {
    52  	Args      common.Args
    53  	Namespace string
    54  	Filter    Filter
    55  	App       *v1beta1.Application
    56  }
    57  
    58  func (p *PodArgs) listPods(ctx context.Context) (*uitable.Table, error) {
    59  	pods, err := GetApplicationPods(ctx, p.App.Name, p.Namespace, p.Args, p.Filter)
    60  	if err != nil {
    61  		return nil, err
    62  	}
    63  	table := uitable.New()
    64  	table.AddRow("CLUSTER", "COMPONENT", "POD NAME", "NAMESPACE", "PHASE", "CREATE TIME", "REVISION", "HOST")
    65  	for _, pod := range pods {
    66  		table.AddRow(pod.Cluster, pod.Component, pod.Metadata.Name, pod.Metadata.Namespace, pod.Status.Phase, pod.Metadata.CreationTime, pod.Metadata.Version.DeployVersion, pod.Status.NodeName)
    67  	}
    68  	return table, nil
    69  }