This repository has been archived by the owner on Nov 25, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rancher.go
214 lines (179 loc) · 4.83 KB
/
rancher.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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
package main
import (
"context"
"fmt"
"math"
"reflect"
"strconv"
"strings"
akashv1 "github.com/ovrclk/akash/pkg/apis/akash.network/v1"
"github.com/pkg/errors"
corev1 "k8s.io/api/core/v1"
storagev1 "k8s.io/api/storage/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/watch"
)
type rancher struct {
exe RemotePodCommandExecutor
ctx context.Context
cancel context.CancelFunc
querier
}
type rancherStorage struct {
isRancher bool
isAkashManaged bool
allocated uint64
}
type rancherStorageClasses map[string]*rancherStorage
func NewRancher(ctx context.Context) (Storage, error) {
ctx, cancel := context.WithCancel(ctx)
r := &rancher{
exe: NewRemotePodCommandExecutor(KubeConfigFromCtx(ctx), KubeClientFromCtx(ctx)),
ctx: ctx,
cancel: cancel,
querier: newQuerier(),
}
group := ErrGroupFromCtx(ctx)
group.Go(r.run)
return r, nil
}
func (c *rancher) run() error {
defer func() {
c.cancel()
}()
events := make(chan interface{}, 1000)
pubsub := PubSubFromCtx(c.ctx)
defer pubsub.Unsub(events)
pubsub.AddSub(events, "ns", "sc", "pv", "nodes")
log := LogFromCtx(c.ctx).WithName("rancher")
scs := make(rancherStorageClasses)
resources := akashv1.ResourcePair{
Allocatable: math.MaxUint64,
}
scSynced := false
pvSynced := false
pvCount := 0
var pendingPVs []watch.Event
for {
select {
case <-c.ctx.Done():
return c.ctx.Err()
case rawEvt := <-events:
if scSynced && len(pendingPVs) > 0 {
select {
case events <- pendingPVs[0]:
pendingPVs = pendingPVs[1:]
default:
}
}
switch evt := rawEvt.(type) {
case watch.Event:
kind := reflect.TypeOf(evt.Object).String()
if idx := strings.LastIndex(kind, "."); idx > 0 {
kind = kind[idx+1:]
}
msg := fmt.Sprintf("%8s monitoring %s", evt.Type, kind)
evtdone:
switch obj := evt.Object.(type) {
case *corev1.Node:
if allocatable, exists := obj.Status.Allocatable[corev1.ResourceEphemeralStorage]; exists {
resources.Allocatable = uint64(allocatable.Value())
}
case *storagev1.StorageClass:
switch evt.Type {
case watch.Added:
fallthrough
case watch.Modified:
lblVal := obj.Labels["akash.network"]
if lblVal == "" {
lblVal = "false"
}
sc, exists := scs[obj.Name]
if !exists {
sc = &rancherStorage{
isRancher: obj.Provisioner == "rancher.io/local-path",
}
}
sc.isAkashManaged, _ = strconv.ParseBool(lblVal)
scs[obj.Name] = sc
scList, _ := KubeClientFromCtx(c.ctx).StorageV1().StorageClasses().List(c.ctx, metav1.ListOptions{})
if len(scList.Items) == len(scs) && !scSynced {
scSynced = true
if len(pendingPVs) > 0 {
select {
case events <- pendingPVs[0]:
pendingPVs = pendingPVs[1:]
default:
}
}
pvList, _ := KubeClientFromCtx(c.ctx).CoreV1().PersistentVolumes().List(c.ctx, metav1.ListOptions{})
if len(pvList.Items) == pvCount && !pvSynced {
pvSynced = true
}
}
case watch.Deleted:
// volumes can remain without storage class so to keep metrics wight when storage class suddenly
// recreated we don't delete it
default:
break evtdone
}
log.Info(msg, "name", obj.Name)
case *corev1.PersistentVolume:
switch evt.Type {
case watch.Added:
pvCount++
fallthrough
case watch.Modified:
resource, exists := obj.Spec.Capacity[corev1.ResourceStorage]
if !exists {
break
}
if params, exists := scs[obj.Name]; !exists {
scs[obj.Spec.StorageClassName] = &rancherStorage{
allocated: uint64(resource.Value()),
}
} else {
params.allocated += uint64(resource.Value())
}
pvList, _ := KubeClientFromCtx(c.ctx).CoreV1().PersistentVolumes().List(c.ctx, metav1.ListOptions{})
if len(pvList.Items) == pvCount && !pvSynced {
pvSynced = true
}
case watch.Deleted:
pvCount--
resource, exists := obj.Spec.Capacity[corev1.ResourceStorage]
if !exists {
break
}
scs[obj.Spec.StorageClassName].allocated -= uint64(resource.Value())
default:
break evtdone
}
log.Info(msg, "name", obj.Name)
default:
break evtdone
}
}
case req := <-c.reqch:
var resp resp
if pvSynced {
var res []akashv1.InventoryClusterStorage
for class, params := range scs {
if params.isRancher && params.isAkashManaged {
res = append(res, akashv1.InventoryClusterStorage{
Class: class,
ResourcePair: akashv1.ResourcePair{
Allocated: params.allocated,
Allocatable: resources.Allocatable,
},
})
}
}
resp.res = res
} else {
resp.err = errors.New("rancher inventory is being updated")
}
req.resp <- resp
}
}
}