-
Notifications
You must be signed in to change notification settings - Fork 76
Expand file tree
/
Copy pathutil.go
More file actions
159 lines (135 loc) · 4.32 KB
/
util.go
File metadata and controls
159 lines (135 loc) · 4.32 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
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
package k8s
import (
"context"
"crypto/sha256"
"encoding/hex"
"encoding/json"
"fmt"
"os"
"strings"
"github.com/pkg/errors"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/controller-runtime/pkg/client"
"github.com/percona/percona-postgresql-operator/percona/naming"
"github.com/percona/percona-postgresql-operator/pkg/apis/postgres-operator.crunchydata.com/v1beta1"
)
const WatchNamespaceEnvVar = "WATCH_NAMESPACE"
// GetWatchNamespace returns the namespace the operator should be watching for changes
func GetWatchNamespace() (string, error) {
// This is needed in order to preserve backwards compatibility with the
// users that are using the PGO_TARGET_NAMESPACE env var.
ns, found := os.LookupEnv("PGO_TARGET_NAMESPACE")
if found {
return ns, nil
}
ns, found = os.LookupEnv(WatchNamespaceEnvVar)
if !found {
return "", fmt.Errorf("%s must be set", WatchNamespaceEnvVar)
}
return ns, nil
}
func InitContainer(componentName, image string,
pullPolicy corev1.PullPolicy,
secCtx *corev1.SecurityContext,
resources corev1.ResourceRequirements,
component ComponentWithInit,
) corev1.Container {
if component != nil && component.GetInitContainer() != nil && component.GetInitContainer().Resources != nil {
resources = *component.GetInitContainer().Resources
}
if component != nil && component.GetInitContainer() != nil && component.GetInitContainer().ContainerSecurityContext != nil {
secCtx = component.GetInitContainer().ContainerSecurityContext
}
volumeMounts := []corev1.VolumeMount{
{
Name: naming.CrunchyBinVolumeName,
MountPath: naming.CrunchyBinVolumePath,
},
}
return corev1.Container{
Name: componentName + "-init",
Image: image,
ImagePullPolicy: pullPolicy,
VolumeMounts: volumeMounts,
Command: []string{"/usr/local/bin/init-entrypoint.sh"},
TerminationMessagePath: "/dev/termination-log",
TerminationMessagePolicy: corev1.TerminationMessageReadFile,
SecurityContext: secCtx,
Resources: resources,
}
}
type ComponentWithInit interface {
GetInitContainer() *v1beta1.InitContainerSpec
}
func InitImage(ctx context.Context, cl client.Reader, cluster *v1beta1.PostgresCluster, componentWithInit ComponentWithInit) (string, error) {
if componentWithInit != nil && componentWithInit.GetInitContainer() != nil && componentWithInit.GetInitContainer().Image != "" {
return componentWithInit.GetInitContainer().Image, nil
}
if cluster != nil && cluster.Spec.InitContainer != nil && len(cluster.Spec.InitContainer.Image) > 0 {
return cluster.Spec.InitContainer.Image, nil
}
return operatorImage(ctx, cl)
}
func operatorImage(ctx context.Context, cl client.Reader) (string, error) {
pod, err := operatorPod(ctx, cl)
if err != nil {
return "", errors.Wrap(err, "get operator pod")
}
for _, container := range pod.Spec.Containers {
if container.Name == "operator" {
return container.Image, nil
}
}
return "", errors.New("manager container not found")
}
func operatorPod(ctx context.Context, cl client.Reader) (*corev1.Pod, error) {
ns, err := GetOperatorNamespace()
if err != nil {
return nil, errors.Wrap(err, "get namespace")
}
pod := new(corev1.Pod)
nn := types.NamespacedName{
Namespace: ns,
Name: os.Getenv("HOSTNAME"),
}
if err := cl.Get(ctx, nn, pod); err != nil {
return nil, err
}
return pod, nil
}
// GetOperatorNamespace returns the namespace of the operator pod
func GetOperatorNamespace() (string, error) {
ns, found := os.LookupEnv("OPERATOR_NAMESPACE")
if found {
return ns, nil
}
nsBytes, err := os.ReadFile("/var/run/secrets/kubernetes.io/serviceaccount/namespace")
if err != nil {
return "", err
}
return strings.TrimSpace(string(nsBytes)), nil
}
func ObjectHash(obj runtime.Object) (string, error) {
var dataToMarshal interface{}
switch object := obj.(type) {
case *appsv1.StatefulSet:
dataToMarshal = object.Spec
case *appsv1.Deployment:
dataToMarshal = object.Spec
case *corev1.Service:
dataToMarshal = object.Spec
case *corev1.Secret:
dataToMarshal = object.Data
default:
dataToMarshal = obj
}
data, err := json.Marshal(dataToMarshal)
if err != nil {
return "", err
}
hash := sha256.Sum256(data)
return hex.EncodeToString(hash[:]), nil
}