From 7f35342563058a4d988410080b26b9b833bc106e Mon Sep 17 00:00:00 2001 From: Asklv Date: Wed, 15 May 2024 02:13:33 +0800 Subject: [PATCH] feat: add api define step1. Signed-off-by: Asklv --- .../antrea/templates/agent/clusterrole.yaml | 8 ++ build/yamls/antrea-aks.yml | 8 ++ build/yamls/antrea-eks.yml | 8 ++ build/yamls/antrea-gke.yml | 8 ++ build/yamls/antrea-ipsec.yml | 8 ++ build/yamls/antrea.yml | 8 ++ cmd/antrea-agent/agent.go | 1 + pkg/agent/monitortool/monitor.go | 7 +- pkg/apis/controlplane/register.go | 1 + pkg/apis/controlplane/types.go | 25 ++++++ .../controlplane/nodelatencystat/rest.go | 77 +++++++++++++++++++ .../controlplane/nodelatencystat/rest_test.go | 59 ++++++++++++++ 12 files changed, 217 insertions(+), 1 deletion(-) create mode 100644 pkg/apiserver/registry/controlplane/nodelatencystat/rest.go create mode 100644 pkg/apiserver/registry/controlplane/nodelatencystat/rest_test.go diff --git a/build/charts/antrea/templates/agent/clusterrole.yaml b/build/charts/antrea/templates/agent/clusterrole.yaml index 25bce70e3d5..d8c3941a449 100644 --- a/build/charts/antrea/templates/agent/clusterrole.yaml +++ b/build/charts/antrea/templates/agent/clusterrole.yaml @@ -82,6 +82,14 @@ rules: - nodestatssummaries verbs: - create + - apiGroups: + - controlplane.antrea.io + resources: + - nodeiplatencystats + verbs: + - create + - get + - list - apiGroups: - controlplane.antrea.io resources: diff --git a/build/yamls/antrea-aks.yml b/build/yamls/antrea-aks.yml index b36b1a81673..1ba068e3935 100644 --- a/build/yamls/antrea-aks.yml +++ b/build/yamls/antrea-aks.yml @@ -6081,6 +6081,14 @@ rules: - nodestatssummaries verbs: - create + - apiGroups: + - controlplane.antrea.io + resources: + - nodeiplatencystats + verbs: + - create + - get + - list - apiGroups: - controlplane.antrea.io resources: diff --git a/build/yamls/antrea-eks.yml b/build/yamls/antrea-eks.yml index 25ae2048d5f..98c46f7c4a5 100644 --- a/build/yamls/antrea-eks.yml +++ b/build/yamls/antrea-eks.yml @@ -6081,6 +6081,14 @@ rules: - nodestatssummaries verbs: - create + - apiGroups: + - controlplane.antrea.io + resources: + - nodeiplatencystats + verbs: + - create + - get + - list - apiGroups: - controlplane.antrea.io resources: diff --git a/build/yamls/antrea-gke.yml b/build/yamls/antrea-gke.yml index c3dddd778c8..f05ab96d146 100644 --- a/build/yamls/antrea-gke.yml +++ b/build/yamls/antrea-gke.yml @@ -6081,6 +6081,14 @@ rules: - nodestatssummaries verbs: - create + - apiGroups: + - controlplane.antrea.io + resources: + - nodeiplatencystats + verbs: + - create + - get + - list - apiGroups: - controlplane.antrea.io resources: diff --git a/build/yamls/antrea-ipsec.yml b/build/yamls/antrea-ipsec.yml index c1e3570fb7f..6b78f9e5001 100644 --- a/build/yamls/antrea-ipsec.yml +++ b/build/yamls/antrea-ipsec.yml @@ -6094,6 +6094,14 @@ rules: - nodestatssummaries verbs: - create + - apiGroups: + - controlplane.antrea.io + resources: + - nodeiplatencystats + verbs: + - create + - get + - list - apiGroups: - controlplane.antrea.io resources: diff --git a/build/yamls/antrea.yml b/build/yamls/antrea.yml index df107eb9b0c..4f214832a05 100644 --- a/build/yamls/antrea.yml +++ b/build/yamls/antrea.yml @@ -6081,6 +6081,14 @@ rules: - nodestatssummaries verbs: - create + - apiGroups: + - controlplane.antrea.io + resources: + - nodeiplatencystats + verbs: + - create + - get + - list - apiGroups: - controlplane.antrea.io resources: diff --git a/cmd/antrea-agent/agent.go b/cmd/antrea-agent/agent.go index 59fc841c99d..e4fb463dbc2 100644 --- a/cmd/antrea-agent/agent.go +++ b/cmd/antrea-agent/agent.go @@ -927,6 +927,7 @@ func run(o *Options) error { // Start the node latency monitor. if features.DefaultFeatureGate.Enabled(features.NodeLatencyMonitor) && o.nodeType == config.K8sNode { nodeLatencyMonitor := monitortool.NewNodeLatencyMonitor( + antreaClientProvider, nodeInformer, nodeLatencyMonitorInformer, nodeConfig, diff --git a/pkg/agent/monitortool/monitor.go b/pkg/agent/monitortool/monitor.go index 2d4fe1e4abb..bf25ef461ca 100644 --- a/pkg/agent/monitortool/monitor.go +++ b/pkg/agent/monitortool/monitor.go @@ -28,6 +28,7 @@ import ( "k8s.io/client-go/tools/cache" "k8s.io/klog/v2" + "antrea.io/antrea/pkg/agent" config "antrea.io/antrea/pkg/agent/config" "antrea.io/antrea/pkg/apis/crd/v1alpha1" crdinformers "antrea.io/antrea/pkg/client/informers/externalversions/crd/v1alpha1" @@ -73,6 +74,8 @@ type NodeLatencyMonitor struct { // isIPv6Enabled is the flag to indicate whether the IPv6 is enabled. isIPv6Enabled bool + // antreaClientProvider provides interfaces to get antreaClient, which will be used to report the statistics + antreaClientProvider agent.AntreaClientProvider nodeInformer coreinformers.NodeInformer nodeLatencyMonitorInformer crdinformers.NodeLatencyMonitorInformer } @@ -86,7 +89,8 @@ type LatencyConfig struct { } // NewNodeLatencyMonitor creates a new NodeLatencyMonitor. -func NewNodeLatencyMonitor(nodeInformer coreinformers.NodeInformer, +func NewNodeLatencyMonitor(antreaClientProvider agent.AntreaClientProvider, + nodeInformer coreinformers.NodeInformer, nlmInformer crdinformers.NodeLatencyMonitorInformer, nodeConfig *config.NodeConfig, trafficEncapMode config.TrafficEncapModeType) *NodeLatencyMonitor { @@ -94,6 +98,7 @@ func NewNodeLatencyMonitor(nodeInformer coreinformers.NodeInformer, latencyStore: NewLatencyStore(trafficEncapMode.IsNetworkPolicyOnly()), latencyConfig: &LatencyConfig{Enable: false}, latencyConfigChanged: make(chan struct{}, 1), + antreaClientProvider: antreaClientProvider, nodeInformer: nodeInformer, nodeLatencyMonitorInformer: nlmInformer, } diff --git a/pkg/apis/controlplane/register.go b/pkg/apis/controlplane/register.go index 10da9773424..11255483640 100644 --- a/pkg/apis/controlplane/register.go +++ b/pkg/apis/controlplane/register.go @@ -67,6 +67,7 @@ func addKnownTypes(scheme *runtime.Scheme) error { &SupportBundleCollection{}, &SupportBundleCollectionList{}, &SupportBundleCollectionStatus{}, + // &NodeIPLatencyStat{}, ) return nil } diff --git a/pkg/apis/controlplane/types.go b/pkg/apis/controlplane/types.go index d5c601287e8..515445ac017 100644 --- a/pkg/apis/controlplane/types.go +++ b/pkg/apis/controlplane/types.go @@ -408,6 +408,31 @@ type NodeStatsSummary struct { Multicast []MulticastGroupInfo } +// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object + +// NodeIPLatencyStat contains the latency stat of a Node. +type NodeIPLatencyStat struct { + metav1.TypeMeta + metav1.ObjectMeta + + // The list of NodeIPLatency. + NodeIPLatencyList []NodeIPLatencyEntry +} + +// NodeIPLatencyEntry contains the latency stats of a Node. +type NodeIPLatencyEntry struct { + // The Node's name. + NodeName string + // The Node's target IP address. + TargetIP string + // The timestamp of the last send packet. + LastSendTime int64 + // The timestamp of the last receive packet. + LastRecvTime int64 + // The last valid rtt of the Node. + LastMeasuredRTT int64 +} + // MulticastGroupInfo contains the list of Pods that have joined a multicast group, for a given Node. type MulticastGroupInfo struct { // Group is the IP of the multicast group. diff --git a/pkg/apiserver/registry/controlplane/nodelatencystat/rest.go b/pkg/apiserver/registry/controlplane/nodelatencystat/rest.go new file mode 100644 index 00000000000..58e6ae6c1a6 --- /dev/null +++ b/pkg/apiserver/registry/controlplane/nodelatencystat/rest.go @@ -0,0 +1,77 @@ +// Copyright 2024 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 nodelatencystat + +// import ( +// "context" + +// "antrea.io/antrea/pkg/apis/controlplane" +// metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" +// v1 "k8s.io/apimachinery/pkg/apis/meta/v1" +// "k8s.io/apimachinery/pkg/runtime" +// "k8s.io/apiserver/pkg/registry/rest" +// ) + +// // nodeLatencyCollector is the interface required by the handler. +// type nodeLatencyCollector interface { +// Collect(summary *controlplane.NodeIPLatencyStat) +// Get(name string) *controlplane.NodeIPLatencyStat +// List() []*controlplane.NodeIPLatencyStat +// } + +// type REST struct { +// nodeLatencyCollector nodeLatencyCollector +// } + +// var ( +// _ rest.Scoper = &REST{} +// _ rest.Getter = &REST{} +// _ rest.SingularNameProvider = &REST{} +// _ rest.Creater = &REST{} +// ) + +// // NewREST returns a REST object that will work against API services. +// func NewREST(c nodeLatencyCollector) *REST { +// return &REST{c} +// } + +// func (r *REST) New() runtime.Object { +// return &controlplane.NodeIPLatencyStat{} +// } + +// func (r *REST) Destroy() { +// } + +// func (r *REST) Create(ctx context.Context, obj runtime.Object, createValidation rest.ValidateObjectFunc, options *v1.CreateOptions) (runtime.Object, error) { +// // Try to store the NodeIPLatencyStat in the store. +// summary := obj.(*controlplane.NodeIPLatencyStat) +// r.nodeLatencyCollector.Collect(summary) +// // a valid runtime.Object must be returned, otherwise the client would throw error. +// return &controlplane.NodeIPLatencyStat{}, nil +// } + +// func (r *REST) Get(ctx context.Context, name string, options *metav1.GetOptions) (runtime.Object, error) { +// // Try to retrieve the NodeIPLatencyStat from the store. +// entry := r.nodeLatencyCollector.Get(name) +// return entry, nil +// } + +// func (r *REST) NamespaceScoped() bool { +// return false +// } + +// func (r *REST) GetSingularName() string { +// return "nodeiplatencystat" +// } diff --git a/pkg/apiserver/registry/controlplane/nodelatencystat/rest_test.go b/pkg/apiserver/registry/controlplane/nodelatencystat/rest_test.go new file mode 100644 index 00000000000..2cc541cbac5 --- /dev/null +++ b/pkg/apiserver/registry/controlplane/nodelatencystat/rest_test.go @@ -0,0 +1,59 @@ +// Copyright 2024 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 nodelatencystat + +// import ( +// "context" +// "testing" + +// "github.com/stretchr/testify/assert" +// v1 "k8s.io/apimachinery/pkg/apis/meta/v1" + +// "antrea.io/antrea/pkg/apis/controlplane" +// ) + +// func TestREST(t *testing.T) { +// r := NewREST(nil) +// assert.Equal(t, &controlplane.NodeIPLatencyStat{}, r.New()) +// assert.False(t, r.NamespaceScoped()) +// } + +// type fakeCollector struct { +// gotSummary *controlplane.NodeIPLatencyStat +// } + +// func (f *fakeCollector) Collect(summary *controlplane.NodeIPLatencyStat) { +// f.gotSummary = summary +// } + +// func (f *fakeCollector) Get(name string) (*controlplane.NodeIPLatencyStat, error) { +// return nil, nil +// } + +// func TestRESTCreate(t *testing.T) { +// collector := &fakeCollector{} +// r := NewREST(collector) + +// summary := &controlplane.NodeIPLatencyStat{ +// ObjectMeta: v1.ObjectMeta{ +// Name: "foo", +// }, +// } +// actualObj, err := r.Create(context.TODO(), summary, nil, &v1.CreateOptions{}) +// assert.NoError(t, err) +// // Empty struct is returned on success. +// assert.Equal(t, &controlplane.NodeIPLatencyStat{}, actualObj) +// assert.Equal(t, summary, collector.gotSummary) +// }