kubernetes_helpers.rb 7.2 KB
Newer Older
1 2 3
module KubernetesHelpers
  include Gitlab::Kubernetes

4 5 6 7 8 9 10 11
  def kube_response(body)
    { body: body.to_json }
  end

  def kube_pods_response
    kube_response(kube_pods_body)
  end

12 13 14 15
  def kube_deployments_response
    kube_response(kube_deployments_body)
  end

16 17
  def stub_kubeclient_discover(api_url)
    WebMock.stub_request(:get, api_url + '/api/v1').to_return(kube_response(kube_v1_discovery_body))
18
    WebMock.stub_request(:get, api_url + '/apis/extensions/v1beta1').to_return(kube_response(kube_v1beta1_discovery_body))
19
    WebMock.stub_request(:get, api_url + '/apis/rbac.authorization.k8s.io/v1').to_return(kube_response(kube_v1_rbac_authorization_discovery_body))
20 21 22
  end

  def stub_kubeclient_pods(response = nil)
23
    stub_kubeclient_discover(service.api_url)
24 25 26 27 28
    pods_url = service.api_url + "/api/v1/namespaces/#{service.actual_namespace}/pods"

    WebMock.stub_request(:get, pods_url).to_return(response || kube_pods_response)
  end

29 30 31 32 33 34 35
  def stub_kubeclient_deployments(response = nil)
    stub_kubeclient_discover(service.api_url)
    deployments_url = service.api_url + "/apis/extensions/v1beta1/namespaces/#{service.actual_namespace}/deployments"

    WebMock.stub_request(:get, deployments_url).to_return(response || kube_deployments_response)
  end

36
  def stub_kubeclient_get_secret(api_url, **options)
37
    options[:metadata_name] ||= "default-token-1"
38
    options[:namespace] ||= "default"
39

40
    WebMock.stub_request(:get, api_url + "/api/v1/namespaces/#{options[:namespace]}/secrets/#{options[:metadata_name]}")
41
      .to_return(kube_response(kube_v1_secret_body(options)))
42 43
  end

44
  def stub_kubeclient_get_secret_error(api_url, name, namespace: 'default', status: 404)
45
    WebMock.stub_request(:get, api_url + "/api/v1/namespaces/#{namespace}/secrets/#{name}")
46
      .to_return(status: [status, "Internal Server Error"])
47 48
  end

49 50 51 52 53
  def stub_kubeclient_create_service_account(api_url, namespace: 'default')
    WebMock.stub_request(:post, api_url + "/api/v1/namespaces/#{namespace}/serviceaccounts")
      .to_return(kube_response({}))
  end

54 55 56 57 58 59 60 61 62 63
  def stub_kubeclient_create_service_account_error(api_url, namespace: 'default')
    WebMock.stub_request(:post, api_url + "/api/v1/namespaces/#{namespace}/serviceaccounts")
      .to_return(status: [500, "Internal Server Error"])
  end

  def stub_kubeclient_create_secret(api_url, namespace: 'default')
    WebMock.stub_request(:post, api_url + "/api/v1/namespaces/#{namespace}/secrets")
      .to_return(kube_response({}))
  end

64 65 66 67 68
  def stub_kubeclient_create_cluster_role_binding(api_url)
    WebMock.stub_request(:post, api_url + '/apis/rbac.authorization.k8s.io/v1/clusterrolebindings')
      .to_return(kube_response({}))
  end

69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
  def stub_kubeclient_create_role_binding(api_url, namespace: 'default')
    WebMock.stub_request(:post, api_url + "/apis/rbac.authorization.k8s.io/v1/namespaces/#{namespace}/rolebindings")
      .to_return(kube_response({}))
  end

  def stub_kubeclient_create_namespace(api_url)
    WebMock.stub_request(:post, api_url + "/api/v1/namespaces")
      .to_return(kube_response({}))
  end

  def stub_kubeclient_get_namespace(api_url, namespace: 'default')
    WebMock.stub_request(:get, api_url + "/api/v1/namespaces/#{namespace}")
      .to_return(kube_response({}))
  end

84
  def kube_v1_secret_body(**options)
85 86 87
    {
      "kind" => "SecretList",
      "apiVersion": "v1",
88 89 90 91 92 93 94
      "metadata": {
        "name": options[:metadata_name] || "default-token-1",
        "namespace": "kube-system"
      },
      "data": {
        "token": options[:token] || Base64.encode64('token-sample-123')
      }
95 96 97
    }
  end

98
  def kube_v1_discovery_body
99 100
    {
      "kind" => "APIResourceList",
101
      "resources" => [
102
        { "name" => "pods", "namespaced" => true, "kind" => "Pod" },
103
        { "name" => "deployments", "namespaced" => true, "kind" => "Deployment" },
104
        { "name" => "secrets", "namespaced" => true, "kind" => "Secret" },
105
        { "name" => "serviceaccounts", "namespaced" => true, "kind" => "ServiceAccount" },
106 107
        { "name" => "services", "namespaced" => true, "kind" => "Service" },
        { "name" => "namespaces", "namespaced" => true, "kind" => "Namespace" }
108 109 110 111 112 113 114 115 116 117
      ]
    }
  end

  def kube_v1beta1_discovery_body
    {
      "kind" => "APIResourceList",
      "resources" => [
        { "name" => "pods", "namespaced" => true, "kind" => "Pod" },
        { "name" => "deployments", "namespaced" => true, "kind" => "Deployment" },
118
        { "name" => "secrets", "namespaced" => true, "kind" => "Secret" },
119
        { "name" => "serviceaccounts", "namespaced" => true, "kind" => "ServiceAccount" },
120 121 122 123 124 125 126 127 128 129 130 131 132
        { "name" => "services", "namespaced" => true, "kind" => "Service" }
      ]
    }
  end

  def kube_v1_rbac_authorization_discovery_body
    {
      "kind" => "APIResourceList",
      "resources" => [
        { "name" => "clusterrolebindings", "namespaced" => false, "kind" => "ClusterRoleBinding" },
        { "name" => "clusterroles", "namespaced" => false, "kind" => "ClusterRole" },
        { "name" => "rolebindings", "namespaced" => true, "kind" => "RoleBinding" },
        { "name" => "roles", "namespaced" => true, "kind" => "Role" }
133
      ]
134 135 136
    }
  end

137 138 139 140 141
  def kube_pods_body
    {
      "kind" => "PodList",
      "items" => [kube_pod]
    }
142 143
  end

144 145 146 147 148 149 150
  def kube_deployments_body
    {
      "kind" => "DeploymentList",
      "items" => [kube_deployment]
    }
  end

151 152
  # This is a partial response, it will have many more elements in reality but
  # these are the ones we care about at the moment
153
  def kube_pod(name: "kube-pod", app: "valid-pod-label", status: "Running", track: nil)
154 155
    {
      "metadata" => {
156
        "name" => name,
157
        "generate_name" => "generated-name-with-suffix",
158
        "creationTimestamp" => "2016-11-25T19:55:19Z",
159 160 161 162
        "labels" => {
          "app" => app,
          "track" => track
        }
163 164 165 166
      },
      "spec" => {
        "containers" => [
          { "name" => "container-0" },
167 168
          { "name" => "container-1" }
        ]
169
      },
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
      "status" => { "phase" => status }
    }
  end

  def kube_deployment(name: "kube-deployment", app: "valid-deployment-label", track: nil)
    {
      "metadata" => {
        "name" => name,
        "generation" => 4,
        "labels" => {
          "app" => app,
          "track" => track
        }.compact
      },
      "spec" => { "replicas" => 3 },
      "status" => {
        "observedGeneration" => 4,
        "replicas" => 3,
        "updatedReplicas" => 3,
        "availableReplicas" => 3
      }
191 192 193 194 195 196 197 198 199 200
    }
  end

  def kube_terminals(service, pod)
    pod_name = pod['metadata']['name']
    containers = pod['spec']['containers']

    containers.map do |container|
      terminal = {
        selectors: { pod: pod_name, container: container['name'] },
201
        url:  container_exec_url(service.api_url, service.actual_namespace, pod_name, container['name']),
202 203
        subprotocols: ['channel.k8s.io'],
        headers: { 'Authorization' => ["Bearer #{service.token}"] },
204 205
        created_at: DateTime.parse(pod['metadata']['creationTimestamp']),
        max_session_time: 0
206 207 208 209 210
      }
      terminal[:ca_pem] = service.ca_pem if service.ca_pem.present?
      terminal
    end
  end
211 212 213 214 215 216 217 218

  def kube_deployment_rollout_status
    ::Gitlab::Kubernetes::RolloutStatus.from_deployments(kube_deployment)
  end

  def empty_deployment_rollout_status
    ::Gitlab::Kubernetes::RolloutStatus.from_deployments()
  end
219
end