github.com/docker/compose-on-kubernetes@v0.5.0/internal/registry/tableconverter.go (about)

     1  package registry
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"strconv"
     7  
     8  	"github.com/docker/compose-on-kubernetes/api/compose/latest"
     9  	iv "github.com/docker/compose-on-kubernetes/internal/internalversion"
    10  	metav1beta1 "k8s.io/apimachinery/pkg/apis/meta/v1beta1"
    11  	"k8s.io/apimachinery/pkg/runtime"
    12  )
    13  
    14  type stackTableConvertor struct {
    15  }
    16  
    17  func (c stackTableConvertor) ConvertToTable(ctx context.Context, object runtime.Object, tableOptions runtime.Object) (*metav1beta1.Table, error) {
    18  	var (
    19  		table metav1beta1.Table
    20  		items []iv.Stack
    21  	)
    22  	if lst, ok := object.(*iv.StackList); ok {
    23  		table.ResourceVersion = lst.ResourceVersion
    24  		table.SelfLink = lst.SelfLink
    25  		table.Continue = lst.Continue
    26  		items = lst.Items
    27  	} else if item, ok := object.(*iv.Stack); ok {
    28  		table.ResourceVersion = item.ResourceVersion
    29  		table.SelfLink = item.SelfLink
    30  		items = []iv.Stack{*item}
    31  	} else {
    32  		return nil, fmt.Errorf("unexpected object type %T", object)
    33  	}
    34  	table.ColumnDefinitions = []metav1beta1.TableColumnDefinition{
    35  		{Name: "Name", Type: "string", Format: "name", Description: "Stack name"},
    36  		{Name: "Services", Type: "int", Description: "Number of services"},
    37  		{Name: "Ports", Type: "string", Description: "Exposed ports"},
    38  		{Name: "Status", Type: "string", Description: "Current stack status"},
    39  		{Name: "Created At", Type: "date", Description: "Creation date"},
    40  	}
    41  	for _, item := range items {
    42  		local := item
    43  		serviceCount := 0
    44  		ports := ""
    45  		if item.Spec.Stack != nil {
    46  			serviceCount = len(item.Spec.Stack.Services)
    47  			ports = extractPortsSummary(item.Spec.Stack)
    48  		}
    49  		status := ""
    50  		if item.Status != nil {
    51  			status = fmt.Sprintf("%s (%s)", item.Status.Phase, item.Status.Message)
    52  		}
    53  		table.Rows = append(table.Rows, metav1beta1.TableRow{
    54  			Object: runtime.RawExtension{Object: &local},
    55  			Cells: []interface{}{
    56  				item.Name,
    57  				serviceCount,
    58  				ports,
    59  				status,
    60  				item.CreationTimestamp,
    61  			},
    62  		})
    63  	}
    64  	return &table, nil
    65  }
    66  
    67  func extractPortsSummary(s *latest.StackSpec) string {
    68  	ports := ""
    69  	for _, svc := range s.Services {
    70  		if len(svc.Ports) > 0 {
    71  			if ports != "" {
    72  				ports += ", "
    73  			}
    74  			ports += fmt.Sprintf("%s: ", svc.Name)
    75  			for portix, port := range svc.Ports {
    76  				if portix != 0 {
    77  					ports += ","
    78  				}
    79  				if port.Published == 0 {
    80  					ports += "*"
    81  				} else {
    82  					ports += strconv.FormatInt(int64(port.Published), 10)
    83  				}
    84  			}
    85  		}
    86  	}
    87  	return ports
    88  }