Skip to content

Commit

Permalink
Use our package
Browse files Browse the repository at this point in the history
  • Loading branch information
errordeveloper committed Jul 31, 2018
1 parent 8b76f7c commit 1552ba8
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 150 deletions.
83 changes: 20 additions & 63 deletions pkg/cfn/builder/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
. "github.com/weaveworks/eksctl/pkg/cfn/builder"
"github.com/weaveworks/eksctl/pkg/cloudconfig"
"github.com/weaveworks/eksctl/pkg/eks/api"
)

Expand Down Expand Up @@ -110,74 +111,30 @@ var _ = Describe("CloudFormation template builder API", func() {
})

Describe("UserData", func() {
// rs := NewNodeGroupResourceSet()
// rs.AddAllResources(testAZs)

// template, err := rs.RenderJSON()
// It("should serialise JSON without errors", func() {
// Expect(err).NotTo(HaveOccurred())
// })
// obj := Template{}
// It("should parse JSON withon errors and extract valid cloud-config", func() {
// err := json.Unmarshal(template, &obj)
// Expect(err).NotTo(HaveOccurred())
// Expect(len(obj.Resources)).ToNot(Equal(0))

// userData := obj.Resources["NodeLaunchConfig"].Properties.UserData
// Expect(userData).ToNot(Equal(""))

// buf, err := base64.StdEncoding.DecodeString(userData)
// Expect(err).NotTo(HaveOccurred())
// bufReader, err := gzip.NewReader(bytes.NewReader(buf))
// Expect(err).NotTo(HaveOccurred())
// buf, err = ioutil.ReadAll(bufReader)
// Expect(err).NotTo(HaveOccurred())
// userData = string(buf)

// lines := strings.Split(userData, "\n")
// Expect(len(lines)).Should(BeNumerically(">", 1))
// Expect(lines[0]).To(Equal("#cloud-config"))

// userDataObj := struct {
// RunCmd []string `json:"runcmd"`
// Packages []string `json:"packages"`
// }{}

// err = yaml.Unmarshal(buf, &userDataObj)
// Expect(err).NotTo(HaveOccurred())

// Expect(userDataObj.Packages).To(Equal([]string{"jq"}))
// })

// It("should parse JSON withon errors and extract valid cloud-config using our implementation", func() {
// err := json.Unmarshal(template, &obj)
// Expect(err).NotTo(HaveOccurred())
// Expect(len(obj.Resources)).ToNot(Equal(0))

// userData := obj.Resources["NodeLaunchConfig"].Properties.UserData
// Expect(userData).ToNot(Equal(""))

// c, err := DecodeCloudConfig(userData)
// Expect(err).NotTo(HaveOccurred())

// Expect(c.Packages).To(Equal([]string{"jq"}))
// })

It("make, encode and decode valid cloud-config using our implementation", func() {
input := NewCloudConfig()

input.Packages = []string{"curl", "jq"}
input.Commands = []string{"curl --silen jsonip.com | jq ."}

result, err := input.Encode()
rs := NewNodeGroupResourceSet()
rs.AddAllResources(testAZs)

template, err := rs.RenderJSON()
It("should serialise JSON without errors", func() {
Expect(err).NotTo(HaveOccurred())
})
obj := Template{}

It("should parse JSON withon errors and extract valid cloud-config using our implementation", func() {
err := json.Unmarshal(template, &obj)
Expect(err).NotTo(HaveOccurred())
Expect(len(obj.Resources)).ToNot(Equal(0))

Expect(result).NotTo(Equal("H4sIAAAAAAAA"))
userData := obj.Resources["NodeLaunchConfig"].Properties.UserData
Expect(userData).ToNot(Equal(""))

output, err := DecodeCloudConfig(result)
c, err := cloudconfig.DecodeCloudConfig(userData)
Expect(err).NotTo(HaveOccurred())

Expect(output).To(Equal(input))
Expect(c.Packages).To(Equal([]string{"jq"}))

//Expect(c).To(Equal(nil))
// TODO: more tests
})
})
})
145 changes: 68 additions & 77 deletions pkg/cfn/builder/nodegroup.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ package builder
import (
"os"

"github.com/kubicorn/kubicorn/pkg/logger"
"github.com/pkg/errors"
"github.com/weaveworks/eksctl/pkg/cloudconfig"

gfn "github.com/awslabs/goformation/cloudformation"
)
Expand Down Expand Up @@ -106,11 +108,11 @@ func (n *nodeGroupResourceSet) AddAllResources(availabilityZones []string) error

n.vpc = makeImportValue(ParamClusterStackName, cfnOutputClusterVPC)

// userData, err := n.makeUserData()
// if err != nil {
// return err
// }
// n.userData = userData
userData, err := n.makeUserData()
if err != nil {
return err
}
n.userData = userData

n.newStringParameter(ParamClusterName, "")
n.newStringParameter(ParamClusterStackName, "")
Expand Down Expand Up @@ -215,78 +217,67 @@ func getAsset(name string) (string, os.FileInfo, error) {
return data, info, nil
}

// func (n *nodeGroupResourceSet) makeUserData() (*gfn.StringIntrinsic, error) {

// ubuntu := false
// series := "centos7"
// if ubuntu {
// series = "bionic"
// }

// const (
// scriptDir = "/var/lib/cloud/scripts/per-instance/"
// configDir = "/etc/eksctl/"
// )

// config, err := cloudinit.New(series)
// if err != nil {
// panic(err)
// }

// scripts := []string{
// "get_metadata.sh",
// "get_credentials.sh",
// }
// files := map[string][]string{
// configDir: {
// "authenticator.sh",
// "kubeconfig.yaml",
// },
// }

// files["/etc/systemd/kubelet.service.d"] = []string{"10-eksclt.al2.conf"}

// //config.AddScripts("sudo yum install --yes jq")
// config.AddPackage("jq")

// config.AddScripts("pip install --upgrade awscli")

// if ubuntu {
// scripts = append(scripts, "ubuntu_prepare.sh")
// config.AddRunCmd("cloud-init-per", "once", "rm", "-f", scriptDir+"00-EKS-config.sh")
// }

// for dir, fileNames := range files {
// for _, fileName := range fileNames {
// data, info, err := getAsset(fileName)
// if err != nil {
// return nil, err
// }
// config.AddRunTextFile(dir+fileName, data, uint(info.Mode()))
// }
// }

// for _, scriptName := range scripts {
// scriptPath := scriptDir + scriptName
// data, _, err := getAsset(scriptName)
// if err != nil {
// return nil, err
// }
// config.AddRunTextFile(scriptPath, data, 0755)
// config.AddRunCmd(scriptPath)
// }

// config.AddRunCmd("systemctl", "daemon-reload")
// config.AddRunCmd("systemctl", "restart", "kubelet")

// body, err := renderers.RenderYAML(config, utils.Gzip, renderers.ToBase64)
// if err != nil {
// return nil, errors.Wrap(err, "encoding user data")
// }

// logger.Debug("user-data = %s", string(body))
// return gfn.NewString(string(body)), nil
// }
func (n *nodeGroupResourceSet) makeUserData() (*gfn.StringIntrinsic, error) {
//ubuntu := false

const configDir = "/etc/eksctl/"

config := cloudconfig.New()

scripts := []string{
"get_metadata.sh",
"get_credentials.sh",
}
files := map[string][]string{
configDir: {
"authenticator.sh",
"kubeconfig.yaml",
},
}

files["/etc/systemd/kubelet.service.d"] = []string{"10-eksclt.al2.conf"}

config.AddPackages("jq")
config.AddCommand("pip", "install", "--upgrade", "awscli")

// if ubuntu {
// scripts = append(scripts, "ubuntu_prepare.sh")
// config.AddRunCmd("cloud-init-per", "once", "rm", "-f", "/var/lib/cloud/scripts/per-instance/00-EKS-config.sh")
// }

for dir, fileNames := range files {
for _, fileName := range fileNames {
data, info, err := getAsset(fileName)
if err != nil {
return nil, err
}
config.AddFile(cloudconfig.File{
Path: dir + fileName,
Content: data,
Permissions: info.Mode().String(),
})
}
}

for _, scriptName := range scripts {
data, _, err := getAsset(scriptName)
if err != nil {
return nil, err
}
config.RunScript(scriptName, data)
}

config.AddCommand("systemctl", "daemon-reload")
config.AddCommand("systemctl", "restart", "kubelet")

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

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

func (n *nodeGroupResourceSet) addResourcesForNodeGroup() {
refLC := n.newResource("NodeLaunchConfig", &gfn.AWSAutoScalingLaunchConfiguration{
Expand Down
32 changes: 23 additions & 9 deletions pkg/cloudconfig/cloudconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,26 @@ const (
Shell = "/bin/bash"

scriptDir = "/var/lib/cloud/scripts/per-instance/"

defaultOwner = "root:root"
defaultScriptPermissions = "0755"
defaultFilePermissions = "0644"
)

type CloudConfig struct {
Commands []interface{} `json:"runcmd"`
Packages []string `json:"packages"`
WriteFiles []CloudConfigWriteFile `json:"write_files"`
Commands []interface{} `json:"runcmd"`
Packages []string `json:"packages"`
WriteFiles []File `json:"write_files"`
}

type CloudConfigWriteFile struct {
type File struct {
Content string `json:"content"`
Owner string `json:"owner"`
Path string `json:"path"`
Permissions string `json:"permissions"`
}

func NewCloudConfig() *CloudConfig {
func New() *CloudConfig {
return &CloudConfig{}
}

Expand All @@ -52,12 +56,22 @@ func (c *CloudConfig) AddShellCommand(cmd string) {
c.Commands = append(c.Commands, []string{Shell, "-c", cmd})
}

func (c *CloudConfig) AddFile(f File) {
if f.Owner == "" {
f.Owner = defaultOwner
}
if f.Permissions == "" {
f.Permissions = defaultFilePermissions
}
c.WriteFiles = append(c.WriteFiles, f)
}

func (c *CloudConfig) AddScript(p, s string) {
c.WriteFiles = append(c.WriteFiles, CloudConfigWriteFile{
c.AddFile(File{
Content: s,
Permissions: "0755",
Path: p,
Owner: "root:root",
Permissions: defaultScriptPermissions,
Owner: defaultOwner,
})
}

Expand Down Expand Up @@ -95,7 +109,7 @@ func DecodeCloudConfig(s string) (*CloudConfig, error) {
if s == "" {
return nil, fmt.Errorf("cannot decode empty string")
}
c := NewCloudConfig()
c := New()

data, err := base64.StdEncoding.DecodeString(s)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion pkg/cloudconfig/cloudconfig_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ var _ = Describe("cloudconfig", func() {
output *CloudConfig
)

input := NewCloudConfig()
input := New()
input.AddPackages("curl", "jq")

const (
Expand Down

0 comments on commit 1552ba8

Please sign in to comment.