Skip to content

Commit

Permalink
Create/mount configmap for machine setup configs
Browse files Browse the repository at this point in the history
  • Loading branch information
kcoronado committed Apr 3, 2018
1 parent 242d193 commit 9b9a8b9
Show file tree
Hide file tree
Showing 12 changed files with 182 additions and 60 deletions.
6 changes: 6 additions & 0 deletions cluster-api/cloud/google/config/configtemplate.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,8 @@ spec:
mountPath: /etc/credentials
- name: sshkeys
mountPath: /etc/sshkeys
- name: machine-setup
mountPath: /etc/machinesetup
env:
- name: GOOGLE_APPLICATION_CREDENTIALS
value: /etc/credentials/service-account.json
Expand All @@ -147,6 +149,7 @@ spec:
args:
- --kubeconfig=/etc/kubernetes/admin.conf
- --token={{ .Token }}
- --config=/etc/machinesetup/machine_setup_configs.yaml
resources:
requests:
cpu: 100m
Expand All @@ -171,6 +174,9 @@ spec:
- name: credentials
secret:
secretName: machine-controller-credential
- name: machine-setup
configMap:
name: machine-setup
---
apiVersion: apps/v1beta1
kind: StatefulSet
Expand Down
27 changes: 26 additions & 1 deletion cluster-api/cloud/google/machineactuator.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ import (

"regexp"

corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
gceconfig "k8s.io/kube-deploy/cluster-api/cloud/google/gceproviderconfig"
gceconfigv1 "k8s.io/kube-deploy/cluster-api/cloud/google/gceproviderconfig/v1alpha1"
"k8s.io/kube-deploy/cluster-api/cloud/google/machinesetup"
Expand All @@ -53,6 +56,8 @@ const (

UIDLabelKey = "machine-crd-uid"
BootstrapLabelKey = "boostrap"

MachineSetupConfigsFilename = "machine_setup_configs.yaml"
)

type SshCreds struct {
Expand Down Expand Up @@ -128,7 +133,7 @@ func NewMachineActuator(kubeadmToken string, machineClient client.MachineInterfa
}, nil
}

func (gce *GCEClient) CreateMachineController(cluster *clusterv1.Cluster, initialMachines []*clusterv1.Machine) error {
func (gce *GCEClient) CreateMachineController(cluster *clusterv1.Cluster, initialMachines []*clusterv1.Machine, clientSet kubernetes.Clientset) error {
if err := gce.CreateMachineControllerServiceAccount(cluster, initialMachines); err != nil {
return err
}
Expand All @@ -142,6 +147,26 @@ func (gce *GCEClient) CreateMachineController(cluster *clusterv1.Cluster, initia
return err
}

// Create the configmap so the machine setup configs can be mounted into the node.
machineSetupConfigs, err := gce.configWatch.ValidConfigs()
if err != nil {
return err
}
yaml, err := machineSetupConfigs.GetYaml()
if err != nil {
return err
}
configMap := corev1.ConfigMap{
ObjectMeta: v1.ObjectMeta{Name: "machine-setup"},
Data: map[string]string{
MachineSetupConfigsFilename: yaml,
},
}
configMaps := clientSet.CoreV1().ConfigMaps(corev1.NamespaceDefault)
if _, err := configMaps.Create(&configMap); err != nil {
return err
}

if err := CreateApiServerAndController(gce.kubeadmToken); err != nil {
return err
}
Expand Down
8 changes: 8 additions & 0 deletions cluster-api/cloud/google/machinesetup/config_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,14 @@ func parseMachineSetupYaml(reader io.Reader) (*ValidConfigs, error) {
return &ValidConfigs{configList}, nil
}

func (vc *ValidConfigs) GetYaml() (string, error) {
bytes, err := yaml.Marshal(vc.configList)
if err != nil {
return "", err
}
return string(bytes), nil
}

func (vc *ValidConfigs) GetImage(params *ConfigParams) (string, error) {
machineSetupConfig, err := vc.matchMachineSetupConfig(params)
if err != nil {
Expand Down
127 changes: 100 additions & 27 deletions cluster-api/cloud/google/machinesetup/config_types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ package machinesetup

import (
"io"
"k8s.io/kube-deploy/cluster-api/pkg/apis/cluster/common"
"k8s.io/kube-deploy/cluster-api/pkg/apis/cluster/v1alpha1"
clustercommon "k8s.io/kube-deploy/cluster-api/pkg/apis/cluster/common"
clusterv1 "k8s.io/kube-deploy/cluster-api/pkg/apis/cluster/v1alpha1"
"reflect"
"strings"
"testing"
Expand Down Expand Up @@ -90,28 +90,101 @@ func TestParseMachineSetupYaml(t *testing.T) {
}
}

func TestGetYaml(t *testing.T) {
testTables := []struct {
validConfigs ValidConfigs
expectedStrings []string
expectedErr bool
}{
{
validConfigs: ValidConfigs{
configList: &configList{
Items: []config{
{
Params: []ConfigParams{
{
OS: "ubuntu-1710",
Roles: []clustercommon.MachineRole{clustercommon.MasterRole},
Versions: clusterv1.MachineVersionInfo{
Kubelet: "1.9.4",
ControlPlane: "1.9.4",
ContainerRuntime: clusterv1.ContainerRuntimeInfo{
Name: "docker",
Version: "1.12.0",
},
},
},
},
Image: "projects/ubuntu-os-cloud/global/images/family/ubuntu-1710",
Metadata: Metadata{
StartupScript: "Master startup script",
},
},
{
Params: []ConfigParams{
{
OS: "ubuntu-1710",
Roles: []clustercommon.MachineRole{clustercommon.NodeRole},
Versions: clusterv1.MachineVersionInfo{
Kubelet: "1.9.4",
ContainerRuntime: clusterv1.ContainerRuntimeInfo{
Name: "docker",
Version: "1.12.0",
},
},
},
},
Image: "projects/ubuntu-os-cloud/global/images/family/ubuntu-1710",
Metadata: Metadata{
StartupScript: "Node startup script",
},
},
},
},
},
expectedStrings: []string{"startupScript: Master startup script", "startupScript: Node startup script"},
expectedErr: false,
},
}

for _, table := range testTables {
yaml, err := table.validConfigs.GetYaml()
if table.expectedErr && err == nil {
t.Errorf("An error was not received as expected.")
}
if !table.expectedErr && err != nil {
t.Errorf("Got unexpected error: %s", err)
}
for _, expectedString := range table.expectedStrings {
if !strings.Contains(yaml, expectedString) {
t.Errorf("Yaml did not contain expected string, got:\n%s\nwant:\n%s", yaml, expectedString)
}
}
}
}

func TestMatchMachineSetupConfig(t *testing.T) {
masterMachineSetupConfig := config{
Params: []ConfigParams{
{
OS: "ubuntu-1710",
Roles: []common.MachineRole{common.MasterRole},
Versions: v1alpha1.MachineVersionInfo{
Roles: []clustercommon.MachineRole{clustercommon.MasterRole},
Versions: clusterv1.MachineVersionInfo{
Kubelet: "1.9.3",
ControlPlane: "1.9.3",
ContainerRuntime: v1alpha1.ContainerRuntimeInfo{
ContainerRuntime: clusterv1.ContainerRuntimeInfo{
Name: "docker",
Version: "1.12.0",
},
},
},
{
OS: "ubuntu-1710",
Roles: []common.MachineRole{common.MasterRole},
Versions: v1alpha1.MachineVersionInfo{
Roles: []clustercommon.MachineRole{clustercommon.MasterRole},
Versions: clusterv1.MachineVersionInfo{
Kubelet: "1.9.4",
ControlPlane: "1.9.4",
ContainerRuntime: v1alpha1.ContainerRuntimeInfo{
ContainerRuntime: clusterv1.ContainerRuntimeInfo{
Name: "docker",
Version: "1.12.0",
},
Expand All @@ -127,21 +200,21 @@ func TestMatchMachineSetupConfig(t *testing.T) {
Params: []ConfigParams{
{
OS: "ubuntu-1710",
Roles: []common.MachineRole{common.NodeRole},
Versions: v1alpha1.MachineVersionInfo{
Roles: []clustercommon.MachineRole{clustercommon.NodeRole},
Versions: clusterv1.MachineVersionInfo{
Kubelet: "1.9.3",
ContainerRuntime: v1alpha1.ContainerRuntimeInfo{
ContainerRuntime: clusterv1.ContainerRuntimeInfo{
Name: "docker",
Version: "1.12.0",
},
},
},
{
OS: "ubuntu-1710",
Roles: []common.MachineRole{common.NodeRole},
Versions: v1alpha1.MachineVersionInfo{
Roles: []clustercommon.MachineRole{clustercommon.NodeRole},
Versions: clusterv1.MachineVersionInfo{
Kubelet: "1.9.4",
ContainerRuntime: v1alpha1.ContainerRuntimeInfo{
ContainerRuntime: clusterv1.ContainerRuntimeInfo{
Name: "docker",
Version: "1.12.0",
},
Expand All @@ -168,11 +241,11 @@ func TestMatchMachineSetupConfig(t *testing.T) {
{
params: ConfigParams{
OS: "ubuntu-1710",
Roles: []common.MachineRole{common.MasterRole},
Versions: v1alpha1.MachineVersionInfo{
Roles: []clustercommon.MachineRole{clustercommon.MasterRole},
Versions: clusterv1.MachineVersionInfo{
Kubelet: "1.9.4",
ControlPlane: "1.9.4",
ContainerRuntime: v1alpha1.ContainerRuntimeInfo{
ContainerRuntime: clusterv1.ContainerRuntimeInfo{
Name: "docker",
Version: "1.12.0",
},
Expand All @@ -184,10 +257,10 @@ func TestMatchMachineSetupConfig(t *testing.T) {
{
params: ConfigParams{
OS: "ubuntu-1710",
Roles: []common.MachineRole{common.NodeRole},
Versions: v1alpha1.MachineVersionInfo{
Roles: []clustercommon.MachineRole{clustercommon.NodeRole},
Versions: clusterv1.MachineVersionInfo{
Kubelet: "1.9.4",
ContainerRuntime: v1alpha1.ContainerRuntimeInfo{
ContainerRuntime: clusterv1.ContainerRuntimeInfo{
Name: "docker",
Version: "1.12.0",
},
Expand All @@ -199,10 +272,10 @@ func TestMatchMachineSetupConfig(t *testing.T) {
{
params: ConfigParams{
OS: "ubuntu-1710",
Roles: []common.MachineRole{common.NodeRole},
Versions: v1alpha1.MachineVersionInfo{
Kubelet: "1.9.4",
ContainerRuntime: v1alpha1.ContainerRuntimeInfo{
Roles: []clustercommon.MachineRole{clustercommon.NodeRole},
Versions: clusterv1.MachineVersionInfo{
Kubelet: "1.9.4",
ContainerRuntime: clusterv1.ContainerRuntimeInfo{
Name: "docker",
Version: "1.13.0",
},
Expand All @@ -214,10 +287,10 @@ func TestMatchMachineSetupConfig(t *testing.T) {
{
params: ConfigParams{
OS: "ubuntu-1710",
Roles: []common.MachineRole{common.MasterRole, common.NodeRole},
Versions: v1alpha1.MachineVersionInfo{
Roles: []clustercommon.MachineRole{clustercommon.MasterRole, clustercommon.NodeRole},
Versions: clusterv1.MachineVersionInfo{
Kubelet: "1.9.3",
ContainerRuntime: v1alpha1.ContainerRuntimeInfo{
ContainerRuntime: clusterv1.ContainerRuntimeInfo{
Name: "docker",
Version: "1.12.0",
},
Expand Down
6 changes: 3 additions & 3 deletions cluster-api/cloud/google/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,15 +87,15 @@ const masterEnvironmentVars = `
#!/bin/bash
KUBELET_VERSION={{ .Machine.Spec.Versions.Kubelet }}
export VERSION=v${KUBELET_VERSION}
export ARCH=amd64
VERSION=v${KUBELET_VERSION}
ARCH=amd64
TOKEN={{ .Token }}
PORT=443
MACHINE={{ .Machine.ObjectMeta.Name }}
CONTROL_PLANE_VERSION={{ .Machine.Spec.Versions.ControlPlane }}
CLUSTER_DNS_DOMAIN={{ .Cluster.Spec.ClusterNetwork.DNSDomain }}
POD_CIDR={{ .PodCIDR }}
SERVICE_CIDER={{ .ServiceCIDR }}
SERVICE_CIDR={{ .ServiceCIDR }}
# Environment variables for GCE cloud config
PROJECT={{ .Project }}
Expand Down
2 changes: 1 addition & 1 deletion cluster-api/gcp-deployer/cmd/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func RunAdd(ao *AddOptions) error {
return err
}

d := deploy.NewDeployer(provider, kubeConfig)
d := deploy.NewDeployer(provider, kubeConfig, "")

return d.AddNodes(machines)
}
Expand Down
13 changes: 10 additions & 3 deletions cluster-api/gcp-deployer/cmd/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,9 @@ import (
)

type CreateOptions struct {
Cluster string
Machine string
Cluster string
Machine string
MachineSetup string
}

var co = &CreateOptions{}
Expand All @@ -46,6 +47,11 @@ var createCmd = &cobra.Command{
cmd.Help()
os.Exit(1)
}
if co.MachineSetup == "" {
glog.Error("Please provide yaml file for machine setup configs.")
cmd.Help()
os.Exit(1)
}
if err := RunCreate(co); err != nil {
glog.Exit(err)
}
Expand All @@ -63,13 +69,14 @@ func RunCreate(co *CreateOptions) error {
return err
}

d := deploy.NewDeployer(provider, kubeConfig)
d := deploy.NewDeployer(provider, kubeConfig, co.MachineSetup)

return d.CreateCluster(cluster, machines)
}
func init() {
createCmd.Flags().StringVarP(&co.Cluster, "cluster", "c", "", "cluster yaml file")
createCmd.Flags().StringVarP(&co.Machine, "machines", "m", "", "machine yaml file")
createCmd.Flags().StringVarP(&co.MachineSetup, "setup", "s", "", "machine setup configs yaml file")

RootCmd.AddCommand(createCmd)
}
2 changes: 1 addition & 1 deletion cluster-api/gcp-deployer/cmd/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ var deleteCmd = &cobra.Command{
}

func RunDelete() error {
d := deploy.NewDeployer(provider, kubeConfig)
d := deploy.NewDeployer(provider, kubeConfig, "")
return d.DeleteCluster()
}

Expand Down
Loading

0 comments on commit 9b9a8b9

Please sign in to comment.