Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[KO-245] Deleting PVC of local volumes during node maintenance. #256

Closed
wants to merge 10 commits into from
8 changes: 6 additions & 2 deletions api/v1/aerospikecluster_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,6 @@ type AerospikeClusterSpec struct { //nolint:govet // for readability
SeedsFinderServices SeedsFinderServices `json:"seedsFinderServices,omitempty"`
// RosterNodeBlockList is a list of blocked nodeIDs from roster in a strong-consistency setup
RosterNodeBlockList []string `json:"rosterNodeBlockList,omitempty"`
// CleanLocalPVC is a flag which allows operator to delete local PVC in case of kubernetes node crash
CleanLocalPVC bool `json:"cleanLocalPVC,omitempty"`
}

type SeedsFinderServices struct {
Expand Down Expand Up @@ -557,6 +555,12 @@ type AerospikeStorageSpec struct { //nolint:govet // for readability
// CleanupThreads contains maximum number of cleanup threads(dd or blkdiscard) per init container.
CleanupThreads int `json:"cleanupThreads,omitempty"`

// CleanLocalPVC is a flag which allows operator to delete local PVC in case of kubernetes node crash
abhishekdwivedi3060 marked this conversation as resolved.
Show resolved Hide resolved
CleanLocalPVC bool `json:"cleanLocalPVC,omitempty"`

// LocalStorageClasses contains list of storage classes which provisions local volumes.
LocalStorageClasses []string `json:"localStorageClasses,omitempty"`

// Volumes list to attach to created pods.
// +patchMergeKey=name
// +patchStrategy=merge
Expand Down
5 changes: 5 additions & 0 deletions api/v1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

64 changes: 60 additions & 4 deletions config/crd/bases/asdb.aerospike.com_aerospikeclusters.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -278,10 +278,6 @@ spec:
- customInterface
type: string
type: object
cleanLocalPVC:
description: CleanLocalPVC is a flag which allows operator to delete
local PVC in case of kubernetes node crash
type: boolean
image:
description: Aerospike server image
type: string
Expand Down Expand Up @@ -5657,6 +5653,10 @@ spec:
- deleteFiles
type: string
type: object
cleanLocalPVC:
description: CleanLocalPVC is a flag which allows operator
to delete local PVC in case of kubernetes node crash
type: boolean
cleanupThreads:
description: CleanupThreads contains maximum number
of cleanup threads(dd or blkdiscard) per init container.
Expand Down Expand Up @@ -5713,6 +5713,12 @@ spec:
- deleteFiles
type: string
type: object
localStorageClasses:
description: LocalStorageClasses contains list of storage
classes which provisions local volumes.
items:
type: string
type: array
volumes:
description: Volumes list to attach to created pods.
items:
Expand Down Expand Up @@ -7345,6 +7351,10 @@ spec:
- deleteFiles
type: string
type: object
cleanLocalPVC:
description: CleanLocalPVC is a flag which allows operator
to delete local PVC in case of kubernetes node crash
type: boolean
cleanupThreads:
description: CleanupThreads contains maximum number
of cleanup threads(dd or blkdiscard) per init container.
Expand Down Expand Up @@ -7401,6 +7411,12 @@ spec:
- deleteFiles
type: string
type: object
localStorageClasses:
description: LocalStorageClasses contains list of storage
classes which provisions local volumes.
items:
type: string
type: array
volumes:
description: Volumes list to attach to created pods.
items:
Expand Down Expand Up @@ -8049,6 +8065,10 @@ spec:
- deleteFiles
type: string
type: object
cleanLocalPVC:
description: CleanLocalPVC is a flag which allows operator to
delete local PVC in case of kubernetes node crash
type: boolean
cleanupThreads:
description: CleanupThreads contains maximum number of cleanup
threads(dd or blkdiscard) per init container.
Expand Down Expand Up @@ -8105,6 +8125,12 @@ spec:
- deleteFiles
type: string
type: object
localStorageClasses:
description: LocalStorageClasses contains list of storage classes
which provisions local volumes.
items:
type: string
type: array
volumes:
description: Volumes list to attach to created pods.
items:
Expand Down Expand Up @@ -14334,6 +14360,10 @@ spec:
- deleteFiles
type: string
type: object
cleanLocalPVC:
description: CleanLocalPVC is a flag which allows operator
to delete local PVC in case of kubernetes node crash
type: boolean
cleanupThreads:
description: CleanupThreads contains maximum number
of cleanup threads(dd or blkdiscard) per init container.
Expand Down Expand Up @@ -14390,6 +14420,12 @@ spec:
- deleteFiles
type: string
type: object
localStorageClasses:
description: LocalStorageClasses contains list of storage
classes which provisions local volumes.
items:
type: string
type: array
volumes:
description: Volumes list to attach to created pods.
items:
Expand Down Expand Up @@ -16022,6 +16058,10 @@ spec:
- deleteFiles
type: string
type: object
cleanLocalPVC:
description: CleanLocalPVC is a flag which allows operator
to delete local PVC in case of kubernetes node crash
type: boolean
cleanupThreads:
description: CleanupThreads contains maximum number
of cleanup threads(dd or blkdiscard) per init container.
Expand Down Expand Up @@ -16078,6 +16118,12 @@ spec:
- deleteFiles
type: string
type: object
localStorageClasses:
description: LocalStorageClasses contains list of storage
classes which provisions local volumes.
items:
type: string
type: array
volumes:
description: Volumes list to attach to created pods.
items:
Expand Down Expand Up @@ -16775,6 +16821,10 @@ spec:
- deleteFiles
type: string
type: object
cleanLocalPVC:
description: CleanLocalPVC is a flag which allows operator to
delete local PVC in case of kubernetes node crash
type: boolean
cleanupThreads:
description: CleanupThreads contains maximum number of cleanup
threads(dd or blkdiscard) per init container.
Expand Down Expand Up @@ -16831,6 +16881,12 @@ spec:
- deleteFiles
type: string
type: object
localStorageClasses:
description: LocalStorageClasses contains list of storage classes
which provisions local volumes.
items:
type: string
type: array
volumes:
description: Volumes list to attach to created pods.
items:
Expand Down
7 changes: 6 additions & 1 deletion config/rbac/role.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,12 @@ rules:
- patch
- update
- watch
- apiGroups:
- ""
resources:
- persistentvolumes
verbs:
- get
- apiGroups:
- ""
resources:
Expand All @@ -98,7 +104,6 @@ rules:
- ""
resources:
- secrets
- persistentvolumes
verbs:
- get
- apiGroups:
Expand Down
4 changes: 2 additions & 2 deletions controllers/pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -261,8 +261,8 @@ func (r *SingleClusterReconciler) restartPods(
}
}

if r.aeroCluster.Spec.CleanLocalPVC {
if err := r.deleteLocalPVCs(pod); err != nil {
if rackState.Rack.Storage.CleanLocalPVC {
if err := r.deleteLocalPVCs(pod, rackState.Rack.Storage.LocalStorageClasses); err != nil {
return reconcileError(err)
}
}
Expand Down
24 changes: 5 additions & 19 deletions controllers/pvc.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@ import (
"time"

corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/controller-runtime/pkg/client"

asdbv1 "github.com/aerospike/aerospike-kubernetes-operator/api/v1"
Expand Down Expand Up @@ -105,7 +103,7 @@ func (r *SingleClusterReconciler) removePVCsAsync(
return deletedPVCs, nil
}

func (r *SingleClusterReconciler) deleteLocalPVCs(pod *corev1.Pod) error {
func (r *SingleClusterReconciler) deleteLocalPVCs(pod *corev1.Pod, localStorageClasses []string) error {
if pod.Status.Phase == corev1.PodPending && utils.IsPodNeedsToMigrate(pod) {
rackID, err := utils.GetRackIDFromPodName(pod.Name)
if err != nil {
Expand All @@ -118,26 +116,14 @@ func (r *SingleClusterReconciler) deleteLocalPVCs(pod *corev1.Pod) error {
}

for idx := range pvcItems {
pv := &corev1.PersistentVolume{}

volumeName := pvcItems[idx].Spec.VolumeName
if volumeName == "" {
r.Log.Info("PVC is not bounded with any volume, no need to delete PVC", pvcItems[idx].Name)
pvcStorageClass := pvcItems[idx].Spec.StorageClassName
if pvcStorageClass == nil {
r.Log.Info("PVC does not have storageclass set, no need to delete PVC", pvcItems[idx].Name)
abhishekdwivedi3060 marked this conversation as resolved.
Show resolved Hide resolved

continue
}

pvName := types.NamespacedName{Name: volumeName}
if err := r.Client.Get(context.TODO(), pvName, pv); err != nil {
if errors.IsNotFound(err) {
r.Log.Info("Volume bounded with PVC not found, no need to delete PVC", pvcItems[idx].Name)
continue
}

return err
}

if pv.Spec.Local != nil {
if utils.ContainsString(localStorageClasses, *pvcStorageClass) {
if err := r.Client.Delete(context.TODO(), &pvcItems[idx]); err != nil {
return fmt.Errorf(
"could not delete pvc %s: %v", pvcItems[idx].Name, err,
Expand Down
33 changes: 5 additions & 28 deletions controllers/statefulset.go
Original file line number Diff line number Diff line change
Expand Up @@ -604,18 +604,16 @@ func (r *SingleClusterReconciler) updateSTS(
return err
}

// Updating statefulSet object only if there is a difference in spec.
if reflect.DeepEqual(found.Spec, statefulSet.Spec) {
r.Log.Info("Skipping StatefulSet update, no change in spec")

return nil
}
// Save the updated stateful set.
found.Spec = statefulSet.Spec
return r.Client.Update(context.TODO(), found, updateOption)
})
if err != nil {
return err
return fmt.Errorf(
"failed to update StatefulSet %s: %v",
statefulSet.Name,
err,
)
}

r.Log.V(1).Info(
Expand Down Expand Up @@ -785,7 +783,6 @@ func (r *SingleClusterReconciler) updateSTSNonPVStorage(

// Add volume in statefulSet template
k8sVolume := createVolumeForVolumeAttachment(volume)

st.Spec.Template.Spec.Volumes = append(
st.Spec.Template.Spec.Volumes, k8sVolume,
)
Expand Down Expand Up @@ -1200,8 +1197,6 @@ func getDefaultAerospikeInitContainerVolumeMounts() []corev1.VolumeMount {
func getDefaultSTSVolumes(
aeroCluster *asdbv1.AerospikeCluster, rackState *RackState,
) []corev1.Volume {
defaultMode := corev1.SecretVolumeSourceDefaultMode

return []corev1.Volume{
{
Name: confDirName,
Expand All @@ -1218,7 +1213,6 @@ func getDefaultSTSVolumes(
aeroCluster, rackState.Rack.ID,
).Name,
},
DefaultMode: &defaultMode,
},
},
},
Expand Down Expand Up @@ -1355,19 +1349,6 @@ func createPVCForVolumeAttachment(
}

func createVolumeForVolumeAttachment(volume *asdbv1.VolumeSpec) corev1.Volume {
perm := corev1.SecretVolumeSourceDefaultMode

switch {
case volume.Source.Secret != nil:
if volume.Source.Secret.DefaultMode == nil {
volume.Source.Secret.DefaultMode = &perm
}
case volume.Source.ConfigMap != nil:
if volume.Source.ConfigMap.DefaultMode == nil {
volume.Source.ConfigMap.DefaultMode = &perm
}
}

return corev1.Volume{
Name: volume.Name,
// Add all type of source,
Expand Down Expand Up @@ -1517,10 +1498,6 @@ func getSTSContainerPort(
containerPort.HostPort = containerPort.ContainerPort
}

if containerPort.Protocol == "" {
containerPort.Protocol = corev1.ProtocolTCP
}

ports = append(ports, containerPort)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,6 @@ rules:
- ""
resources:
- secrets
- persistentvolumes
verbs:
- get
- apiGroups:
Expand Down
4 changes: 2 additions & 2 deletions pkg/utils/pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -224,8 +224,8 @@ func IsPodReasonUnschedulable(pod *corev1.Pod) bool {
func IsPodNeedsToMigrate(pod *corev1.Pod) bool {
for _, condition := range pod.Status.Conditions {
if condition.Type == corev1.PodScheduled && (condition.Reason == corev1.PodReasonUnschedulable ||
condition.Reason == corev1.PodReasonSchedulerError &&
strings.Contains(condition.Message, "nodeinfo not found for node name")) {
(condition.Reason == corev1.PodReasonSchedulerError &&
strings.Contains(condition.Message, "nodeinfo not found for node name"))) {
return true
}
}
Expand Down
5 changes: 1 addition & 4 deletions test/cluster_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -1373,14 +1373,11 @@ func getStorageVolumeForAerospike(name, path string) asdbv1.VolumeSpec {
}

func getStorageVolumeForSecret() asdbv1.VolumeSpec {
perm := corev1.SecretVolumeSourceDefaultMode

return asdbv1.VolumeSpec{
Name: aerospikeConfigSecret,
Source: asdbv1.VolumeSource{
Secret: &corev1.SecretVolumeSource{
SecretName: tlsSecretName,
DefaultMode: &perm,
SecretName: tlsSecretName,
},
},
Aerospike: &asdbv1.AerospikeServerVolumeAttachment{
Expand Down
Loading