Skip to content

Commit

Permalink
feat: add api define step1.
Browse files Browse the repository at this point in the history
Signed-off-by: Asklv <boironic@gmail.com>
  • Loading branch information
IRONICBo committed Jun 3, 2024
1 parent baea218 commit d414fe9
Show file tree
Hide file tree
Showing 12 changed files with 218 additions and 2 deletions.
8 changes: 8 additions & 0 deletions build/charts/antrea/templates/agent/clusterrole.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,14 @@ rules:
- nodestatssummaries
verbs:
- create
- apiGroups:
- controlplane.antrea.io
resources:
- nodeiplatencystats
verbs:
- create
- get
- list
- apiGroups:
- controlplane.antrea.io
resources:
Expand Down
8 changes: 8 additions & 0 deletions build/yamls/antrea-aks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4221,6 +4221,14 @@ rules:
- nodestatssummaries
verbs:
- create
- apiGroups:
- controlplane.antrea.io
resources:
- nodeiplatencystats
verbs:
- create
- get
- list
- apiGroups:
- controlplane.antrea.io
resources:
Expand Down
8 changes: 8 additions & 0 deletions build/yamls/antrea-eks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4221,6 +4221,14 @@ rules:
- nodestatssummaries
verbs:
- create
- apiGroups:
- controlplane.antrea.io
resources:
- nodeiplatencystats
verbs:
- create
- get
- list
- apiGroups:
- controlplane.antrea.io
resources:
Expand Down
8 changes: 8 additions & 0 deletions build/yamls/antrea-gke.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4221,6 +4221,14 @@ rules:
- nodestatssummaries
verbs:
- create
- apiGroups:
- controlplane.antrea.io
resources:
- nodeiplatencystats
verbs:
- create
- get
- list
- apiGroups:
- controlplane.antrea.io
resources:
Expand Down
8 changes: 8 additions & 0 deletions build/yamls/antrea-ipsec.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4234,6 +4234,14 @@ rules:
- nodestatssummaries
verbs:
- create
- apiGroups:
- controlplane.antrea.io
resources:
- nodeiplatencystats
verbs:
- create
- get
- list
- apiGroups:
- controlplane.antrea.io
resources:
Expand Down
8 changes: 8 additions & 0 deletions build/yamls/antrea.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4221,6 +4221,14 @@ rules:
- nodestatssummaries
verbs:
- create
- apiGroups:
- controlplane.antrea.io
resources:
- nodeiplatencystats
verbs:
- create
- get
- list
- apiGroups:
- controlplane.antrea.io
resources:
Expand Down
1 change: 1 addition & 0 deletions cmd/antrea-agent/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -936,6 +936,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,
Expand Down
9 changes: 7 additions & 2 deletions pkg/agent/monitortool/monitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ 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/agent/config"
"antrea.io/antrea/pkg/apis/crd/v1alpha1"
crdinformers "antrea.io/antrea/pkg/client/informers/externalversions/crd/v1alpha1"
)
Expand Down Expand Up @@ -69,6 +70,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
// nodeName is the name of the current Node, used to filter out the current Node from the latency monitor.
nodeName string
nodeInformer coreinformers.NodeInformer
Expand All @@ -84,13 +87,15 @@ 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 {
m := &NodeLatencyMonitor{
latencyStore: NewLatencyStore(trafficEncapMode.IsNetworkPolicyOnly()),
latencyConfigChanged: make(chan latencyConfig),
antreaClientProvider: antreaClientProvider,
nodeInformer: nodeInformer,
nodeLatencyMonitorInformer: nlmInformer,
nodeName: nodeConfig.Name,
Expand Down
1 change: 1 addition & 0 deletions pkg/apis/controlplane/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ func addKnownTypes(scheme *runtime.Scheme) error {
&SupportBundleCollection{},
&SupportBundleCollectionList{},
&SupportBundleCollectionStatus{},
// &NodeIPLatencyStat{},
)
return nil
}
25 changes: 25 additions & 0 deletions pkg/apis/controlplane/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
77 changes: 77 additions & 0 deletions pkg/apiserver/registry/controlplane/nodelatencystat/rest.go
Original file line number Diff line number Diff line change
@@ -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"
// }
59 changes: 59 additions & 0 deletions pkg/apiserver/registry/controlplane/nodelatencystat/rest_test.go
Original file line number Diff line number Diff line change
@@ -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)
// }

0 comments on commit d414fe9

Please sign in to comment.