Puppet Class: profile::kubernetes::deployment_server

Defined in:
modules/profile/manifests/kubernetes/deployment_server.pp

Summary

Profile to make sure we got a deployment server ready

Overview

SPDX-License-Identifier: Apache-2.0

Parameters:

  • version (K8s::KubernetesVersion) (defaults to: lookup('profile::kubernetes::version', { default_value => '1.16' }))

    kubectl version to install

  • kubernetes_cluster_groups (Hash[String, Hash]) (defaults to: lookup('kubernetes_cluster_groups'))

    dict of kubernetes cluster groups

  • user_defaults (Profile::Kubernetes::User_defaults) (defaults to: lookup('profile::kubernetes::deployment_server::user_defaults'))

    user group and mode defaults

  • services (Hash[String, Hash[String,Profile::Kubernetes::Services]]) (defaults to: lookup('profile::kubernetes::deployment_server::services', { default_value => {} }))

    Dict of services

  • tokens (Hash[String, Hash[String, Hash]]) (defaults to: lookup('profile::kubernetes::infrastructure_users', { default_value => {} }))

    dict of tokens

  • include_admin (Boolean) (defaults to: lookup('profile::kubernetes::deployment_server::include_admin', { default_value => false }))

    if true include profile::kubernetes::kubeconfig::admin

  • helm_user_group (String) (defaults to: lookup('profile::kubernetes::helm_user_group'))

    the group used for the helm cache directory



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'modules/profile/manifests/kubernetes/deployment_server.pp', line 10

class profile::kubernetes::deployment_server (
    K8s::KubernetesVersion $version                                         = lookup('profile::kubernetes::version', { default_value => '1.16' }),
    Hash[String, Hash] $kubernetes_cluster_groups                      = lookup('kubernetes_cluster_groups'),
    Profile::Kubernetes::User_defaults $user_defaults                  = lookup('profile::kubernetes::deployment_server::user_defaults'),
    Hash[String, Hash[String,Profile::Kubernetes::Services]] $services = lookup('profile::kubernetes::deployment_server::services', { default_value => {} }),
    Hash[String, Hash[String, Hash]] $tokens                           = lookup('profile::kubernetes::infrastructure_users', { default_value => {} }),
    Boolean $include_admin                                             = lookup('profile::kubernetes::deployment_server::include_admin', { default_value => false }),
    String $helm_user_group                                            = lookup('profile::kubernetes::helm_user_group')
) {
    class { 'helm':
        helm_user_group => $helm_user_group,
    }
    class { 'k8s::client':
        version => $version,
    }

    ensure_packages('istioctl')

    # For each cluster group, then for each cluster, we gather
    # the list of services, and the corresponding tokens, then we build
    # the kubeconfigs for all of them.
    $kubernetes_cluster_groups.map |$cluster_group, $clusters| {
        $_tokens = $tokens[$cluster_group]
        $_services = pick($services[$cluster_group], {})
        # for each service installed on the cluster group,
        # cycle through all clusters to define their kubeconfigs.
        $_services.each |$srv, $data| {
            # If the namespace is undefined, use the service name.
            $namespace = $data['namespace'] ? {
                undef   => $srv,
                default => $data['namespace']
            }
            $service_ensure = $data['ensure'] ? {
                undef   => present,
                default => $data['ensure'],
            }
            $clusters.each |$cluster, $cluster_data| {
                $data['usernames'].each |$usr_raw| {
                    $usr = $user_defaults.merge($usr_raw)
                    $token = $_tokens[$usr['name']]
                    # Allow overriding the kubeconfig name
                    $kubeconfig_name = $usr['kubeconfig'] ? {
                        undef => $usr['name'],
                        default => $usr['kubeconfig']
                    }

                    $kubeconfig_path = "/etc/kubernetes/${kubeconfig_name}-${cluster}.config"
                    # TODO: separate username data from the services structure?
                    if ($token and !defined(K8s::Kubeconfig[$kubeconfig_path])) {
                        k8s::kubeconfig { $kubeconfig_path:
                            ensure      => $service_ensure,
                            master_host => $cluster_data['master'],
                            username    => $usr['name'],
                            token       => $token['token'],
                            owner       => $usr['owner'],
                            group       => $usr['group'],
                            mode        => $usr['mode'],
                            namespace   => $namespace,
                        }
                    }
                }
            }
        }
    }
    # Now if we're including the admin account, add it for every cluster in the cluster
    # group.
    if $include_admin {
        class { 'profile::kubernetes::kubeconfig::admin': }
    }

    $kube_env_services_base = $include_admin ? {
        true  => ['admin'],
        false => []
    }
    # Used to support the kube-env.sh script. A list of service names is useful
    # to create an auto-complete feature for kube_env.
    # Please note: here we're using the service names because we assume there is a user with that name
    # If not, the service will break down the assumptions kube_env does and should not be included.
    $kube_env_services = $kube_env_services_base + $services.map |$_, $srvs| {
        # Filter out services that don't have a username
        keys($srvs).filter |$svc_name| { $svc_name in $srvs[$svc_name]['usernames'].map |$u| { $u['name'] } }
    }.flatten().unique()
    $kube_env_environments = $kubernetes_cluster_groups.map |$_, $clusters| {
        keys($clusters)
    }.flatten().unique()

    # Add separate environment variable file for kube-env config.
    # profile.d is sourced alphabetically, so it needs to be named such as it comes before kube-env
    file { '/etc/profile.d/kube-conf.sh':
        ensure  => file,
        content => template('profile/kubernetes/kube-conf.sh.erb'),
        mode    => '0555',
    }
    # Add a script to profile.d with functions to set the configuration for kubernetes.
    file { '/etc/profile.d/kube-env.sh':
        ensure => file,
        source => 'puppet:///modules/profile/kubernetes/kube-env.sh',
        mode   => '0555',
    }
}