Skip to content

Commit 7a416e5

Browse files
authored
[Feature] Detach PVC in Ordered indexing (#1118)
1 parent 8cc41cd commit 7a416e5

File tree

14 files changed

+173
-35
lines changed

14 files changed

+173
-35
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
- (Bugfix) Fix and document action timeouts
77
- (Feature) Propagate sidecars' ports to a member's service
88
- (Debug Package) Initial commit
9+
- (Feature) Detach PVC from deployment in Ordered indexing method
910

1011
## [1.2.16](https://github.com/arangodb/kube-arangodb/tree/1.2.16) (2022-09-14)
1112
- (Feature) Add ArangoDeployment ServerGroupStatus

pkg/apis/deployment/v1/deployment.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525

2626
"github.com/arangodb/kube-arangodb/pkg/apis/deployment"
2727
"github.com/arangodb/kube-arangodb/pkg/util/errors"
28+
"github.com/arangodb/kube-arangodb/pkg/util/k8sutil/tools"
2829
)
2930

3031
// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
@@ -49,6 +50,10 @@ type ArangoDeployment struct {
4950
Status DeploymentStatus `json:"status,omitempty"`
5051
}
5152

53+
func (d *ArangoDeployment) OwnerOf(in meta.Object) bool {
54+
return tools.IsOwner(d.AsOwner(), in)
55+
}
56+
5257
type ServerGroupFunc func(ServerGroup, ServerGroupSpec, *MemberStatusList) error
5358

5459
// AsOwner creates an OwnerReference for the given deployment

pkg/apis/deployment/v2alpha1/deployment.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525

2626
"github.com/arangodb/kube-arangodb/pkg/apis/deployment"
2727
"github.com/arangodb/kube-arangodb/pkg/util/errors"
28+
"github.com/arangodb/kube-arangodb/pkg/util/k8sutil/tools"
2829
)
2930

3031
// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
@@ -49,6 +50,10 @@ type ArangoDeployment struct {
4950
Status DeploymentStatus `json:"status,omitempty"`
5051
}
5152

53+
func (d *ArangoDeployment) OwnerOf(in meta.Object) bool {
54+
return tools.IsOwner(d.AsOwner(), in)
55+
}
56+
5257
type ServerGroupFunc func(ServerGroup, ServerGroupSpec, *MemberStatusList) error
5358

5459
// AsOwner creates an OwnerReference for the given deployment

pkg/apis/replication/v1/replication.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
meta "k8s.io/apimachinery/pkg/apis/meta/v1"
2525

2626
"github.com/arangodb/kube-arangodb/pkg/apis/replication"
27+
"github.com/arangodb/kube-arangodb/pkg/util/k8sutil/tools"
2728
)
2829

2930
// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
@@ -49,6 +50,10 @@ type ArangoDeploymentReplication struct {
4950
Status DeploymentReplicationStatus `json:"status"`
5051
}
5152

53+
func (d *ArangoDeploymentReplication) OwnerOf(in meta.Object) bool {
54+
return tools.IsOwner(d.AsOwner(), in)
55+
}
56+
5257
// AsOwner creates an OwnerReference for the given replication
5358
func (d *ArangoDeploymentReplication) AsOwner() meta.OwnerReference {
5459
trueVar := true

pkg/apis/replication/v2alpha1/replication.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
meta "k8s.io/apimachinery/pkg/apis/meta/v1"
2525

2626
"github.com/arangodb/kube-arangodb/pkg/apis/replication"
27+
"github.com/arangodb/kube-arangodb/pkg/util/k8sutil/tools"
2728
)
2829

2930
// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
@@ -49,6 +50,10 @@ type ArangoDeploymentReplication struct {
4950
Status DeploymentReplicationStatus `json:"status"`
5051
}
5152

53+
func (d *ArangoDeploymentReplication) OwnerOf(in meta.Object) bool {
54+
return tools.IsOwner(d.AsOwner(), in)
55+
}
56+
5257
// AsOwner creates an OwnerReference for the given replication
5358
func (d *ArangoDeploymentReplication) AsOwner() meta.OwnerReference {
5459
trueVar := true

pkg/apis/storage/v1alpha/local_storage.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ package v1alpha
2222

2323
import (
2424
meta "k8s.io/apimachinery/pkg/apis/meta/v1"
25+
26+
"github.com/arangodb/kube-arangodb/pkg/util/k8sutil/tools"
2527
)
2628

2729
// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
@@ -48,6 +50,10 @@ type ArangoLocalStorage struct {
4850
Status LocalStorageStatus `json:"status"`
4951
}
5052

53+
func (d *ArangoLocalStorage) OwnerOf(in meta.Object) bool {
54+
return tools.IsOwner(d.AsOwner(), in)
55+
}
56+
5157
// AsOwner creates an OwnerReference for the given storage
5258
func (d *ArangoLocalStorage) AsOwner() meta.OwnerReference {
5359
return meta.OwnerReference{

pkg/deployment/reconcile/plan_builder_tls.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ import (
4343
"github.com/arangodb/kube-arangodb/pkg/util/k8sutil"
4444
inspectorInterface "github.com/arangodb/kube-arangodb/pkg/util/k8sutil/inspector"
4545
memberTls "github.com/arangodb/kube-arangodb/pkg/util/k8sutil/tls"
46+
"github.com/arangodb/kube-arangodb/pkg/util/k8sutil/tools"
4647
)
4748

4849
const CertificateRenewalMargin = 7 * 24 * time.Hour
@@ -207,7 +208,7 @@ func (r *Reconciler) createCARenewalPlan(ctx context.Context, apiObject k8sutil.
207208
return nil
208209
}
209210

210-
if !k8sutil.IsOwner(apiObject.AsOwner(), caSecret) {
211+
if !tools.IsOwner(apiObject.AsOwner(), caSecret) {
211212
r.planLogger.Str("secret", spec.TLS.GetCASecretName()).Debug("CA Secret is not owned by Operator, we wont do anything")
212213
return nil
213214
}

pkg/deployment/resources/annotations.go

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ import (
3737
"github.com/arangodb/kube-arangodb/pkg/util/globals"
3838
"github.com/arangodb/kube-arangodb/pkg/util/k8sutil"
3939
inspectorInterface "github.com/arangodb/kube-arangodb/pkg/util/k8sutil/inspector"
40+
"github.com/arangodb/kube-arangodb/pkg/util/k8sutil/tools"
4041
)
4142

4243
type PatchFunc func(name string, d []byte) error
@@ -180,7 +181,7 @@ func (r *Resources) ensureSecretsAnnotations(patch PatchFunc, cachedStatus inspe
180181
r.ensureAnnotationsMap(secret.Kind, secret, spec, patch)
181182
return nil
182183
}, func(secret *core.Secret) bool {
183-
return k8sutil.IsChildResource(kind, name, namespace, secret)
184+
return tools.IsChildResource(kind, name, namespace, secret)
184185
}); err != nil {
185186
return err
186187
}
@@ -193,7 +194,7 @@ func (r *Resources) ensureServiceAccountsAnnotations(patch PatchFunc, cachedStat
193194
r.ensureAnnotationsMap(serviceAccount.Kind, serviceAccount, spec, patch)
194195
return nil
195196
}, func(serviceAccount *core.ServiceAccount) bool {
196-
return k8sutil.IsChildResource(kind, name, namespace, serviceAccount)
197+
return tools.IsChildResource(kind, name, namespace, serviceAccount)
197198
}); err != nil {
198199
return err
199200
}
@@ -206,7 +207,7 @@ func (r *Resources) ensureServicesAnnotations(patch PatchFunc, cachedStatus insp
206207
r.ensureAnnotationsMap(service.Kind, service, spec, patch)
207208
return nil
208209
}, func(service *core.Service) bool {
209-
return k8sutil.IsChildResource(kind, name, namespace, service)
210+
return tools.IsChildResource(kind, name, namespace, service)
210211
}); err != nil {
211212
return err
212213
}
@@ -221,7 +222,7 @@ func (r *Resources) ensurePdbsAnnotations(patch PatchFunc, cachedStatus inspecto
221222
r.ensureAnnotationsMap(podDisruptionBudget.Kind, podDisruptionBudget, spec, patch)
222223
return nil
223224
}, func(podDisruptionBudget *policyv1.PodDisruptionBudget) bool {
224-
return k8sutil.IsChildResource(kind, name, namespace, podDisruptionBudget)
225+
return tools.IsChildResource(kind, name, namespace, podDisruptionBudget)
225226
}); err != nil {
226227
return err
227228
}
@@ -237,7 +238,7 @@ func (r *Resources) ensurePdbsAnnotations(patch PatchFunc, cachedStatus inspecto
237238
r.ensureAnnotationsMap(podDisruptionBudget.Kind, podDisruptionBudget, spec, patch)
238239
return nil
239240
}, func(podDisruptionBudget *policyv1beta1.PodDisruptionBudget) bool {
240-
return k8sutil.IsChildResource(kind, name, namespace, podDisruptionBudget)
241+
return tools.IsChildResource(kind, name, namespace, podDisruptionBudget)
241242
}); err != nil {
242243
return err
243244
}
@@ -250,7 +251,7 @@ func (r *Resources) ensurePvcsAnnotations(patch PatchFunc, cachedStatus inspecto
250251
r.ensureGroupAnnotationsMap(persistentVolumeClaim.Kind, persistentVolumeClaim, spec, patch)
251252
return nil
252253
}, func(persistentVolumeClaim *core.PersistentVolumeClaim) bool {
253-
return k8sutil.IsChildResource(kind, name, namespace, persistentVolumeClaim)
254+
return tools.IsChildResource(kind, name, namespace, persistentVolumeClaim)
254255
}); err != nil {
255256
return err
256257
}
@@ -270,7 +271,7 @@ func (r *Resources) ensureServiceMonitorsAnnotations(patch PatchFunc, cachedStat
270271
r.ensureAnnotationsMap(serviceMonitor.Kind, serviceMonitor, spec, patch)
271272
return nil
272273
}, func(serviceMonitor *monitoring.ServiceMonitor) bool {
273-
return k8sutil.IsChildResource(kind, name, namespace, serviceMonitor)
274+
return tools.IsChildResource(kind, name, namespace, serviceMonitor)
274275
}); err != nil {
275276
return err
276277
}
@@ -298,7 +299,7 @@ func (r *Resources) ensurePodsAnnotations(patch PatchFunc, cachedStatus inspecto
298299
r.ensureGroupAnnotationsMap(pod.Kind, pod, spec, patch)
299300
return nil
300301
}, func(pod *core.Pod) bool {
301-
return k8sutil.IsChildResource(kind, name, namespace, pod)
302+
return tools.IsChildResource(kind, name, namespace, pod)
302303
}); err != nil {
303304
return err
304305
}
@@ -307,7 +308,7 @@ func (r *Resources) ensurePodsAnnotations(patch PatchFunc, cachedStatus inspecto
307308
}
308309

309310
func (r *Resources) isChildResource(obj meta.Object) bool {
310-
return k8sutil.IsChildResource(deployment.ArangoDeploymentResourceKind,
311+
return tools.IsChildResource(deployment.ArangoDeploymentResourceKind,
311312
r.context.GetAPIObject().GetName(),
312313
r.context.GetAPIObject().GetNamespace(),
313314
obj)

pkg/deployment/resources/pvc_inspector.go

Lines changed: 49 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import (
2828
meta "k8s.io/apimachinery/pkg/apis/meta/v1"
2929
"k8s.io/apimachinery/pkg/types"
3030

31+
api "github.com/arangodb/kube-arangodb/pkg/apis/deployment/v1"
3132
"github.com/arangodb/kube-arangodb/pkg/deployment/patch"
3233
"github.com/arangodb/kube-arangodb/pkg/metrics"
3334
"github.com/arangodb/kube-arangodb/pkg/util"
@@ -66,6 +67,12 @@ func (r *Resources) InspectPVCs(ctx context.Context, cachedStatus inspectorInter
6667
memberStatus, group, found := status.Members.MemberStatusByPVCName(pvc.GetName())
6768
if !found {
6869
log.Str("pvc", pvc.GetName()).Debug("no memberstatus found for PVC")
70+
71+
if !r.context.GetAPIObject().OwnerOf(pvc) {
72+
log.Str("pvc", pvc.GetName()).Debug("PVC is not owned by us")
73+
return nil
74+
}
75+
6976
if k8sutil.IsPersistentVolumeClaimMarkedForDeletion(pvc) && len(pvc.GetFinalizers()) > 0 {
7077
// Strange, pvc belongs to us, but we have no member for it.
7178
// Remove all finalizers, so it can be removed.
@@ -79,9 +86,23 @@ func (r *Resources) InspectPVCs(ctx context.Context, cachedStatus inspectorInter
7986
return nil
8087
}
8188

89+
groupSpec := r.context.GetSpec().GetServerGroupSpec(group)
90+
8291
owner := r.context.GetAPIObject().AsOwner()
83-
if k8sutil.UpdateOwnerRefToObjectIfNeeded(pvc.GetObjectMeta(), &owner) {
84-
q := patch.NewPatch(patch.ItemReplace(patch.NewPath("metadata", "ownerReferences"), pvc.ObjectMeta.OwnerReferences))
92+
93+
ownerUpdate := k8sutil.UpdateOwnerRefToObjectIfNeeded
94+
if groupSpec.IndexMethod.Get() == api.ServerGroupIndexMethodOrdered {
95+
ownerUpdate = k8sutil.RemoveOwnerRefToObjectIfNeeded
96+
}
97+
98+
if ownerUpdate(pvc.GetObjectMeta(), &owner) {
99+
q := patch.NewPatch()
100+
if f := pvc.ObjectMeta.OwnerReferences; len(f) == 0 {
101+
q.Add(patch.ItemRemove(patch.NewPath("metadata", "ownerReferences")))
102+
} else {
103+
q.Add(patch.ItemReplace(patch.NewPath("metadata", "ownerReferences"), pvc.ObjectMeta.OwnerReferences))
104+
}
105+
85106
d, err := q.Marshal()
86107
if err != nil {
87108
log.Err(err).Debug("Failed to prepare PVC patch (ownerReferences)")
@@ -107,6 +128,32 @@ func (r *Resources) InspectPVCs(ctx context.Context, cachedStatus inspectorInter
107128
} else {
108129
nextInterval = nextInterval.ReduceTo(x)
109130
}
131+
} else {
132+
// Ensure finalizers
133+
if r.ensurePVCFinalizers(pvc) {
134+
q := patch.NewPatch()
135+
if f := pvc.Finalizers; len(f) == 0 {
136+
q.Add(patch.ItemRemove(patch.NewPath("metadata", "finalizers")))
137+
} else {
138+
q.Add(patch.ItemReplace(patch.NewPath("metadata", "finalizers"), f))
139+
}
140+
141+
d, err := q.Marshal()
142+
if err != nil {
143+
log.Err(err).Debug("Failed to prepare PVC patch (finalizers)")
144+
return errors.WithStack(err)
145+
}
146+
147+
err = globals.GetGlobalTimeouts().Kubernetes().RunWithTimeout(ctx, func(ctxChild context.Context) error {
148+
_, err := cachedStatus.PersistentVolumeClaimsModInterface().V1().Patch(ctxChild, pvc.GetName(), types.JSONPatchType, d, meta.PatchOptions{})
149+
return err
150+
})
151+
152+
if err != nil {
153+
log.Err(err).Debug("Failed to update PVC (ownerReferences)")
154+
return errors.WithStack(err)
155+
}
156+
}
110157
}
111158

112159
return nil

pkg/deployment/resources/pvcs.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ package resources
2323
import (
2424
"context"
2525

26+
meta "k8s.io/apimachinery/pkg/apis/meta/v1"
27+
2628
api "github.com/arangodb/kube-arangodb/pkg/apis/deployment/v1"
2729
"github.com/arangodb/kube-arangodb/pkg/util/constants"
2830
"github.com/arangodb/kube-arangodb/pkg/util/errors"
@@ -32,10 +34,14 @@ import (
3234
)
3335

3436
// createPVCFinalizers creates a list of finalizers for a PVC created for the given group.
35-
func (r *Resources) createPVCFinalizers(group api.ServerGroup) []string {
37+
func (r *Resources) createPVCFinalizers() []string {
3638
return []string{constants.FinalizerPVCMemberExists}
3739
}
3840

41+
func (r *Resources) ensurePVCFinalizers(in meta.Object) bool {
42+
return k8sutil.EnsureFinalizers(in, r.createPVCFinalizers(), nil)
43+
}
44+
3945
// EnsurePVCs creates all PVC's listed in member status
4046
func (r *Resources) EnsurePVCs(ctx context.Context, cachedStatus inspectorInterface.Inspector) error {
4147
apiObject := r.context.GetAPIObject()
@@ -59,7 +65,7 @@ func (r *Resources) EnsurePVCs(ctx context.Context, cachedStatus inspectorInterf
5965
role := group.AsRole()
6066
resources := spec.Resources
6167
vct := spec.VolumeClaimTemplate
62-
finalizers := r.createPVCFinalizers(group)
68+
finalizers := r.createPVCFinalizers()
6369
err := globals.GetGlobalTimeouts().Kubernetes().RunWithTimeout(ctx, func(ctxChild context.Context) error {
6470
return k8sutil.CreatePersistentVolumeClaim(ctxChild, cachedStatus.PersistentVolumeClaimsModInterface().V1(),
6571
m.PersistentVolumeClaimName, deploymentName, storageClassName, role, enforceAntiAffinity,

0 commit comments

Comments
 (0)