-
Notifications
You must be signed in to change notification settings - Fork 328
/
storage.go
123 lines (111 loc) · 4.46 KB
/
storage.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
/*
* Tencent is pleased to support the open source community by making TKEStack available.
*
* Copyright (C) 2012-2020 Tencent. All Rights Reserved.
*
* 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
*
* https://opensource.org/licenses/Apache-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 OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/
package storage
import (
"context"
"fmt"
"k8s.io/apimachinery/pkg/api/errors"
metainternal "k8s.io/apimachinery/pkg/apis/meta/internalversion"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
genericregistry "k8s.io/apiserver/pkg/registry/generic"
"k8s.io/apiserver/pkg/registry/rest"
businessv1 "tkestack.io/tke/api/business/v1"
businessversionedclient "tkestack.io/tke/api/client/clientset/versioned/typed/business/v1"
platformversionedclient "tkestack.io/tke/api/client/clientset/versioned/typed/platform/v1"
"tkestack.io/tke/api/monitor"
"tkestack.io/tke/pkg/apiserver/authentication"
apiserverutil "tkestack.io/tke/pkg/apiserver/util"
"tkestack.io/tke/pkg/monitor/util/cache"
"tkestack.io/tke/pkg/util/log"
)
// Storage includes storage for metrics and all sub resources.
type Storage struct {
ClusterOverview *REST
}
// NewStorage returns a Storage object that will work against metrics.
func NewStorage(_ genericregistry.RESTOptionsGetter, platformClient platformversionedclient.PlatformV1Interface,
businessClient businessversionedclient.BusinessV1Interface, cacher cache.Cacher) *Storage {
return &Storage{
ClusterOverview: &REST{
platformClient: platformClient,
businessClient: businessClient,
cacher: cacher,
},
}
}
// REST implements a RESTStorage for metrics against etcd.
type REST struct {
rest.Storage
platformClient platformversionedclient.PlatformV1Interface
businessClient businessversionedclient.BusinessV1Interface
cacher cache.Cacher
}
var _ rest.Creater = &REST{}
var _ rest.Scoper = &REST{}
// NamespaceScoped returns true if the storage is namespaced
func (r *REST) NamespaceScoped() bool {
return false
}
// New returns an empty object that can be used with Create and Update after request data has been put into it.
func (r *REST) New() runtime.Object {
return &monitor.ClusterOverview{}
}
// Create creates a new version of a resource.
func (r *REST) Create(ctx context.Context, obj runtime.Object, _ rest.ValidateObjectFunc, _ *metav1.CreateOptions) (runtime.Object, error) {
clusterOverview, ok := obj.(*monitor.ClusterOverview)
if !ok {
return nil, errors.NewBadRequest("failed to processed request body")
}
_, tenantID := authentication.UsernameAndTenantID(ctx)
listOptions := metav1.ListOptions{}
if tenantID != "" {
listOptions.FieldSelector = fmt.Sprintf("spec.tenantID=%s", tenantID)
}
wrappedOptions := apiserverutil.PredicateListOptions(ctx, &metainternal.ListOptions{})
log.Infof("create cluster overview: %+v, tenantID: %+v, wrappedOptions: %+v",
listOptions, tenantID, wrappedOptions)
clusterIDs := make([]string, 0)
if clusterList, err := r.platformClient.Clusters().List(ctx, listOptions); err == nil && clusterList != nil {
for _, cls := range clusterList.Items {
clusterIDs = append(clusterIDs, cls.GetName())
}
}
clusterOverview.Result = r.cacher.GetClusterOverviewResult(clusterIDs)
if r.businessClient == nil {
log.Info("The client for Business API Server is not installed")
clusterOverview.Result.ProjectCount = int32(monitor.OverviewProjectStatusDisable)
clusterOverview.Result.ProjectAbnormal = int32(monitor.OverviewProjectStatusDisable)
return clusterOverview, nil
}
projectCount := int32(0)
projectAbnormal := int32(0)
if projectList, err := r.businessClient.Projects().List(ctx, listOptions); err == nil && projectList != nil {
projectCount = int32(len(projectList.Items))
for _, prj := range projectList.Items {
if prj.Status.Phase == businessv1.ProjectFailed {
projectAbnormal++
}
}
} else {
projectCount = int32(monitor.OverviewProjectStatusError)
projectAbnormal = int32(monitor.OverviewProjectStatusError)
}
clusterOverview.Result.ProjectCount = projectCount
clusterOverview.Result.ProjectAbnormal = projectAbnormal
return clusterOverview, nil
}