forked from cloudnative-pg/cloudnative-pg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoperator.go
265 lines (231 loc) · 7.5 KB
/
operator.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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
/*
Copyright The CloudNativePG Contributors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package utils
import (
"bufio"
"encoding/json"
"fmt"
"os"
"path/filepath"
"time"
"github.com/avast/retry-go/v4"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/rand"
ctrlclient "sigs.k8s.io/controller-runtime/pkg/client"
apiv1 "github.com/cloudnative-pg/cloudnative-pg/api/v1"
"github.com/cloudnative-pg/cloudnative-pg/pkg/utils"
)
// ReloadOperatorDeployment finds and deletes the operator pod. Returns
// error if the new pod is not ready within a defined timeout
func ReloadOperatorDeployment(env *TestingEnvironment, timeoutSeconds uint) error {
operatorPod, err := env.GetOperatorPod()
if err != nil {
return err
}
zero := int64(0)
err = env.Client.Delete(env.Ctx, &operatorPod,
&ctrlclient.DeleteOptions{GracePeriodSeconds: &zero},
)
if err != nil {
return err
}
err = retry.Do(
func() error {
ready, err := env.IsOperatorReady()
if err != nil {
return err
}
if !ready {
return fmt.Errorf("operator pod %v is not ready", operatorPod.Name)
}
return nil
},
retry.Delay(time.Second),
retry.Attempts(timeoutSeconds),
)
return err
}
// DumpOperator logs the JSON for the deployment in an operator namespace, its pods and endpoints
func (env TestingEnvironment) DumpOperator(namespace string, filename string) {
f, err := os.Create(filepath.Clean(filename))
if err != nil {
fmt.Println(err)
return
}
w := bufio.NewWriter(f)
deployment, _ := env.GetOperatorDeployment()
out, _ := json.MarshalIndent(deployment, "", " ")
_, _ = fmt.Fprintf(w, "Dumping %v/%v deployment\n", namespace, deployment.Name)
_, _ = fmt.Fprintln(w, string(out))
podList, _ := env.GetPodList(namespace)
for _, pod := range podList.Items {
out, _ := json.MarshalIndent(pod, "", " ")
_, _ = fmt.Fprintf(w, "Dumping %v/%v pod\n", namespace, pod.Name)
_, _ = fmt.Fprintln(w, string(out))
}
err = w.Flush()
if err != nil {
fmt.Println(err)
return
}
_ = f.Sync()
_ = f.Close()
}
// GetOperatorDeployment returns the operator Deployment if there is a single one running, error otherwise
func (env TestingEnvironment) GetOperatorDeployment() (appsv1.Deployment, error) {
const operatorDeploymentName = "cnpg-controller-manager"
deploymentList := &appsv1.DeploymentList{}
if err := GetObjectList(&env, deploymentList,
ctrlclient.MatchingLabels{"app.kubernetes.io/name": "cloudnative-pg"},
); err != nil {
return appsv1.Deployment{}, err
}
// We check if we have one or more deployments
switch {
case len(deploymentList.Items) > 1:
err := fmt.Errorf("number of operator deployments != 1")
return appsv1.Deployment{}, err
case len(deploymentList.Items) == 1:
return deploymentList.Items[0], nil
}
if err := GetObjectList(
&env,
deploymentList,
ctrlclient.HasLabels{"operators.coreos.com/cloudnative-pg.openshift-operators"},
); err != nil {
return appsv1.Deployment{}, err
}
// We check if we have one or more deployments
switch {
case len(deploymentList.Items) > 1:
err := fmt.Errorf("number of operator deployments != 1")
return appsv1.Deployment{}, err
case len(deploymentList.Items) == 1:
return deploymentList.Items[0], nil
}
// This is for deployments created before 1.4.0
if err := GetObjectList(
&env, deploymentList, ctrlclient.MatchingFields{"metadata.name": operatorDeploymentName},
); err != nil {
return appsv1.Deployment{}, err
}
if len(deploymentList.Items) != 1 {
err := fmt.Errorf("number of %v deployments != 1", operatorDeploymentName)
return appsv1.Deployment{}, err
}
return deploymentList.Items[0], nil
}
// GetOperatorPod returns the operator pod if there is a single one running, error otherwise
func (env TestingEnvironment) GetOperatorPod() (corev1.Pod, error) {
podList := &corev1.PodList{}
// This will work for newer version of the operator, which are using
// our custom label
if err := GetObjectList(
&env, podList, ctrlclient.MatchingLabels{"app.kubernetes.io/name": "cloudnative-pg"}); err != nil {
return corev1.Pod{}, err
}
activePods := utils.FilterActivePods(podList.Items)
switch {
case len(activePods) > 1:
err := fmt.Errorf("number of running operator pods greater than 1: %v pods running", len(activePods))
return corev1.Pod{}, err
case len(activePods) == 1:
return activePods[0], nil
}
operatorNamespace, err := env.GetOperatorNamespaceName()
if err != nil {
return corev1.Pod{}, err
}
// This will work for older version of the operator, which are using
// the default label from kube-builder
if err := GetObjectList(
&env, podList,
ctrlclient.MatchingLabels{"control-plane": "controller-manager"},
ctrlclient.InNamespace(operatorNamespace)); err != nil {
return corev1.Pod{}, err
}
activePods = utils.FilterActivePods(podList.Items)
if len(activePods) != 1 {
err := fmt.Errorf("number of running operator different than 1: %v pods running", len(activePods))
return corev1.Pod{}, err
}
return podList.Items[0], nil
}
// GetOperatorNamespaceName returns the namespace the operator Deployment is running in
func (env TestingEnvironment) GetOperatorNamespaceName() (string, error) {
deployment, err := env.GetOperatorDeployment()
if err != nil {
return "", err
}
return deployment.GetNamespace(), err
}
// IsOperatorReady ensures that the operator will be ready.
func (env TestingEnvironment) IsOperatorReady() (bool, error) {
pod, err := env.GetOperatorPod()
if err != nil {
return false, err
}
isPodReady := utils.IsPodReady(pod)
if !isPodReady {
return false, err
}
namespace := pod.Namespace
// Detect if we are running under OLM
var webhookManagedByOLM bool
for _, envVar := range pod.Spec.Containers[0].Env {
if envVar.Name == "WEBHOOK_CERT_DIR" {
webhookManagedByOLM = true
}
}
// If the operator is managing certificates for webhooks, check that the setup is completed
if !webhookManagedByOLM {
err = CheckWebhookReady(&env, namespace)
if err != nil {
return false, err
}
}
// Dry run object creation to check that webhook Service is correctly running
testCluster := &apiv1.Cluster{
ObjectMeta: metav1.ObjectMeta{
Name: "readiness-check-" + rand.String(5),
Namespace: "default",
},
Spec: apiv1.ClusterSpec{
Instances: 3,
StorageConfiguration: apiv1.StorageConfiguration{
Size: "1Gi",
},
},
}
_, err = CreateObject(&env, testCluster, &ctrlclient.CreateOptions{DryRun: []string{metav1.DryRunAll}})
if err != nil {
return false, err
}
return true, err
}
// OperatorPodRenamed checks if the operator pod was renamed
func OperatorPodRenamed(operatorPod corev1.Pod, expectedOperatorPodName string) bool {
return operatorPod.GetName() != expectedOperatorPodName
}
// OperatorPodRestarted checks if the operator pod was restarted
func OperatorPodRestarted(operatorPod corev1.Pod) bool {
restartCount := 0
for _, containerStatus := range operatorPod.Status.ContainerStatuses {
if containerStatus.Name == "manager" {
restartCount = int(containerStatus.RestartCount)
}
}
return restartCount != 0
}