-
Notifications
You must be signed in to change notification settings - Fork 362
/
rest.go
134 lines (117 loc) · 4.79 KB
/
rest.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
// Copyright 2020 Antrea 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 antreaclusternetworkpolicystats
import (
"context"
"time"
"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/api/meta"
metatable "k8s.io/apimachinery/pkg/api/meta/table"
"k8s.io/apimachinery/pkg/apis/meta/internalversion"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apiserver/pkg/registry/rest"
statsv1alpha1 "antrea.io/antrea/pkg/apis/stats/v1alpha1"
"antrea.io/antrea/pkg/features"
)
type REST struct {
statsProvider statsProvider
}
// NewREST returns a REST object that will work against API services.
func NewREST(p statsProvider) *REST {
return &REST{p}
}
var (
_ rest.Storage = &REST{}
_ rest.Scoper = &REST{}
_ rest.Getter = &REST{}
_ rest.Lister = &REST{}
)
type statsProvider interface {
ListAntreaClusterNetworkPolicyStats() []statsv1alpha1.AntreaClusterNetworkPolicyStats
GetAntreaClusterNetworkPolicyStats(name string) (*statsv1alpha1.AntreaClusterNetworkPolicyStats, bool)
}
func (r *REST) New() runtime.Object {
return &statsv1alpha1.AntreaClusterNetworkPolicyStats{}
}
func (r *REST) NewList() runtime.Object {
return &statsv1alpha1.AntreaClusterNetworkPolicyStatsList{}
}
func (r *REST) List(ctx context.Context, options *internalversion.ListOptions) (runtime.Object, error) {
if !features.DefaultFeatureGate.Enabled(features.NetworkPolicyStats) {
return &statsv1alpha1.AntreaClusterNetworkPolicyStatsList{}, nil
}
if !features.DefaultFeatureGate.Enabled(features.AntreaPolicy) {
return &statsv1alpha1.AntreaClusterNetworkPolicyStatsList{}, nil
}
labelSelector := labels.Everything()
if options != nil && options.LabelSelector != nil {
labelSelector = options.LabelSelector
}
stats := r.statsProvider.ListAntreaClusterNetworkPolicyStats()
items := make([]statsv1alpha1.AntreaClusterNetworkPolicyStats, 0, len(stats))
for i := range stats {
if labelSelector.Matches(labels.Set(stats[i].Labels)) {
items = append(items, stats[i])
}
}
metricList := &statsv1alpha1.AntreaClusterNetworkPolicyStatsList{
Items: items,
}
return metricList, nil
}
func (r *REST) Get(ctx context.Context, name string, options *metav1.GetOptions) (runtime.Object, error) {
if !features.DefaultFeatureGate.Enabled(features.NetworkPolicyStats) {
return &statsv1alpha1.AntreaClusterNetworkPolicyStats{}, nil
}
if !features.DefaultFeatureGate.Enabled(features.AntreaPolicy) {
return &statsv1alpha1.AntreaClusterNetworkPolicyStats{}, nil
}
metric, exists := r.statsProvider.GetAntreaClusterNetworkPolicyStats(name)
if !exists {
return nil, errors.NewNotFound(statsv1alpha1.Resource("antreaclusternetworkpolicystats"), name)
}
return metric, nil
}
var swaggerMetadataDescriptions = metav1.ObjectMeta{}.SwaggerDoc()
func (r *REST) ConvertToTable(ctx context.Context, obj runtime.Object, tableOptions runtime.Object) (*metav1.Table, error) {
table := &metav1.Table{
ColumnDefinitions: []metav1.TableColumnDefinition{
{Name: "Name", Type: "string", Format: "name", Description: swaggerMetadataDescriptions["name"]},
{Name: "Sessions", Type: "integer", Description: "The sessions count hit by the Antrea ClusterNetworkPolicy."},
{Name: "Packets", Type: "integer", Description: "The packets count hit by the Antrea ClusterNetworkPolicy."},
{Name: "Bytes", Type: "integer", Description: "The bytes count hit by the Antrea ClusterNetworkPolicy."},
{Name: "Created At", Type: "date", Description: swaggerMetadataDescriptions["creationTimestamp"]},
},
}
if m, err := meta.ListAccessor(obj); err == nil {
table.ResourceVersion = m.GetResourceVersion()
table.Continue = m.GetContinue()
table.RemainingItemCount = m.GetRemainingItemCount()
} else {
if m, err := meta.CommonAccessor(obj); err == nil {
table.ResourceVersion = m.GetResourceVersion()
}
}
var err error
table.Rows, err = metatable.MetaToTableRow(obj, func(obj runtime.Object, m metav1.Object, name, age string) ([]interface{}, error) {
stats := obj.(*statsv1alpha1.AntreaClusterNetworkPolicyStats)
return []interface{}{name, stats.TrafficStats.Sessions, stats.TrafficStats.Packets, stats.TrafficStats.Bytes, m.GetCreationTimestamp().Time.UTC().Format(time.RFC3339)}, nil
})
return table, err
}
func (r *REST) NamespaceScoped() bool {
return false
}