summaryrefslogtreecommitdiff
path: root/spec/services/clusters/kubernetes/create_or_update_namespace_service_spec.rb
blob: 7e3f1fdb37913ea69b3aa677bcd5fcdb5e63c4df (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Clusters::Kubernetes::CreateOrUpdateNamespaceService, '#execute' do
  include KubernetesHelpers

  let(:cluster) { create(:cluster, :project, :provided_by_gcp) }
  let(:platform) { cluster.platform }
  let(:api_url) { 'https://kubernetes.example.com' }
  let(:project) { cluster.project }
  let(:environment) { create(:environment, project: project) }
  let(:cluster_project) { cluster.cluster_project }
  let(:namespace) { "#{project.name}-#{project.id}-#{environment.slug}" }

  subject do
    described_class.new(
      cluster: cluster,
      kubernetes_namespace: kubernetes_namespace
    ).execute
  end

  before do
    stub_kubeclient_discover(api_url)
    stub_kubeclient_get_namespace(api_url)
    stub_kubeclient_get_service_account_error(api_url, 'gitlab')
    stub_kubeclient_create_service_account(api_url)
    stub_kubeclient_get_secret_error(api_url, 'gitlab-token')
    stub_kubeclient_create_secret(api_url)

    stub_kubeclient_put_role_binding(api_url, "gitlab-#{namespace}", namespace: namespace)
    stub_kubeclient_get_namespace(api_url, namespace: namespace)
    stub_kubeclient_get_service_account_error(api_url, "#{namespace}-service-account", namespace: namespace)
    stub_kubeclient_create_service_account(api_url, namespace: namespace)
    stub_kubeclient_create_secret(api_url, namespace: namespace)
    stub_kubeclient_put_secret(api_url, "#{namespace}-token", namespace: namespace)
    stub_kubeclient_put_role(api_url, Clusters::Kubernetes::GITLAB_KNATIVE_SERVING_ROLE_NAME, namespace: namespace)
    stub_kubeclient_put_role_binding(api_url, Clusters::Kubernetes::GITLAB_KNATIVE_SERVING_ROLE_BINDING_NAME, namespace: namespace)
    stub_kubeclient_put_role(api_url, Clusters::Kubernetes::GITLAB_CROSSPLANE_DATABASE_ROLE_NAME, namespace: namespace)
    stub_kubeclient_put_role_binding(api_url, Clusters::Kubernetes::GITLAB_CROSSPLANE_DATABASE_ROLE_BINDING_NAME, namespace: namespace)

    stub_kubeclient_get_secret(
      api_url,
      metadata_name: "#{namespace}-token",
      token: Base64.encode64('sample-token'),
      namespace: namespace
    )
  end

  shared_examples 'successful creation of kubernetes namespace' do
    it 'creates a Clusters::KubernetesNamespace' do
      expect do
        subject
      end.to change(Clusters::KubernetesNamespace, :count).by(1)
    end

    it 'creates project service account and namespace' do
      account_service = double(Clusters::Kubernetes::CreateOrUpdateServiceAccountService)
      expect(Clusters::Kubernetes::CreateOrUpdateServiceAccountService).to(
        receive(:namespace_creator).with(
          cluster.platform.kubeclient,
          service_account_name: "#{namespace}-service-account",
          service_account_namespace: namespace,
          service_account_namespace_labels: {
            'app.gitlab.com/app' => project.full_path_slug,
            'app.gitlab.com/env' => environment.slug
          },
          rbac: true
        ).and_return(account_service)
      )
      expect(account_service).to receive(:execute).once
      subject
    end

    it 'configures kubernetes token' do
      subject

      kubernetes_namespace.reload
      expect(kubernetes_namespace.namespace).to eq(namespace)
      expect(kubernetes_namespace.service_account_name).to eq("#{namespace}-service-account")
      expect(kubernetes_namespace.encrypted_service_account_token).to be_present
    end

    context 'without environment' do
      before do
        kubernetes_namespace.environment = nil
      end

      it 'creates project service account and namespace' do
        account_service = double(Clusters::Kubernetes::CreateOrUpdateServiceAccountService)
        expect(Clusters::Kubernetes::CreateOrUpdateServiceAccountService).to(
          receive(:namespace_creator).with(
            cluster.platform.kubeclient,
            service_account_name: "#{namespace}-service-account",
            service_account_namespace: namespace,
            service_account_namespace_labels: {
              'app.gitlab.com/app' => project.full_path_slug
            },
            rbac: true
          ).and_return(account_service)
        )
        expect(account_service).to receive(:execute).once
        subject
      end
    end
  end

  context 'group clusters' do
    let(:cluster) { create(:cluster, :group, :provided_by_gcp) }
    let(:group) { cluster.group }
    let(:project) { create(:project, group: group) }

    context 'when kubernetes namespace is not persisted' do
      let(:kubernetes_namespace) do
        build(:cluster_kubernetes_namespace,
              cluster: cluster,
              project: project,
              environment: environment)
      end

      it_behaves_like 'successful creation of kubernetes namespace'
    end
  end

  context 'project clusters' do
    context 'when kubernetes namespace is not persisted' do
      let(:kubernetes_namespace) do
        build(:cluster_kubernetes_namespace,
              cluster: cluster,
              project: cluster_project.project,
              cluster_project: cluster_project,
              environment: environment)
      end

      it_behaves_like 'successful creation of kubernetes namespace'
    end

    context 'when there is a Kubernetes Namespace associated' do
      let(:namespace) { "new-namespace-#{environment.slug}" }

      let(:kubernetes_namespace) do
        create(:cluster_kubernetes_namespace,
               cluster: cluster,
               project: cluster_project.project,
               cluster_project: cluster_project,
               environment: environment)
      end

      before do
        platform.update_column(:namespace, 'new-namespace')
      end

      it 'does not create any Clusters::KubernetesNamespace' do
        subject

        expect(cluster.kubernetes_namespaces).to eq([kubernetes_namespace])
      end

      it 'creates project service account' do
        expect_next_instance_of(Clusters::Kubernetes::CreateOrUpdateServiceAccountService) do |instance|
          expect(instance).to receive(:execute).once
        end

        subject
      end

      it 'updates Clusters::KubernetesNamespace' do
        subject

        kubernetes_namespace.reload

        expect(kubernetes_namespace.namespace).to eq(namespace)
        expect(kubernetes_namespace.service_account_name).to eq("#{namespace}-service-account")
        expect(kubernetes_namespace.encrypted_service_account_token).to be_present
      end
    end
  end
end