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

Adds controller tag support to instance profile. #13352

Merged
merged 1 commit into from Sep 24, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 6 additions & 1 deletion provider/ec2/environ.go
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
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
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