|
2 | 2 | Contains build of a docker image from a git repository.
|
3 | 3 | """
|
4 | 4 |
|
| 5 | +import base64 |
5 | 6 | import datetime
|
6 | 7 | import json
|
7 | 8 | import os
|
@@ -382,8 +383,24 @@ def submit(self):
|
382 | 383 |
|
383 | 384 | env = []
|
384 | 385 | if self.git_credentials:
|
| 386 | + secret_content = base64.b64encode( |
| 387 | + self.git_credentials.encode("utf-8") |
| 388 | + ).decode("utf-8") |
| 389 | + data = {"credentials": secret_content} |
| 390 | + |
| 391 | + secret = client.V1Secret() |
| 392 | + secret.data = data |
| 393 | + secret.metadata = {"name": self.name} |
| 394 | + secret.type = "Opaque" |
| 395 | + |
| 396 | + self.api.create_namespaced_secret(self.namespace, secret) |
| 397 | + |
| 398 | + secret_key_ref = client.V1SecretKeySelector( |
| 399 | + name=self.name, key="credentials", optional=False |
| 400 | + ) |
| 401 | + value_from = client.V1EnvVarSource(secret_key_ref=secret_key_ref) |
385 | 402 | env.append(
|
386 |
| - client.V1EnvVar(name="GIT_CREDENTIAL_ENV", value=self.git_credentials) |
| 403 | + client.V1EnvVar(name="GIT_CREDENTIAL_ENV", value_from=value_from) |
387 | 404 | )
|
388 | 405 |
|
389 | 406 | self.pod = client.V1Pod(
|
@@ -515,10 +532,9 @@ def submit(self):
|
515 | 532 | f"Found unknown phase {phase} when building {self.name}"
|
516 | 533 | )
|
517 | 534 |
|
518 |
| - if self.pod.status.phase == "Succeeded": |
519 |
| - self.cleanup() |
520 |
| - elif self.pod.status.phase == "Failed": |
| 535 | + if self.pod.status.phase in ["Succeeded", "Failed"]: |
521 | 536 | self.cleanup()
|
| 537 | + |
522 | 538 | except Exception:
|
523 | 539 | app_log.exception("Error in watch stream for %s", self.name)
|
524 | 540 | raise
|
@@ -568,21 +584,32 @@ def stream_logs(self):
|
568 | 584 |
|
569 | 585 | def cleanup(self):
|
570 | 586 | """
|
571 |
| - Delete the kubernetes build pod |
| 587 | + Delete the kubernetes build pod and secret if exists |
572 | 588 | """
|
573 |
| - try: |
574 |
| - self.api.delete_namespaced_pod( |
575 |
| - name=self.name, |
576 |
| - namespace=self.namespace, |
577 |
| - body=client.V1DeleteOptions(grace_period_seconds=0), |
578 |
| - _request_timeout=KUBE_REQUEST_TIMEOUT, |
579 |
| - ) |
580 |
| - except client.rest.ApiException as e: |
581 |
| - if e.status == 404: |
582 |
| - # Is ok, someone else has already deleted it |
583 |
| - pass |
584 |
| - else: |
585 |
| - raise |
| 589 | + |
| 590 | + exceptions = [] |
| 591 | + deletion_methods = [self.api.delete_namespaced_pod] |
| 592 | + |
| 593 | + if self.git_credentials: |
| 594 | + deletion_methods.append(self.api.delete_namespaced_secret) |
| 595 | + |
| 596 | + for deletion_method in deletion_methods: |
| 597 | + try: |
| 598 | + deletion_method( |
| 599 | + name=self.name, |
| 600 | + namespace=self.namespace, |
| 601 | + body=client.V1DeleteOptions(grace_period_seconds=0), |
| 602 | + _request_timeout=KUBE_REQUEST_TIMEOUT, |
| 603 | + ) |
| 604 | + except client.rest.ApiException as e: |
| 605 | + if e.status == 404: |
| 606 | + # Is ok, someone else has already deleted it |
| 607 | + pass |
| 608 | + else: |
| 609 | + exceptions.append(str(e)) |
| 610 | + |
| 611 | + if exceptions: |
| 612 | + raise RuntimeError("Error(s) occurred during cleanup", exceptions) |
586 | 613 |
|
587 | 614 |
|
588 | 615 | class KubernetesCleaner(LoggingConfigurable):
|
|
0 commit comments