GoogleCloudPlatform / deploymentmanager-samples

Deployment Manager samples and templates.
Apache License 2.0
935 stars 716 forks source link

GKE example with deprecated Kubernetes Service and Deployments APIs #619

Closed gsake closed 3 years ago

gsake commented 3 years ago

As a continuation of the #618 (GKE example with custom type provider is outdated (swaggerapi/openapi endpoint))

Kubernetes' API that are mentioned in the sample files have been deprecated. The following sample code inherits the latest changes in the Service and Deployments API, which are exposed by /openapi/v2 endpoint.

Kubernetes API Concepts: https://kubernetes.io/docs/reference/using-api/api-concepts/ Kubernetes API: https://kubernetes.io/docs/reference/kubernetes-api/

Relevant snippet:

def GenerateConfig(context):
    """Generate YAML resource configuration."""

    cluster_types_root = ''.join([context.properties['project'], '/', context.properties['clusterType']])
    cluster_types = {
        'Service': ''.join([cluster_types_root, ':', '/api/v1/namespaces/{namespace}/services/{name}']),
        'Deployment': ''.join([cluster_types_root, ':', '/apis/apps/v1/namespaces/{namespace}/deployments/{name}'])
    }

    name_prefix = context.properties['deployment'] + '-' + context.properties['name']
    port = context.properties['port']

    resources = [{
        'name': name_prefix + '-service',
        'type': cluster_types['Service'],
        'properties': {
            'apiVersion': 'v1',
            'kind': 'Service',
            'metadata': {
                'name': name_prefix + '-service',
                'namespace': 'default',
                'labels': {
                    'id': 'deployment-manager'
                }
            },
            'spec': {
                'type': 'NodePort',
                'ports': [{
                    'port': port,
                    'targetPort': port,
                    'protocol': 'TCP'
                }],
                'selector': {
                    'app': name_prefix
                }
            }
        }
    }, {
        'name': name_prefix + '-deployment',
        'type': cluster_types['Deployment'],
        'properties': {
            'apiVersion': 'apps/v1',
            'kind': 'Deployment',
            'metadata': {
                'name': name_prefix + '-deployment',
                'namespace': 'default'
            },
            'spec': {
                'replicas': 1,
                'selector': {
                    'matchLabels': {
                        'app': name_prefix
                    }
                }, 
                'template': {
                    'metadata': {
                        'labels': {
                            'name': name_prefix + '-deployment',
                            'app': name_prefix
                        }
                    },
                    'spec': {
                        'containers': [{
                            'name': 'container',
                            'image': context.properties['image'],
                            'ports': [{
                                'containerPort': port
                            }]
                        }]
                    }
                }
            }
        }
    }]

    return {'resources': resources}
ocsig commented 3 years ago

Thank you for sharing George!