@@ -20,6 +20,7 @@ class Config(UuidAuditedModel):
2020 cpu = JSONField (default = {}, blank = True )
2121 tags = JSONField (default = {}, blank = True )
2222 registry = JSONField (default = {}, blank = True )
23+ healthcheck = JSONField (default = {}, blank = True )
2324
2425 class Meta :
2526 get_latest_by = 'created'
@@ -29,13 +30,13 @@ class Meta:
2930 def __str__ (self ):
3031 return "{}-{}" .format (self .app .id , str (self .uuid )[:7 ])
3132
32- def healthcheck (self ):
33+ def _migrate_legacy_healthcheck (self ):
3334 """
3435 Get all healthchecks options together for use in scheduler
3536 """
36- # return empty dict if no healthcheck is found
37+ # return if no legacy healthcheck is found
3738 if 'HEALTHCHECK_URL' not in self .values .keys ():
38- return {}
39+ return
3940
4041 path = self .values .get ('HEALTHCHECK_URL' , '/' )
4142 timeout = int (self .values .get ('HEALTHCHECK_TIMEOUT' , 50 ))
@@ -44,44 +45,17 @@ def healthcheck(self):
4445 success_threshold = int (self .values .get ('HEALTHCHECK_SUCCESS_THRESHOLD' , 1 ))
4546 failure_threshold = int (self .values .get ('HEALTHCHECK_FAILURE_THRESHOLD' , 3 ))
4647
47- return {
48- 'path' : path ,
49- 'timeout' : timeout ,
50- 'delay' : delay ,
51- 'period_seconds' : period_seconds ,
52- 'success_threshold' : success_threshold ,
53- 'failure_threshold' : failure_threshold ,
48+ self .healthcheck ['livenessProbe' ] = {
49+ 'initialDelaySeconds' : delay ,
50+ 'timeoutSeconds' : timeout ,
51+ 'periodSeconds' : period_seconds ,
52+ 'successThreshold' : success_threshold ,
53+ 'failureThreshold' : failure_threshold ,
54+ 'httpGet' : {
55+ 'path' : path ,
56+ }
5457 }
5558
56- def set_healthchecks (self ):
57- """Defines default values for HTTP healthchecks"""
58- if not {k : v for k , v in self .values .items () if k .startswith ('HEALTHCHECK_' )}:
59- return
60-
61- # fetch set health values and any defaults
62- # this approach allows new health items to be added without issues
63- health = self .healthcheck ()
64- if not health :
65- return
66-
67- # HTTP GET related
68- self .values ['HEALTHCHECK_URL' ] = health ['path' ]
69-
70- # Number of seconds after which the probe times out.
71- # More info: http://releases.k8s.io/HEAD/docs/user-guide/pod-states.md#container-probes
72- self .values ['HEALTHCHECK_TIMEOUT' ] = health ['timeout' ]
73- # Number of seconds after the container has started before liveness probes are initiated.
74- # More info: http://releases.k8s.io/HEAD/docs/user-guide/pod-states.md#container-probes
75- self .values ['HEALTHCHECK_INITIAL_DELAY' ] = health ['delay' ]
76- # How often (in seconds) to perform the probe.
77- self .values ['HEALTHCHECK_PERIOD_SECONDS' ] = health ['period_seconds' ]
78- # Minimum consecutive successes for the probe to be considered successful
79- # after having failed.
80- self .values ['HEALTHCHECK_SUCCESS_THRESHOLD' ] = health ['success_threshold' ]
81- # Minimum consecutive failures for the probe to be considered failed after
82- # having succeeded.
83- self .values ['HEALTHCHECK_FAILURE_THRESHOLD' ] = health ['failure_threshold' ]
84-
8559 def set_registry (self ):
8660 # lower case all registry options for consistency
8761 self .registry = {key .lower (): value for key , value in self .registry .copy ().items ()}
@@ -127,7 +101,7 @@ def save(self, **kwargs):
127101 # usually means a totally new app
128102 previous_config = self .app .config_set .latest ()
129103
130- for attr in ['cpu' , 'memory' , 'tags' , 'registry' , 'values' ]:
104+ for attr in ['cpu' , 'memory' , 'tags' , 'registry' , 'values' , 'healthcheck' ]:
131105 data = getattr (previous_config , attr , {}).copy ()
132106 new_data = getattr (self , attr , {}).copy ()
133107
@@ -137,13 +111,12 @@ def save(self, **kwargs):
137111 # error if unsetting non-existing key
138112 if key not in data :
139113 raise UnprocessableEntity ('{} does not exist under {}' .format (key , attr )) # noqa
140-
141114 data .pop (key )
142115 else :
143116 data [key ] = value
144117 setattr (self , attr , data )
145118
146- self .set_healthchecks ()
119+ self ._migrate_legacy_healthcheck ()
147120 self .set_registry ()
148121 self .set_tags (previous_config )
149122 except Config .DoesNotExist :
0 commit comments