/
framework_list.go
87 lines (77 loc) · 2.7 KB
/
framework_list.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package main
import (
"context"
"fmt"
"io"
"strings"
"github.com/spf13/cobra"
"github.com/theketchio/ketch/cmd/ketch/output"
ketchv1 "github.com/theketchio/ketch/internal/api/v1beta1"
)
const frameworkListHelp = `
List all frameworks available for deploy.
`
type frameworkListOutput struct {
Name string `json:"name" yaml:"name"`
Status string `json:"status" yaml:"status"`
Namespace string `json:"namespace" yaml:"namespace"`
IngressType string `json:"ingressType" yaml:"ingressType"`
IngressClassName string `json:"ingressClassName" yaml:"ingressClassName"`
ClusterIssuer string `json:"clusterIssuer" yaml:"clusterIssuer"`
Apps string `json:"apps" yaml:"apps"`
}
func newFrameworkListCmd(cfg config, out io.Writer) *cobra.Command {
cmd := &cobra.Command{
Use: "list",
Short: "List all frameworks available for deploy.",
Long: frameworkListHelp,
RunE: func(cmd *cobra.Command, args []string) error {
return frameworkList(cmd.Context(), cfg, out)
},
}
return cmd
}
func frameworkList(ctx context.Context, cfg config, out io.Writer) error {
frameworks := ketchv1.FrameworkList{}
if err := cfg.Client().List(ctx, &frameworks); err != nil {
return fmt.Errorf("failed to get list of frameworks: %w", err)
}
return output.Write(generateFrameworkListOutput(frameworks), out, "column")
}
func generateFrameworkListOutput(frameworks ketchv1.FrameworkList) []frameworkListOutput {
var output []frameworkListOutput
for _, item := range frameworks.Items {
apps := fmt.Sprintf("%d", len(item.Status.Apps))
if item.Spec.AppQuotaLimit != nil && *item.Spec.AppQuotaLimit > 0 {
apps = fmt.Sprintf("%d/%d", len(item.Status.Apps), *item.Spec.AppQuotaLimit)
}
output = append(output, frameworkListOutput{
Name: item.Name,
Status: string(item.Status.Phase),
Namespace: item.Spec.NamespaceName,
IngressType: item.Spec.IngressController.IngressType.String(),
IngressClassName: item.Spec.IngressController.ClassName,
ClusterIssuer: item.Spec.IngressController.ClusterIssuer,
Apps: apps,
})
}
return output
}
func frameworkListNames(cfg config, nameFilter ...string) ([]string, error) {
frameworks := ketchv1.FrameworkList{}
if err := cfg.Client().List(context.TODO(), &frameworks); err != nil {
return nil, fmt.Errorf("failed to get list of frameworks: %w", err)
}
frameworkNames := make([]string, 0)
for _, a := range frameworks.Items {
if len(nameFilter) == 0 {
frameworkNames = append(frameworkNames, a.Name)
}
for _, filter := range nameFilter {
if strings.Contains(a.Name, filter) {
frameworkNames = append(frameworkNames, a.Name)
}
}
}
return frameworkNames, nil
}