From 684cfc7961eed22a56a3b7468f9c86227f3bcd37 Mon Sep 17 00:00:00 2001
From: AkiraFukushima
Date: Sat, 6 Feb 2021 15:53:41 +0900
Subject: [PATCH] Fix mock for IAM Instance Profile
---
pkg/testutils/BUILD.bazel | 1 +
pkg/testutils/integrationtestharness.go | 12 ++++++++++++
upup/pkg/fi/cloudup/awsup/aws_cloud.go | 3 ++-
upup/pkg/fi/cloudup/bootstrapchannelbuilder_test.go | 1 +
upup/pkg/fi/cloudup/template_functions.go | 5 ++++-
5 files changed, 20 insertions(+), 2 deletions(-)
diff --git a/pkg/testutils/BUILD.bazel b/pkg/testutils/BUILD.bazel
index c639e91e1c70b..71ae0307ca37c 100644
--- a/pkg/testutils/BUILD.bazel
+++ b/pkg/testutils/BUILD.bazel
@@ -37,6 +37,7 @@ go_library(
"//vendor/github.com/aws/aws-sdk-go/aws:go_default_library",
"//vendor/github.com/aws/aws-sdk-go/service/ec2:go_default_library",
"//vendor/github.com/aws/aws-sdk-go/service/elbv2:go_default_library",
+ "//vendor/github.com/aws/aws-sdk-go/service/iam:go_default_library",
"//vendor/github.com/aws/aws-sdk-go/service/route53:go_default_library",
"//vendor/github.com/gophercloud/gophercloud/openstack/compute/v2/flavors:go_default_library",
"//vendor/github.com/gophercloud/gophercloud/openstack/dns/v2/zones:go_default_library",
diff --git a/pkg/testutils/integrationtestharness.go b/pkg/testutils/integrationtestharness.go
index 07825d67b99a4..9362bb260a941 100644
--- a/pkg/testutils/integrationtestharness.go
+++ b/pkg/testutils/integrationtestharness.go
@@ -26,6 +26,7 @@ import (
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/aws/aws-sdk-go/service/elbv2"
+ "github.com/aws/aws-sdk-go/service/iam"
"github.com/aws/aws-sdk-go/service/route53"
"github.com/gophercloud/gophercloud/openstack/compute/v2/flavors"
"github.com/gophercloud/gophercloud/openstack/dns/v2/zones"
@@ -256,6 +257,17 @@ func (h *IntegrationTestHarness) SetupMockAWS() *awsup.MockAWSCloud {
Name: aws.String("my-external-tg-3"),
})
+ mockIAM.CreateRole(&iam.CreateRoleInput{
+ RoleName: aws.String("kops-custom-node-role"),
+ })
+ mockIAM.CreateInstanceProfile(&iam.CreateInstanceProfileInput{
+ InstanceProfileName: aws.String("kops-custom-node-role"),
+ })
+ mockIAM.AddRoleToInstanceProfile(&iam.AddRoleToInstanceProfileInput{
+ InstanceProfileName: aws.String("kops-custom-node-role"),
+ RoleName: aws.String("kops-custom-node-role"),
+ })
+
return cloud
}
diff --git a/upup/pkg/fi/cloudup/awsup/aws_cloud.go b/upup/pkg/fi/cloudup/awsup/aws_cloud.go
index 1fcd157bbd5b1..3de777781a245 100644
--- a/upup/pkg/fi/cloudup/awsup/aws_cloud.go
+++ b/upup/pkg/fi/cloudup/awsup/aws_cloud.go
@@ -1752,12 +1752,13 @@ func (c *awsCloudImplementation) AccountInfo() (string, string, error) {
return arn.AccountID, arn.Partition, nil
}
-// GetRolesInInstanceProfile return role names whic are associated with the instance profile specified by profileName.
+// GetRolesInInstanceProfile return role names which are associated with the instance profile specified by profileName.
func (c *awsCloudImplementation) GetRolesInInstanceProfile(profileName string) ([]string, error) {
return getRolesInInstanceProfile(c, profileName)
}
func getRolesInInstanceProfile(c AWSCloud, profileName string) ([]string, error) {
+ fmt.Printf("[h3poteto debug3] called")
output, err := c.IAM().GetInstanceProfile(&iam.GetInstanceProfileInput{
InstanceProfileName: aws.String(profileName),
})
diff --git a/upup/pkg/fi/cloudup/bootstrapchannelbuilder_test.go b/upup/pkg/fi/cloudup/bootstrapchannelbuilder_test.go
index 406df0bbe0991..26111fc1a776c 100644
--- a/upup/pkg/fi/cloudup/bootstrapchannelbuilder_test.go
+++ b/upup/pkg/fi/cloudup/bootstrapchannelbuilder_test.go
@@ -154,6 +154,7 @@ func runChannelBuilderTest(t *testing.T, key string, addonManifests []string) {
tf := &TemplateFunctions{
KopsModelContext: kopsModel,
+ cloud: cloud,
}
tf.AddTo(templates.TemplateFunctions, secretStore)
diff --git a/upup/pkg/fi/cloudup/template_functions.go b/upup/pkg/fi/cloudup/template_functions.go
index de0172d63d02f..bca5172ee22ee 100644
--- a/upup/pkg/fi/cloudup/template_functions.go
+++ b/upup/pkg/fi/cloudup/template_functions.go
@@ -453,10 +453,13 @@ func (tf *TemplateFunctions) KopsControllerConfig() (string, error) {
// Because the IAM Instance Profile and the IAM Role are created in IAMModelBuilder tasks.
// Therefore, the IAM Role associated with IAM Instance Profile is acquired only when it is not specified.
if ig.Spec.IAM != nil && ig.Spec.IAM.Profile != nil {
+ fmt.Printf("[h3poteto debug1] %+v\n", *profile.Name)
+ fmt.Printf("[h3poteto debug2] %+v\n", tf.cloud)
roles, err := tf.cloud.GetRolesInInstanceProfile(*profile.Name)
if err != nil {
- return "", fmt.Errorf("getting role from profile %s: %v", profile, err)
+ return "", fmt.Errorf("getting role from profile %s: %v", *profile.Name, err)
}
+ fmt.Printf("[h3poteto debug4] %+v\n", roles)
nodesRoles.Insert(roles...)
} else {
// When the IAM Instance Profile is not specified, IAM Instance Profile is created by kOps.