-
Notifications
You must be signed in to change notification settings - Fork 0
/
list.go
80 lines (66 loc) · 2.79 KB
/
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
// Copyright 2017 The Kubernetes Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package serviceaccount
import (
"context"
v1 "k8s.io/api/core/v1"
"github.com/ogsyoo/dashboard/v2/src/app/backend/api"
"github.com/ogsyoo/dashboard/v2/src/app/backend/errors"
"github.com/ogsyoo/dashboard/v2/src/app/backend/resource/common"
"github.com/ogsyoo/dashboard/v2/src/app/backend/resource/dataselect"
client "k8s.io/client-go/kubernetes"
)
// ServiceAccount contains an information about single service account in the list.
type ServiceAccount struct {
api.ObjectMeta `json:"objectMeta"`
api.TypeMeta `json:"typeMeta"`
}
// ServiceAccountList contains a list of service accounts.
type ServiceAccountList struct {
api.ListMeta `json:"listMeta"`
Items []ServiceAccount `json:"items"`
Errors []error `json:"errors"`
}
// GetServiceAccountList lists service accounts from given namespace using given data select query.
func GetServiceAccountList(client client.Interface, namespace *common.NamespaceQuery,
dsQuery *dataselect.DataSelectQuery) (*ServiceAccountList, error) {
saList, err := client.CoreV1().ServiceAccounts(namespace.ToRequestParam()).List(context.TODO(),
api.ListEverything)
nonCriticalErrors, criticalError := errors.HandleError(err)
if criticalError != nil {
return nil, criticalError
}
return toServiceAccountList(saList.Items, nonCriticalErrors, dsQuery), nil
}
func toServiceAccount(sa *v1.ServiceAccount) ServiceAccount {
return ServiceAccount{
ObjectMeta: api.NewObjectMeta(sa.ObjectMeta),
TypeMeta: api.NewTypeMeta(api.ResourceKindServiceAccount),
}
}
func toServiceAccountList(serviceAccounts []v1.ServiceAccount, nonCriticalErrors []error,
dsQuery *dataselect.DataSelectQuery) *ServiceAccountList {
newServiceAccountList := &ServiceAccountList{
ListMeta: api.ListMeta{TotalItems: len(serviceAccounts)},
Items: make([]ServiceAccount, 0),
Errors: nonCriticalErrors,
}
saCells, filteredTotal := dataselect.GenericDataSelectWithFilter(toCells(serviceAccounts), dsQuery)
serviceAccounts = fromCells(saCells)
newServiceAccountList.ListMeta = api.ListMeta{TotalItems: filteredTotal}
for _, sa := range serviceAccounts {
newServiceAccountList.Items = append(newServiceAccountList.Items, toServiceAccount(&sa))
}
return newServiceAccountList
}