-
Notifications
You must be signed in to change notification settings - Fork 62
/
main.go
187 lines (156 loc) · 4.79 KB
/
main.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
package main
import (
"flag"
"fmt"
"log"
"os"
"path/filepath"
"time"
coreV1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/util/runtime"
"k8s.io/client-go/informers"
"k8s.io/client-go/kubernetes"
corelisters "k8s.io/client-go/listers/core/v1"
"k8s.io/client-go/tools/cache"
"k8s.io/client-go/tools/clientcmd"
)
type Controller struct {
factory informers.SharedInformerFactory
lister corelisters.PersistentVolumeClaimLister
synced cache.InformerSynced
totalClaimedQuant resource.Quantity
maxClaimedQuant resource.Quantity
}
func New(clientset kubernetes.Interface, namespace, maxClaimQuant string) *Controller {
informerFactory := informers.NewFilteredSharedInformerFactory(clientset, time.Second*3, namespace, nil)
informer := informerFactory.Core().V1().PersistentVolumeClaims()
ctrl := &Controller{
factory: informerFactory,
maxClaimedQuant: resource.MustParse(maxClaimQuant),
}
informer.Informer().AddEventHandler(cache.ResourceEventHandlerFuncs{
AddFunc: ctrl.add,
UpdateFunc: func(old, new interface{}) {
newOne := new.(*coreV1.PersistentVolumeClaim)
oldOne := old.(*coreV1.PersistentVolumeClaim)
if newOne.ResourceVersion == oldOne.ResourceVersion {
// only update when new is different from old.
return
}
ctrl.update(new)
},
DeleteFunc: ctrl.delete,
})
ctrl.lister = informer.Lister()
ctrl.synced = informer.Informer().HasSynced
return ctrl
}
func (c *Controller) list() error {
pvcs, err := c.lister.List(labels.Everything())
if err != nil {
return err
}
template := "%-32s%-8s%-8s\n"
fmt.Println("--- PVCs ----")
fmt.Printf(template, "NAME", "STATUS", "CAPACITY")
var cap resource.Quantity
for _, pvc := range pvcs {
quant := pvc.Spec.Resources.Requests[coreV1.ResourceStorage]
cap.Add(quant)
fmt.Printf(template, pvc.Name, string(pvc.Status.Phase), quant.String())
}
fmt.Println("-----------------------------")
fmt.Printf("Total capacity claimed: %s\n", cap.String())
fmt.Println("-----------------------------")
return nil
}
// Run starts the custom controller
func (c *Controller) Run(stopCh chan struct{}) {
defer runtime.HandleCrash()
log.Println("starting controller")
defer log.Println("shutting down controller ")
c.factory.Start(stopCh)
if ok := cache.WaitForCacheSync(stopCh, c.synced); !ok {
log.Println("failed to wait for caches to sync")
return
}
// print initial list of claims
if err := c.list(); err != nil {
log.Println(err)
return
}
<-stopCh
}
func (c *Controller) add(obj interface{}) {
pvc, ok := obj.(*coreV1.PersistentVolumeClaim)
if !ok {
log.Println("unexpected type for object")
return
}
quant := pvc.Spec.Resources.Requests[coreV1.ResourceStorage]
c.totalClaimedQuant.Add(quant)
log.Printf("ADD: PVC %s added, claim size %s\n", pvc.Name, quant.String())
// is claim overage?
if c.totalClaimedQuant.Cmp(c.maxClaimedQuant) == 1 {
log.Printf("\nClaim overage reached: max %s at %s",
c.maxClaimedQuant.String(),
c.totalClaimedQuant.String(),
)
// trigger action
log.Println("*** Taking action ***")
}
}
func (c *Controller) update(obj interface{}) {
pvc, ok := obj.(*coreV1.PersistentVolumeClaim)
if !ok {
log.Println("unexpected type for object")
return
}
quant := pvc.Spec.Resources.Requests[coreV1.ResourceStorage]
log.Printf("UPDATE: PVC %s updated, claim size %s\n", pvc.Name, quant.String())
}
func (c *Controller) delete(obj interface{}) {
pvc, ok := obj.(*coreV1.PersistentVolumeClaim)
if !ok {
log.Println("unexpected type for object")
return
}
quant := pvc.Spec.Resources.Requests[coreV1.ResourceStorage]
c.totalClaimedQuant.Sub(quant)
log.Printf("DELETED: PVC %s removed, claim size %s\n", pvc.Name, quant.String())
if c.totalClaimedQuant.Cmp(c.maxClaimedQuant) <= 0 {
log.Printf("Claim usage normal: max %s at %s",
c.maxClaimedQuant.String(),
c.totalClaimedQuant.String(),
)
// trigger action
log.Println("*** Taking action ***")
}
}
func main() {
var ns, label, field, maxClaims string
kubeconfig := filepath.Join(os.Getenv("HOME"), ".kube", "config")
flag.StringVar(&ns, "namespace", "default", "namespace")
flag.StringVar(&label, "l", "", "Label selector")
flag.StringVar(&field, "f", "", "Field selector")
flag.StringVar(&maxClaims, "max-claims", "200Gi", "Maximum total claims to watch")
flag.StringVar(&kubeconfig, "kubeconfig", kubeconfig, "kubeconfig file")
flag.Parse()
log.Println("Using kubeconfig: ", kubeconfig)
config, err := clientcmd.BuildConfigFromFlags("", kubeconfig)
if err != nil {
panic(err.Error())
}
// create the clientset
clientset, err := kubernetes.NewForConfig(config)
if err != nil {
log.Fatal(err)
}
stopCh := make(chan struct{})
defer close(stopCh)
ctrl := New(clientset, ns, maxClaims)
go ctrl.Run(stopCh)
select {}
}