-
Notifications
You must be signed in to change notification settings - Fork 29
/
autodiscover_podset.go
170 lines (160 loc) · 6.53 KB
/
autodiscover_podset.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
// Copyright (C) 2020-2024 Red Hat, Inc.
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License along
// with this program; if not, write to the Free Software Foundation, Inc.,
// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
package autodiscover
import (
"context"
"github.com/test-network-function/cnf-certification-test/internal/log"
appsv1 "k8s.io/api/apps/v1"
scalingv1 "k8s.io/api/autoscaling/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/client-go/kubernetes"
appv1client "k8s.io/client-go/kubernetes/typed/apps/v1"
"k8s.io/client-go/scale"
)
func FindDeploymentByNameByNamespace(appClient appv1client.AppsV1Interface, namespace, name string) (*appsv1.Deployment, error) {
dp, err := appClient.Deployments(namespace).Get(context.TODO(), name, metav1.GetOptions{})
if err != nil {
log.Error("Cannot retrieve deployment in ns=%s name=%s", namespace, name)
return nil, err
}
return dp, nil
}
func FindStatefulsetByNameByNamespace(appClient appv1client.AppsV1Interface, namespace, name string) (*appsv1.StatefulSet, error) {
ss, err := appClient.StatefulSets(namespace).Get(context.TODO(), name, metav1.GetOptions{})
if err != nil {
log.Error("Cannot retrieve deployment in ns=%s name=%s", namespace, name)
return nil, err
}
return ss, nil
}
func FindCrObjectByNameByNamespace(scalesGetter scale.ScalesGetter, ns, name string, groupResourceSchema schema.GroupResource) (*scalingv1.Scale, error) {
crScale, err := scalesGetter.Scales(ns).Get(context.TODO(), groupResourceSchema, name, metav1.GetOptions{})
if err != nil {
log.Error("Cannot retrieve deployment in ns=%s name=%s", ns, name)
return nil, err
}
return crScale, nil
}
func isDeploymentsPodsMatchingAtLeastOneLabel(labels []labelObject, namespace string, deployment *appsv1.Deployment) bool {
for _, aLabelObject := range labels {
log.Debug("Searching pods in deployment %q found in ns %q using label %s=%s", deployment.Name, namespace, aLabelObject.LabelKey, aLabelObject.LabelValue)
if deployment.Spec.Template.ObjectMeta.Labels[aLabelObject.LabelKey] == aLabelObject.LabelValue {
log.Info("Deployment %s found in ns=%s", deployment.Name, namespace)
return true
}
}
return false
}
//nolint:dupl
func findDeploymentsByLabels(
appClient appv1client.AppsV1Interface,
labels []labelObject,
namespaces []string,
) []appsv1.Deployment {
allDeployments := []appsv1.Deployment{}
for _, ns := range namespaces {
dps, err := appClient.Deployments(ns).List(context.TODO(), metav1.ListOptions{})
if err != nil {
log.Error("Failed to list deployments in ns=%s, err: %v . Trying to proceed.", ns, err)
continue
}
if len(dps.Items) == 0 {
log.Warn("Did not find any deployments in ns=%s", ns)
}
for i := 0; i < len(dps.Items); i++ {
if len(labels) > 0 {
// The deployment is added only once if at least one pod matches one label in the Deployment
if isDeploymentsPodsMatchingAtLeastOneLabel(labels, ns, &dps.Items[i]) {
allDeployments = append(allDeployments, dps.Items[i])
continue
}
} else {
// If labels are not provided, all deployments in the namespaces under test, are tested by the CNF suite
log.Debug("Searching pods in deployment %q found in ns %q without label", dps.Items[i].Name, ns)
allDeployments = append(allDeployments, dps.Items[i])
log.Info("Deployment %s found in ns=%s", dps.Items[i].Name, ns)
}
}
}
if len(allDeployments) == 0 {
log.Warn("Did not find any deployment in the configured namespaces %v", namespaces)
}
return allDeployments
}
func isStatefulSetsMatchingAtLeastOneLabel(labels []labelObject, namespace string, statefulSet *appsv1.StatefulSet) bool {
for _, aLabelObject := range labels {
log.Debug("Searching pods in statefulset %q found in ns %q using label %s=%s", statefulSet.Name, namespace, aLabelObject.LabelKey, aLabelObject.LabelValue)
if statefulSet.Spec.Template.ObjectMeta.Labels[aLabelObject.LabelKey] == aLabelObject.LabelValue {
log.Info("StatefulSet %s found in ns=%s", statefulSet.Name, namespace)
return true
}
}
return false
}
//nolint:dupl
func findStatefulSetsByLabels(
appClient appv1client.AppsV1Interface,
labels []labelObject,
namespaces []string,
) []appsv1.StatefulSet {
allStatefulSets := []appsv1.StatefulSet{}
for _, ns := range namespaces {
statefulSet, err := appClient.StatefulSets(ns).List(context.TODO(), metav1.ListOptions{})
if err != nil {
log.Error("Failed to list statefulsets in ns=%s, err: %v . Trying to proceed.", ns, err)
continue
}
if len(statefulSet.Items) == 0 {
log.Warn("Did not find any statefulSet in ns=%s", ns)
}
for i := 0; i < len(statefulSet.Items); i++ {
if len(labels) > 0 {
// The StatefulSet is added only once if at least one pod matches one label in the Statefulset
if isStatefulSetsMatchingAtLeastOneLabel(labels, ns, &statefulSet.Items[i]) {
allStatefulSets = append(allStatefulSets, statefulSet.Items[i])
continue
}
} else {
// If labels are not provided, all statefulsets in the namespaces under test, are tested by the CNF suite
log.Debug("Searching pods in statefulset %q found in ns %q without label", statefulSet.Items[i].Name, ns)
allStatefulSets = append(allStatefulSets, statefulSet.Items[i])
log.Info("StatefulSet %s found in ns=%s", statefulSet.Items[i].Name, ns)
}
}
}
if len(allStatefulSets) == 0 {
log.Warn("Did not find any statefulset in the configured namespaces %v", namespaces)
}
return allStatefulSets
}
func findHpaControllers(cs kubernetes.Interface, namespaces []string) []*scalingv1.HorizontalPodAutoscaler {
var m []*scalingv1.HorizontalPodAutoscaler
for _, ns := range namespaces {
hpas, err := cs.AutoscalingV1().HorizontalPodAutoscalers(ns).List(context.TODO(), metav1.ListOptions{})
if err != nil {
log.Error("Cannot list HorizontalPodAutoscalers on namespace %q, err: %v", ns, err)
return m
}
for i := 0; i < len(hpas.Items); i++ {
m = append(m, &hpas.Items[i])
}
}
if len(m) == 0 {
log.Info("Cannot find any deployed HorizontalPodAutoscaler")
}
return m
}