@@ -29,51 +29,6 @@ class TLS(UuidAuditedModel):
2929 https_enforced = models .BooleanField (null = True )
3030 certs_auto_enabled = models .BooleanField (null = True )
3131
32- class Meta :
33- get_latest_by = 'created'
34- unique_together = (('app' , 'uuid' ))
35- ordering = ['-created' ]
36-
37- def __str__ (self ):
38- return "{}-{}" .format (self .app .id , str (self .uuid )[:7 ])
39-
40- def _check_previous_tls_settings (self ):
41- """
42- Only one value can be set at a time
43- If the other value is None, using the previous setting.
44- """
45- try :
46- previous_tls_settings = self .app .tls_set .latest ()
47- if self .https_enforced is not None :
48- if previous_tls_settings .https_enforced == self .https_enforced :
49- raise AlreadyExists (
50- "{} changed nothing" .format (self .owner ))
51- self .certs_auto_enabled = previous_tls_settings .certs_auto_enabled
52- elif self .certs_auto_enabled is not None :
53- if previous_tls_settings .certs_auto_enabled == self .certs_auto_enabled :
54- raise AlreadyExists (
55- "{} changed nothing" .format (self .owner ))
56- self .https_enforced = previous_tls_settings .https_enforced
57- previous_tls_settings .delete ()
58- except TLS .DoesNotExist :
59- pass
60-
61- def _refresh_secret_to_k8s (self ):
62- secret_name = f"{ self .app .id } -acme-external-account-binding-secret"
63- try :
64- try :
65- data = self .scheduler ().secret .get (self .app .id , secret_name ).json ()
66- self .scheduler ().secret .patch (self .app .id , secret_name , {
67- "secret" : self .issuer ["key_secret" ],
68- "version" : data ["metadata" ]["resourceVersion" ],
69- })
70- except KubeException :
71- self .scheduler ().secret .create (self .app .id , secret_name , {
72- "secret" : self .issuer ["key_secret" ],
73- })
74- except KubeException as e :
75- raise ServiceUnavailable ('Kubernetes secret could not be created' ) from e
76-
7732 def log (self , message , level = logging .INFO ):
7833 """Logs a message in the context of this application.
7934
@@ -85,6 +40,37 @@ def log(self, message, level=logging.INFO):
8540 """
8641 logger .log (level , "[{}]: {}" .format (self .app .id , message ))
8742
43+ @property
44+ def events (self ):
45+ def to_result (name , kind , condition ):
46+ return {
47+ "name" : name ,
48+ "kind" : kind ,
49+ "time" : condition ["lastTransitionTime" ],
50+ "type" : condition ["type" ],
51+ "status" : condition ["status" ],
52+ "message" : condition ["message" ],
53+ }
54+
55+ results = []
56+ name = namespace = self .app .id
57+ response = self .scheduler ().issuer .get (namespace , name , ignore_exception = True )
58+ if response .status_code == 200 :
59+ for condition in response .json ()["status" ]["conditions" ]:
60+ results .append (to_result (name , "Issuer" , condition ))
61+ name = f"{ self .app .id } -auto-tls"
62+ response = self .scheduler ().certificate .get (namespace , name , ignore_exception = True )
63+ if response .status_code == 200 :
64+ for condition in response .json ()["status" ]["conditions" ]:
65+ results .append (to_result (name , "Certificate" , condition ))
66+ response = self .scheduler ().certificaterequest .get (namespace , ignore_exception = True )
67+ if response .status_code == 200 :
68+ for item in response .json ()["items" ]:
69+ for condition in item ["status" ]["conditions" ]:
70+ results .append (to_result (
71+ item ["metadata" ]["name" ], "CertificateRequest" , condition ))
72+ return results
73+
8874 def refresh_issuer_to_k8s (self ):
8975 name = namespace = self .app .id
9076 try :
@@ -110,7 +96,7 @@ def refresh_issuer_to_k8s(self):
11096 raise ServiceUnavailable ('Kubernetes issuer could not be created' ) from e
11197
11298 def refresh_certificate_to_k8s (self ):
113- namespace = name = self .app .id
99+ namespace , name = self .app .id , f" { self . app . id } -auto-tls"
114100 if self .certs_auto_enabled :
115101 hosts = [domain .domain for domain in self .app .domain_set .all ()]
116102 if len (hosts ) > 0 :
@@ -132,3 +118,48 @@ def refresh_certificate_to_k8s(self):
132118 def save (self , * args , ** kwargs ):
133119 self ._check_previous_tls_settings ()
134120 super (TLS , self ).save (* args , ** kwargs )
121+
122+ def __str__ (self ):
123+ return "{}-{}" .format (self .app .id , str (self .uuid )[:7 ])
124+
125+ def _check_previous_tls_settings (self ):
126+ """
127+ Only one value can be set at a time
128+ If the other value is None, using the previous setting.
129+ """
130+ try :
131+ previous_tls_settings = self .app .tls_set .latest ()
132+ if self .https_enforced is not None :
133+ if previous_tls_settings .https_enforced == self .https_enforced :
134+ raise AlreadyExists (
135+ "{} changed nothing" .format (self .owner ))
136+ self .certs_auto_enabled = previous_tls_settings .certs_auto_enabled
137+ elif self .certs_auto_enabled is not None :
138+ if previous_tls_settings .certs_auto_enabled == self .certs_auto_enabled :
139+ raise AlreadyExists (
140+ "{} changed nothing" .format (self .owner ))
141+ self .https_enforced = previous_tls_settings .https_enforced
142+ previous_tls_settings .delete ()
143+ except TLS .DoesNotExist :
144+ pass
145+
146+ def _refresh_secret_to_k8s (self ):
147+ secret_name = f"{ self .app .id } -acme-external-account-binding-secret"
148+ try :
149+ try :
150+ data = self .scheduler ().secret .get (self .app .id , secret_name ).json ()
151+ self .scheduler ().secret .patch (self .app .id , secret_name , {
152+ "secret" : self .issuer ["key_secret" ],
153+ "version" : data ["metadata" ]["resourceVersion" ],
154+ })
155+ except KubeException :
156+ self .scheduler ().secret .create (self .app .id , secret_name , {
157+ "secret" : self .issuer ["key_secret" ],
158+ })
159+ except KubeException as e :
160+ raise ServiceUnavailable ('Kubernetes secret could not be created' ) from e
161+
162+ class Meta :
163+ get_latest_by = 'created'
164+ unique_together = (('app' , 'uuid' ))
165+ ordering = ['-created' ]
0 commit comments