Skip to content

Commit 0e7e391

Browse files
committed
CP-47656 Anti-affinity feature generate alert - Code refactoring
Signed-off-by: Lunfan Zhang <Lunfan.Zhang@cloud.com>
1 parent bd727ef commit 0e7e391

File tree

2 files changed

+15
-13
lines changed

2 files changed

+15
-13
lines changed

ocaml/xapi-types/features.mli

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,8 @@ type feature =
7474
| VTPM (** Support VTPM device required by Win11 guests *)
7575
| VM_anti_affinity (** Enable use of VM anti-affinity placement *)
7676

77+
type orientation = Positive | Negative (** Orientation type for features *)
78+
7779
val feature_of_rpc : Rpc.t -> feature
7880
(** Convert RPC into {!feature}s *)
7981

@@ -83,6 +85,9 @@ val rpc_of_feature : feature -> Rpc.t
8385
val name_of_feature : feature -> string
8486
(** Convert {!feature}s into strings *)
8587

88+
val string_of_feature : feature -> string * orientation
89+
(** Return restriction and orientation of feature *)
90+
8691
val all_features : feature list
8792
(** The list of all known features. *)
8893

ocaml/xapi/xapi_vm_group_helpers.ml

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ let get_anti_affinity_alerts ~__context =
6666
.all_vms_in_the_anti_affinity_group_are_running_on_a_single_host
6767
)
6868

69-
let alert_of_group ~__context ~label_name ~id alert =
69+
let alert_matched ~__context ~label_name ~id alert =
7070
let alert_rec = snd alert in
7171
match Xml.parse_string alert_rec.API.message_body with
7272
| Xml.Element ("body", _, children) -> (
@@ -94,17 +94,15 @@ let alert_of_group ~__context ~label_name ~id alert =
9494
let filter_alerts_with_group ~__context ~group ~alerts =
9595
let group_uuid = Db.VM_group.get_uuid ~__context ~self:group in
9696
List.filter
97-
(alert_of_group ~__context ~label_name:"VM_group" ~id:group_uuid)
97+
(alert_matched ~__context ~label_name:"VM_group" ~id:group_uuid)
9898
alerts
9999

100100
let filter_alerts_with_host ~__context ~host ~alerts =
101101
let host_uuid = Db.Host.get_uuid ~__context ~self:host in
102-
List.filter
103-
(alert_of_group ~__context ~label_name:"host" ~id:host_uuid)
104-
alerts
102+
List.filter (alert_matched ~__context ~label_name:"host" ~id:host_uuid) alerts
105103

106-
(** If it is breach and no alerts exists, generate one,
107-
If it is not breach and alerts exists, dismiss the existing alert *)
104+
(** If it is a breach and no alerts exist, generate one,
105+
If it is not a breach and alerts exist, dismiss the existing alert *)
108106
let update_vm_anti_affinity_alert_for_group ~__context ~group ~alerts =
109107
let breach_on_host =
110108
check_breach_on_vm_anti_affinity_rules ~__context ~group
@@ -147,11 +145,11 @@ let maybe_update_vm_anti_affinity_alert_for_vm ~__context ~vm =
147145
()
148146
| group :: _ ->
149147
let alerts = get_anti_affinity_alerts ~__context in
150-
let alert_of_group =
148+
let alerts_of_group =
151149
filter_alerts_with_group ~__context ~group ~alerts
152150
in
153151
update_vm_anti_affinity_alert_for_group ~__context ~group
154-
~alerts:alert_of_group
152+
~alerts:alerts_of_group
155153
with e -> error "%s" (Printexc.to_string e)
156154

157155
let remove_vm_anti_affinity_alert_for_group ~__context ~group ~alerts =
@@ -173,10 +171,10 @@ let update_alert ~__context ~groups ~action =
173171
Db.VM_group.get_placement ~__context ~self:g = `anti_affinity
174172
)
175173
|> List.iter (fun group ->
176-
let alert_of_group =
174+
let alerts_of_group =
177175
filter_alerts_with_group ~__context ~group ~alerts
178176
in
179-
action ~__context ~group ~alerts:alert_of_group
177+
action ~__context ~group ~alerts:alerts_of_group
180178
)
181179
with e -> error "%s" (Printexc.to_string e)
182180

@@ -191,8 +189,7 @@ let remove_vm_anti_affinity_alert ~__context ~groups =
191189
let maybe_update_alerts_on_feature_change ~__context ~old_restrictions
192190
~new_restrictions =
193191
try
194-
let feature_name = Features.name_of_feature Features.VM_anti_affinity in
195-
let feature = "restrict_" ^ String.lowercase_ascii feature_name in
192+
let feature, _ = Features.(string_of_feature Features.VM_anti_affinity) in
196193
let groups = Db.VM_group.get_all ~__context in
197194
match
198195
( List.assoc_opt feature old_restrictions

0 commit comments

Comments
 (0)