Skip to content

Commit

Permalink
feat: validate aws-sdk credentials func (#36)
Browse files Browse the repository at this point in the history
  • Loading branch information
saumas authored Aug 13, 2021
1 parent af10c3c commit 3238205
Show file tree
Hide file tree
Showing 11 changed files with 46 additions and 11 deletions.
2 changes: 1 addition & 1 deletion internal/services/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ func (c *Controller) nodeAddHandler(log logrus.FieldLogger, event event, obj int
}

if spot {
node.Labels[labels.FakeSpot] = "true"
node.Labels[labels.CastaiFakeSpot] = "true"
}

genericHandler(log, c.queue, reflect.TypeOf(&corev1.Node{}), event, node)
Expand Down
2 changes: 1 addition & 1 deletion internal/services/controller/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func Test(t *testing.T) {

node := &v1.Node{ObjectMeta: metav1.ObjectMeta{Name: "node1", Labels: map[string]string{}}}
expectedNode := node.DeepCopy()
expectedNode.Labels[labels.FakeSpot] = "true"
expectedNode.Labels[labels.CastaiFakeSpot] = "true"
nodeData, err := encode(expectedNode)
require.NoError(t, err)

Expand Down
2 changes: 1 addition & 1 deletion internal/services/providers/castai/castai.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ type Provider struct {
}

func (p *Provider) IsSpot(_ context.Context, node *v1.Node) (bool, error) {
if val, ok := node.Labels[labels.Spot]; ok && val == "true" {
if val, ok := node.Labels[labels.CastaiSpot]; ok && val == "true" {
return true, nil
}
return false, nil
Expand Down
12 changes: 12 additions & 0 deletions internal/services/providers/eks/client/aws.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,23 @@ func WithEC2Client() func(ctx context.Context, c *client) error {
return fmt.Errorf("creating aws sdk session: %w", err)
}

c.sess = sess
c.ec2Client = ec2.New(sess)

return nil
}
}

// WithValidateCredentials validates the aws-sdk credentials chain.
func WithValidateCredentials() func(ctx context.Context, c *client) error {
return func(ctx context.Context, c *client) error {
if _, err := c.sess.Config.Credentials.Get(); err != nil {
return fmt.Errorf("validating aws credentials: %w", err)
}
return nil
}
}

// WithMetadata configures the discoverable EC2 instance metadata and EKS properties by setting static values instead
// of relying on the discovery mechanism.
func WithMetadata(accountID, region, clusterName string) func(ctx context.Context, c *client) error {
Expand Down Expand Up @@ -112,6 +123,7 @@ func WithMetadataDiscovery() func(ctx context.Context, c *client) error {

type client struct {
log logrus.FieldLogger
sess *session.Session
metaClient *ec2metadata.EC2Metadata
ec2Client *ec2.EC2
region *string
Expand Down
2 changes: 1 addition & 1 deletion internal/services/providers/eks/eks.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ func (p *Provider) IsSpot(ctx context.Context, node *v1.Node) (bool, error) {
return true, nil
}

if val, ok := node.Labels[labels.Spot]; ok && val == "true" {
if val, ok := node.Labels[labels.CastaiSpot]; ok && val == "true" {
return true, nil
}

Expand Down
2 changes: 1 addition & 1 deletion internal/services/providers/eks/eks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ func TestProvider_IsSpot(t *testing.T) {
}

got, err := p.IsSpot(context.Background(), &v1.Node{ObjectMeta: metav1.ObjectMeta{Labels: map[string]string{
labels.Spot: "true",
labels.CastaiSpot: "true",
}}})

require.NoError(t, err)
Expand Down
2 changes: 1 addition & 1 deletion internal/services/providers/gke/gke.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func (p *Provider) RegisterCluster(ctx context.Context, client castai.Client) (*
}

func (p *Provider) IsSpot(_ context.Context, node *corev1.Node) (bool, error) {
if val, ok := node.Labels[labels.Spot]; ok && val == "true" {
if val, ok := node.Labels[labels.CastaiSpot]; ok && val == "true" {
return true, nil
}

Expand Down
2 changes: 1 addition & 1 deletion internal/services/providers/gke/gke_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func TestProvider_IsSpot(t *testing.T) {
}{
{
name: "castai spot node",
node: &v1.Node{ObjectMeta: metav1.ObjectMeta{Labels: map[string]string{labels.Spot: "true"}}},
node: &v1.Node{ObjectMeta: metav1.ObjectMeta{Labels: map[string]string{labels.CastaiSpot: "true"}}},
expected: true,
},
{
Expand Down
7 changes: 6 additions & 1 deletion internal/services/providers/kops/kops.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ func (p *Provider) RegisterCluster(ctx context.Context, client castai.Client) (*
opts := []awsclient.Opt{
awsclient.WithMetadata("", region, clusterName),
awsclient.WithEC2Client(),
awsclient.WithValidateCredentials(),
}
c, err := awsclient.New(ctx, p.log, opts...)
if err != nil {
Expand Down Expand Up @@ -114,7 +115,11 @@ func (p *Provider) RegisterCluster(ctx context.Context, client castai.Client) (*
}

func (p *Provider) IsSpot(ctx context.Context, node *v1.Node) (bool, error) {
if val, ok := node.Labels[labels.Spot]; ok && val == "true" {
if val, ok := node.Labels[labels.CastaiSpot]; ok && val == "true" {
return true, nil
}

if val, ok := node.Labels[labels.KopsSpot]; ok && val == "true" {
return true, nil
}

Expand Down
19 changes: 18 additions & 1 deletion internal/services/providers/kops/kops_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,24 @@ func TestProvider_IsSpot(t *testing.T) {
node := &v1.Node{
ObjectMeta: metav1.ObjectMeta{
Labels: map[string]string{
labels.Spot: "true",
labels.CastaiSpot: "true",
},
},
}

p := &Provider{}

got, err := p.IsSpot(context.Background(), node)

require.NoError(t, err)
require.True(t, got)
})

t.Run("kops instance group spot nodes", func(t *testing.T) {
node := &v1.Node{
ObjectMeta: metav1.ObjectMeta{
Labels: map[string]string{
labels.KopsSpot: "true",
},
},
}
Expand Down
5 changes: 3 additions & 2 deletions pkg/labels/labels.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package labels

const (
Spot = "scheduling.cast.ai/spot"
FakeSpot = "scheduling.cast.ai/fake-spot"
CastaiSpot = "scheduling.cast.ai/spot"
CastaiFakeSpot = "scheduling.cast.ai/fake-spot"
KopsSpot = "spot"
)

0 comments on commit 3238205

Please sign in to comment.