create K8S cluster by TOSCA
[oom.git] / TOSCA / kubernetes-cluster-TOSCA / policies / scale.clj
1 ;;;; ============LICENSE_START==========================================
2 ;;;; ===================================================================
3 ;;;; Copyright © 2017 AT&T
4 ;;;;
5 ;;;; Licensed under the Apache License, Version 2.0 (the "License");
6 ;;;; you may not use this file except in compliance with the License.
7 ;;;; You may obtain a copy of the License at
8 ;;;;
9 ;;;;         http://www.apache.org/licenses/LICENSE-2.0
10 ;;;;
11 ;;;; Unless required by applicable law or agreed to in writing, software
12 ;;;; distributed under the License is distributed on an "AS IS" BASIS,
13 ;;;; WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 ;;;; See the License for the specific language governing permissions and
15 ;;;; limitations under the License.
16 ;;;;============LICENSE_END============================================
17
18 (where (service #"{{service_selector}}")
19   #(info "got event: " %)
20
21   (where (not (expired? event))
22     (moving-time-window {{moving_window_size}}
23       (fn [events]
24         (let [
25                hostmap (atom {})
26                hostcnt (atom {})
27              ]
28           (do
29             (doseq [m events]
30               (if (nil? (@hostmap (m :host)))
31                 (do
32                   (swap! hostmap assoc (m :host) (m :metric))
33                   (swap! hostcnt assoc (m :host) 1)
34                 )
35                 (do
36                   (swap! hostmap assoc (m :host) (+ (m :metric) (@hostmap (m :host))))
37                   (swap! hostcnt assoc (m :host) (inc (@hostcnt (m :host))))
38                 )
39               )
40             )
41             (doseq [entry @hostmap]
42               (swap! hostmap assoc (key entry) (/ (val entry) (@hostcnt (key entry))))
43             )
44
45             (let
46               [ hostcnt (count @hostmap)
47                 conns (/ (apply + (map (fn [a] (val a)) @hostmap)) hostcnt)
48                 cooling (not (nil? (riemann.index/lookup index "scaling" "suspended")))]
49
50               (do
51                 (info "cooling=" cooling " scale_direction={{scale_direction}} hostcnt=" hostcnt " scale_threshold={{scale_threshold}} conns=" conns)
52                 (if (and (not cooling) ({{scale_direction}} hostcnt {{scale_limit}}) ({{scale_direction}} {{scale_threshold}} conns))
53                   (do
54                     (info "=== SCALE ===" "{{scale_direction}}")
55                     (process-policy-triggers {})
56                     (riemann.index/update index {:host "scaling" :service "suspended" :time (unix-time) :description "cooldown flag" :metric 0 :ttl {{cooldown_time}} :state "ok"})
57                   )
58                 )
59               )
60             )
61           )
62         )
63       )
64     )
65   )
66 )