Skip to content

Commit

Permalink
Adds controller tag support to instance profile.
Browse files Browse the repository at this point in the history
Instance profiles created by the controller now contain the controller
tag that they are made for.
  • Loading branch information
tlm committed Sep 22, 2021
1 parent d91348c commit 3361bf8
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 5 deletions.
7 changes: 6 additions & 1 deletion provider/ec2/environ.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,12 @@ func (e *environ) Bootstrap(ctx environs.BootstrapContext, callCtx context.Provi
if !ok {
return nil, errors.NewNotValid(nil, "cannot find controller name in config")
}
instProfile, err := ensureControllerInstanceProfile(ctx.Context(), e.iamClient, controllerName)
controllerUUID := args.ControllerConfig[controller.ControllerUUIDKey].(string)
instProfile, err := ensureControllerInstanceProfile(
ctx.Context(),
e.iamClient,
controllerName,
controllerUUID)
if err != nil {
return nil, err
}
Expand Down
8 changes: 8 additions & 0 deletions provider/ec2/iam.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"github.com/juju/juju/environs/cloudspec"
"github.com/juju/juju/environs/context"
"github.com/juju/juju/environs/instances"
"github.com/juju/juju/environs/tags"
)

// instanceProfileClient is a subset interface of the ec2 client for attaching
Expand Down Expand Up @@ -70,10 +71,17 @@ func ensureControllerInstanceProfile(
ctx stdcontext.Context,
client IAMClient,
controllerName string,
controllerUUID string,
) (*iamtypes.InstanceProfile, error) {
profileName := fmt.Sprintf("juju-controller-%s", controllerName)
res, err := client.CreateInstanceProfile(ctx, &iam.CreateInstanceProfileInput{
InstanceProfileName: aws.String(profileName),
Tags: []iamtypes.Tag{
{
Key: aws.String(tags.JujuController),
Value: aws.String(controllerUUID),
},
},
})
if err != nil {
var alreadyExistsErr *iamtypes.EntityAlreadyExistsException
Expand Down
20 changes: 16 additions & 4 deletions provider/ec2/iam_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ import (
"github.com/juju/errors"
jc "github.com/juju/testing/checkers"
gc "gopkg.in/check.v1"

"github.com/juju/juju/environs/tags"
)

type IAMSuite struct{}
Expand Down Expand Up @@ -58,7 +60,12 @@ func (*IAMSuite) TestEnsureControllerInstanceProfileFromScratch(c *gc.C) {

c.Assert(*i.InstanceProfileName, gc.Equals, "juju-controller-test")
c.Assert(i.Path, gc.IsNil)
c.Assert(len(i.Tags), gc.Equals, 0)
c.Assert(i.Tags, jc.DeepEquals, []types.Tag{
{
Key: aws.String(tags.JujuController),
Value: aws.String("AABBCC"),
},
})

t := time.Now()
return &iam.CreateInstanceProfileOutput{
Expand All @@ -71,7 +78,7 @@ func (*IAMSuite) TestEnsureControllerInstanceProfileFromScratch(c *gc.C) {
},
}

_, err := ensureControllerInstanceProfile(context.TODO(), client, "test")
_, err := ensureControllerInstanceProfile(context.TODO(), client, "test", "AABBCC")
c.Assert(err, jc.ErrorIsNil)
}

Expand All @@ -86,7 +93,12 @@ func (*IAMSuite) TestEnsureControllerInstanceProfileAlreadyExists(c *gc.C) {

c.Assert(*i.InstanceProfileName, gc.Equals, "juju-controller-test")
c.Assert(i.Path, gc.IsNil)
c.Assert(len(i.Tags), gc.Equals, 0)
c.Assert(i.Tags, jc.DeepEquals, []types.Tag{
{
Key: aws.String(tags.JujuController),
Value: aws.String("ABCD"),
},
})

return nil, &types.EntityAlreadyExistsException{
Message: aws.String("already exists"),
Expand All @@ -111,7 +123,7 @@ func (*IAMSuite) TestEnsureControllerInstanceProfileAlreadyExists(c *gc.C) {
},
}

instanceProfile, err := ensureControllerInstanceProfile(context.TODO(), client, "test")
instanceProfile, err := ensureControllerInstanceProfile(context.TODO(), client, "test", "ABCD")
c.Assert(err, jc.ErrorIsNil)
c.Assert(getInstanceProfileCalled, jc.IsTrue)
c.Assert(*instanceProfile.Arn, gc.Equals, "arn://12345")
Expand Down

0 comments on commit 3361bf8

Please sign in to comment.