-
Notifications
You must be signed in to change notification settings - Fork 0
/
first.txt
65 lines (49 loc) · 1.53 KB
/
first.txt
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
func (r *MyResourceReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
// Get Kubernetes client
client := r.Client
nodes := &corev1.NodeList{}
// List nodes
if err := client.List(ctx, nodes); err != nil {
return ctrl.Result{}, err
}
// Print CPU and memory
for _, node := range nodes.Items {
cpu := node.Status.Capacity["cpu"]
memory := node.Status.Capacity["memory"]
log.Info("Node", "name", node.Name, "cpu", cpu, "memory", memory)
}
// Requeue
return ctrl.Result{Requeue: true}, nil
}
go
import (
"context"
"time"
corev1 "k8s.io/api/core/v1"
discovery "k8s.io/api/discovery/v1beta1"
"sigs.k8s.io/controller-runtime/pkg/client"
"github.com/go-logr/logr"
ctrl "sigs.k8s.io/controller-runtime"
)
func (r *MyResourceReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
logger := r.Log.WithValues("myresource", req.NamespacedName)
// Get client
client := r.Client
// List nodes
nodeList := &discovery.NodeList{}
if err := client.List(ctx, nodeList); err != nil {
logger.Error(err, "unable to list nodes")
return ctrl.Result{}, err
}
// Print CPU and memory for each node
for _, node := range nodeList.Items {
cpu := node.Status.Capacity.Cpu()
memory := node.Status.Capacity.Memory()
logger.Info("Node details",
"Name", node.Name,
"CPU", cpu.String(),
"Memory", memory.String())
}
// Requeue
return ctrl.Result{RequeueAfter: 30 * time.Second}, nil
}