Skip to content
This repository has been archived by the owner on Jul 14, 2019. It is now read-only.

Add node labels during bootstrapping. #168

Merged
merged 2 commits into from Dec 4, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
35 changes: 29 additions & 6 deletions cloud/ssh/actuators/machine/setupconfigs_metadata.go
@@ -1,17 +1,15 @@
package machine

import (
"bytes"
"errors"
"fmt"
"strings"
"text/template"

"bytes"

"github.com/golang/glog"
clusterv1 "sigs.k8s.io/cluster-api/pkg/apis/cluster/v1alpha1"

"fmt"

"github.com/samsung-cnct/cluster-api-provider-ssh/cloud/ssh/providerconfig/v1alpha1"
clusterv1 "sigs.k8s.io/cluster-api/pkg/apis/cluster/v1alpha1"
)

var (
Expand Down Expand Up @@ -48,16 +46,27 @@ type metadataParams struct {
ServiceCIDR string
MasterEndpoint string // for node joining a cluster, should be available after master created
MasterIP string // for injection to startup script
NodeLabels string
}

func masterMetadata(c *clusterv1.Cluster, m *clusterv1.Machine, metadata *Metadata, sshConfig v1alpha1.SSHConfig) (map[string]string, error) {
// The Cluster API Machine controller adds machine labels to nodes. Since
// our Machines do not exist in the managed cluster we have to manually
// add the labels from the bootstrapping scripts.
nodeLabelList := make([]string, 0)
for k, v := range m.Labels {
nodeLabelList = append(nodeLabelList, fmt.Sprintf("%s=%s", k, v))
}
nodeLabels := strings.Join(nodeLabelList, " ")

params := metadataParams{
Cluster: c,
Machine: m,
Metadata: metadata,
PodCIDR: getSubnet(c.Spec.ClusterNetwork.Pods),
ServiceCIDR: getSubnet(c.Spec.ClusterNetwork.Services),
MasterIP: sshConfig.Host,
NodeLabels: nodeLabels,
}
masterMetadata := map[string]string{}
var buf bytes.Buffer
Expand Down Expand Up @@ -87,9 +96,20 @@ func masterMetadata(c *clusterv1.Cluster, m *clusterv1.Machine, metadata *Metada

func nodeMetadata(token string, c *clusterv1.Cluster, m *clusterv1.Machine, metadata *Metadata) (map[string]string, error) {
nodeMetadata := map[string]string{}

if len(c.Status.APIEndpoints) < 1 {
return nodeMetadata, errors.New("The master APIEndpoints has not been initialized in ClusterStatus")
}

// The Cluster API Machine controller adds machine labels to nodes. Since
// our Machines do not exist in the managed cluster we have to manually
// add the labels from the bootstrapping scripts.
nodeLabelList := make([]string, 0)
for k, v := range m.Labels {
nodeLabelList = append(nodeLabelList, fmt.Sprintf("%s=%s", k, v))
}
nodeLabels := strings.Join(nodeLabelList, " ")

params := metadataParams{
Token: token,
Cluster: c,
Expand All @@ -98,6 +118,7 @@ func nodeMetadata(token string, c *clusterv1.Cluster, m *clusterv1.Machine, meta
PodCIDR: getSubnet(c.Spec.ClusterNetwork.Pods),
ServiceCIDR: getSubnet(c.Spec.ClusterNetwork.Services),
MasterEndpoint: getEndpoint(c.Status.APIEndpoints[0]),
NodeLabels: nodeLabels,
}
glog.Infof("The MasterEndpoint = %s, machine %s cluster %s", params.MasterEndpoint, m.Name, c.Name)
var buf bytes.Buffer
Expand Down Expand Up @@ -151,6 +172,7 @@ CLUSTER_DNS_DOMAIN={{ .Cluster.Spec.ClusterNetwork.ServiceDomain }}
POD_CIDR={{ .PodCIDR }}
SERVICE_CIDR={{ .ServiceCIDR }}
MASTER_IP={{ .MasterIP }}
NODE_LABELS="{{ .NodeLabels }}"
`
const nodeEnvironmentVars = `#!/usr/bin/env bash
KUBELET_VERSION={{ .Machine.Spec.Versions.Kubelet }}
Expand All @@ -164,4 +186,5 @@ MACHINE+=$MACHINE_NAME
CLUSTER_DNS_DOMAIN={{ .Cluster.Spec.ClusterNetwork.ServiceDomain }}
POD_CIDR={{ .PodCIDR }}
SERVICE_CIDR={{ .ServiceCIDR }}
NODE_LABELS="{{ .NodeLabels }}"
`
Expand Up @@ -225,7 +225,7 @@
setenforce 0
sed -i 's/^SELINUX=enforcing$/SELINUX=permissive/' /etc/selinux/config

toolsImage=$(docker create quay.io/samsung_cnct/cm-vmware-bootstrap:0.1.39-2)
toolsImage=$(docker create quay.io/samsung_cnct/cm-vmware-bootstrap:0.1.42)
echo >&2 "Using boostrap image ${toolsImage}"

if ! docker cp ${toolsImage}:/resources/rpms/ /var/log/rpms;then
Expand Down Expand Up @@ -360,6 +360,10 @@
echo >&2 "Unable apply flannel config."
return 1
fi

for label in $NODE_LABELS ; do
kubectl --kubeconfig /etc/kubernetes/kubelet.conf -n $(NAMESPACE) label node $(hostname) "${label}"
done
}

run_kubeadm_join()
Expand All @@ -376,6 +380,10 @@
kubectl --kubeconfig /etc/kubernetes/kubelet.conf annotate --overwrite node "$(echo $(hostname) | tr '[:upper:]' '[:lower:]')" machine=${MACHINE} && break
sleep 5
done

for label in $NODE_LABELS ; do
kubectl --kubeconfig /etc/kubernetes/kubelet.conf -n $(NAMESPACE) label node $(hostname) "${label}"
done
}

drain()
Expand Down
Expand Up @@ -46,7 +46,7 @@ spec:
cpu: 100m
memory: 30Mi
- name: ssh-cluster-controller
image: quay.io/samsung_cnct/ssh-cluster-controller:0.1.39
image: quay.io/samsung_cnct/ssh-cluster-controller:0.1.42
imagePullPolicy: Always
volumeMounts:
- name: config
Expand All @@ -73,7 +73,7 @@ spec:
cpu: 400m
memory: 500Mi
- name: ssh-machine-controller
image: quay.io/samsung_cnct/ssh-machine-controller:0.1.39
image: quay.io/samsung_cnct/ssh-machine-controller:0.1.42
imagePullPolicy: Always
volumeMounts:
- name: config
Expand Down