/
admin.go
135 lines (112 loc) · 4 KB
/
admin.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package v1alpha1
import (
"context"
"sort"
"github.com/caicloud/nirvana/log"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"github.com/caicloud/cyclone/pkg/apis/cyclone/v1alpha1"
"github.com/caicloud/cyclone/pkg/server/handler"
"github.com/caicloud/cyclone/pkg/server/handler/v1alpha1/sorter"
"github.com/caicloud/cyclone/pkg/server/types"
)
// AllWorkflows lists all workflows matched the provided label from all projects and tenants.
func AllWorkflows(ctx context.Context, label string, query *types.QueryParams) (*types.ListResponse, error) {
workflows, err := handler.K8sClient.CycloneV1alpha1().Workflows("").List(context.TODO(), metav1.ListOptions{
LabelSelector: label,
})
if err != nil {
log.Errorf("Get workflows from k8s error: %v", err)
return nil, err
}
items := workflows.Items
size := uint64(len(items))
if query.Start >= size {
return types.NewListResponse(int(size), []v1alpha1.Workflow{}), nil
}
end := query.Start + query.Limit
if end > size {
end = size
}
return types.NewListResponse(int(size), items[query.Start:end]), nil
}
// AllStages lists all stages matched the provided label from all projects and tenants.
func AllStages(ctx context.Context, label string, query *types.QueryParams) (*types.ListResponse, error) {
stages, err := handler.K8sClient.CycloneV1alpha1().Stages("").List(context.TODO(), metav1.ListOptions{
LabelSelector: label,
})
if err != nil {
log.Errorf("Get workflows from k8s error: %v", err)
return nil, err
}
items := stages.Items
size := uint64(len(items))
if query.Start >= size {
return types.NewListResponse(int(size), []v1alpha1.Stage{}), nil
}
end := query.Start + query.Limit
if end > size {
end = size
}
return types.NewListResponse(int(size), items[query.Start:end]), nil
}
// AllResources lists all resources matched the provided label from all projects and tenants.
func AllResources(ctx context.Context, label string, query *types.QueryParams) (*types.ListResponse, error) {
resources, err := handler.K8sClient.CycloneV1alpha1().Resources("").List(context.TODO(), metav1.ListOptions{
LabelSelector: label,
})
if err != nil {
log.Errorf("Get workflows from k8s error: %v", err)
return nil, err
}
items := resources.Items
size := uint64(len(items))
if query.Start >= size {
return types.NewListResponse(int(size), []v1alpha1.Resource{}), nil
}
end := query.Start + query.Limit
if end > size {
end = size
}
return types.NewListResponse(int(size), items[query.Start:end]), nil
}
// AllWorkflowRuns lists all workflowruns matched the provided label from all projects and tenants.
func AllWorkflowRuns(ctx context.Context, label string, query *types.QueryParams) (*types.ListResponse, error) {
workflowruns, err := handler.K8sClient.CycloneV1alpha1().WorkflowRuns("").List(context.TODO(), metav1.ListOptions{
LabelSelector: label,
})
if err != nil {
log.Errorf("Get workflows from k8s error: %v", err)
return nil, err
}
items := workflowruns.Items
sort.Sort(sorter.NewWorkflowRunSorter(items, false))
size := uint64(len(items))
if query.Start >= size {
return types.NewListResponse(int(size), []v1alpha1.WorkflowRun{}), nil
}
end := query.Start + query.Limit
if end > size {
end = size
}
return types.NewListResponse(int(size), items[query.Start:end]), nil
}
// AllWorkflowTriggers lists all workflow triggers matched the provided label from all projects and tenants.
func AllWorkflowTriggers(ctx context.Context, label string, query *types.QueryParams) (*types.ListResponse, error) {
triggers, err := handler.K8sClient.CycloneV1alpha1().WorkflowTriggers("").List(context.TODO(), metav1.ListOptions{
LabelSelector: label,
})
if err != nil {
log.Errorf("Get workflows from k8s error: %v", err)
return nil, err
}
items := triggers.Items
size := uint64(len(items))
if query.Start >= size {
return types.NewListResponse(int(size), []v1alpha1.WorkflowTrigger{}), nil
}
end := query.Start + query.Limit
if end > size {
end = size
}
return types.NewListResponse(int(size), items[query.Start:end]), nil
}