github.com/oam-dev/kubevela@v1.9.11/references/cli/top/model/namespace.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  	"time"
    22  
    23  	v1 "k8s.io/api/core/v1"
    24  	"sigs.k8s.io/controller-runtime/pkg/client"
    25  
    26  	"github.com/oam-dev/kubevela/references/cli/top/utils"
    27  )
    28  
    29  // Namespace is namespace struct
    30  type Namespace struct {
    31  	name   string
    32  	status string
    33  	age    string
    34  }
    35  
    36  // NamespaceList is namespace list
    37  type NamespaceList []Namespace
    38  
    39  // ListNamespaces return all namespaces
    40  func ListNamespaces(ctx context.Context, c client.Client) (NamespaceList, error) {
    41  	var nsList v1.NamespaceList
    42  	if err := c.List(ctx, &nsList); err != nil {
    43  		return NamespaceList{}, err
    44  	}
    45  	nsInfoList := make(NamespaceList, len(nsList.Items))
    46  	for index, ns := range nsList.Items {
    47  		nsInfoList[index] = Namespace{
    48  			name:   ns.Name,
    49  			status: string(ns.Status.Phase),
    50  			age:    utils.TimeFormat(time.Since(ns.CreationTimestamp.Time)),
    51  		}
    52  	}
    53  	return nsInfoList, nil
    54  }
    55  
    56  // ToTableBody generate body of table in namespace view
    57  func (l NamespaceList) ToTableBody() [][]string {
    58  	data := make([][]string, len(l)+1)
    59  	data[0] = []string{"all", "*", "*"}
    60  	for index, ns := range l {
    61  		data[index+1] = []string{ns.name, ns.status, ns.age}
    62  	}
    63  	return data
    64  }