/
chart.go
135 lines (126 loc) · 4.88 KB
/
chart.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 util
import (
"context"
metainternal "k8s.io/apimachinery/pkg/apis/meta/internalversion"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apiserver/pkg/endpoints/request"
"k8s.io/apiserver/pkg/registry/generic/registry"
registryinternalclient "tkestack.io/tke/api/client/clientset/internalversion/typed/registry/internalversion"
authversionedclient "tkestack.io/tke/api/client/clientset/versioned/typed/auth/v1"
businessversionedclient "tkestack.io/tke/api/client/clientset/versioned/typed/business/v1"
registryapi "tkestack.io/tke/api/registry"
)
func commonListAndMerge(ctx context.Context, options *metainternal.ListOptions, store *registry.Store, chartGroupList *registryapi.ChartGroupList) (runtime.Object, error) {
allList := ®istryapi.ChartList{}
for k, v := range chartGroupList.Items {
// ctx with namespace, Look through
// https://github.com/kubernetes/apiserver/blob/release-1.17/pkg/endpoints/installer.go#L1083
// https://github.com/kubernetes/apiserver/blob/release-1.17/pkg/endpoints/handlers/get.go#L186
ctx = request.WithNamespace(ctx, v.Name)
obj, err := store.List(ctx, options)
if err != nil {
return obj, err
}
chartList := obj.(*registryapi.ChartList)
if k == 0 {
allList = chartList
} else {
allList.Items = append(allList.Items, chartList.Items...)
}
}
return allList, nil
}
// ListUserChartsFromStore list all charts that belongs to personal chartgroup
func ListUserChartsFromStore(ctx context.Context,
options *metainternal.ListOptions,
businessClient businessversionedclient.BusinessV1Interface,
registryClient *registryinternalclient.RegistryClient,
privilegedUsername string,
store *registry.Store) (runtime.Object, error) {
obj, err := ListUserChartGroups(ctx, options.DeepCopy(), businessClient, registryClient, privilegedUsername)
if err != nil {
return nil, err
}
chartGroupList := obj.(*registryapi.ChartGroupList)
if len(chartGroupList.Items) == 0 {
return ®istryapi.ChartList{}, nil
}
return commonListAndMerge(ctx, options, store, chartGroupList)
}
// ListProjectChartsFromStore list all charts that belongs to project chartgroup
func ListProjectChartsFromStore(ctx context.Context,
options *metainternal.ListOptions,
targetProjectID string,
businessClient businessversionedclient.BusinessV1Interface,
authClient authversionedclient.AuthV1Interface,
registryClient *registryinternalclient.RegistryClient,
privilegedUsername string,
store *registry.Store) (runtime.Object, error) {
obj, err := ListProjectChartGroups(ctx, options.DeepCopy(), targetProjectID, businessClient, authClient, registryClient, privilegedUsername)
if err != nil {
return nil, err
}
chartGroupList := obj.(*registryapi.ChartGroupList)
if len(chartGroupList.Items) == 0 {
return ®istryapi.ChartList{}, nil
}
return commonListAndMerge(ctx, options, store, chartGroupList)
}
// ListPublicChartsFromStore list all charts that belongs to public chartgroup
func ListPublicChartsFromStore(ctx context.Context,
options *metainternal.ListOptions,
businessClient businessversionedclient.BusinessV1Interface,
registryClient *registryinternalclient.RegistryClient,
privilegedUsername string,
store *registry.Store) (runtime.Object, error) {
obj, err := ListPublicChartGroups(ctx, options.DeepCopy(), businessClient, registryClient, privilegedUsername)
if err != nil {
return nil, err
}
chartGroupList := obj.(*registryapi.ChartGroupList)
if len(chartGroupList.Items) == 0 {
return ®istryapi.ChartList{}, nil
}
return commonListAndMerge(ctx, options, store, chartGroupList)
}
func mergeCharts(cgs ...runtime.Object) *registryapi.ChartList {
list := ®istryapi.ChartList{}
exist := make(map[types.UID]bool)
for _, v := range cgs {
if v != nil {
vList := v.(*registryapi.ChartList)
for _, v := range vList.Items {
if _, ok := exist[v.UID]; !ok {
list.Items = append(list.Items, v)
exist[v.UID] = true
}
}
}
}
return list
}
// ListAllChartsFromStore list all charts
func ListAllChartsFromStore(ctx context.Context,
options *metainternal.ListOptions,
targetProjectID string,
businessClient businessversionedclient.BusinessV1Interface,
authClient authversionedclient.AuthV1Interface,
registryClient *registryinternalclient.RegistryClient,
privilegedUsername string,
store *registry.Store) (runtime.Object, error) {
personal, err := ListUserChartsFromStore(ctx, options.DeepCopy(), businessClient, registryClient, privilegedUsername, store)
if err != nil {
return nil, err
}
project, err := ListProjectChartsFromStore(ctx, options.DeepCopy(), targetProjectID, businessClient, authClient, registryClient, privilegedUsername, store)
if err != nil {
return nil, err
}
public, err := ListPublicChartsFromStore(ctx, options.DeepCopy(), businessClient, registryClient, privilegedUsername, store)
if err != nil {
return nil, err
}
list := mergeCharts(personal, project, public)
return list, nil
}