-
Notifications
You must be signed in to change notification settings - Fork 2
/
cli-kubernetes.go
150 lines (145 loc) · 5.28 KB
/
cli-kubernetes.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
package idler
import (
"context"
"encoding/json"
"fmt"
"io"
"strconv"
"strings"
"github.com/go-logr/logr"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/types"
client "sigs.k8s.io/controller-runtime/pkg/client"
)
// kubernetesCLI handles scaling CLI based deployments in kubernetes.
func (h *Idler) kubernetesCLI(ctx context.Context, opLog logr.Logger, namespace corev1.Namespace) {
labelRequirements := generateLabelRequirements(h.Selectors.CLI.Builds)
listOption := (&client.ListOptions{}).ApplyOptions([]client.ListOption{
client.InNamespace(namespace.ObjectMeta.Name),
client.MatchingLabelsSelector{
Selector: labels.NewSelector().Add(labelRequirements...),
},
})
builds := &corev1.PodList{}
runningBuild := false
if !h.Selectors.CLI.SkipBuildCheck {
if err := h.Client.List(ctx, builds, listOption); err != nil {
opLog.Error(err, fmt.Sprintf("Error getting running builds for namespace %s", namespace.ObjectMeta.Name))
} else {
for _, build := range builds.Items {
if build.Status.Phase == "Running" {
opLog.Info(fmt.Sprintf("Environment has running build, skipping"))
runningBuild = true
break
}
}
}
}
// if there are no running builds, then check the cli pods
if !runningBuild {
// @TODO: eventually replace the `lagoon.sh/service=cli` check with `lagoon.sh/service-type=cli|cli-persistent` for better coverage
labelRequirements := generateLabelRequirements(h.Selectors.CLI.Deployments)
listOption = (&client.ListOptions{}).ApplyOptions([]client.ListOption{
client.InNamespace(namespace.ObjectMeta.Name),
client.MatchingLabelsSelector{
Selector: labels.NewSelector().Add(labelRequirements...),
},
})
deployments := &appsv1.DeploymentList{}
if err := h.Client.List(ctx, deployments, listOption); err != nil {
opLog.Error(err, fmt.Sprintf("Error getting deployments"))
} else {
for _, deployment := range deployments.Items {
// if we have any services=cli, act on them
zeroReps := new(int32)
*zeroReps = 0
if deployment.Spec.Replicas != zeroReps {
opLog.Info(fmt.Sprintf("Deployment %s has %d running replicas", deployment.ObjectMeta.Name, *deployment.Spec.Replicas))
} else {
opLog.Info(fmt.Sprintf("Deployment %s is already idled", deployment.ObjectMeta.Name))
break
}
if h.Debug {
opLog.Info(fmt.Sprintf("Checking deployment %s for cronjobs", deployment.ObjectMeta.Name))
}
hasCrons := false
if !h.Selectors.CLI.SkipCronCheck {
for _, container := range deployment.Spec.Template.Spec.Containers {
for _, env := range container.Env {
if env.Name == "CRONJOBS" {
if len(env.Value) > 0 {
cronjobs := strings.Split(env.Value, `\n`)
opLog.Info(fmt.Sprintf("Deployment %s has %d cronjobs defined", deployment.ObjectMeta.Name, len(cronjobs)))
hasCrons = true
break
}
}
}
}
}
if !hasCrons {
pods := &corev1.PodList{}
labelRequirements := generateLabelRequirements(h.Selectors.CLI.Pods)
listOption = (&client.ListOptions{}).ApplyOptions([]client.ListOption{
client.InNamespace(namespace.ObjectMeta.Name),
client.MatchingLabelsSelector{
Selector: labels.NewSelector().Add(labelRequirements...),
},
})
if err := h.Client.List(ctx, pods, listOption); err != nil {
opLog.Error(err, fmt.Sprintf("Error listing pods"))
} else {
for _, pod := range pods.Items {
processCount := 0
if !h.Selectors.CLI.SkipProcessCheck {
if h.Debug {
opLog.Info(fmt.Sprintf("Checking pod %s for running processes", pod.ObjectMeta.Name))
}
/*
Anything running with parent PID0 is likely a user process
/bin/bash -c "pgrep -P 0 | tail -n +3 | wc -l | tr -d ' '"
*/
var stdin io.Reader
stdout, _, err := execPod(pod.ObjectMeta.Name, namespace.ObjectMeta.Name, []string{`/bin/sh`, `-c`, `pgrep -P 0|tail -n +3|wc -l|tr -d ' '`}, stdin, false)
if err != nil {
opLog.Error(err, fmt.Sprintf("Error when trying to exec to pod %s", pod.ObjectMeta.Name))
break
}
trimmed := strings.TrimSpace(string(stdout))
pcInt, err := strconv.Atoi(trimmed[len(trimmed)-1:])
if err == nil {
if pcInt > 0 {
processCount = pcInt
}
}
if processCount == 0 {
opLog.Info(fmt.Sprintf("Pod %s has no running processes, idling", pod.ObjectMeta.Name))
}
}
if processCount == 0 {
if !h.DryRun {
scaleDeployment := deployment.DeepCopy()
mergePatch, _ := json.Marshal(map[string]interface{}{
"spec": map[string]interface{}{
"replicas": 0,
},
})
if err := h.Client.Patch(ctx, scaleDeployment, client.RawPatch(types.MergePatchType, mergePatch)); err != nil {
opLog.Error(err, fmt.Sprintf("Error scaling deployment %s", deployment.ObjectMeta.Name))
} else {
opLog.Info(fmt.Sprintf("Deployment %s scaled to 0", deployment.ObjectMeta.Name))
}
cliIdleEvents.Inc()
} else {
opLog.Info(fmt.Sprintf("Deployment %s would be scaled to 0", deployment.ObjectMeta.Name))
}
}
}
}
}
}
}
}
}