2016-11-22 14:55:56 -05:00
|
|
|
module KubernetesHelpers
|
|
|
|
include Gitlab::Kubernetes
|
|
|
|
|
2017-06-13 11:31:23 -04:00
|
|
|
def kube_response(body)
|
|
|
|
{ body: body.to_json }
|
|
|
|
end
|
|
|
|
|
|
|
|
def kube_pods_response
|
|
|
|
kube_response(kube_pods_body)
|
|
|
|
end
|
|
|
|
|
2017-10-30 08:55:18 -04:00
|
|
|
def stub_kubeclient_discover(api_url)
|
|
|
|
WebMock.stub_request(:get, api_url + '/api/v1').to_return(kube_response(kube_v1_discovery_body))
|
2017-06-13 11:31:23 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def stub_kubeclient_pods(response = nil)
|
2017-10-31 04:47:48 -04:00
|
|
|
stub_kubeclient_discover(service.api_url)
|
2017-06-13 11:31:23 -04:00
|
|
|
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
|
|
|
|
|
2017-10-30 08:55:18 -04:00
|
|
|
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')
|
2017-11-02 14:37:32 -04:00
|
|
|
.to_return(status: [404, "Internal Server Error"])
|
2017-10-30 08:55:18 -04:00
|
|
|
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
|
|
|
|
|
2017-06-13 11:31:23 -04:00
|
|
|
def kube_v1_discovery_body
|
2017-02-22 12:44:44 -05:00
|
|
|
{
|
|
|
|
"kind" => "APIResourceList",
|
2016-11-22 14:55:56 -05:00
|
|
|
"resources" => [
|
2017-10-30 08:55:18 -04:00
|
|
|
{ "name" => "pods", "namespaced" => true, "kind" => "Pod" },
|
|
|
|
{ "name" => "secrets", "namespaced" => true, "kind" => "Secret" }
|
2017-02-22 12:44:44 -05:00
|
|
|
]
|
2016-11-22 14:55:56 -05:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2017-06-13 11:31:23 -04:00
|
|
|
def kube_pods_body
|
|
|
|
{
|
|
|
|
"kind" => "PodList",
|
|
|
|
"items" => [kube_pod]
|
|
|
|
}
|
2016-11-22 14:55:56 -05:00
|
|
|
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
|
2017-06-13 11:31:23 -04:00
|
|
|
def kube_pod(name: "kube-pod", app: "valid-pod-label")
|
2017-02-22 12:44:44 -05:00
|
|
|
{
|
|
|
|
"metadata" => {
|
2017-06-13 11:31:23 -04:00
|
|
|
"name" => name,
|
2016-11-22 14:55:56 -05:00
|
|
|
"creationTimestamp" => "2016-11-25T19:55:19Z",
|
2017-05-03 07:22:03 -04:00
|
|
|
"labels" => { "app" => app }
|
2016-11-22 14:55:56 -05:00
|
|
|
},
|
|
|
|
"spec" => {
|
|
|
|
"containers" => [
|
|
|
|
{ "name" => "container-0" },
|
2017-05-03 07:22:03 -04:00
|
|
|
{ "name" => "container-1" }
|
|
|
|
]
|
2016-11-22 14:55:56 -05:00
|
|
|
},
|
2017-02-22 12:44:44 -05:00
|
|
|
"status" => { "phase" => "Running" }
|
2016-11-22 14:55:56 -05:00
|
|
|
}
|
|
|
|
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'] },
|
2017-05-08 11:28:05 -04:00
|
|
|
url: container_exec_url(service.api_url, service.actual_namespace, pod_name, container['name']),
|
2016-11-22 14:55:56 -05:00
|
|
|
subprotocols: ['channel.k8s.io'],
|
|
|
|
headers: { 'Authorization' => ["Bearer #{service.token}"] },
|
2017-01-26 13:16:50 -05:00
|
|
|
created_at: DateTime.parse(pod['metadata']['creationTimestamp']),
|
|
|
|
max_session_time: 0
|
2016-11-22 14:55:56 -05:00
|
|
|
}
|
|
|
|
terminal[:ca_pem] = service.ca_pem if service.ca_pem.present?
|
|
|
|
terminal
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|