import re
import uuid


def generate_service_component_name(service_component_type):
    """Generate service component id used to pass into the service component
    instance and used as the key to the service component configuration.

    Updated for use with Kubernetes.  Sometimes the service component name gets
    used in Kubernetes in contexts (such as naming a Kubernetes Service) that
    requires the name to conform to the RFC1035 DNS "label" syntax:
       -- starts with an alpha
       -- contains only of alphanumerics and "-"
       -- <= 63 characters long

    Format:
    s<service component id>-<service component type>,
        truncated to 63 characters, "_" replaced with "-" in service_component_type,
        other non-conforming characters removed from service_component_type
    """
    # Random generated
    # Copied from cdap plugin
    sct = re.sub('[^A-Za-z0-9-]','',(service_component_type.replace('_','-')))
    return ("s{0}-{1}".format(str(uuid.uuid4()).replace("-",""),sct))[:63]