Skip to content

Commit

Permalink
Minor refactoring of userdata.go
Browse files Browse the repository at this point in the history
  • Loading branch information
errordeveloper committed Oct 29, 2018
1 parent 875e50d commit bbb9e30
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 59 deletions.
82 changes: 23 additions & 59 deletions pkg/nodebootstrap/userdata.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,10 @@ package nodebootstrap
import (
"fmt"
"os"
"strings"

"github.com/pkg/errors"
"k8s.io/client-go/tools/clientcmd"

"github.com/kubicorn/kubicorn/pkg/logger"

"github.com/weaveworks/eksctl/pkg/cloudconfig"
"github.com/weaveworks/eksctl/pkg/eks/api"
"github.com/weaveworks/eksctl/pkg/utils/kubeconfig"
Expand Down Expand Up @@ -71,75 +68,42 @@ func addFilesAndScripts(config *cloudconfig.CloudConfig, files configFiles, scri
return nil
}

func makeAmazonLinux2Config(spec *api.ClusterConfig, nodeGroupID int) (configFiles, error) {
c := spec.NodeGroups[nodeGroupID]
if c.MaxPodsPerNode == 0 {
c.MaxPodsPerNode = maxPodsPerNodeType[c.InstanceType]
}
// TODO: use componentconfig or kubelet config file – https://github.com/weaveworks/eksctl/issues/156
clusterDNS := "10.100.0.10"
if spec.VPC.CIDR.IP[0] == 10 {
// Default service network is 10.100.0.0, but it gets set 172.20.0.0 automatically when pod network
// is anywhere within 10.0.0.0/8
clusterDNS = "172.20.0.10"
}
kubeletParams := []string{
fmt.Sprintf("MAX_PODS=%d", c.MaxPodsPerNode),
fmt.Sprintf("CLUSTER_DNS=%s", clusterDNS),
}

metadata := []string{
fmt.Sprintf("AWS_DEFAULT_REGION=%s", spec.Region),
fmt.Sprintf("AWS_EKS_CLUSTER_NAME=%s", spec.ClusterName),
fmt.Sprintf("AWS_EKS_ENDPOINT=%s", spec.Endpoint),
}

func makeClientConfigData(spec *api.ClusterConfig) ([]byte, error) {
clientConfig, _, _ := kubeconfig.New(spec, "kubelet", configDir+"ca.crt")
kubeconfig.AppendAuthenticator(clientConfig, spec, kubeconfig.AWSIAMAuthenticator)

clientConfigData, err := clientcmd.Write(*clientConfig)
if err != nil {
return nil, errors.Wrap(err, "serialising kubeconfig for nodegroup")
}

files := configFiles{
kubeletDropInUnitDir: {
"10-eksclt.al2.conf": {isAsset: true},
},
configDir: {
"metadata.env": {content: strings.Join(metadata, "\n")},
"kubelet.env": {content: strings.Join(kubeletParams, "\n")},
// TODO: https://github.com/weaveworks/eksctl/issues/161
"ca.crt": {content: string(spec.CertificateAuthorityData)},
"kubeconfig.yaml": {content: string(clientConfigData)},
},
}

return files, nil
return clientConfigData, nil
}

// NewUserDataForAmazonLinux2 creates new user data for Amazon Linux 2 nodes
func NewUserDataForAmazonLinux2(spec *api.ClusterConfig, nodeGroupID int) (string, error) {
config := cloudconfig.New()

scripts := []string{
"bootstrap.al2.sh",
func clusterDNS(spec *api.ClusterConfig) string {
// Default service network is 10.100.0.0, but it gets set 172.20.0.0 automatically when pod network
// is anywhere within 10.0.0.0/8
if spec.VPC.CIDR.IP[0] == 10 {
return "172.20.0.10"
}
return "10.100.0.10"
}

files, err := makeAmazonLinux2Config(spec, nodeGroupID)
if err != nil {
return "", err
func makeKubeletParams(spec *api.ClusterConfig, nodeGroupID int) []string {
ng := spec.NodeGroups[nodeGroupID]
if ng.MaxPodsPerNode == 0 {
ng.MaxPodsPerNode = maxPodsPerNodeType[ng.InstanceType]
}

if err = addFilesAndScripts(config, files, scripts); err != nil {
return "", err
// TODO: use componentconfig or kubelet config file – https://github.com/weaveworks/eksctl/issues/156
return []string{
fmt.Sprintf("MAX_PODS=%d", ng.MaxPodsPerNode),
fmt.Sprintf("CLUSTER_DNS=%s", clusterDNS(spec)),
}
}

body, err := config.Encode()
if err != nil {
return "", errors.Wrap(err, "encoding user data")
func makeMetadata(spec *api.ClusterConfig) []string {
return []string{
fmt.Sprintf("AWS_DEFAULT_REGION=%s", spec.Region),
fmt.Sprintf("AWS_EKS_CLUSTER_NAME=%s", spec.ClusterName),
fmt.Sprintf("AWS_EKS_ENDPOINT=%s", spec.Endpoint),
}

logger.Debug("user-data = %s", body)
return body, nil
}
60 changes: 60 additions & 0 deletions pkg/nodebootstrap/userdata_al2.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package nodebootstrap

import (
"strings"

"github.com/pkg/errors"

"github.com/kubicorn/kubicorn/pkg/logger"

"github.com/weaveworks/eksctl/pkg/cloudconfig"
"github.com/weaveworks/eksctl/pkg/eks/api"
)

func makeAmazonLinux2Config(spec *api.ClusterConfig, nodeGroupID int) (configFiles, error) {
clientConfigData, err := makeClientConfigData(spec)
if err != nil {
return nil, err
}

files := configFiles{
kubeletDropInUnitDir: {
"10-eksclt.al2.conf": {isAsset: true},
},
configDir: {
"metadata.env": {content: strings.Join(makeMetadata(spec), "\n")},
"kubelet.env": {content: strings.Join(makeKubeletParams(spec, nodeGroupID), "\n")},
// TODO: https://github.com/weaveworks/eksctl/issues/161
"ca.crt": {content: string(spec.CertificateAuthorityData)},
"kubeconfig.yaml": {content: string(clientConfigData)},
},
}

return files, nil
}

// NewUserDataForAmazonLinux2 creates new user data for Amazon Linux 2 nodes
func NewUserDataForAmazonLinux2(spec *api.ClusterConfig, nodeGroupID int) (string, error) {
config := cloudconfig.New()

scripts := []string{
"bootstrap.al2.sh",
}

files, err := makeAmazonLinux2Config(spec, nodeGroupID)
if err != nil {
return "", err
}

if err = addFilesAndScripts(config, files, scripts); err != nil {
return "", err
}

body, err := config.Encode()
if err != nil {
return "", errors.Wrap(err, "encoding user data")
}

logger.Debug("user-data = %s", body)
return body, nil
}

0 comments on commit bbb9e30

Please sign in to comment.