-
Notifications
You must be signed in to change notification settings - Fork 77
/
node.go
153 lines (133 loc) · 3.68 KB
/
node.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
//
// Copyright (C) 2019-2020 Vdaas.org Vald team ( kpango, rinx, kmrmt )
//
// 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://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 node provides kubernetes node information and preriodically update
package node
import (
"context"
"time"
"github.com/vdaas/vald/internal/k8s"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
_ "k8s.io/client-go/plugin/pkg/client/auth/gcp"
"sigs.k8s.io/controller-runtime/pkg/builder"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/handler"
"sigs.k8s.io/controller-runtime/pkg/manager"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
"sigs.k8s.io/controller-runtime/pkg/source"
)
type NodeWatcher k8s.ResourceController
type reconciler struct {
mgr manager.Manager
name string
namespace string
onError func(err error)
onReconcile func(nodes []Node)
}
type Node struct {
Name string
InternalAddr string
ExternalAddr string
CPUCapacity float64
CPURemain float64
MemCapacity float64
MemRemain float64
}
func New(opts ...Option) NodeWatcher {
r := new(reconciler)
for _, opt := range append(defaultOptions, opts...) {
opt(r)
}
return r
}
func (r *reconciler) Reconcile(ctx context.Context, req reconcile.Request) (res reconcile.Result, err error) {
ns := &corev1.NodeList{}
err = r.mgr.GetClient().List(ctx, ns)
if err != nil {
if r.onError != nil {
r.onError(err)
}
res = reconcile.Result{
Requeue: true,
RequeueAfter: time.Millisecond * 100,
}
if errors.IsNotFound(err) {
res = reconcile.Result{
Requeue: true,
RequeueAfter: time.Second,
}
}
return
}
nodes := make([]Node, 0, len(ns.Items))
for _, node := range ns.Items {
if node.GetObjectMeta().GetDeletionTimestamp() != nil {
continue
}
remain := node.Status.Allocatable
limit := node.Status.Capacity
var eip, iip string
for _, addr := range node.Status.Addresses {
switch addr.Type {
case corev1.NodeInternalIP:
iip = addr.Address
case corev1.NodeInternalDNS:
if iip == "" {
iip = addr.Address
}
case corev1.NodeExternalIP:
eip = addr.Address
case corev1.NodeExternalDNS:
if eip == "" {
eip = addr.Address
}
}
}
nodes = append(nodes, Node{
Name: node.GetName(),
ExternalAddr: eip,
InternalAddr: iip,
CPUCapacity: float64(limit.Cpu().Value()),
CPURemain: float64(remain.Cpu().Value()),
MemCapacity: float64(limit.Memory().Value()),
MemRemain: float64(remain.Memory().Value()),
})
}
if r.onReconcile != nil {
r.onReconcile(nodes)
}
return
}
func (r *reconciler) GetName() string {
return r.name
}
func (r *reconciler) NewReconciler(mgr manager.Manager) reconcile.Reconciler {
if r.mgr == nil && mgr != nil {
r.mgr = mgr
}
corev1.AddToScheme(r.mgr.GetScheme())
return r
}
func (r *reconciler) For() (client.Object, []builder.ForOption) {
return new(corev1.Node), nil
}
func (r *reconciler) Owns() (client.Object, []builder.OwnsOption) {
return nil, nil
}
func (r *reconciler) Watches() (*source.Kind, handler.EventHandler, []builder.WatchesOption) {
// return &source.Kind{Type: new(corev1.Node)}, &handler.EnqueueRequestForObject{}
return nil, nil, nil
}