コード例 #1
0
def start_worker():
    ''' Start kubelet using the provided API and DNS info.'''
    # Note that the DNS server doesn't necessarily exist at this point. We know
    # what its IP will eventually be, though, so we can go ahead and configure
    # kubelet with that info. This ensures that early pods are configured with
    # the correct DNS even though the server isn't ready yet.
    kube_api = endpoint_from_flag('kube-api-endpoint.available')
    kube_control = endpoint_from_flag('kube-control.dns.available')
    cni = endpoint_from_flag('cni.available')

    servers = get_kube_api_servers(kube_api)
    dns = kube_control.get_dns()
    ingress_ip = get_ingress_address(kube_control.relation_name)
    cluster_cidr = cni.get_config()['cidr']

    if cluster_cidr is None:
        hookenv.log('Waiting for cluster cidr.')
        return

    creds = db.get('credentials')
    data_changed('kube-control.creds', creds)

    create_config(servers[get_unit_number() % len(servers)], creds)
    configure_kubelet(dns, ingress_ip)
    configure_kube_proxy(configure_prefix, servers, cluster_cidr)
    set_state('kubernetes-worker.config.created')
    restart_unit_services()
    update_kubelet_status()
    set_state('kubernetes-worker.label-config-required')
    remove_state('kubernetes-worker.restart-needed')
コード例 #2
0
def get_api_url(endpoints):
    """
    Choose an API endpoint from the list and build a URL from it.
    """
    if not endpoints:
        return None
    urls = get_api_urls(endpoints)
    return urls[kubernetes_common.get_unit_number() % len(urls)]
コード例 #3
0
def get_api_endpoint(relation=None):
    """
    Determine the best endpoint for a client to connect to.

    If a relation is given, it will take that into account when choosing an
    endpoint.
    """
    endpoints = get_lb_endpoints()
    if endpoints:
        # select a single endpoint based on our local unit number
        return endpoints[kubernetes_common.get_unit_number() % len(endpoints)]
    elif relation:
        ingress_address = hookenv.ingress_address(relation.relation_id,
                                                  hookenv.local_unit())
        return (ingress_address, STANDARD_API_PORT)
    else:
        return (hookenv.unit_public_ip(), STANDARD_API_PORT)
コード例 #4
0
def start_worker():
    """Start kubelet using the provided API and DNS info."""
    # Note that the DNS server doesn't necessarily exist at this point. We know
    # what its IP will eventually be, though, so we can go ahead and configure
    # kubelet with that info. This ensures that early pods are configured with
    # the correct DNS even though the server isn't ready yet.
    kube_control = endpoint_from_flag("kube-control.dns.available")

    servers = get_kube_api_servers()
    dns = kube_control.get_dns()
    dns_domain = dns["domain"]
    dns_ip = dns["sdn-ip"]
    registry = get_registry_location()
    cluster_cidr = kubernetes_common.cluster_cidr()

    if cluster_cidr is None:
        hookenv.log("Waiting for cluster cidr.")
        return

    if not servers:
        hookenv.log("Waiting for API server URL")
        return

    if kubernetes_common.is_ipv6(cluster_cidr):
        kubernetes_common.enable_ipv6_forwarding()

    creds = db.get("credentials")
    data_changed("kube-control.creds", creds)

    create_config(servers[get_unit_number() % len(servers)], creds)
    configure_default_cni(kube_control.get_default_cni())
    configure_kubelet(dns_domain, dns_ip, registry, has_xcp=kube_control.has_xcp)
    configure_kube_proxy(configure_prefix, servers, cluster_cidr)
    set_state("kubernetes-worker.config.created")
    restart_unit_services()
    update_kubelet_status()
    set_state("kubernetes-worker.label-config-required")
    set_state("nrpe-external-master.reconfigure")
    remove_state("kubernetes-worker.restart-needed")
    remove_state("endpoint.kube-control.has-xcp.changed")
コード例 #5
0
def update_nrpe_config():
    services = ["snap.{}.daemon".format(s) for s in worker_services]
    data = render("nagios_plugin.py", None, {"node_name": get_node_name()})
    plugin_path = install_nagios_plugin_from_text(data, "check_k8s_worker.py")
    hostname = nrpe.get_nagios_hostname()
    current_unit = nrpe.get_nagios_unit_name()
    nrpe_setup = nrpe.NRPE(hostname=hostname)
    nrpe_setup.add_check("node", "Node registered with API Server", str(plugin_path))
    nrpe.add_init_service_checks(nrpe_setup, services, current_unit)
    nrpe_setup.write()

    creds = db.get("credentials")
    servers = get_kube_api_servers()
    if creds and servers:
        server = servers[get_unit_number() % len(servers)]
        create_kubeconfig(
            nrpe_kubeconfig_path,
            server,
            ca_crt_path,
            token=creds["client_token"],
            user="******",
        )
        # Make sure Nagios dirs are the correct permissions.
        cmd = ["chown", "-R", "nagios:nagios"]
        for p in ["/var/lib/nagios/", os.path.dirname(nrpe_kubeconfig_path)]:
            if os.path.exists(p):
                check_call(cmd + [p])

        remove_state("nrpe-external-master.reconfigure")
        set_state("nrpe-external-master.initial-config")
    # request CPU governor check from nrpe relation to be performance
    rel_settings = {
        "requested_cpu_governor": "performance",
    }
    for rid in hookenv.relation_ids("nrpe-external-master"):
        hookenv.relation_set(relation_id=rid, relation_settings=rel_settings)