def _run_dsl(args): os: MockOsSystem = args["os_system"] fs: MockFileSystem = args["file_system"] app_name = 'test-service' cluster_name = 'prod-us-east1' kubectl_version = "v1.15.3" container_1 = container(name='myservice', image='busybox:1.0') container_2 = container(name='myservice-2', image='busybox:2.0') os.mock_clusters_list([cluster_name]) os.mock_kubernetes_release(E.success(kubectl_version)) os.mock_download_install_kubectl(kubectl_version, E.success()) os.mock_create_kube_config(cluster_name, E.success()) os.mock_kubectl_apply_temp_file(cluster_name, E.success()) _, result = gen_template([ deployment(app_name) .with_containers([container_1, container_2]) ]) deployment_yaml = find_write_template_calls(fs)[0] containers = deployment_yaml['spec']['template']['spec']['containers'] assert containers[0]['name'] == 'myservice-app' assert containers[0]['image'] == 'busybox:1.0' assert containers[1]['name'] == 'myservice-2-app' assert containers[1]['image'] == 'busybox:2.0'
def _run_dsl(args): os: MockOsSystem = args["os_system"] fs: MockFileSystem = args["file_system"] project_id = "project1" app_name = 'test-service' cluster_name = 'prod-us-east1' kubectl_version = "v1.15.3" service_account_b64 = b64encode(json.dumps({'key': 'someKey'}).encode('utf-8')) os.mock_clusters_list([cluster_name]) os.mock_kubernetes_release(E.success(kubectl_version)) os.mock_download_install_kubectl(kubectl_version, E.success()) os.mock_create_kube_config(cluster_name, E.success()) os.mock_kubectl_apply_temp_file(cluster_name, E.success()) _, result = run_in_cluster( conn=gke_conn(cluster_name, project_id, service_account_b64), templates=[ deployment(app_name) .with_rolling_update(max_surge='20%', max_unavailable='20%') .with_empty_volume(name='test-volume') .with_container(container('test', 'test:100')) .with_container(container('test-2', 'test-2:100')) ] ) deployment_yaml = find_write_template_calls(fs)[0] assert deployment_yaml['spec']['strategy'] == {'rollingUpdate': {'maxSurge': '20%', 'maxUnavailable': '20%'}, 'type': 'RollingUpdate'}
def _run_dsl(args): os: MockOsSystem = args["os_system"] fs: MockFileSystem = args["file_system"] project_id = "project1" app_name = 'test-service' cluster_name = 'prod-us-east1' kubectl_version = "v1.15.3" service_account_b64 = b64encode(json.dumps({'key': 'someKey'}).encode('utf-8')) container_1 = container(name='myservice', image='busybox:1.0') container_2 = container(name='myservice-2', image='busybox:2.0') os.mock_clusters_list([cluster_name]) os.mock_kubernetes_release(E.success(kubectl_version)) os.mock_download_install_kubectl(kubectl_version, E.success()) os.mock_create_kube_config(cluster_name, E.success()) os.mock_kubectl_apply_temp_file(cluster_name, E.success()) _, result = run_in_cluster( conn=gke_conn(cluster_name, project_id, service_account_b64), templates=[ deployment(app_name) .with_containers([container_1, container_2]) ] ) deployment_yaml = find_write_template_calls(fs)[0] containers = deployment_yaml['spec']['template']['spec']['containers'] assert containers[0]['name'] == 'myservice-app' assert containers[0]['image'] == 'busybox:1.0' assert containers[1]['name'] == 'myservice-2-app' assert containers[1]['image'] == 'busybox:2.0'
def _run_dsl(args): os: MockOsSystem = args["os_system"] fs: MockFileSystem = args["file_system"] project_id = "project1" app_name = 'test-service' cluster_name = 'prod-us-east1' kubectl_version = "v1.15.3" service_account_b64 = b64encode(json.dumps({'key': 'someKey'}).encode('utf-8')) os.mock_clusters_list([cluster_name]) os.mock_kubernetes_release(E.success(kubectl_version)) os.mock_download_install_kubectl(kubectl_version, E.success()) os.mock_create_kube_config(cluster_name, E.success()) os.mock_kubectl_apply_temp_file(cluster_name, E.success()) _, result = run_in_cluster( conn=gke_conn(cluster_name, project_id, service_account_b64), templates=[ deployment(app_name) .with_rolling_update(max_surge='20%', max_unavailable='20%') .with_volume_claim(name='test-volume', claim_name='some-claim-name') .with_container(container('test', 'test:100') .with_volume_mount('test-volume', '/var/data/my-data') .with_environment_from_context('ENV', lambda c: c('cluster_name'))) ] ) deployment_yaml = find_write_template_calls(fs)[0] assert deployment_yaml['spec']['template']['spec']['containers'][0]['env'][1]['name'] == 'ENV' assert deployment_yaml['spec']['template']['spec']['containers'][0]['env'][1]['value'] == cluster_name
def _run_dsl(args): fs: MockFileSystem = args['file_system'] cluster_name = 'prod-us-east1' project_id = 'project1' service_account_b64 = b64encode( json.dumps({ 'key': 'someKey' }).encode('utf-8')) _, result = run_in_cluster( conn=gke_conn(cluster_name, project_id, service_account_b64), templates=[ micro_service( 'test', "test-service:1.0.0", 3000).with_rolling_update( max_surge='20%', max_unavailable='20%').with_empty_volume( 'my-volume', '/var/data/my-data').with_sidecar( container( 'my-sidecar', 'my-sidecar:1.0.0').with_volume_mount( 'my-volume', 'var/data/sidecar-data')) ]) deployment = find_write_template_calls(fs)[1] assert deployment['spec']['strategy'] == { 'rollingUpdate': { 'maxSurge': '20%', 'maxUnavailable': '20%' }, 'type': 'RollingUpdate' }
def _run_dsl(args): fs: MockFileSystem = args['file_system'] cluster_name = 'prod-us-east1' project_id = 'project1' service_account_b64 = b64encode( json.dumps({ 'key': 'someKey' }).encode('utf-8')) _, result = run_in_cluster( conn=gke_conn(cluster_name, project_id, service_account_b64), templates=[ micro_service( 'test', "test-service:1.0.0", 3000).with_annotations({ 'some': 'thing' }).with_empty_volume( 'my-volume', '/var/data/my-data').with_sidecar( container('my-sidecar', 'my-sidecar:1.0.0').with_volume_mount( 'my-volume', 'var/data/sidecar-data')) ]) deployment = find_write_template_calls(fs)[1] assert deployment['spec']['template']['metadata']['annotations'] == { 'some': 'thing' }
def _run_dsl(args): fs: MockFileSystem = args['file_system'] cluster_name = 'prod-us-east1' project_id = 'project1' service_account_b64 = b64encode( json.dumps({ 'key': 'someKey' }).encode('utf-8')) _, result = run_in_cluster( conn=gke_conn(cluster_name, project_id, service_account_b64), templates=[ micro_service( 'test', "test-service:1.0.0", 3000).with_replicas( min=5, max=10, cpu_threshold_percentage=80).with_empty_volume( 'my-volume', '/var/data/my-data').with_sidecar( container( 'my-sidecar', 'my-sidecar:1.0.0').with_volume_mount( 'my-volume', 'var/data/sidecar-data')) ]) deployment = find_write_template_calls(fs)[1] assert deployment['spec']['replicas'] == 5 hpa = find_write_template_calls(fs)[2] assert hpa['spec']['minReplicas'] == 5 assert hpa['spec']['maxReplicas'] == 10 assert hpa['spec']['targetCPUUtilizationPercentage'] == 80
def _run_dsl(args): fs: MockFileSystem = args['file_system'] cluster_name = 'prod-us-east1' project_id = 'project1' service_account_b64 = b64encode( json.dumps({ 'key': 'someKey' }).encode('utf-8')) _, result = run_in_cluster( conn=gke_conn(cluster_name, project_id, service_account_b64), templates=[ micro_service( 'test', "test-service:1.0.0", 3000).with_ensure_only_one().with_empty_volume( 'my-volume', '/var/data/my-data').with_sidecar( container('my-sidecar', 'my-sidecar:1.0.0').with_volume_mount( 'my-volume', 'var/data/sidecar-data')) ]) deployment = find_write_template_calls(fs)[1] assert deployment['spec']['replicas'] == 1 assert deployment['spec']['strategy'] == {'type': 'Recreate'} hpa = find_write_template_calls(fs)[2] assert hpa['spec']['minReplicas'] == 1 assert hpa['spec']['maxReplicas'] == 1
def _run_dsl(args): fs: MockFileSystem = args['file_system'] os: MockOsSystem = args["os_system"] os.mock_clusters_list([ 'prod-us-east1-api-cluster', 'prod-10-api-cluster', 'stage-10-api-cluster' ]) cluster_name = 'prod-.*-api-cluster' project_id = 'project1' service_account_b64 = b64encode( json.dumps({ 'key': 'someKey' }).encode('utf-8')) _, result = run_in_cluster( conn=gke_conn(cluster_name, project_id, service_account_b64), templates=[ micro_service( 'test', "test-service:1.0.0", 3000).with_empty_volume( 'my-volume', '/var/data/my-data').with_sidecar( container('my-sidecar', 'my-sidecar:1.0.0').with_volume_mount( 'my-volume', 'var/data/sidecar-data')) ]) assert 'prod-us-east1-api-cluster' in find_cluster_templates(fs) assert 'prod-10-api-cluster' in find_cluster_templates(fs) assert 'stage-10-api-cluster' not in find_cluster_templates(fs)
def _run_dsl(args): fs: MockFileSystem = args["file_system"] cluster_name = 'prod-us-east1' project_id = 'project1' service_account_b64 = b64encode( json.dumps({ 'key': 'someKey' }).encode('utf-8')) _, result = run_in_cluster( conn=gke_conn(cluster_name, project_id, service_account_b64), templates=[ micro_service( 'test', "test-service:1.0.0", 3000).with_empty_volume( 'my-volume', '/var/data/my-data').with_sidecar( container('my-sidecar', 'my-sidecar:1.0.0').with_volume_mount( 'my-volume', 'var/data/sidecar-data')) ]) deployment = find_write_template_calls(fs)[1] assert deployment['spec']['template']['spec']['volumes'][0] == { 'name': 'my-volume', 'emptyDir': {} } microservice = deployment['spec']['template']['spec']['containers'][0] assert microservice['volumeMounts'][0] == { 'name': 'my-volume', 'mountPath': '/var/data/my-data' } sidecar = deployment['spec']['template']['spec']['containers'][1] assert sidecar['volumeMounts'][0] == { 'name': 'my-volume', 'mountPath': 'var/data/sidecar-data' }
def _run_dsl(args): os: MockOsSystem = args["os_system"] fs: MockFileSystem = args["file_system"] project_id = "project1" app_name = 'test-service' cluster_name = 'prod-us-east1' kubectl_version = "v1.15.3" service_account_b64 = b64encode(json.dumps({'key': 'someKey'}).encode('utf-8')) os.mock_clusters_list([cluster_name]) os.mock_kubernetes_release(E.success(kubectl_version)) os.mock_download_install_kubectl(kubectl_version, E.success()) os.mock_create_kube_config(cluster_name, E.success()) os.mock_kubectl_apply_temp_file(cluster_name, E.success()) _, result = run_in_cluster( conn=gke_conn(cluster_name, project_id, service_account_b64), templates=[ deployment(app_name) .with_empty_volume(name='test-volume') .with_container(container('test', 'test:100')) .with_container(container('test-2', 'test-2:100')) ] ) deployment_yaml = find_write_template_calls(fs)[0] assert deployment_yaml['kind'] == 'Deployment' assert deployment_yaml['metadata']['name'] == app_name+'-dp' assert deployment_yaml['spec']['selector']['matchLabels']['app'] == app_name+'-app' assert deployment_yaml['spec']['template']['spec']['volumes'][0]['name'] == 'test-volume' container1 = deployment_yaml['spec']['template']['spec']['containers'][0] container2 = deployment_yaml['spec']['template']['spec']['containers'][1] assert container1['name'] == 'test-app' assert container1['image'] == 'test:100' assert container2['name'] == 'test-2-app' assert container2['image'] == 'test-2:100'
def _run_dsl(args): fs: MockFileSystem = args["file_system"] cluster_name = 'prod-us-east1' project_id = 'project1' service_account_b64 = b64encode( json.dumps({ 'key': 'someKey' }).encode('utf-8')) _, result = run_in_cluster( conn=gke_conn(cluster_name, project_id, service_account_b64), templates=[ micro_service('test', "test-service:1.0.0", 3000).with_sidecar( container('my-sidecar', 'my-sidecar:1.0.0')) ]) yamls = find_write_template_calls(fs) assert yamls[1]['kind'] == 'Deployment' sidecar = yamls[1]['spec']['template']['spec']['containers'][1] assert sidecar['name'] == 'my-sidecar-app' assert sidecar['image'] == 'my-sidecar:1.0.0'
def _run_dsl(args): fs: MockFileSystem = args['file_system'] cluster_name = 'prod-us-east1' project_id = 'project1' service_account_b64 = b64encode( json.dumps({ 'key': 'someKey' }).encode('utf-8')) _, result = run_in_cluster( conn=gke_conn(cluster_name, project_id, service_account_b64), templates=[ micro_service('test', "test-service:1.0.0", 3000).with_sidecar( container('my-sidecar', 'my-sidecar:1.0.0').with_volume_mount( 'my-volume', 'var/data/sidecar-data'). with_environment_from_context('ENV', lambda c: c('cluster_name'))) ]) deployment = find_write_template_calls(fs)[1] microservice = deployment['spec']['template']['spec']['containers'][1] assert microservice['env'][1]['name'] == 'ENV' assert microservice['env'][1]['value'] == 'prod-us-east1'