lab-ui-manager/operator/operator.py
def manage_bookbag_deployment(namespace, name, bookbag_config, bookbag_vars, owner_ref, image=None, image_stream=None, logger=None):
    """
    Create Deployment, RoleBinding, Route, Service, ServiceAccount for bookbag.
    """
    auth = bookbag_config.get('auth', {})
    auth_user = auth.get('user', '*')
    auth_password = auth.get('password', '')

    service_account = get_service_account(namespace, name)
    if not service_account:
        core_v1_api.create_namespaced_service_account(
            namespace,
            {
                "apiVersion": "v1",
                "kind": "ServiceAccount",
                "metadata": {
                    "name": name,
                    "ownerReferences": [owner_ref],
                }
            }
        )

    role_binding = get_role_binding(namespace, name)
    if not role_binding:
        custom_objects_api.create_namespaced_custom_object(
            'rbac.authorization.k8s.io', 'v1', namespace, 'rolebindings',
            {
                "apiVersion": "rbac.authorization.k8s.io/v1",
                "kind": "RoleBinding",
                "metadata": {
                    "name": name,
                    "ownerReferences": [owner_ref],
                },
                "roleRef": {
                    "apiGroup": "rbac.authorization.k8s.io",
                    "kind": "ClusterRole",
                    "name": "basic-user",
                },
                "subjects": [{
                    "kind": "ServiceAccount",
                    "name": name,
                    "namespace": namespace,
                }]
            }
        )

    deployment = get_deployment(namespace, name)
    deployment_spec = {
        "replicas": 1,
        "selector": {
            "matchLabels": {
                "name": name,
            }
        },
        "strategy": {
            "type": "Recreate",
        },
        "template": {
            "metadata": {
                "labels": {
                    "name": name,
                }
            },
            "spec": {
                "containers": [{
                    "name": "bookbag",
                    "env": [{
                        "name": "APPLICATION_NAME",
                        "value": name,
                    }, {
                        "name": "AUTH_USERNAME",
                        "value": auth_user,
                    }, {
                        "name": "AUTH_PASSWORD",
                        "value": auth_password,
                    }, {
                        "name": "CLUSTER_SUBDOMAIN",
                    }, {
                        "name": "OAUTH_SERVICE_ACCOUNT",
                        "value": name,
                    }, {
                        "name": "DOWNLOAD_URL",
                    }, {
                        "name": "WORKSHOP_FILE",
                    }, {
                        "name": "OC_VERSION",
                    }, {
                        "name": "ODO_VERSION",
                    }, {
                        "name": "KUBECTL_VERSION",
                    }, {
                        "name": "WORKSHOP_VARS",
                        "value": json.dumps(bookbag_vars),
                    }],
                    "imagePullPolicy": "Always",
                    "ports": [{
                        "containerPort": 10080,
                    }],
                    "resources": {},
                }],
                "serviceAccountName": name,
            }
        }
    }
    # If image is set then use provided value.
    if image:
        deployment_spec['template']['spec']['containers'][0]['image'] = image

    if deployment:
        merged_spec = deepmerge(deepcopy(deployment['spec']), deployment_spec)
        if merged_spec != deployment['spec']:
            try:
                custom_objects_api.replace_namespaced_custom_object(
                    'apps', 'v1', namespace, 'deployments', name, {
                        "apiVersion": "apps/v1",
                        "kind": "Deployment",
                        "metadata": deployment['metadata'],
                        "spec": merged_spec,
                    }
                )
            except kubernetes.client.rest.ApiException as e:
                if e.status == 409:
                    pass
                else:
                    raise
    else:
        deployment_meta = {
            "name": name,
            "namespace": namespace,
            "ownerReferences": [owner_ref],
        }

        if not image:
            # Set image-change trigger on image stream
            deployment_meta['annotations'] = {
                "image.openshift.io/triggers": json.dumps([{
                    "from": {
                        "kind": "ImageStreamTag",
                        "name": f"{image_stream}:latest",
                    },
                    "fieldPath": 'spec.template.spec.containers[?(@.name=="bookbag")].image',
                }])
            }
            # This is image value is just a place-holder, required for validation
            deployment_spec['template']['spec']['containers'][0]['image'] = \
                f"image-registry.openshift-image-registry.svc:5000/{namespace}/{image_stream}:latest"

        custom_objects_api.create_namespaced_custom_object(
            'apps', 'v1', namespace, 'deployments',
            {
                "apiVersion": "apps/v1",
                "kind": "Deployment",
                "metadata": deployment_meta,
                "spec": deployment_spec,
            }
        )

    service = get_service(namespace, name)
    if not service:
        core_v1_api.create_namespaced_service(
            namespace,
            {
                "apiVersion": "v1",
                "kind": "Service",
                "metadata": {
                    "name": name,
                    "namespace": namespace,
                    "ownerReferences": [owner_ref],
                },
                "spec": {
                    "ports": [{
                        "name": "10080-tcp",
                        "port": 10080,
                        "protocol": "TCP",
                        "targetPort": 10080,
                    }],
                    "selector": {
                        "name": name,
                    },
                    "type": "ClusterIP",
                }
            }
        )

    route = get_route(namespace, name)
    if not route:
        route = custom_objects_api.create_namespaced_custom_object(
            'route.openshift.io', 'v1', namespace, 'routes',
            {
                "apiVersion": "route.openshift.io/v1",
                "kind": "Route",
                "metadata": {
                    "name": name,
                    "namespace": namespace,
                    "ownerReferences": [owner_ref],
                },
                "spec": {
                    "port": {
                        "targetPort": "10080-tcp",
                    },
                    "tls": {
                        "insecureEdgeTerminationPolicy": "Redirect",
                        "termination": "edge",
                    },
                    "to": {
                        "kind": "Service",
                        "name": name,
                    },
                }
            }
        )
    return route['spec']['host']

    Create Deployment, RoleBinding, Route, Service, ServiceAccount for bookbag.
    
Create Deployment, RoleBinding, Route, Service, ServiceAccount for bookbag.
Create Deployment RoleBinding Route Service ServiceAccount for bookbag .
def manage_bookbag_deployment(namespace, name, bookbag_config, bookbag_vars, owner_ref, image=None, image_stream=None, logger=None):
    auth = bookbag_config.get('auth', {})
    auth_user = auth.get('user', '*')
    auth_password = auth.get('password', '')
    service_account = get_service_account(namespace, name)
    if not service_account:
        core_v1_api.create_namespaced_service_account(
            namespace,
            {
                "apiVersion": "v1",
                "kind": "ServiceAccount",
                "metadata": {
                    "name": name,
                    "ownerReferences": [owner_ref],
                }
            }
        )
    role_binding = get_role_binding(namespace, name)
    if not role_binding:
        custom_objects_api.create_namespaced_custom_object(
            'rbac.authorization.k8s.io', 'v1', namespace, 'rolebindings',
            {
                "apiVersion": "rbac.authorization.k8s.io/v1",
                "kind": "RoleBinding",
                "metadata": {
                    "name": name,
                    "ownerReferences": [owner_ref],
                },
                "roleRef": {
                    "apiGroup": "rbac.authorization.k8s.io",
                    "kind": "ClusterRole",
                    "name": "basic-user",
                },
                "subjects": [{
                    "kind": "ServiceAccount",
                    "name": name,
                    "namespace": namespace,
                }]
            }
        )
    deployment = get_deployment(namespace, name)
    deployment_spec = {
        "replicas": 1,
        "selector": {
            "matchLabels": {
                "name": name,
            }
        },
        "strategy": {
            "type": "Recreate",
        },
        "template": {
            "metadata": {
                "labels": {
                    "name": name,
                }
            },
            "spec": {
                "containers": [{
                    "name": "bookbag",
                    "env": [{
                        "name": "APPLICATION_NAME",
                        "value": name,
                    }, {
                        "name": "AUTH_USERNAME",
                        "value": auth_user,
                    }, {
                        "name": "AUTH_PASSWORD",
                        "value": auth_password,
                    }, {
                        "name": "CLUSTER_SUBDOMAIN",
                    }, {
                        "name": "OAUTH_SERVICE_ACCOUNT",
                        "value": name,
                    }, {
                        "name": "DOWNLOAD_URL",
                    }, {
                        "name": "WORKSHOP_FILE",
                    }, {
                        "name": "OC_VERSION",
                    }, {
                        "name": "ODO_VERSION",
                    }, {
                        "name": "KUBECTL_VERSION",
                    }, {
                        "name": "WORKSHOP_VARS",
                        "value": json.dumps(bookbag_vars),
                    }],
                    "imagePullPolicy": "Always",
                    "ports": [{
                        "containerPort": 10080,
                    }],
                    "resources": {},
                }],
                "serviceAccountName": name,
            }
        }
    }
    if image:
        deployment_spec['template']['spec']['containers'][0]['image'] = image
    if deployment:
        merged_spec = deepmerge(deepcopy(deployment['spec']), deployment_spec)
        if merged_spec != deployment['spec']:
            try:
                custom_objects_api.replace_namespaced_custom_object(
                    'apps', 'v1', namespace, 'deployments', name, {
                        "apiVersion": "apps/v1",
                        "kind": "Deployment",
                        "metadata": deployment['metadata'],
                        "spec": merged_spec,
                    }
                )
            except kubernetes.client.rest.ApiException as e:
                if e.status == 409:
                    pass
                else:
                    raise
    else:
        deployment_meta = {
            "name": name,
            "namespace": namespace,
            "ownerReferences": [owner_ref],
        }
        if not image:
            deployment_meta['annotations'] = {
                "image.openshift.io/triggers": json.dumps([{
                    "from": {
                        "kind": "ImageStreamTag",
                        "name": f"{image_stream}:latest",
                    },
                    "fieldPath": 'spec.template.spec.containers[?(@.name=="bookbag")].image',
                }])
            }
            deployment_spec['template']['spec']['containers'][0]['image'] = \
                f"image-registry.openshift-image-registry.svc:5000/{namespace}/{image_stream}:latest"
        custom_objects_api.create_namespaced_custom_object(
            'apps', 'v1', namespace, 'deployments',
            {
                "apiVersion": "apps/v1",
                "kind": "Deployment",
                "metadata": deployment_meta,
                "spec": deployment_spec,
            }
        )
    service = get_service(namespace, name)
    if not service:
        core_v1_api.create_namespaced_service(
            namespace,
            {
                "apiVersion": "v1",
                "kind": "Service",
                "metadata": {
                    "name": name,
                    "namespace": namespace,
                    "ownerReferences": [owner_ref],
                },
                "spec": {
                    "ports": [{
                        "name": "10080-tcp",
                        "port": 10080,
                        "protocol": "TCP",
                        "targetPort": 10080,
                    }],
                    "selector": {
                        "name": name,
                    },
                    "type": "ClusterIP",
                }
            }
        )
    route = get_route(namespace, name)
    if not route:
        route = custom_objects_api.create_namespaced_custom_object(
            'route.openshift.io', 'v1', namespace, 'routes',
            {
                "apiVersion": "route.openshift.io/v1",
                "kind": "Route",
                "metadata": {
                    "name": name,
                    "namespace": namespace,
                    "ownerReferences": [owner_ref],
                },
                "spec": {
                    "port": {
                        "targetPort": "10080-tcp",
                    },
                    "tls": {
                        "insecureEdgeTerminationPolicy": "Redirect",
                        "termination": "edge",
                    },
                    "to": {
                        "kind": "Service",
                        "name": name,
                    },
                }
            }
        )
    return route['spec']['host']
def manage_bookbag_deployment ( namespace , name , bookbag_config , bookbag_vars , owner_ref , image = None , image_stream = None , logger = None ) : auth = bookbag_config . get ( 'auth' , { } ) auth_user = auth . get ( 'user' , '*' ) auth_password = auth . get ( 'password' , '' ) service_account = get_service_account ( namespace , name ) if not service_account : core_v1_api . create_namespaced_service_account ( namespace , { "apiVersion" : "v1" , "kind" : "ServiceAccount" , "metadata" : { "name" : name , "ownerReferences" : [ owner_ref ] , } } ) role_binding = get_role_binding ( namespace , name ) if not role_binding : custom_objects_api . create_namespaced_custom_object ( 'rbac.authorization.k8s.io' , 'v1' , namespace , 'rolebindings' , { "apiVersion" : "rbac.authorization.k8s.io/v1" , "kind" : "RoleBinding" , "metadata" : { "name" : name , "ownerReferences" : [ owner_ref ] , } , "roleRef" : { "apiGroup" : "rbac.authorization.k8s.io" , "kind" : "ClusterRole" , "name" : "basic-user" , } , "subjects" : [ { "kind" : "ServiceAccount" , "name" : name , "namespace" : namespace , } ] } ) deployment = get_deployment ( namespace , name ) deployment_spec = { "replicas" : 1 , "selector" : { "matchLabels" : { "name" : name , } } , "strategy" : { "type" : "Recreate" , } , "template" : { "metadata" : { "labels" : { "name" : name , } } , "spec" : { "containers" : [ { "name" : "bookbag" , "env" : [ { "name" : "APPLICATION_NAME" , "value" : name , } , { "name" : "AUTH_USERNAME" , "value" : auth_user , } , { "name" : "AUTH_PASSWORD" , "value" : auth_password , } , { "name" : "CLUSTER_SUBDOMAIN" , } , { "name" : "OAUTH_SERVICE_ACCOUNT" , "value" : name , } , { "name" : "DOWNLOAD_URL" , } , { "name" : "WORKSHOP_FILE" , } , { "name" : "OC_VERSION" , } , { "name" : "ODO_VERSION" , } , { "name" : "KUBECTL_VERSION" , } , { "name" : "WORKSHOP_VARS" , "value" : json . dumps ( bookbag_vars ) , } ] , "imagePullPolicy" : "Always" , "ports" : [ { "containerPort" : 10080 , } ] , "resources" : { } , } ] , "serviceAccountName" : name , } } } if image : deployment_spec [ 'template' ] [ 'spec' ] [ 'containers' ] [ 0 ] [ 'image' ] = image if deployment : merged_spec = deepmerge ( deepcopy ( deployment [ 'spec' ] ) , deployment_spec ) if merged_spec != deployment [ 'spec' ] : try : custom_objects_api . replace_namespaced_custom_object ( 'apps' , 'v1' , namespace , 'deployments' , name , { "apiVersion" : "apps/v1" , "kind" : "Deployment" , "metadata" : deployment [ 'metadata' ] , "spec" : merged_spec , } ) except kubernetes . client . rest . ApiException as e : if e . status == 409 : pass else : raise else : deployment_meta = { "name" : name , "namespace" : namespace , "ownerReferences" : [ owner_ref ] , } if not image : deployment_meta [ 'annotations' ] = { "image.openshift.io/triggers" : json . dumps ( [ { "from" : { "kind" : "ImageStreamTag" , "name" : f"{image_stream}:latest" , } , "fieldPath" : 'spec.template.spec.containers[?(@.name=="bookbag")].image' , } ] ) } deployment_spec [ 'template' ] [ 'spec' ] [ 'containers' ] [ 0 ] [ 'image' ] = f"image-registry.openshift-image-registry.svc:5000/{namespace}/{image_stream}:latest" custom_objects_api . create_namespaced_custom_object ( 'apps' , 'v1' , namespace , 'deployments' , { "apiVersion" : "apps/v1" , "kind" : "Deployment" , "metadata" : deployment_meta , "spec" : deployment_spec , } ) service = get_service ( namespace , name ) if not service : core_v1_api . create_namespaced_service ( namespace , { "apiVersion" : "v1" , "kind" : "Service" , "metadata" : { "name" : name , "namespace" : namespace , "ownerReferences" : [ owner_ref ] , } , "spec" : { "ports" : [ { "name" : "10080-tcp" , "port" : 10080 , "protocol" : "TCP" , "targetPort" : 10080 , } ] , "selector" : { "name" : name , } , "type" : "ClusterIP" , } } ) route = get_route ( namespace , name ) if not route : route = custom_objects_api . create_namespaced_custom_object ( 'route.openshift.io' , 'v1' , namespace , 'routes' , { "apiVersion" : "route.openshift.io/v1" , "kind" : "Route" , "metadata" : { "name" : name , "namespace" : namespace , "ownerReferences" : [ owner_ref ] , } , "spec" : { "port" : { "targetPort" : "10080-tcp" , } , "tls" : { "insecureEdgeTerminationPolicy" : "Redirect" , "termination" : "edge" , } , "to" : { "kind" : "Service" , "name" : name , } , } } ) return route [ 'spec' ] [ 'host' ]
Create Deployment, RoleBinding, Route, Service, ServiceAccount for bookbag.
Create Deployment RoleBinding Route Service ServiceAccount for bookbag .
"""
    Create Deployment, RoleBinding, Route, Service, ServiceAccount for bookbag.
    """ # If image is set then use provided value. # Set image-change trigger on image stream # This is image value is just a place-holder, required for validation
namespace
name
bookbag_config
bookbag_vars
owner_ref
image
image_stream
logger