This repository has been archived by the owner on Sep 5, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 228
/
ps_services.go
102 lines (92 loc) · 2.3 KB
/
ps_services.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package ps
import (
"fmt"
"github.com/rancher/rio/cli/cmd/revision"
"github.com/rancher/rio/cli/pkg/clicontext"
"github.com/rancher/rio/cli/pkg/tables"
"github.com/rancher/rio/cli/pkg/types"
riov1 "github.com/rancher/rio/pkg/apis/rio.cattle.io/v1"
"github.com/rancher/rio/pkg/services"
corev1 "k8s.io/api/core/v1"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/util/sets"
)
type ServiceData struct {
ID string
Created string
Service *riov1.Service
Endpoint string
External string
}
func (p *Ps) apps(ctx *clicontext.CLIContext) error {
var appDataList []runtime.Object
appDatas := map[string]tables.AppData{}
appObjs, err := ctx.List(types.AppType)
if err != nil {
return err
}
namespaces := sets.NewString()
for _, app := range appObjs {
namespaces.Insert(app.(*riov1.App).Namespace)
}
m, err := revision.PodsMap(ctx, namespaces.List())
if err != nil {
return err
}
for _, v := range appObjs {
app := v.(*riov1.App)
appDatas[app.Namespace+"/"+app.Name] = tables.AppData{
ObjectMeta: app.ObjectMeta,
App: app,
Revisions: map[string]struct {
Revision *riov1.Service
Pods []corev1.Pod
}{},
}
}
svcObjs, err := ctx.List(types.ServiceType)
if err != nil {
return err
}
for _, v := range svcObjs {
svc := v.(*riov1.Service)
appName, version := services.AppAndVersion(svc)
key := svc.Namespace + "/" + appName
app, ok := appDatas[key]
if !ok {
app = tables.AppData{
App: riov1.NewApp(svc.Namespace, appName, riov1.App{
ObjectMeta: v1.ObjectMeta{
CreationTimestamp: svc.CreationTimestamp,
},
Spec: riov1.AppSpec{
Revisions: []riov1.Revision{
{
Scale: *svc.Spec.Scale,
Version: version,
},
},
},
}),
Revisions: map[string]struct {
Revision *riov1.Service
Pods []corev1.Pod
}{},
}
appDatas[key] = app
}
app.Revisions[version] = struct {
Revision *riov1.Service
Pods []corev1.Pod
}{Revision: svc, Pods: m[fmt.Sprintf("%s/%s/%s", svc.Namespace, appName, version)]}
}
for _, v := range appDatas {
copy := v
copy.Namespace = v.App.Namespace
copy.Name = v.App.Name
appDataList = append(appDataList, ©)
}
writer := tables.NewApp(ctx)
return writer.Write(appDataList)
}