Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add --user-data-file option #453

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 18 additions & 15 deletions kubetest2/internal/deployers/eksapi/deployer.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ type deployerOptions struct {
UnmanagedNodes bool `flag:"unmanaged-nodes" desc:"Use an AutoScalingGroup instead of an EKS-managed nodegroup. Requires --ami"`
UpClusterHeaders []string `flag:"up-cluster-header" desc:"Additional header to add to eks:CreateCluster requests. Specified in the same format as curl's -H flag."`
UserDataFormat string `flag:"user-data-format" desc:"Format of the node instance user data"`
UserDataFile string `flag:"user-data-file" desc:"File containing a golang template of the node instance user data"`
}

// NewDeployer implements deployer.New for EKS using the EKS (and other AWS) API(s) directly (no cloudformation)
Expand Down Expand Up @@ -229,13 +230,13 @@ func (d *deployer) verifyUpFlags() error {
d.IPFamily = string(ekstypes.IpFamilyIpv4)
klog.V(2).Infof("Using default IP family: %s", d.IPFamily)
}
if d.UnmanagedNodes && d.AMI == "" {
return fmt.Errorf("--ami must be specified for --unmanaged-nodes")
}
if !d.UnmanagedNodes && d.AMI != "" {
return fmt.Errorf("--ami should not be provided without --unmanaged-nodes")
if d.UserDataFile != "" && d.UserDataFormat != "" {
return fmt.Errorf("--user-data-file should not be provided with --user-data-format")
}
if d.UnmanagedNodes {
if d.AMI == "" {
return fmt.Errorf("--ami must be specified for --unmanaged-nodes")
}
if d.NodeNameStrategy == "" {
d.NodeNameStrategy = "EC2PrivateDNSName"
klog.V(2).Infof("Using default node name strategy: EC2PrivateDNSName")
Expand All @@ -244,16 +245,18 @@ func (d *deployer) verifyUpFlags() error {
return fmt.Errorf("--node-name-strategy must be one of the following values: ['SessionName', 'EC2PrivateDNSName']")
}
}
}
if d.UnmanagedNodes && d.UserDataFormat == "" {
d.UserDataFormat = "bootstrap.sh"
klog.V(2).Infof("Using default user data format: %s", d.UserDataFormat)
}
if d.UnmanagedNodes && d.EFA && len(d.InstanceTypes) != 1 {
return fmt.Errorf("--efa requires a single instance type")
}
if d.UnmanagedNodes && d.AMIType != "" {
return fmt.Errorf("--ami-type should not be provided with --unmanaged-nodes")
if d.UserDataFile == "" && d.UserDataFormat == "" {
d.UserDataFormat = "bootstrap.sh"
klog.V(2).Infof("Using default user data format: %s", d.UserDataFormat)
}
if d.EFA && len(d.InstanceTypes) != 1 {
return fmt.Errorf("--efa requires a single instance type")
}
if d.AMIType != "" {
return fmt.Errorf("--ami-type should not be provided with --unmanaged-nodes")
}
} else if d.AMI != "" {
return fmt.Errorf("--ami should not be provided without --unmanaged-nodes")
}
if d.NodeReadyTimeout == 0 {
d.NodeReadyTimeout = time.Minute * 5
Expand Down
4 changes: 2 additions & 2 deletions kubetest2/internal/deployers/eksapi/nodegroup.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ func (m *NodegroupManager) createManagedNodegroup(infra *Infrastructure, cluster
func (m *NodegroupManager) createUnmanagedNodegroup(infra *Infrastructure, cluster *Cluster, opts *deployerOptions) error {
stackName := m.getUnmanagedNodegroupStackName()
klog.Infof("creating unmanaged nodegroup stack...")
userData, userDataIsMimePart, err := generateUserData(opts.UserDataFormat, cluster)
userData, userDataIsMimePart, err := generateUserData(opts.UserDataFile, opts.UserDataFormat, cluster)
if err != nil {
return err
}
Expand Down Expand Up @@ -246,7 +246,7 @@ func (m *NodegroupManager) createUnmanagedNodegroup(infra *Infrastructure, clust
func (m *NodegroupManager) createUnmanagedNodegroupWithEFA(infra *Infrastructure, cluster *Cluster, opts *deployerOptions) error {
stackName := m.getUnmanagedNodegroupStackName()
klog.Infof("creating unmanaged nodegroup with EFA stack...")
userData, _, err := generateUserData(opts.UserDataFormat, cluster)
userData, _, err := generateUserData(opts.UserDataFile, opts.UserDataFormat, cluster)
if err != nil {
return err
}
Expand Down
31 changes: 20 additions & 11 deletions kubetest2/internal/deployers/eksapi/userdata.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,29 @@ import (
"github.com/aws/aws-k8s-tester/kubetest2/internal/deployers/eksapi/templates"
)

func generateUserData(format string, cluster *Cluster) (string, bool, error) {
func generateUserData(file string, format string, cluster *Cluster) (string, bool, error) {
userDataIsMimePart := true
var t *template.Template
switch format {
case "bootstrap.sh":
t = templates.UserDataBootstrapSh
case "nodeadm":
// TODO: replace the YAML template with proper usage of the nodeadm API go types
t = templates.UserDataNodeadm
case "bottlerocket":
t = templates.UserDataBottlerocket
if file != "" {
userDataIsMimePart = false
default:
return "", false, fmt.Errorf("uknown user data format: '%s'", format)
tFromFile, err := template.ParseFiles(file)
if err != nil {
return "", false, fmt.Errorf("failed to parse user data file as template: %s: %v", file, err)
}
t = tFromFile
} else {
switch format {
case "bootstrap.sh":
t = templates.UserDataBootstrapSh
case "nodeadm":
// TODO: replace the YAML template with proper usage of the nodeadm API go types
t = templates.UserDataNodeadm
case "bottlerocket":
t = templates.UserDataBottlerocket
userDataIsMimePart = false
default:
return "", false, fmt.Errorf("uknown user data format: '%s'", format)
}
}
buf := bytes.Buffer{}
if err := t.Execute(&buf, templates.UserDataTemplateData{
Expand Down
Loading