|
| 1 | +use stackable_operator::{ |
| 2 | + commons::affinity::{affinity_between_role_pods, StackableAffinityFragment}, |
| 3 | + k8s_openapi::api::core::v1::PodAntiAffinity, |
| 4 | +}; |
| 5 | + |
| 6 | +use crate::{SupersetRole, APP_NAME}; |
| 7 | + |
| 8 | +pub fn get_affinity(cluster_name: &str, role: &SupersetRole) -> StackableAffinityFragment { |
| 9 | + StackableAffinityFragment { |
| 10 | + pod_affinity: None, |
| 11 | + pod_anti_affinity: Some(PodAntiAffinity { |
| 12 | + preferred_during_scheduling_ignored_during_execution: Some(vec![ |
| 13 | + affinity_between_role_pods(APP_NAME, cluster_name, &role.to_string(), 70), |
| 14 | + ]), |
| 15 | + required_during_scheduling_ignored_during_execution: None, |
| 16 | + }), |
| 17 | + node_affinity: None, |
| 18 | + node_selector: None, |
| 19 | + } |
| 20 | +} |
| 21 | + |
| 22 | +#[cfg(test)] |
| 23 | +mod tests { |
| 24 | + use super::*; |
| 25 | + |
| 26 | + use std::collections::BTreeMap; |
| 27 | + |
| 28 | + use crate::SupersetCluster; |
| 29 | + use stackable_operator::{ |
| 30 | + commons::affinity::{StackableAffinity, StackableNodeSelector}, |
| 31 | + k8s_openapi::{ |
| 32 | + api::core::v1::{ |
| 33 | + NodeAffinity, NodeSelector, NodeSelectorRequirement, NodeSelectorTerm, |
| 34 | + PodAffinityTerm, PodAntiAffinity, WeightedPodAffinityTerm, |
| 35 | + }, |
| 36 | + apimachinery::pkg::apis::meta::v1::LabelSelector, |
| 37 | + }, |
| 38 | + }; |
| 39 | + |
| 40 | + #[test] |
| 41 | + fn test_affinity_defaults() { |
| 42 | + let input = r#" |
| 43 | + apiVersion: superset.stackable.tech/v1alpha1 |
| 44 | + kind: SupersetCluster |
| 45 | + metadata: |
| 46 | + name: simple-superset |
| 47 | + spec: |
| 48 | + image: |
| 49 | + productVersion: 1.5.1 |
| 50 | + stackableVersion: "23.1" |
| 51 | + credentialsSecret: simple-superset-credentials |
| 52 | + nodes: |
| 53 | + roleGroups: |
| 54 | + default: |
| 55 | + replicas: 1 |
| 56 | + "#; |
| 57 | + let superset: SupersetCluster = serde_yaml::from_str(input).expect("illegal test input"); |
| 58 | + let merged_config = superset |
| 59 | + .merged_config(&SupersetRole::Node, &superset.node_rolegroup_ref("default")) |
| 60 | + .unwrap(); |
| 61 | + |
| 62 | + assert_eq!( |
| 63 | + merged_config.affinity, |
| 64 | + StackableAffinity { |
| 65 | + pod_affinity: None, |
| 66 | + pod_anti_affinity: Some(PodAntiAffinity { |
| 67 | + preferred_during_scheduling_ignored_during_execution: Some(vec![ |
| 68 | + WeightedPodAffinityTerm { |
| 69 | + pod_affinity_term: PodAffinityTerm { |
| 70 | + label_selector: Some(LabelSelector { |
| 71 | + match_expressions: None, |
| 72 | + match_labels: Some(BTreeMap::from([ |
| 73 | + ( |
| 74 | + "app.kubernetes.io/name".to_string(), |
| 75 | + "superset".to_string(), |
| 76 | + ), |
| 77 | + ( |
| 78 | + "app.kubernetes.io/instance".to_string(), |
| 79 | + "simple-superset".to_string(), |
| 80 | + ), |
| 81 | + ( |
| 82 | + "app.kubernetes.io/component".to_string(), |
| 83 | + "node".to_string(), |
| 84 | + ) |
| 85 | + ])) |
| 86 | + }), |
| 87 | + namespace_selector: None, |
| 88 | + namespaces: None, |
| 89 | + topology_key: "kubernetes.io/hostname".to_string(), |
| 90 | + }, |
| 91 | + weight: 70 |
| 92 | + } |
| 93 | + ]), |
| 94 | + required_during_scheduling_ignored_during_execution: None, |
| 95 | + }), |
| 96 | + node_affinity: None, |
| 97 | + node_selector: None, |
| 98 | + } |
| 99 | + ); |
| 100 | + } |
| 101 | + |
| 102 | + #[test] |
| 103 | + fn test_affinity_legacy_node_selector() { |
| 104 | + let input = r#" |
| 105 | + apiVersion: superset.stackable.tech/v1alpha1 |
| 106 | + kind: SupersetCluster |
| 107 | + metadata: |
| 108 | + name: simple-superset |
| 109 | + spec: |
| 110 | + image: |
| 111 | + productVersion: 1.5.1 |
| 112 | + stackableVersion: "23.1" |
| 113 | + credentialsSecret: simple-superset-credentials |
| 114 | + nodes: |
| 115 | + roleGroups: |
| 116 | + default: |
| 117 | + replicas: 1 |
| 118 | + selector: |
| 119 | + matchLabels: |
| 120 | + disktype: ssd |
| 121 | + matchExpressions: |
| 122 | + - key: topology.kubernetes.io/zone |
| 123 | + operator: In |
| 124 | + values: |
| 125 | + - antarctica-east1 |
| 126 | + - antarctica-west1 |
| 127 | + "#; |
| 128 | + let superset: SupersetCluster = serde_yaml::from_str(input).expect("illegal test input"); |
| 129 | + let merged_config = superset |
| 130 | + .merged_config(&SupersetRole::Node, &superset.node_rolegroup_ref("default")) |
| 131 | + .unwrap(); |
| 132 | + |
| 133 | + assert_eq!( |
| 134 | + merged_config.affinity, |
| 135 | + StackableAffinity { |
| 136 | + pod_affinity: None, |
| 137 | + pod_anti_affinity: Some(PodAntiAffinity { |
| 138 | + preferred_during_scheduling_ignored_during_execution: Some(vec![ |
| 139 | + WeightedPodAffinityTerm { |
| 140 | + pod_affinity_term: PodAffinityTerm { |
| 141 | + label_selector: Some(LabelSelector { |
| 142 | + match_expressions: None, |
| 143 | + match_labels: Some(BTreeMap::from([ |
| 144 | + ( |
| 145 | + "app.kubernetes.io/name".to_string(), |
| 146 | + "superset".to_string(), |
| 147 | + ), |
| 148 | + ( |
| 149 | + "app.kubernetes.io/instance".to_string(), |
| 150 | + "simple-superset".to_string(), |
| 151 | + ), |
| 152 | + ( |
| 153 | + "app.kubernetes.io/component".to_string(), |
| 154 | + "node".to_string(), |
| 155 | + ) |
| 156 | + ])) |
| 157 | + }), |
| 158 | + namespace_selector: None, |
| 159 | + namespaces: None, |
| 160 | + topology_key: "kubernetes.io/hostname".to_string(), |
| 161 | + }, |
| 162 | + weight: 70 |
| 163 | + } |
| 164 | + ]), |
| 165 | + required_during_scheduling_ignored_during_execution: None, |
| 166 | + }), |
| 167 | + node_affinity: Some(NodeAffinity { |
| 168 | + preferred_during_scheduling_ignored_during_execution: None, |
| 169 | + required_during_scheduling_ignored_during_execution: Some(NodeSelector { |
| 170 | + node_selector_terms: vec![NodeSelectorTerm { |
| 171 | + match_expressions: Some(vec![NodeSelectorRequirement { |
| 172 | + key: "topology.kubernetes.io/zone".to_string(), |
| 173 | + operator: "In".to_string(), |
| 174 | + values: Some(vec![ |
| 175 | + "antarctica-east1".to_string(), |
| 176 | + "antarctica-west1".to_string() |
| 177 | + ]), |
| 178 | + }]), |
| 179 | + match_fields: None, |
| 180 | + }] |
| 181 | + }), |
| 182 | + }), |
| 183 | + node_selector: Some(StackableNodeSelector { |
| 184 | + node_selector: BTreeMap::from([("disktype".to_string(), "ssd".to_string())]) |
| 185 | + }), |
| 186 | + } |
| 187 | + ); |
| 188 | + } |
| 189 | +} |
0 commit comments