forked from CrunchyData/postgres-operator
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontroller_ref_manager.go
More file actions
200 lines (169 loc) · 7.03 KB
/
controller_ref_manager.go
File metadata and controls
200 lines (169 loc) · 7.03 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
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
// Copyright 2021 - 2025 Crunchy Data Solutions, Inc.
//
// SPDX-License-Identifier: Apache-2.0
package postgrescluster
import (
"context"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/util/workqueue"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/event"
"sigs.k8s.io/controller-runtime/pkg/handler"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
"github.com/crunchydata/postgres-operator/internal/controller/runtime"
"github.com/crunchydata/postgres-operator/internal/logging"
"github.com/crunchydata/postgres-operator/internal/naming"
"github.com/crunchydata/postgres-operator/pkg/apis/postgres-operator.crunchydata.com/v1beta1"
)
// adoptObject adopts the provided Object by adding controller owner refs for the provided
// PostgresCluster.
func (r *Reconciler) adoptObject(ctx context.Context, postgresCluster *v1beta1.PostgresCluster,
obj client.Object) error {
if err := r.setControllerReference(postgresCluster, obj); err != nil {
return err
}
patchBytes, err := runtime.NewMergePatch().
Add("metadata", "ownerReferences")(obj.GetOwnerReferences()).Bytes()
if err != nil {
return err
}
return r.Writer.Patch(ctx, obj, client.RawPatch(types.StrategicMergePatchType, patchBytes))
}
// claimObject is responsible for adopting or releasing Objects based on their current
// controller ownership and whether or not they meet the provided labeling requirements.
// This solution is modeled after the ControllerRefManager logic as found within the controller
// package in the Kubernetes source:
// https://github.com/kubernetes/kubernetes/blob/master/pkg/controller/controller_ref_manager.go
//
// TODO do a non-cache based get of the PostgresCluster prior to adopting anything to prevent
// race conditions with the garbage collector (see
// https://github.com/kubernetes/kubernetes/issues/42639)
func (r *Reconciler) claimObject(ctx context.Context, postgresCluster *v1beta1.PostgresCluster,
obj client.Object) error {
controllerRef := metav1.GetControllerOfNoCopy(obj)
if controllerRef != nil {
// if not owned by this postgrescluster then ignore
if controllerRef.UID != postgresCluster.GetUID() {
return nil
}
// If owned by this PostgresCluster and the proper PostgresCluster label is present then
// return true. Additional labels checks can be added here as needed to determine whether
// or not a StatefulSet is part of a PostgreSQL cluster and should be adopted or released.
if _, ok := obj.GetLabels()[naming.LabelCluster]; ok {
return nil
}
// If owned but selector doesn't match, then attempt to release. However, if the
// PostgresCluster is being deleted then simply return.
if postgresCluster.GetDeletionTimestamp() != nil {
return nil
}
if err := r.releaseObject(ctx, postgresCluster,
obj); client.IgnoreNotFound(err) != nil {
return err
}
// successfully released resource or resource no longer exists
return nil
}
// At this point the resource has no controller ref and is therefore an orphan. Ignore if
// either the PostgresCluster resource or the orphaned resource is being deleted, or if the selector
// for the orphaned resource doesn't include the proper PostgresCluster label
_, hasPGClusterLabel := obj.GetLabels()[naming.LabelCluster]
if postgresCluster.GetDeletionTimestamp() != nil || !hasPGClusterLabel {
return nil
}
if obj.GetDeletionTimestamp() != nil {
return nil
}
if err := r.adoptObject(ctx, postgresCluster, obj); err != nil {
// If adopt attempt failed because the resource no longer exists, then simply
// ignore. Otherwise return the error.
if apierrors.IsNotFound(err) {
return nil
}
return err
}
// successfully adopted resource
return nil
}
// getPostgresClusterForObject is responsible for obtaining the PostgresCluster associated
// with an Object.
func (r *Reconciler) getPostgresClusterForObject(ctx context.Context,
obj client.Object) (bool, *v1beta1.PostgresCluster, error) {
clusterName := ""
// first see if it has a PostgresCluster ownership ref or a PostgresCluster label
controllerRef := metav1.GetControllerOfNoCopy(obj)
if controllerRef != nil && controllerRef.Kind == "PostgresCluster" {
clusterName = controllerRef.Name
} else if _, ok := obj.GetLabels()[naming.LabelCluster]; ok {
clusterName = obj.GetLabels()[naming.LabelCluster]
}
if clusterName == "" {
return false, nil, nil
}
postgresCluster := &v1beta1.PostgresCluster{}
if err := r.Reader.Get(ctx, types.NamespacedName{
Name: clusterName,
Namespace: obj.GetNamespace(),
}, postgresCluster); err != nil {
if apierrors.IsNotFound(err) {
return false, nil, nil
}
return false, nil, err
}
return true, postgresCluster, nil
}
// manageControllerRefs is responsible for determining whether or not an attempt should be made
// to adopt or release/orphan an Object. This includes obtaining the PostgresCluster for
// the Object and then calling the logic needed to either adopt or release it.
func (r *Reconciler) manageControllerRefs(ctx context.Context,
obj client.Object) error {
found, postgresCluster, err := r.getPostgresClusterForObject(ctx, obj)
if err != nil {
return err
}
if !found {
return nil
}
return r.claimObject(ctx, postgresCluster, obj)
}
// releaseObject releases the provided Object set from ownership by the provided
// PostgresCluster. This is done by removing the PostgresCluster's controller owner
// refs from the Object.
func (r *Reconciler) releaseObject(ctx context.Context,
postgresCluster *v1beta1.PostgresCluster, obj client.Object) error {
// TODO create a strategic merge type instead of using Merge7386
patch, err := runtime.NewMergePatch().
Add("metadata", "ownerReferences")([]map[string]string{{
"$patch": "delete",
"uid": string(postgresCluster.GetUID()),
}}).Bytes()
if err != nil {
return err
}
return r.Writer.Patch(ctx, obj, client.RawPatch(types.StrategicMergePatchType, patch))
}
// controllerRefHandlerFuncs returns the handler funcs that should be utilized to watch
// StatefulSets within the cluster as needed to manage controller ownership refs.
func (r *Reconciler) controllerRefHandlerFuncs() *handler.Funcs {
log := logging.FromContext(context.Background())
errMsg := "managing StatefulSet controller refs"
return &handler.Funcs{
CreateFunc: func(ctx context.Context, updateEvent event.CreateEvent, workQueue workqueue.TypedRateLimitingInterface[reconcile.Request]) {
if err := r.manageControllerRefs(ctx, updateEvent.Object); err != nil {
log.Error(err, errMsg)
}
},
UpdateFunc: func(ctx context.Context, updateEvent event.UpdateEvent, workQueue workqueue.TypedRateLimitingInterface[reconcile.Request]) {
if err := r.manageControllerRefs(ctx, updateEvent.ObjectNew); err != nil {
log.Error(err, errMsg)
}
},
DeleteFunc: func(ctx context.Context, updateEvent event.DeleteEvent, workQueue workqueue.TypedRateLimitingInterface[reconcile.Request]) {
if err := r.manageControllerRefs(ctx, updateEvent.Object); err != nil {
log.Error(err, errMsg)
}
},
}
}