debian-mirror-gitlab/spec/support/helpers/kubernetes_helpers.rb

220 lines
7.2 KiB
Ruby
Raw Normal View History

2017-08-17 22:00:37 +05:30
module KubernetesHelpers
include Gitlab::Kubernetes
2017-09-10 17:25:29 +05:30
def kube_response(body)
{ body: body.to_json }
end
def kube_pods_response
kube_response(kube_pods_body)
end
2018-11-08 19:23:39 +05:30
def kube_deployments_response
kube_response(kube_deployments_body)
end
2018-03-17 18:26:18 +05:30
def stub_kubeclient_discover(api_url)
WebMock.stub_request(:get, api_url + '/api/v1').to_return(kube_response(kube_v1_discovery_body))
2018-11-08 19:23:39 +05:30
WebMock.stub_request(:get, api_url + '/apis/extensions/v1beta1').to_return(kube_response(kube_v1beta1_discovery_body))
2018-11-20 20:47:30 +05:30
WebMock.stub_request(:get, api_url + '/apis/rbac.authorization.k8s.io/v1').to_return(kube_response(kube_v1_rbac_authorization_discovery_body))
2017-09-10 17:25:29 +05:30
end
def stub_kubeclient_pods(response = nil)
2018-03-17 18:26:18 +05:30
stub_kubeclient_discover(service.api_url)
2017-09-10 17:25:29 +05:30
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
2018-11-08 19:23:39 +05:30
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
2018-12-13 13:39:08 +05:30
def stub_kubeclient_get_secret(api_url, **options)
2018-12-05 23:21:45 +05:30
options[:metadata_name] ||= "default-token-1"
2018-12-13 13:39:08 +05:30
options[:namespace] ||= "default"
2018-12-05 23:21:45 +05:30
2018-12-13 13:39:08 +05:30
WebMock.stub_request(:get, api_url + "/api/v1/namespaces/#{options[:namespace]}/secrets/#{options[:metadata_name]}")
2018-12-05 23:21:45 +05:30
.to_return(kube_response(kube_v1_secret_body(options)))
2018-03-17 18:26:18 +05:30
end
2018-12-05 23:21:45 +05:30
def stub_kubeclient_get_secret_error(api_url, name, namespace: 'default')
WebMock.stub_request(:get, api_url + "/api/v1/namespaces/#{namespace}/secrets/#{name}")
2018-03-17 18:26:18 +05:30
.to_return(status: [404, "Internal Server Error"])
end
2018-12-05 23:21:45 +05:30
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
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
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
2018-12-13 13:39:08 +05:30
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
2018-12-05 23:21:45 +05:30
def kube_v1_secret_body(**options)
2018-03-17 18:26:18 +05:30
{
"kind" => "SecretList",
"apiVersion": "v1",
2018-12-05 23:21:45 +05:30
"metadata": {
"name": options[:metadata_name] || "default-token-1",
"namespace": "kube-system"
},
"data": {
"token": options[:token] || Base64.encode64('token-sample-123')
}
2018-03-17 18:26:18 +05:30
}
end
2017-09-10 17:25:29 +05:30
def kube_v1_discovery_body
2017-08-17 22:00:37 +05:30
{
"kind" => "APIResourceList",
"resources" => [
2018-03-17 18:26:18 +05:30
{ "name" => "pods", "namespaced" => true, "kind" => "Pod" },
2018-11-08 19:23:39 +05:30
{ "name" => "deployments", "namespaced" => true, "kind" => "Deployment" },
2018-11-20 20:47:30 +05:30
{ "name" => "secrets", "namespaced" => true, "kind" => "Secret" },
2018-12-05 23:21:45 +05:30
{ "name" => "serviceaccounts", "namespaced" => true, "kind" => "ServiceAccount" },
2018-12-13 13:39:08 +05:30
{ "name" => "services", "namespaced" => true, "kind" => "Service" },
{ "name" => "namespaces", "namespaced" => true, "kind" => "Namespace" }
2018-11-08 19:23:39 +05:30
]
}
end
def kube_v1beta1_discovery_body
{
"kind" => "APIResourceList",
"resources" => [
{ "name" => "pods", "namespaced" => true, "kind" => "Pod" },
{ "name" => "deployments", "namespaced" => true, "kind" => "Deployment" },
2018-11-20 20:47:30 +05:30
{ "name" => "secrets", "namespaced" => true, "kind" => "Secret" },
2018-12-05 23:21:45 +05:30
{ "name" => "serviceaccounts", "namespaced" => true, "kind" => "ServiceAccount" },
2018-11-20 20:47:30 +05:30
{ "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" }
2017-08-17 22:00:37 +05:30
]
}
end
2017-09-10 17:25:29 +05:30
def kube_pods_body
{
"kind" => "PodList",
"items" => [kube_pod]
}
2017-08-17 22:00:37 +05:30
end
2018-11-08 19:23:39 +05:30
def kube_deployments_body
{
"kind" => "DeploymentList",
"items" => [kube_deployment]
}
end
2017-08-17 22:00:37 +05:30
# This is a partial response, it will have many more elements in reality but
# these are the ones we care about at the moment
2018-11-08 19:23:39 +05:30
def kube_pod(name: "kube-pod", app: "valid-pod-label", status: "Running", track: nil)
2017-08-17 22:00:37 +05:30
{
"metadata" => {
2017-09-10 17:25:29 +05:30
"name" => name,
2018-11-08 19:23:39 +05:30
"generate_name" => "generated-name-with-suffix",
2017-08-17 22:00:37 +05:30
"creationTimestamp" => "2016-11-25T19:55:19Z",
2018-11-08 19:23:39 +05:30
"labels" => {
"app" => app,
"track" => track
}
2017-08-17 22:00:37 +05:30
},
"spec" => {
"containers" => [
{ "name" => "container-0" },
2017-09-10 17:25:29 +05:30
{ "name" => "container-1" }
]
2017-08-17 22:00:37 +05:30
},
2018-11-08 19:23:39 +05:30
"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
}
2017-08-17 22:00:37 +05:30
}
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'] },
url: container_exec_url(service.api_url, service.actual_namespace, pod_name, container['name']),
subprotocols: ['channel.k8s.io'],
headers: { 'Authorization' => ["Bearer #{service.token}"] },
created_at: DateTime.parse(pod['metadata']['creationTimestamp']),
max_session_time: 0
}
terminal[:ca_pem] = service.ca_pem if service.ca_pem.present?
terminal
end
end
2018-11-08 19:23:39 +05:30
def kube_deployment_rollout_status
::Gitlab::Kubernetes::RolloutStatus.from_deployments(kube_deployment)
end
def empty_deployment_rollout_status
::Gitlab::Kubernetes::RolloutStatus.from_deployments()
end
2017-08-17 22:00:37 +05:30
end