CleanupKubernetesJob::newCleanupForGCloud()   A
last analyzed

Complexity

Conditions 4
Paths 8

Size

Total Lines 36
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 29
nc 8
nop 3
dl 0
loc 36
rs 9.456
c 0
b 0
f 0
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
0 ignored issues
show
Unused Code introduced by
The parameter $isManual is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

18
    public static function newCleanupForGCloud(BaseGitLabCIContext $context, BranchesModel $branchesModel, /** @scrutinizer ignore-unused */ bool $isManual): self

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
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
0 ignored issues
show
Unused Code introduced by
The parameter $isManual is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

63
    public static function newCleanupForRancher(BaseGitLabCIContext $context, BranchesModel $branchesModel, /** @scrutinizer ignore-unused */ bool $isManual): self

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
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