Skip to content

Commit cb4fd81

Browse files
yulkenderekbit
authored andcommitted
feat: create hard/soft podAntiAffinity preset configuration
Signed-off-by: Caio Torres <caio.torres@suse.com>
1 parent 355ab51 commit cb4fd81

3 files changed

Lines changed: 65 additions & 31 deletions

File tree

app/driver.go

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@ const (
4545
EnvCSINodeDriverRegistrarImage = "CSI_NODE_DRIVER_REGISTRAR_IMAGE"
4646
EnvCSILivenessProbeImage = "CSI_LIVENESS_PROBE_IMAGE"
4747

48+
FlagCSIPodAntiAffinityPreset = "csi-pod-anti-affinity-preset"
49+
EnvCSIPodAntiAffinityPreset = "CSI_POD_ANTI_AFFINITY_PRESET"
50+
4851
FlagCSIAttacherReplicaCount = "csi-attacher-replica-count"
4952
FlagCSIProvisionerReplicaCount = "csi-provisioner-replica-count"
5053
FlagCSIResizerReplicaCount = "csi-resizer-replica-count"
@@ -116,6 +119,12 @@ func DeployDriverCmd() cli.Command {
116119
EnvVar: EnvCSISnapshotterReplicaCount,
117120
Value: csi.DefaultCSISnapshotterReplicaCount,
118121
},
122+
cli.StringFlag{
123+
Name: FlagCSIPodAntiAffinityPreset,
124+
Usage: "Specify CSI deployment podAntiAffinity",
125+
EnvVar: EnvCSIPodAntiAffinityPreset,
126+
Value: csi.DefaultCSIPodAntiAffinityPreset,
127+
},
119128
cli.StringFlag{
120129
Name: FlagCSINodeDriverRegistrarImage,
121130
Usage: "Specify CSI node-driver-registrar image",
@@ -221,6 +230,7 @@ func deployCSIDriver(kubeClient *clientset.Clientset, lhClient *lhclientset.Clie
221230
csiProvisionerReplicaCount := c.Int(FlagCSIProvisionerReplicaCount)
222231
csiSnapshotterReplicaCount := c.Int(FlagCSISnapshotterReplicaCount)
223232
csiResizerReplicaCount := c.Int(FlagCSIResizerReplicaCount)
233+
csiPodAntiAffinityPreset := c.String(FlagCSIPodAntiAffinityPreset)
224234
namespace := os.Getenv(types.EnvPodNamespace)
225235
serviceAccountName := os.Getenv(types.EnvServiceAccount)
226236
rootDir := c.String(FlagKubeletRootDir)
@@ -303,6 +313,10 @@ func deployCSIDriver(kubeClient *clientset.Clientset, lhClient *lhclientset.Clie
303313
logrus.Infof("User specified root dir: %v", rootDir)
304314
}
305315

316+
if csiPodAntiAffinityPreset != csi.CSIPodAntiAffinityPresetSoft && csiPodAntiAffinityPreset != csi.CSIPodAntiAffinityPresetHard {
317+
return fmt.Errorf("invalid csiPodAntiAffinityPreset %v", csiPodAntiAffinityPreset)
318+
}
319+
306320
if err := upgradeLonghornRelatedComponents(kubeClient, namespace); err != nil {
307321
return err
308322
}
@@ -312,22 +326,22 @@ func deployCSIDriver(kubeClient *clientset.Clientset, lhClient *lhclientset.Clie
312326
return err
313327
}
314328

315-
attacherDeployment := csi.NewAttacherDeployment(namespace, serviceAccountName, csiAttacherImage, rootDir, csiAttacherReplicaCount, tolerations, string(tolerationsByte), priorityClass, registrySecret, imagePullPolicy, nodeSelector)
329+
attacherDeployment := csi.NewAttacherDeployment(namespace, serviceAccountName, csiAttacherImage, rootDir, csiAttacherReplicaCount, csiPodAntiAffinityPreset, tolerations, string(tolerationsByte), priorityClass, registrySecret, imagePullPolicy, nodeSelector)
316330
if err := attacherDeployment.Deploy(kubeClient); err != nil {
317331
return err
318332
}
319333

320-
provisionerDeployment := csi.NewProvisionerDeployment(namespace, serviceAccountName, csiProvisionerImage, rootDir, csiProvisionerReplicaCount, tolerations, string(tolerationsByte), priorityClass, registrySecret, imagePullPolicy, nodeSelector)
334+
provisionerDeployment := csi.NewProvisionerDeployment(namespace, serviceAccountName, csiProvisionerImage, rootDir, csiProvisionerReplicaCount, csiPodAntiAffinityPreset, tolerations, string(tolerationsByte), priorityClass, registrySecret, imagePullPolicy, nodeSelector)
321335
if err := provisionerDeployment.Deploy(kubeClient); err != nil {
322336
return err
323337
}
324338

325-
resizerDeployment := csi.NewResizerDeployment(namespace, serviceAccountName, csiResizerImage, rootDir, csiResizerReplicaCount, tolerations, string(tolerationsByte), priorityClass, registrySecret, imagePullPolicy, nodeSelector)
339+
resizerDeployment := csi.NewResizerDeployment(namespace, serviceAccountName, csiResizerImage, rootDir, csiResizerReplicaCount, csiPodAntiAffinityPreset, tolerations, string(tolerationsByte), priorityClass, registrySecret, imagePullPolicy, nodeSelector)
326340
if err := resizerDeployment.Deploy(kubeClient); err != nil {
327341
return err
328342
}
329343

330-
snapshotterDeployment := csi.NewSnapshotterDeployment(namespace, serviceAccountName, csiSnapshotterImage, rootDir, csiSnapshotterReplicaCount, tolerations, string(tolerationsByte), priorityClass, registrySecret, imagePullPolicy, nodeSelector)
344+
snapshotterDeployment := csi.NewSnapshotterDeployment(namespace, serviceAccountName, csiSnapshotterImage, rootDir, csiSnapshotterReplicaCount, csiPodAntiAffinityPreset, tolerations, string(tolerationsByte), priorityClass, registrySecret, imagePullPolicy, nodeSelector)
331345
if err := snapshotterDeployment.Deploy(kubeClient); err != nil {
332346
return err
333347
}

csi/deployment.go

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ const (
2525
DefaultCSIResizerReplicaCount = 3
2626
DefaultCSISnapshotterReplicaCount = 3
2727

28+
DefaultCSIPodAntiAffinityPreset = CSIPodAntiAffinityPresetSoft
2829
DefaultCSISocketFileName = "csi.sock"
2930
DefaultCSIRegistrationDirSuffix = "/plugins_registry"
3031
DefaultCSIPluginsDirSuffix = "/plugins/"
@@ -35,6 +36,9 @@ const (
3536

3637
AnnotationCSIGitCommit = types.LonghornDriverName + "/git-commit"
3738
AnnotationCSIVersion = types.LonghornDriverName + "/version"
39+
40+
CSIPodAntiAffinityPresetSoft = "soft"
41+
CSIPodAntiAffinityPresetHard = "hard"
3842
)
3943

4044
var (
@@ -46,7 +50,7 @@ type AttacherDeployment struct {
4650
deployment *appsv1.Deployment
4751
}
4852

49-
func NewAttacherDeployment(namespace, serviceAccount, attacherImage, rootDir string, replicaCount int, tolerations []corev1.Toleration,
53+
func NewAttacherDeployment(namespace, serviceAccount, attacherImage, rootDir string, replicaCount int, podAntiAffinityPreset string, tolerations []corev1.Toleration,
5054
tolerationsString, priorityClass, registrySecret string, imagePullPolicy corev1.PullPolicy, nodeSelector map[string]string) *AttacherDeployment {
5155

5256
deployment := getCommonDeployment(
@@ -66,6 +70,7 @@ func NewAttacherDeployment(namespace, serviceAccount, attacherImage, rootDir str
6670
fmt.Sprintf("--http-endpoint=:%v", types.CSISidecarMetricsPort),
6771
},
6872
int32(replicaCount),
73+
podAntiAffinityPreset,
6974
tolerations,
7075
tolerationsString,
7176
priorityClass,
@@ -101,7 +106,7 @@ type ProvisionerDeployment struct {
101106
deployment *appsv1.Deployment
102107
}
103108

104-
func NewProvisionerDeployment(namespace, serviceAccount, provisionerImage, rootDir string, replicaCount int, tolerations []corev1.Toleration,
109+
func NewProvisionerDeployment(namespace, serviceAccount, provisionerImage, rootDir string, replicaCount int, podAntiAffinityPreset string, tolerations []corev1.Toleration,
105110
tolerationsString, priorityClass, registrySecret string, imagePullPolicy corev1.PullPolicy, nodeSelector map[string]string) *ProvisionerDeployment {
106111

107112
deployment := getCommonDeployment(
@@ -124,6 +129,7 @@ func NewProvisionerDeployment(namespace, serviceAccount, provisionerImage, rootD
124129
fmt.Sprintf("--http-endpoint=:%v", types.CSISidecarMetricsPort),
125130
},
126131
int32(replicaCount),
132+
podAntiAffinityPreset,
127133
tolerations,
128134
tolerationsString,
129135
priorityClass,
@@ -159,7 +165,7 @@ type ResizerDeployment struct {
159165
deployment *appsv1.Deployment
160166
}
161167

162-
func NewResizerDeployment(namespace, serviceAccount, resizerImage, rootDir string, replicaCount int, tolerations []corev1.Toleration,
168+
func NewResizerDeployment(namespace, serviceAccount, resizerImage, rootDir string, replicaCount int, podAntiAffinityPreset string, tolerations []corev1.Toleration,
163169
tolerationsString, priorityClass, registrySecret string, imagePullPolicy corev1.PullPolicy, nodeSelector map[string]string) *ResizerDeployment {
164170

165171
deployment := getCommonDeployment(
@@ -189,6 +195,7 @@ func NewResizerDeployment(namespace, serviceAccount, resizerImage, rootDir strin
189195
"--feature-gates=RecoverVolumeExpansionFailure=false",
190196
},
191197
int32(replicaCount),
198+
podAntiAffinityPreset,
192199
tolerations,
193200
tolerationsString,
194201
priorityClass,
@@ -224,7 +231,7 @@ type SnapshotterDeployment struct {
224231
deployment *appsv1.Deployment
225232
}
226233

227-
func NewSnapshotterDeployment(namespace, serviceAccount, snapshotterImage, rootDir string, replicaCount int, tolerations []corev1.Toleration,
234+
func NewSnapshotterDeployment(namespace, serviceAccount, snapshotterImage, rootDir string, replicaCount int, podAntiAffinityPreset string, tolerations []corev1.Toleration,
228235
tolerationsString, priorityClass, registrySecret string, imagePullPolicy corev1.PullPolicy, nodeSelector map[string]string) *SnapshotterDeployment {
229236

230237
deployment := getCommonDeployment(
@@ -244,6 +251,7 @@ func NewSnapshotterDeployment(namespace, serviceAccount, snapshotterImage, rootD
244251
fmt.Sprintf("--http-endpoint=:%v", types.CSISidecarMetricsPort),
245252
},
246253
int32(replicaCount),
254+
podAntiAffinityPreset,
247255
tolerations,
248256
tolerationsString,
249257
priorityClass,

csi/deployment_util.go

Lines changed: 35 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ const (
3434
maxRetryForDeletion = 120
3535
)
3636

37-
func getCommonDeployment(commonName, namespace, serviceAccount, image, rootDir string, args []string, replicaCount int32,
37+
func getCommonDeployment(commonName, namespace, serviceAccount, image, rootDir string, args []string, replicaCount int32, podAntiAffinityPreset string,
3838
tolerations []corev1.Toleration, tolerationsString, priorityClass, registrySecret string, imagePullPolicy corev1.PullPolicy, nodeSelector map[string]string, ports []corev1.ContainerPort) *appsv1.Deployment {
3939

4040
deploymentLabels := types.GetBaseLabelsForSystemManagedComponent()
@@ -62,28 +62,7 @@ func getCommonDeployment(commonName, namespace, serviceAccount, image, rootDir s
6262
NodeSelector: nodeSelector,
6363
PriorityClassName: priorityClass,
6464
Affinity: &corev1.Affinity{
65-
PodAntiAffinity: &corev1.PodAntiAffinity{
66-
PreferredDuringSchedulingIgnoredDuringExecution: []corev1.WeightedPodAffinityTerm{
67-
{
68-
Weight: 1,
69-
PodAffinityTerm: corev1.PodAffinityTerm{
70-
LabelSelector: &metav1.LabelSelector{
71-
MatchExpressions: []metav1.LabelSelectorRequirement{
72-
{
73-
Key: "app",
74-
Operator: metav1.LabelSelectorOpIn,
75-
Values: []string{
76-
commonName,
77-
},
78-
},
79-
},
80-
},
81-
82-
TopologyKey: corev1.LabelHostname,
83-
},
84-
},
85-
},
86-
},
65+
PodAntiAffinity: getAffinity(podAntiAffinityPreset, commonName),
8766
},
8867
Containers: []corev1.Container{
8968
{
@@ -448,3 +427,36 @@ func GetCSIPluginsDir(kubeletRootDir string) string {
448427
func GetCSIEndpoint() string {
449428
return "unix://" + GetInContainerCSISocketFilePath()
450429
}
430+
431+
func getAffinity(podAntiAffinityPreset string, commonName string) *corev1.PodAntiAffinity {
432+
podAffinityTerm := corev1.PodAffinityTerm{
433+
LabelSelector: &metav1.LabelSelector{
434+
MatchExpressions: []metav1.LabelSelectorRequirement{
435+
{
436+
Key: "app",
437+
Operator: metav1.LabelSelectorOpIn,
438+
Values: []string{
439+
commonName,
440+
},
441+
},
442+
},
443+
},
444+
445+
TopologyKey: corev1.LabelHostname,
446+
}
447+
448+
if podAntiAffinityPreset == CSIPodAntiAffinityPresetHard {
449+
return &corev1.PodAntiAffinity{
450+
RequiredDuringSchedulingIgnoredDuringExecution: []corev1.PodAffinityTerm{podAffinityTerm},
451+
}
452+
}
453+
454+
return &corev1.PodAntiAffinity{
455+
PreferredDuringSchedulingIgnoredDuringExecution: []corev1.WeightedPodAffinityTerm{
456+
{
457+
Weight: 1,
458+
PodAffinityTerm: podAffinityTerm,
459+
},
460+
},
461+
}
462+
}

0 commit comments

Comments
 (0)