-
Notifications
You must be signed in to change notification settings - Fork 0
/
simple_k8s.go
235 lines (192 loc) · 6.61 KB
/
simple_k8s.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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
package simple_k8s
import (
"context"
"errors"
"fmt"
"log"
"os"
"os/signal"
"strings"
"syscall"
"time"
"github.com/geniussportsgroup/FunctionalLib"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
List "github.com/geniussportsgroup/Slist"
Set "github.com/geniussportsgroup/treaps"
)
var ctx = context.TODO()
const HealthyFileName = "/tmp/healthy"
func CreateHealthyFile(name string) {
os.OpenFile(name, os.O_RDONLY|os.O_CREATE, 0666)
}
func RemoveHealthyFile(name string) {
_ = os.Remove(name)
}
func NewKubernetesClient(pathToConf string) (*kubernetes.Clientset, error) {
var config *rest.Config
var err error
if pathToConf == "" {
config, err = rest.InClusterConfig()
} else {
config, err = clientcmd.BuildConfigFromFlags("", pathToConf)
}
if err != nil {
return nil, err
}
return kubernetes.NewForConfig(config)
}
// FindDeploymentNames Return a set containing all the found namespaces whose name contains any given clue as substring.
func FindDeploymentNames(kubectl *kubernetes.Clientset, kubeNamespace, labelSelector string,
clues ...interface{}) (*Set.Treap, error) {
list, err := kubectl.AppsV1().Deployments(kubeNamespace).List(ctx, metav1.ListOptions{
TypeMeta: metav1.TypeMeta{},
LabelSelector: labelSelector,
FieldSelector: "",
Watch: false,
AllowWatchBookmarks: false,
ResourceVersion: "",
TimeoutSeconds: nil,
Limit: 0,
Continue: "",
})
if err != nil {
return nil, err
}
cmpStr := func(i1, i2 interface{}) bool {
return i1.(string) < i2.(string)
}
ret := Set.NewTreap(cmpStr)
foundClues := Set.NewTreap(cmpStr)
for _, item := range list.Items {
for _, clue := range clues {
if strings.Contains(item.ObjectMeta.Name, clue.(string)) {
ret.Insert(item.ObjectMeta.Name)
foundClues.Insert(clue)
break
}
}
}
// check that all the clues were found in the deployment names
for _, clue := range clues {
if foundClues.Search(clue) == nil {
return nil, errors.New(fmt.Sprintf("Deployment name containing clue %s was not found", clue))
}
}
return ret, nil
}
// ReadDeploymentNames Return a list of pair <clue, deployName> containing all the found namespaces whose name contains any given clue as substring.
func ReadDeploymentNames(kubectl *kubernetes.Clientset, kubeNamespace, labelSelector string,
clues *List.Slist) (*List.Slist, error) {
list, err := kubectl.AppsV1().Deployments(kubeNamespace).List(ctx, metav1.ListOptions{
TypeMeta: metav1.TypeMeta{},
LabelSelector: labelSelector,
FieldSelector: "",
Watch: false,
AllowWatchBookmarks: false,
ResourceVersion: "",
TimeoutSeconds: nil,
Limit: 0,
Continue: "",
})
if err != nil {
return nil, err
}
cluesToDeployName := make(map[string]string)
for _, item := range list.Items {
for it := List.NewIterator(clues); it.HasCurr(); it.Next() {
clue := it.GetCurr().(string)
if strings.Contains(item.ObjectMeta.Name, clue) {
cluesToDeployName[clue] = item.ObjectMeta.Name
break
}
}
}
// check that all the clues were found in the deployment names
for it := List.NewIterator(clues); it.HasCurr(); it.Next() {
clue := it.GetCurr().(string)
if _, found := cluesToDeployName[clue]; !found {
return nil, errors.New(fmt.Sprintf("Deployment name containing clue %s was not found", clue))
}
}
return FunctionalLib.Map(clues, func(i interface{}) interface{} {
clue := i.(string)
return FunctionalLib.Pair{
Item1: clue,
Item2: cluesToDeployName[clue],
}
}), nil
}
func GetNumberOfPods(kubectl *kubernetes.Clientset, kubeNamespace string,
deploymentName string) (n int32, err error) {
result, err := kubectl.AppsV1().Deployments(kubeNamespace).
GetScale(ctx, deploymentName, metav1.GetOptions{})
if err != nil {
return
}
n = result.Spec.Replicas
return
}
func SetNumberOfPods(numPods int32, currentNumOfPods *int32,
kubectl *kubernetes.Clientset, kubeNamespace string, deploymentName string) (bool, error) {
// Consult the current number of pods under the deployment
scale, err := kubectl.AppsV1().Deployments(kubeNamespace).
GetScale(ctx, deploymentName, metav1.GetOptions{})
if err != nil {
return false, err
}
*currentNumOfPods = scale.Spec.Replicas
if *currentNumOfPods == numPods {
return false, nil
}
// Set new scale
scale.Spec.Replicas = numPods
scale, err = kubectl.AppsV1().
Deployments(kubeNamespace).
UpdateScale(ctx, deploymentName, scale, metav1.UpdateOptions{})
if err != nil {
return false, err
}
*currentNumOfPods = scale.Spec.Replicas
return true, nil
}
// TerminationHandler goroutine for handling SIGTERM
func TerminationHandler(timeout time.Duration) {
TerminationHandlerCont(timeout, nil)
}
// TerminationHandlerCont set a termination handler. When SIGTERM is received, waits for timeout. Next
// the continuation function is called with the parameters pars... (it is up programmer to perform the casting).
// continuation is a point for giving to the programmer some additional actions that they could require
// before to terminate the process
func TerminationHandlerCont(timeout time.Duration, continuation func(pars ...interface{}), pars ...interface{}) {
log.Printf("Setting termination handler for process pid = %d", os.Getpid())
signChannel := make(chan os.Signal, 1)
signal.Ignore(syscall.SIGINT, syscall.SIGQUIT, syscall.SIGCONT)
signal.Notify(signChannel, syscall.SIGTERM)
sig := <-signChannel
log.Printf("Termination received (%s)", sig.String())
if continuation != nil {
continuation(pars...)
}
log.Printf("Waiting for %s seconds before to exit", timeout/time.Second)
time.Sleep(timeout)
log.Print("Exiting")
os.Exit(0)
}
// SetTerminationHandler Wrapper for setting the goroutine prepared for handling the SIGTERM
func SetTerminationHandler(TerminationTimeout time.Duration) {
go TerminationHandler(TerminationTimeout)
}
// SetTerminationHandlerWithContinuation Wrapper for setting the goroutine prepared for handling the SIGTERM
func SetTerminationHandlerWithContinuation(TerminationTimeout time.Duration,
continuation func(pars ...interface{}), pars ...interface{}) {
go TerminationHandlerCont(TerminationTimeout, continuation, pars...)
}
// TerminationAndLivelinessWithContinuation Wrapper for setting the goroutine prepared for handling the SIGTERM
func TerminationAndLivelinessWithContinuation(TerminationTimeout time.Duration,
continuation func(pars ...interface{}), pars ...interface{}) {
go TerminationHandlerCont(TerminationTimeout, continuation, pars...)
go EnableLivelinessCheck()
}