-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathservice.go
More file actions
79 lines (73 loc) · 3.19 KB
/
service.go
File metadata and controls
79 lines (73 loc) · 3.19 KB
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
package idler
import (
"context"
"fmt"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/labels"
client "sigs.k8s.io/controller-runtime/pkg/client"
)
// +kubebuilder:rbac:groups="",resources=services,verbs=list;get;watch;patch
// +kubebuilder:rbac:groups="",resources=endpoints,verbs=list;get;watch;patch
// +kubebuilder:rbac:groups="",resources=namespaces,verbs=list;get;watch;patch
// +kubebuilder:rbac:groups="",resources=pods,verbs=list;get;watch
// +kubebuilder:rbac:groups=apps,resources=deployments,verbs=list;get;watch;patch
// +kubebuilder:rbac:groups=*,resources=ingresses,verbs=get;list;watch;update;patch
// +kubebuilder:rbac:groups=*,resources=ingress/status,verbs=get;update;patch
// +kubebuilder:rbac:groups=batch,resources=cronjobs,verbs=list;get;update;patch
// ServiceIdler will run the Service idler process.
func (h *Idler) ServiceIdler() {
ctx := context.Background()
opLog := h.Log
// in kubernetes, we can reliably check for the existence of this label so that
// we only check namespaces that have been deployed by a lagoon at one point
labelRequirements := generateLabelRequirements(h.Selectors.Service.Namespace)
// only evaluate namespaces that are not idled
// @TODO: reintroduce this later on, since there are some cases where an environment is unidled where this
// does not get changed currently
// selector := generateSelector(idlerSelector{
// Name: "idling.amazee.io/idled",
// Operator: selection.NotEquals,
// Values: []string{
// "true",
// },
// })
// labelRequirements = append(labelRequirements, *selector)
listOption := (&client.ListOptions{}).ApplyOptions([]client.ListOption{
client.MatchingLabelsSelector{
Selector: labels.NewSelector().Add(labelRequirements...),
},
})
// get the namespaces in the cluster
namespaces := &corev1.NamespaceList{}
if err := h.Client.List(ctx, namespaces, listOption); err != nil {
opLog.Info(fmt.Sprintf("unable to get any namespaces: %v", err))
return
}
// loop over the namespaces
for _, namespace := range namespaces.Items {
projectAutoIdle, ok1 := namespace.Labels[h.Selectors.NamespaceSelectorsLabels.ProjectIdling]
environmentAutoIdle, ok2 := namespace.Labels[h.Selectors.NamespaceSelectorsLabels.EnvironmentIdling]
environmentType, ok3 := namespace.Labels[h.Selectors.NamespaceSelectorsLabels.EnvironmentType]
if ok1 && ok2 && ok3 {
if environmentAutoIdle == "1" && projectAutoIdle == "1" {
envOpLog := opLog.WithValues("namespace", namespace.Name).
WithValues("project", namespace.Labels[h.Selectors.NamespaceSelectorsLabels.ProjectName]).
WithValues("environment", namespace.Labels[h.Selectors.NamespaceSelectorsLabels.EnvironmentName]).
WithValues("dry-run", h.DryRun)
envOpLog.Info("Checking namespace")
h.KubernetesServiceIdler(ctx, envOpLog, namespace, namespace.Labels[h.Selectors.NamespaceSelectorsLabels.ProjectName], false, false)
} else if h.Debug {
opLog.Info(fmt.Sprintf("skipping namespace %s; type is %s, autoidle values are env:%s proj:%s",
namespace.Name,
environmentType,
environmentAutoIdle,
projectAutoIdle))
}
} else {
if h.Debug {
opLog.Info(fmt.Sprintf("skipping namespace %s; not in lagoon",
namespace.Name))
}
}
}
}