/
agent.go
92 lines (79 loc) · 3.11 KB
/
agent.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
// 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 monitor
import (
"context"
"time"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/klog/v2"
agentquerier "antrea.io/antrea/pkg/agent/querier"
"antrea.io/antrea/pkg/apis/crd/v1beta1"
clientset "antrea.io/antrea/pkg/client/clientset/versioned"
)
type agentMonitor struct {
client clientset.Interface
querier agentquerier.AgentQuerier
// agentCRD is the desired state of agent monitoring CRD which agentMonitor expects.
agentCRD *v1beta1.AntreaAgentInfo
}
// NewAgentMonitor creates a new agent monitor.
func NewAgentMonitor(client clientset.Interface, querier agentquerier.AgentQuerier) *agentMonitor {
return &agentMonitor{
client: client,
querier: querier,
agentCRD: nil,
}
}
// Run creates AntreaAgentInfo CRD first after controller is running.
// Then updates AntreaAgentInfo CRD every 60 seconds.
func (monitor *agentMonitor) Run(stopCh <-chan struct{}) {
klog.Info("Starting Antrea Agent Monitor")
// Sync agent monitoring CRD every minute util stopCh is closed.
wait.Until(monitor.syncAgentCRD, time.Minute, stopCh)
}
func (monitor *agentMonitor) syncAgentCRD() {
var err error
if monitor.agentCRD != nil {
if monitor.agentCRD, err = monitor.updateAgentCRD(true); err == nil {
return
}
klog.ErrorS(err, "Failed to partially update agent monitoring CRD")
monitor.agentCRD = nil
}
monitor.agentCRD, err = monitor.getAgentCRD()
if err != nil {
klog.ErrorS(err, "Failed to get agent monitoring CRD")
monitor.agentCRD = nil
return
}
monitor.agentCRD, err = monitor.updateAgentCRD(false)
if err != nil {
klog.ErrorS(err, "Failed to entirely update agent monitoring CRD")
monitor.agentCRD = nil
}
}
// getAgentCRD is used to check the existence of agent monitoring CRD.
// So when the pod restarts, it will update this monitoring CRD instead of creating a new one.
func (monitor *agentMonitor) getAgentCRD() (*v1beta1.AntreaAgentInfo, error) {
crdName := monitor.querier.GetNodeConfig().Name
klog.V(2).InfoS("Getting agent monitoring CRD", "name", crdName)
return monitor.client.CrdV1beta1().AntreaAgentInfos().Get(context.TODO(), crdName, metav1.GetOptions{})
}
// updateAgentCRD updates the monitoring CRD.
func (monitor *agentMonitor) updateAgentCRD(partial bool) (*v1beta1.AntreaAgentInfo, error) {
monitor.querier.GetAgentInfo(monitor.agentCRD, partial)
klog.V(2).Infof("Updating agent monitoring CRD %+v, partial: %t", monitor.agentCRD, partial)
return monitor.client.CrdV1beta1().AntreaAgentInfos().Update(context.TODO(), monitor.agentCRD, metav1.UpdateOptions{})
}