|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace TheAentMachine\AentGitLabCI\GitLabCI\Job; |
|
4
|
|
|
|
|
5
|
|
|
use TheAentMachine\AentGitLabCI\Context\BaseGitLabCIContext; |
|
6
|
|
|
use TheAentMachine\AentGitLabCI\Exception\JobException; |
|
7
|
|
|
use TheAentMachine\AentGitLabCI\GitLabCI\Job\Model\BranchesModel; |
|
8
|
|
|
|
|
9
|
|
|
final class CleanupKubernetesJob extends AbstractCleanupJob |
|
10
|
|
|
{ |
|
11
|
|
|
/** |
|
12
|
|
|
* @param BaseGitLabCIContext $context |
|
13
|
|
|
* @param BranchesModel $branchesModel |
|
14
|
|
|
* @param bool $isManual |
|
15
|
|
|
* @return CleanupKubernetesJob |
|
16
|
|
|
* @throws JobException |
|
17
|
|
|
*/ |
|
18
|
|
|
public static function newCleanupForGCloud(BaseGitLabCIContext $context, BranchesModel $branchesModel, bool $isManual): self |
|
|
|
|
|
|
19
|
|
|
{ |
|
20
|
|
|
$self = new self($context->getEnvironmentName()); |
|
21
|
|
|
$self->image = 'thecodingmachine/k8s-gitlabci:latest'; |
|
22
|
|
|
$self->variables = [ |
|
23
|
|
|
'GCLOUD_SERVICE_KEY_BASE64' => 'You should put this value in your secrets CI variables!', |
|
24
|
|
|
'GCLOUD_PROJECT' => 'You should put this value in your secrets CI variables!', |
|
25
|
|
|
'GKE_CLUSTER' => 'You should put this value in your secrets CI variables!', |
|
26
|
|
|
'ZONE' => 'You should put this value in your secrets CI variables!', |
|
27
|
|
|
'KUBECONFIG' => '/root/.kube/config', |
|
28
|
|
|
'REGISTRY_DOMAIN_NAME' => $context->getRegistryDomainName(), |
|
29
|
|
|
'PROJECT_GROUP' => $context->getProjectGroup(), |
|
30
|
|
|
'PROJECT_NAME' => $context->getProjectName() |
|
31
|
|
|
]; |
|
32
|
|
|
$scriptTag = $branchesModel->isSingleBranch() ? strtolower($branchesModel->getBranches()[0]) : '${CI_COMMIT_REF_SLUG}'; |
|
33
|
|
|
$self->script = [ |
|
34
|
|
|
'/delete_image.sh ${REGISTRY_DOMAIN_NAME}/${PROJECT_GROUP}/${PROJECT_NAME}:' . $scriptTag, |
|
35
|
|
|
'echo $GCLOUD_SERVICE_KEY_BASE64 | base64 -d > /secret.json', |
|
36
|
|
|
'gcloud auth activate-service-account --key-file /secret.json', |
|
37
|
|
|
'gcloud config set project $GCLOUD_PROJECT', |
|
38
|
|
|
'gcloud container clusters get-credentials $GKE_CLUSTER --zone $ZONE --project $GCLOUD_PROJECT', |
|
39
|
|
|
'kubectl -n ${CI_PROJECT_PATH_SLUG}-${CI_COMMIT_REF_SLUG} delete all --all', |
|
40
|
|
|
'kubectl delete namespace ${CI_PROJECT_PATH_SLUG}-${CI_COMMIT_REF_SLUG}', |
|
41
|
|
|
]; |
|
42
|
|
|
foreach ($branchesModel->getBranches() as $branch) { |
|
43
|
|
|
$self->addOnly($branch); |
|
44
|
|
|
} |
|
45
|
|
|
foreach ($branchesModel->getBranchesToIgnore() as $branch) { |
|
46
|
|
|
$self->addExcept($branch); |
|
47
|
|
|
} |
|
48
|
|
|
$self->environment = [ |
|
49
|
|
|
'name' => 'review/$CI_COMMIT_REF_NAME', |
|
50
|
|
|
'action' => 'stop', |
|
51
|
|
|
]; |
|
52
|
|
|
$self->manual = true; |
|
53
|
|
|
return $self; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* @param BaseGitLabCIContext $context |
|
58
|
|
|
* @param BranchesModel $branchesModel |
|
59
|
|
|
* @param bool $isManual |
|
60
|
|
|
* @return CleanupKubernetesJob |
|
61
|
|
|
* @throws JobException |
|
62
|
|
|
*/ |
|
63
|
|
|
public static function newCleanupForRancher(BaseGitLabCIContext $context, BranchesModel $branchesModel, bool $isManual): self |
|
|
|
|
|
|
64
|
|
|
{ |
|
65
|
|
|
$self = new self($context->getEnvironmentName()); |
|
66
|
|
|
$self->image = 'thecodingmachine/gitlab-registry-cleaner:latest'; |
|
67
|
|
|
$self->variables = [ |
|
68
|
|
|
'KUBECONFIG' => '/root/.kube/config', |
|
69
|
|
|
'REGISTRY_DOMAIN_NAME' => $context->getRegistryDomainName(), |
|
70
|
|
|
'PROJECT_GROUP' => $context->getProjectGroup(), |
|
71
|
|
|
'PROJECT_NAME' => $context->getProjectName() |
|
72
|
|
|
]; |
|
73
|
|
|
$scriptTag = $branchesModel->isSingleBranch() ? strtolower($branchesModel->getBranches()[0]) : '${CI_COMMIT_REF_SLUG}'; |
|
74
|
|
|
$self->script = [ |
|
75
|
|
|
'mkdir ~/.kube', |
|
76
|
|
|
'echo "$KUBE_CONFIG" > ~/.kube/config', |
|
77
|
|
|
'kubectl -n ${CI_PROJECT_PATH_SLUG}-${CI_COMMIT_REF_SLUG} delete all --all', |
|
78
|
|
|
'kubectl delete namespace ${CI_PROJECT_PATH_SLUG}-${CI_COMMIT_REF_SLUG}', |
|
79
|
|
|
'/delete_image.sh ${REGISTRY_DOMAIN_NAME}/${PROJECT_GROUP}/${PROJECT_NAME}:' . $scriptTag, |
|
80
|
|
|
]; |
|
81
|
|
|
foreach ($branchesModel->getBranches() as $branch) { |
|
82
|
|
|
$self->addOnly($branch); |
|
83
|
|
|
} |
|
84
|
|
|
foreach ($branchesModel->getBranchesToIgnore() as $branch) { |
|
85
|
|
|
$self->addExcept($branch); |
|
86
|
|
|
} |
|
87
|
|
|
$self->environment = [ |
|
88
|
|
|
'name' => 'review/$CI_COMMIT_REF_NAME', |
|
89
|
|
|
'action' => 'stop', |
|
90
|
|
|
]; |
|
91
|
|
|
$self->manual = true; |
|
92
|
|
|
return $self; |
|
93
|
|
|
} |
|
94
|
|
|
} |
|
95
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.