@@ -23,50 +23,82 @@ def as_dict(self):
2323 "path_pattern" : self .path_pattern
2424 }
2525
26- def save (self , * args , ** kwargs ):
27- # app = str(self.app)
28- # domain = str( self.domain)
29-
30- # # get config for the service
31- # config = self._load_service_config(app, 'router')
32-
33- # # See if domains are available
34- # if 'domains' not in config :
35- # config['domains'] = ''
36-
37- # # convert from string to list to work with and filter out empty strings
38- # domains = [_f for _f in config['domains'].split(',') if _f]
39- # if domain not in domains:
40- # domains.append(domain )
41- # config['domains'] = ','.join(domains )
26+ def create (self , * args , ** kwargs ): # noqa
27+ # create required minimum service in k8s for the application
28+ namespace = self .app . id
29+ svc_name = "{}-{}" . format ( self . id , self . procfile_type )
30+ self . log ( 'creating Service: {}' . format ( svc_name ), level = logging . DEBUG )
31+ try :
32+ try :
33+ self . _scheduler . svc . get ( namespace , svc_name )
34+ except KubeException :
35+ self . _scheduler . svc . create ( namespace , svc_name )
36+ except KubeException as e :
37+ raise ServiceUnavailable ( 'Kubernetes service could not be created' ) from e
38+ # config service
39+ annotations = self . _gather_settings ()
40+ routable = annotations . pop ( 'routable' )
41+ self . _update_service ( namespace , self . procfile_type , routable , annotations )
4242
43- # self._save_service_config(app, 'router', config)
43+ def save (self , * args , ** kwargs ):
44+ namespace = self .app .id
45+ svc_name = "{}-{}" .format (self .id , self .procfile_type )
46+ self .log ('updating Service: {}' .format (svc_name ), level = logging .DEBUG )
47+ annotations = self ._gather_settings ()
48+ routable = annotations .pop ('routable' )
49+ self ._update_service (namespace , self .procfile_type , routable , annotations )
4450
4551 # Save to DB
4652 return super (Service , self ).save (* args , ** kwargs )
4753
4854 def delete (self , * args , ** kwargs ):
49- # app = str(self.app)
50- # domain = str(self.domain)
51-
52- # # Deatch cert, updates k8s
53- # if self.certificate:
54- # self.certificate.detach(domain=domain)
55-
56- # # get config for the service
57- # config = self._load_service_config(app, 'router')
58-
59- # # See if domains are available
60- # if 'domains' not in config:
61- # config['domains'] = ''
62-
63- # # convert from string to list to work with and filter out empty strings
64- # domains = [_f for _f in config['domains'].split(',') if _f]
65- # if domain in domains:
66- # domains.remove(domain)
67- # config['domains'] = ','.join(domains)
68-
69- # self._save_service_config(app, 'router', config)
55+ namespace = self .app .id
56+ svc_name = "{}-{}" .format (self .id , self .procfile_type )
57+ self .log ('deleting Service: {}' .format (svc_name ), level = logging .DEBUG )
58+ try :
59+ self ._scheduler .svc .delete (namespace , svc_name )
60+ except KubeException :
61+ # swallow exception
62+ # raise ServiceUnavailable('Kubernetes service could not be deleted') from e
63+ self .log ('Kubernetes service cannot be deleted: {}' .format (svc_name ), level = logging .ERROR )
7064
7165 # Delete from DB
7266 return super (Service , self ).delete (* args , ** kwargs )
67+
68+ def _gather_settings (self ):
69+ app_settings = self .app .appsettings_set .latest ()
70+ return {
71+ 'domains' : "{}-{}" .format (self .app .id , self .procfile_type )
72+ 'maintenance' : app_settings .maintenance ,
73+ 'routable' : app_settings .routable ,
74+ 'proxyDomain' : self .app .id ,
75+ 'proxyLocations' : self .path_pattern
76+ }
77+
78+ def _update_service (self , namespace , app_type , routable , annotations ): # noqa
79+ """Update application service with all the various required information"""
80+ svc_name = "{}-{}" .format (namespace , app_type )
81+ service = self ._fetch_service_config (namespace , svc_name )
82+ old_service = service .copy () # in case anything fails for rollback
83+
84+ try :
85+ # Update service information
86+ for key , value in annotations .items ():
87+ if value is not None :
88+ service ['metadata' ]['annotations' ]['router.deis.io/%s' % key ] = str (value )
89+ else :
90+ service ['metadata' ]['annotations' ].pop ('router.deis.io/%s' % key , None )
91+ if routable :
92+ service ['metadata' ]['labels' ]['router.deis.io/routable' ] = 'true'
93+ else :
94+ # delete the annotation
95+ service ['metadata' ]['labels' ].pop ('router.deis.io/routable' , None )
96+
97+ # Set app type selector
98+ service ['spec' ]['selector' ]['type' ] = app_type
99+
100+ self ._scheduler .svc .update (namespace , svc_name , data = service )
101+ except Exception as e :
102+ # Fix service to old port and app type
103+ self ._scheduler .svc .update (namespace , svc_name , data = old_service )
104+ raise ServiceUnavailable (str (e )) from e
0 commit comments