Skip to content

Commit

Permalink
Fix hard-coded region on EKS shell script parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
marcwickenden committed Oct 31, 2023
1 parent d4f0288 commit 37fb52f
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 13 deletions.
12 changes: 8 additions & 4 deletions pkg/eks/eks.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ import (

"github.com/4armed/kubeletmein/pkg/common"
"github.com/4armed/kubeletmein/pkg/config"
"github.com/aws/aws-sdk-go/aws/ec2metadata"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/kubicorn/kubicorn/pkg/logger"
"k8s.io/client-go/tools/clientcmd"
)
Expand Down Expand Up @@ -77,13 +75,19 @@ func Generate(c *config.Config) error {
// At the moment we have these calls here which ignore the functions in
// metadata.go completely.
func getUserData() (string, error) {
md := ec2metadata.New(session.New())
md, err := NewEC2MetadataClient()
if err != nil {
return "", err
}

return md.GetUserData()
}

func getRegion() (string, error) {
md := ec2metadata.New(session.New())
md, err := NewEC2MetadataClient()
if err != nil {
return "", err
}

return md.Region()
}
8 changes: 4 additions & 4 deletions pkg/eks/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (

// EC2MetadataClient interface
type EC2MetadataClient struct {
*ec2metadata.EC2Metadata
client *ec2metadata.EC2Metadata
}

// NewEC2MetadataClient instantiates an EC2 Metadata client
Expand All @@ -18,19 +18,19 @@ func NewEC2MetadataClient() (*EC2MetadataClient, error) {
}

md := ec2metadata.New(sess)
return &EC2MetadataClient{md}, nil
return &EC2MetadataClient{client: md}, nil
}

// GetUserData wraps the the AWS EC2 Metadata call
// This is all so we can mock it. There has to be a better way but
// the AWS Go SDK seems...a bit rubbish.
func (c *EC2MetadataClient) GetUserData() (string, error) {
return c.GetUserData()
return c.client.GetUserData()
}

// Region wraps the the AWS EC2 Region call
// This is all so we can mock it. There has to be a better way but
// the AWS Go SDK seems...a bit rubbish.
func (c *EC2MetadataClient) Region() (string, error) {
return c.Region()
return c.client.Region()
}
6 changes: 3 additions & 3 deletions pkg/eks/userdata.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func ParseUserData(userData, region string) (*clientcmdapi.Config, error) {
}
} else {
logger.Debug("shell script assuming, looking for /etc/eks/bootstrap.sh")
kubeConfigData, err = ParseShellScript(userData)
kubeConfigData, err = ParseShellScript(userData, region)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -185,7 +185,7 @@ func ParseCloudConfig(cloudConfig []byte, region string) (*clientcmdapi.Config,
}

// ParseShellScript parses shell-script format user-data seen on managed nodegroups
func ParseShellScript(userData string) (*clientcmdapi.Config, error) {
func ParseShellScript(userData string, region string) (*clientcmdapi.Config, error) {
// We must account for all arguments to bootstrap.sh in order to find the non-flag based cluster name
// https://github.com/awslabs/amazon-eks-ami/blob/master/files/bootstrap.sh

Expand Down Expand Up @@ -267,7 +267,7 @@ func ParseShellScript(userData string) (*clientcmdapi.Config, error) {
"--cluster-name",
clusterName,
"--region",
"eu-west-1",
region,
},
Env: []clientcmdapi.ExecEnvVar{
{
Expand Down
4 changes: 2 additions & 2 deletions pkg/eks/userdata_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func TestParseShellScriptManagementConsole(t *testing.T) {
t.Errorf("err: %v", err)
}

kubeConfig, err := ParseShellScript(string(userData))
kubeConfig, err := ParseShellScript(string(userData), "eu-west-1")
if err != nil {
t.Errorf("err: %v", err)
}
Expand All @@ -86,7 +86,7 @@ func TestParseShellScriptCustom(t *testing.T) {
t.Errorf("err: %v", err)
}

kubeConfig, err := ParseShellScript(string(userData))
kubeConfig, err := ParseShellScript(string(userData), "eu-west-1")
if err != nil {
t.Errorf("err: %v", err)
}
Expand Down

0 comments on commit 37fb52f

Please sign in to comment.