-
-
Notifications
You must be signed in to change notification settings - Fork 232
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: redis-cluster support podAntiAffinity
- Loading branch information
Showing
8 changed files
with
226 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -76,3 +76,4 @@ spec: | |
# priorityClassName: | ||
# Affinity: | ||
# Tolerations: [] | ||
leaderFollowerPodAntiAffinity: false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,185 @@ | ||
/* | ||
Copyright 2020 Opstree Solutions. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package webhook | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"github.com/OT-CONTAINER-KIT/redis-operator/api/v1beta2" | ||
"github.com/go-logr/logr" | ||
corev1 "k8s.io/api/core/v1" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"net/http" | ||
"reflect" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
"sigs.k8s.io/controller-runtime/pkg/webhook/admission" | ||
"strings" | ||
) | ||
|
||
//+kubebuilder:webhook:path=/mutate-core-v1-pod,mutating=true,failurePolicy=fail,sideEffects=None,groups=core,resources=pods,verbs=create,versions=v1,name=mpod.kb.io,admissionReviewVersions=v1 | ||
|
||
// PodAntiAffiniytMutate mutate Pods | ||
type PodAntiAffiniytMutate struct { | ||
Client client.Client | ||
decoder *admission.Decoder | ||
logger logr.Logger | ||
} | ||
|
||
func NewPodAffiniytMutate(c client.Client, d *admission.Decoder, log logr.Logger) admission.Handler { | ||
return &PodAntiAffiniytMutate{ | ||
Client: c, | ||
decoder: d, | ||
logger: log} | ||
} | ||
|
||
const ( | ||
podAnnotationsRedisClusterApp = "redis.opstreelabs.instance" | ||
podLabelsPodName = "statefulset.kubernetes.io/pod-name" | ||
podLabelsRedisType = "redis_setup_type" | ||
) | ||
|
||
func (v *PodAntiAffiniytMutate) Handle(ctx context.Context, req admission.Request) admission.Response { | ||
logger := v.logger.WithValues("Request.Namespace", req.Namespace, "Request.Name", req.Name) | ||
|
||
pod := &corev1.Pod{} | ||
err := v.decoder.Decode(req, pod) | ||
if err != nil { | ||
return admission.Errored(http.StatusBadRequest, err) | ||
} | ||
|
||
// only mutate pods that belong to redis cluster | ||
if !v.isRedisClusterPod(pod) { | ||
return admission.Allowed("") | ||
} | ||
|
||
// determine whether to add anti affinity | ||
redisCluster, err := v.getRedisClusterSpec(ctx, pod) | ||
if err != nil { | ||
return admission.Errored(http.StatusInternalServerError, err) | ||
} | ||
if !(redisCluster != nil && redisCluster.Spec.LeaderFollowerPodAntiAffinity != nil && | ||
*redisCluster.Spec.LeaderFollowerPodAntiAffinity == true) { | ||
v.logger.V(1).Info("leader follower pod anti affinity is disabled") | ||
return admission.Allowed("") | ||
} | ||
|
||
old := pod.DeepCopy() | ||
|
||
v.AddPodAntiAffinity(pod) | ||
if !reflect.DeepEqual(old, pod) { | ||
marshaledPod, err := json.Marshal(pod) | ||
if err != nil { | ||
return admission.Errored(http.StatusInternalServerError, err) | ||
} | ||
|
||
logger.Info("mutate pod with anti-affinity") | ||
return admission.PatchResponseFromRaw(req.Object.Raw, marshaledPod) | ||
} | ||
|
||
return admission.Allowed("") | ||
} | ||
|
||
// PodAntiAffiniytMutate implements admission.DecoderInjector. | ||
// A decoder will be automatically injected. | ||
|
||
// InjectDecoder injects the decoder. | ||
func (v *PodAntiAffiniytMutate) InjectDecoder(d *admission.Decoder) error { | ||
v.decoder = d | ||
return nil | ||
} | ||
|
||
func (m *PodAntiAffiniytMutate) InjectLogger(l logr.Logger) error { | ||
m.logger = l | ||
return nil | ||
} | ||
|
||
func (v *PodAntiAffiniytMutate) AddPodAntiAffinity(pod *corev1.Pod) { | ||
podName := pod.ObjectMeta.Name | ||
antiLabelValue := v.getAntiAffinityValue(podName) | ||
|
||
if pod.Spec.Affinity == nil { | ||
pod.Spec.Affinity = &corev1.Affinity{} | ||
} | ||
if pod.Spec.Affinity.PodAntiAffinity == nil { | ||
pod.Spec.Affinity.PodAntiAffinity = &corev1.PodAntiAffinity{} | ||
} | ||
if pod.Spec.Affinity.PodAntiAffinity.RequiredDuringSchedulingIgnoredDuringExecution == nil { | ||
pod.Spec.Affinity.PodAntiAffinity.RequiredDuringSchedulingIgnoredDuringExecution = make([]corev1.PodAffinityTerm, 0) | ||
} | ||
addAntiAffinity := corev1.PodAffinityTerm{ | ||
LabelSelector: &metav1.LabelSelector{ | ||
MatchExpressions: []metav1.LabelSelectorRequirement{ | ||
{ | ||
Key: podLabelsPodName, | ||
Operator: metav1.LabelSelectorOpIn, | ||
Values: []string{antiLabelValue}, | ||
}, | ||
}, | ||
}, | ||
TopologyKey: "kubernetes.io/hostname", | ||
} | ||
|
||
pod.Spec.Affinity.PodAntiAffinity.RequiredDuringSchedulingIgnoredDuringExecution = append(pod.Spec.Affinity.PodAntiAffinity.RequiredDuringSchedulingIgnoredDuringExecution, addAntiAffinity) | ||
} | ||
|
||
func (v *PodAntiAffiniytMutate) getPodAnnotations(pod *corev1.Pod) map[string]string { | ||
if pod.Annotations == nil { | ||
pod.Annotations = make(map[string]string) | ||
} | ||
return pod.Annotations | ||
} | ||
|
||
func (v *PodAntiAffiniytMutate) isRedisClusterPod(pod *corev1.Pod) bool { | ||
annotations := v.getPodAnnotations(pod) | ||
if _, ok := annotations[podAnnotationsRedisClusterApp]; !ok { | ||
return false | ||
} | ||
|
||
labels := pod.GetLabels() | ||
if _, ok := labels[podLabelsRedisType]; !ok { | ||
return false | ||
} | ||
|
||
return true | ||
} | ||
|
||
func (v *PodAntiAffiniytMutate) getAntiAffinityValue(podName string) string { | ||
if strings.Contains(podName, "follower") { | ||
return strings.Replace(podName, "follower", "leader", -1) | ||
} | ||
if strings.Contains(podName, "leader") { | ||
return strings.Replace(podName, "leader", "follower", -1) | ||
} | ||
return "" | ||
} | ||
|
||
func (v *PodAntiAffiniytMutate) getRedisClusterSpec(ctx context.Context, pod *corev1.Pod) (*v1beta2.RedisCluster, error) { | ||
namespaceKey := client.ObjectKey{ | ||
Namespace: pod.Namespace, | ||
Name: pod.Annotations[podAnnotationsRedisClusterApp], | ||
} | ||
|
||
redisCluster := &v1beta2.RedisCluster{} | ||
|
||
err := v.Client.Get(ctx, namespaceKey, redisCluster) | ||
if err != nil { | ||
v.logger.Error(err, "failed to get redis cluster") | ||
return nil, err | ||
} | ||
|
||
return redisCluster, nil | ||
} |