Skip to content

Commit

Permalink
Addressing comments and adding default "Protocol" in STS Ports.
Browse files Browse the repository at this point in the history
  • Loading branch information
tanmayja committed Nov 1, 2023
1 parent 333c19a commit 95221ee
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 22 deletions.
6 changes: 3 additions & 3 deletions controllers/aerospikecluster_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,12 @@ func stsPodPendingPredicate(e event.UpdateEvent) bool {
return false
}

type StatusPendingPredicate struct {
type statusPendingPredicate struct {
predicate.Funcs
}

// Update implements default UpdateEvent filter for statefulSets.
func (StatusPendingPredicate) Update(e event.UpdateEvent) bool {
func (statusPendingPredicate) Update(e event.UpdateEvent) bool {
return stsPodPendingPredicate(e)
}

Expand All @@ -99,7 +99,7 @@ func (r *AerospikeClusterReconciler) SetupWithManager(mgr ctrl.Manager) error {
},
).
WithEventFilter(predicate.Or(predicate.GenerationChangedPredicate{}, predicate.LabelChangedPredicate{},
StatusPendingPredicate{})).
statusPendingPredicate{})).
Complete(r)
}

Expand Down
6 changes: 0 additions & 6 deletions controllers/pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -405,12 +405,6 @@ func (r *SingleClusterReconciler) deletePodAndEnsureImageUpdated(

// Delete pods
for _, p := range podsToUpdate {
if r.aeroCluster.Spec.CleanLocalPVC {
if err := r.deleteLocalPVCs(p); err != nil {
return reconcileError(err)
}
}

if err := r.Client.Delete(context.TODO(), p); err != nil {
return reconcileError(err)
}
Expand Down
20 changes: 16 additions & 4 deletions controllers/pvc.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ package controllers
import (
"context"
"fmt"
"strconv"
"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"
Expand Down Expand Up @@ -107,21 +107,33 @@ func (r *SingleClusterReconciler) removePVCsAsync(

func (r *SingleClusterReconciler) deleteLocalPVCs(pod *corev1.Pod) error {
if pod.Status.Phase == corev1.PodPending && utils.IsPodNeedsToMigrate(pod) {
rackID, err := strconv.Atoi(pod.Labels[asdbv1.AerospikeRackIDLabel])
rackID, err := utils.GetRackIDFromPodName(pod.Name)
if err != nil {
return err
}

pvcItems, err := r.getPodsPVCList([]string{pod.Name}, rackID)
pvcItems, err := r.getPodsPVCList([]string{pod.Name}, *rackID)
if err != nil {
return fmt.Errorf("could not find pvc for pod %v: %v", pod.Name, err)
}

for idx := range pvcItems {
pv := &corev1.PersistentVolume{}
pvName := types.NamespacedName{Name: pvcItems[idx].Spec.VolumeName}

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)

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
}

Expand Down
40 changes: 31 additions & 9 deletions controllers/statefulset.go
Original file line number Diff line number Diff line change
Expand Up @@ -604,7 +604,10 @@ 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.
Expand Down Expand Up @@ -781,16 +784,8 @@ func (r *SingleClusterReconciler) updateSTSNonPVStorage(
)

// Add volume in statefulSet template
perm := corev1.SecretVolumeSourceDefaultMode
k8sVolume := createVolumeForVolumeAttachment(volume)

switch {
case k8sVolume.Secret != nil:
k8sVolume.Secret.DefaultMode = &perm
case k8sVolume.ConfigMap != nil:
k8sVolume.ConfigMap.DefaultMode = &perm
}

st.Spec.Template.Spec.Volumes = append(
st.Spec.Template.Spec.Volumes, k8sVolume,
)
Expand Down Expand Up @@ -1360,6 +1355,19 @@ 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 @@ -1476,8 +1484,18 @@ func getSTSContainerPort(
multiPodPerHost bool, aeroConf *asdbv1.AerospikeConfigSpec,
) []corev1.ContainerPort {
ports := make([]corev1.ContainerPort, 0, len(defaultContainerPorts))
portNames := make([]string, 0, len(defaultContainerPorts))

// Sorting defaultContainerPorts to fetch map in ordered manner.
// Helps in comparing STS before updating.
for portName := range defaultContainerPorts {
portNames = append(portNames, portName)
}

for portName, portInfo := range defaultContainerPorts {
sort.Strings(portNames)

for _, portName := range portNames {
portInfo := defaultContainerPorts[portName]
configPort := asdbv1.GetPortFromConfig(
aeroConf, portInfo.connectionType, portInfo.configParam,
)
Expand All @@ -1499,6 +1517,10 @@ func getSTSContainerPort(
containerPort.HostPort = containerPort.ContainerPort
}

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

ports = append(ports, containerPort)
}

Expand Down

0 comments on commit 95221ee

Please sign in to comment.