kubernetes_helpers.rb 2.8 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
  def stub_kubeclient_discover(api_url)
    WebMock.stub_request(:get, api_url + '/api/v1').to_return(kube_response(kube_v1_discovery_body))
14 15 16
  end

  def stub_kubeclient_pods(response = nil)
17
    stub_kubeclient_discover(service.api_url)
18 19 20 21 22
    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

23 24 25 26 27 28 29
  def stub_kubeclient_get_secrets(api_url, **options)
    WebMock.stub_request(:get, api_url + '/api/v1/secrets')
      .to_return(kube_response(kube_v1_secrets_body(options)))
  end

  def stub_kubeclient_get_secrets_error(api_url)
    WebMock.stub_request(:get, api_url + '/api/v1/secrets')
S
Shinya Maeda 已提交
30
      .to_return(status: [404, "Internal Server Error"])
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
  end

  def kube_v1_secrets_body(**options)
    {
      "kind" => "SecretList",
      "apiVersion": "v1",
      "items" => [
        {
          "metadata": {
            "name": options[:metadata_name] || "default-token-1",
            "namespace": "kube-system"
          },
          "data": {
            "token": options[:token] || Base64.encode64('token-sample-123')
          }
        }
      ]
    }
  end

51
  def kube_v1_discovery_body
52 53
    {
      "kind" => "APIResourceList",
54
      "resources" => [
55 56
        { "name" => "pods", "namespaced" => true, "kind" => "Pod" },
        { "name" => "secrets", "namespaced" => true, "kind" => "Secret" }
57
      ]
58 59 60
    }
  end

61 62 63 64 65
  def kube_pods_body
    {
      "kind" => "PodList",
      "items" => [kube_pod]
    }
66 67 68 69
  end

  # This is a partial response, it will have many more elements in reality but
  # these are the ones we care about at the moment
70
  def kube_pod(name: "kube-pod", app: "valid-pod-label")
71 72
    {
      "metadata" => {
73
        "name" => name,
74
        "creationTimestamp" => "2016-11-25T19:55:19Z",
75
        "labels" => { "app" => app }
76 77 78 79
      },
      "spec" => {
        "containers" => [
          { "name" => "container-0" },
80 81
          { "name" => "container-1" }
        ]
82
      },
83
      "status" => { "phase" => "Running" }
84 85 86 87 88 89 90 91 92 93
    }
  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'] },
94
        url:  container_exec_url(service.api_url, service.actual_namespace, pod_name, container['name']),
95 96
        subprotocols: ['channel.k8s.io'],
        headers: { 'Authorization' => ["Bearer #{service.token}"] },
97 98
        created_at: DateTime.parse(pod['metadata']['creationTimestamp']),
        max_session_time: 0
99 100 101 102 103 104
      }
      terminal[:ca_pem] = service.ca_pem if service.ca_pem.present?
      terminal
    end
  end
end