Created
June 19, 2017 16:25
-
-
Save Fkawala/dae8d24fefca75e7e9596bb6a711630a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Copyright 2016 Google Inc. All rights reserved. | |
# | |
# Licensed under the Apache License, Version 2.0 (the "License"); | |
# you may not use this file except in compliance with the License. | |
# You may obtain a copy of the License at | |
# | |
# http://www.apache.org/licenses/LICENSE-2.0 | |
# | |
# Unless required by applicable law or agreed to in writing, software | |
# distributed under the License is distributed on an "AS IS" BASIS, | |
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
# See the License for the specific language governing permissions and | |
# limitations under the License. | |
"""Helper methods for working with containers in config.""" | |
import json | |
DEFAULT_USER_DATA = '#cloud-config\nruncmd: [{0}]\n'.format( | |
', '.join('\'{0}\''.format(cmd) for cmd in [ '/usr/bin/kubelet', | |
'--allow-privileged=false', | |
'--manifest-url=http://metadata.google.internal/computeMetadata/v1/'\ | |
'instance/attributes/google-container-manifest', | |
'--manifest-url-header=Metadata-Flavor:Google', | |
'--config=/etc/kubernetes/manifests'])) | |
def GenerateManifest(context): | |
"""Generates a Container Manifest given a Template context. | |
Args: | |
context: Template context, which must contain dockerImage and port | |
properties, and an optional dockerEnv property. | |
Returns: | |
A Container Manifest as a YAML string. | |
""" | |
container_name = context.properties['dockerImage'].split('/')[-1] | |
env_list = [] | |
if 'dockerEnv' in context.properties and context.properties['dockerEnv']: | |
for key, value in context.properties['dockerEnv'].iteritems(): | |
env_list.append({'name': key, 'value': str(value)}) | |
manifest = { | |
'apiVersion': 'v1', | |
'kind': 'Pod', | |
'metadata': { | |
'name': context.env['name'] | |
}, | |
'spec': { | |
'containers': [{ | |
'name': container_name, | |
'image': context.properties['dockerImage'], | |
'imagePullPolicy': 'Always' | |
}] | |
} | |
} | |
if 'port' in context.properties and context.properties['port']: | |
ports = {'hostPort': context.properties['port'], | |
'containerPort': context.properties['port']} | |
manifest['spec']['containers'][0]['ports'] = [ports] | |
if env_list: | |
manifest['spec']['containers'][0]['env'] = env_list | |
return json.dumps(manifest) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Copyright 2016 Google Inc. All rights reserved. | |
# | |
# Licensed under the Apache License, Version 2.0 (the "License"); | |
# you may not use this file except in compliance with the License. | |
# You may obtain a copy of the License at | |
# | |
# http://www.apache.org/licenses/LICENSE-2.0 | |
# | |
# Unless required by applicable law or agreed to in writing, software | |
# distributed under the License is distributed on an "AS IS" BASIS, | |
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
# See the License for the specific language governing permissions and | |
# limitations under the License. | |
"""Creates a Container VM with the provided Container manifest.""" | |
from container_helper import GenerateManifest, DEFAULT_USER_DATA | |
def GenerateConfig(context): | |
scopes = [ | |
"https://www.googleapis.com/auth/devstorage.read_only", | |
"https://www.googleapis.com/auth/logging.write", | |
"https://www.googleapis.com/auth/monitoring.write", | |
"https://www.googleapis.com/auth/servicecontrol", | |
"https://www.googleapis.com/auth/service.management.readonly", | |
"https://www.googleapis.com/auth/trace.append" | |
] | |
image = 'projects/cos-cloud/global/images/' + \ | |
context.properties['containerImage'] | |
default_network = ''.join( | |
['https://www.googleapis.com/compute/v1/projects/', context.env['project'], | |
'/global/networks/default']) | |
instance_template = { | |
'name': context.env['name'], | |
'type': 'compute.v1.instanceTemplate', | |
'properties': { | |
'properties': { | |
'metadata': { | |
'items': [ | |
{ | |
'key': 'google-container-manifest', | |
'value': GenerateManifest(context) | |
}, | |
{ | |
'key': 'instance-group-name', | |
'value': context.env['name'] + '-igm' | |
}, | |
{ | |
'key': 'gci-ensure-gke-docker', | |
'value': 'true' | |
}, | |
{ | |
'key': 'user-data', | |
'value': DEFAULT_USER_DATA | |
}]}, | |
'machineType': context.properties['machineType'], | |
'disks': [ | |
{ | |
'deviceName': 'boot', | |
'boot': True, | |
'autoDelete': True, | |
'mode': 'READ_WRITE', | |
'type': 'PERSISTENT', | |
'initializeParams': | |
{ | |
'sourceImage': image, | |
'distName': context.env['name'] + '-disk' | |
}}], | |
'networkInterfaces': [ | |
{ | |
'accessConfigs': [ | |
{ | |
'name': 'external-nat', | |
'type': 'ONE_TO_ONE_NAT' | |
}], | |
'network': default_network}], | |
'scheduling': {'preemptible': True}, | |
'serviceAccounts': [ | |
{ | |
"email": "x@developer.gserviceaccount.com", | |
"scopes": scopes}], | |
} | |
} | |
} | |
return {'resources': [instance_template]} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Copyright 2016 Google Inc. All rights reserved. | |
# | |
# Licensed under the Apache License, Version 2.0 (the "License"); | |
# you may not use this file except in compliance with the License. | |
# You may obtain a copy of the License at | |
# | |
# http://www.apache.org/licenses/LICENSE-2.0 | |
# | |
# Unless required by applicable law or agreed to in writing, software | |
# distributed under the License is distributed on an "AS IS" BASIS, | |
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
# See the License for the specific language governing permissions and | |
# limitations under the License. | |
"""Creates autoscaled, network LB IGM running specified docker image.""" | |
def GenerateConfig(context): | |
"""Generate YAML resource configuration.""" | |
# Pull the region out of the zone | |
region = context.properties['zone'][:context.properties['zone'].rfind('-')] | |
name = context.env['name'] | |
instance_template_name = name + 'xyz' | |
resources = [ | |
{ | |
'name': instance_template_name, | |
'type': 'instance_template.py', | |
'properties': { | |
'port': context.properties['port'] if 'port' in context.properties else None, | |
'dockerEnv': context.properties['dockerEnv'] if 'dockerEnv' in context.properties else None, | |
'dockerImage': context.properties['dockerImage'], | |
'containerImage': context.properties['containerImage'], | |
'machineType': context.properties['machineType'] | |
} | |
}, | |
{ | |
'name': name + '-igm', | |
'type': 'compute.v1.instanceGroupManager', | |
'properties': { | |
'zone': context.properties['zone'], | |
'targetSize': context.properties['minSize'], | |
'baseInstanceName': name, | |
'instanceTemplate': '$(ref.' + instance_template_name + '.selfLink)' | |
} | |
}, | |
{ | |
'name': name + '-as', | |
'type': 'compute.v1.autoscaler', | |
'properties': { | |
'zone': context.properties['zone'], | |
'target': '$(ref.' + name + '-igm.selfLink)', | |
'autoscalingPolicy': { | |
'minNumReplicas': context.properties['minSize'], | |
'maxNumReplicas': context.properties['maxSize'], | |
'coolDownPeriodSec': context.properties['coolDownPeriod'], | |
'cpuUtilization': { | |
'utilizationTarget': context.properties['utilizationTarget'] | |
} | |
} | |
} | |
}] | |
return {'resources': resources} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
imports: | |
- path: match.py | |
- path: instance_template.py | |
- path: container_helper.py | |
resources: | |
- name: match-preprod-ad | |
type: match.py | |
properties: | |
zone: us-central1-f | |
containerImage: cos-stable-58-9334-62-0 | |
dockerImage: gcr.io/<my-project>/<my-docker-image>:<my-docker-image-version> | |
minSize: 1 | |
maxSize: 2 | |
utilizationTarget: .9 | |
coolDownPeriod: 300 | |
machineType: custom-2-4096 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment