11use stackable_operator:: {
2- commons:: affinity:: { StackableAffinityFragment , affinity_between_role_pods} ,
3- k8s_openapi:: api:: core:: v1:: PodAntiAffinity ,
2+ commons:: {
3+ affinity:: { StackableAffinityFragment , affinity_between_role_pods} ,
4+ opa:: OpaConfig ,
5+ } ,
6+ k8s_openapi:: api:: core:: v1:: { PodAffinity , PodAntiAffinity } ,
47} ;
58
69use crate :: crd:: { APP_NAME , NifiRole } ;
710
8- pub fn get_affinity ( cluster_name : & str , role : & NifiRole ) -> StackableAffinityFragment {
11+ pub fn get_affinity (
12+ cluster_name : & str ,
13+ role : & NifiRole ,
14+ opa_config : Option < & OpaConfig > ,
15+ ) -> StackableAffinityFragment {
16+ // With OPA authorization configured, NiFi sends its authorization requests to OPA, so prefer
17+ // to place it next to the OPA Pods.
18+ let pod_affinity = opa_config. map ( |opa_config| PodAffinity {
19+ preferred_during_scheduling_ignored_during_execution : Some ( vec ! [
20+ affinity_between_role_pods(
21+ "opa" ,
22+ & opa_config. config_map_name, // The discovery cm has the same name as the OpaCluster itself
23+ "server" ,
24+ 50 ,
25+ ) ,
26+ ] ) ,
27+ required_during_scheduling_ignored_during_execution : None ,
28+ } ) ;
29+
930 StackableAffinityFragment {
10- pod_affinity : None ,
31+ pod_affinity,
1132 pod_anti_affinity : Some ( PodAntiAffinity {
1233 preferred_during_scheduling_ignored_during_execution : Some ( vec ! [
1334 affinity_between_role_pods( APP_NAME , cluster_name, & role. to_string( ) , 70 ) ,
@@ -26,7 +47,9 @@ mod tests {
2647 use stackable_operator:: {
2748 commons:: affinity:: StackableAffinity ,
2849 k8s_openapi:: {
29- api:: core:: v1:: { PodAffinityTerm , PodAntiAffinity , WeightedPodAffinityTerm } ,
50+ api:: core:: v1:: {
51+ PodAffinity , PodAffinityTerm , PodAntiAffinity , WeightedPodAffinityTerm ,
52+ } ,
3053 apimachinery:: pkg:: apis:: meta:: v1:: LabelSelector ,
3154 } ,
3255 v2:: types:: operator:: RoleGroupName ,
@@ -54,6 +77,10 @@ mod tests {
5477 sensitiveProperties:
5578 keySecret: simple-nifi-sensitive-property-key
5679 autoGenerate: true
80+ authorization:
81+ opa:
82+ configMapName: simple-opa
83+ package: nifi
5784 nodes:
5885 roleGroups:
5986 default:
@@ -80,7 +107,32 @@ mod tests {
80107 assert_eq ! (
81108 merged_config. affinity,
82109 StackableAffinity {
83- pod_affinity: None ,
110+ pod_affinity: Some ( PodAffinity {
111+ preferred_during_scheduling_ignored_during_execution: Some ( vec![
112+ WeightedPodAffinityTerm {
113+ pod_affinity_term: PodAffinityTerm {
114+ label_selector: Some ( LabelSelector {
115+ match_expressions: None ,
116+ match_labels: Some ( BTreeMap :: from( [
117+ ( "app.kubernetes.io/name" . to_string( ) , "opa" . to_string( ) ) ,
118+ (
119+ "app.kubernetes.io/instance" . to_string( ) ,
120+ "simple-opa" . to_string( ) ,
121+ ) ,
122+ (
123+ "app.kubernetes.io/component" . to_string( ) ,
124+ "server" . to_string( ) ,
125+ ) ,
126+ ] ) ) ,
127+ } ) ,
128+ topology_key: "kubernetes.io/hostname" . to_string( ) ,
129+ ..Default :: default ( )
130+ } ,
131+ weight: 50 ,
132+ }
133+ ] ) ,
134+ required_during_scheduling_ignored_during_execution: None ,
135+ } ) ,
84136 pod_anti_affinity: Some ( PodAntiAffinity {
85137 preferred_during_scheduling_ignored_during_execution: Some ( vec![
86138 WeightedPodAffinityTerm {
0 commit comments