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

OCM-2373 | feat: Moved associated to GetRole functions to common folder #1547

Merged
merged 2 commits into from
Oct 19, 2023
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 14 additions & 12 deletions cmd/create/accountroles/creators.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
awscb "github.com/openshift/rosa/pkg/aws/commandbuilder"
"github.com/openshift/rosa/pkg/aws/tags"
"github.com/openshift/rosa/pkg/rosa"

common "github.com/openshift-online/ocm-common/pkg/aws/validations"
)

type creator interface {
Expand Down Expand Up @@ -73,7 +75,7 @@ func (mp *managedPoliciesCreator) createRoles(r *rosa.Runtime, input *accountRol
r.Reporter.Infof("Creating classic account roles using '%s'", r.Creator.ARN)

for file, role := range aws.AccountRoles {
accRoleName := aws.GetRoleName(input.prefix, role.Name)
accRoleName := common.GetRoleName(input.prefix, role.Name)
assumeRolePolicy := getAssumeRolePolicy(file, input)

r.Reporter.Debugf("Creating role '%s'", accRoleName)
Expand Down Expand Up @@ -117,7 +119,7 @@ func attachManagedPolicies(r *rosa.Runtime, input *accountRolesCreationInput, ro
func (mp *managedPoliciesCreator) printCommands(r *rosa.Runtime, input *accountRolesCreationInput) error {
commands := []string{}
for file, role := range aws.AccountRoles {
accRoleName := aws.GetRoleName(input.prefix, role.Name)
accRoleName := common.GetRoleName(input.prefix, role.Name)
iamTags := mp.getRoleTags(file, input)

createRole := buildCreateRoleCommand(accRoleName, file, iamTags, input)
Expand All @@ -143,7 +145,7 @@ func (mp *managedPoliciesCreator) printCommands(r *rosa.Runtime, input *accountR

func (mp *managedPoliciesCreator) getRoleTags(roleType string, input *accountRolesCreationInput) map[string]string {
tagsList := getBaseRoleTags(roleType, input)
tagsList[tags.ManagedPolicies] = tags.True
tagsList[common.ManagedPolicies] = tags.True

return tagsList
}
Expand All @@ -154,7 +156,7 @@ func (up *unmanagedPoliciesCreator) createRoles(r *rosa.Runtime, input *accountR
r.Reporter.Infof("Creating classic account roles using '%s'", r.Creator.ARN)

for file, role := range aws.AccountRoles {
accRoleName := aws.GetRoleName(input.prefix, role.Name)
accRoleName := common.GetRoleName(input.prefix, role.Name)
assumeRolePolicy := getAssumeRolePolicy(file, input)
tagsList := up.getRoleTags(file, input)
filename := fmt.Sprintf("sts_%s_permission_policy", file)
Expand All @@ -171,7 +173,7 @@ func (up *unmanagedPoliciesCreator) createRoles(r *rosa.Runtime, input *accountR
func (up *unmanagedPoliciesCreator) printCommands(r *rosa.Runtime, input *accountRolesCreationInput) error {
commands := []string{}
for file, role := range aws.AccountRoles {
accRoleName := aws.GetRoleName(input.prefix, role.Name)
accRoleName := common.GetRoleName(input.prefix, role.Name)
iamTags := up.getRoleTags(file, input)

createRole := buildCreateRoleCommand(accRoleName, file, iamTags, input)
Expand Down Expand Up @@ -277,7 +279,7 @@ func (hcp *hcpManagedPoliciesCreator) createRoles(r *rosa.Runtime, input *accoun
r.Reporter.Infof("Creating hosted CP account roles using '%s'", r.Creator.ARN)

for file, role := range aws.HCPAccountRoles {
accRoleName := aws.GetRoleName(input.prefix, role.Name)
accRoleName := common.GetRoleName(input.prefix, role.Name)
assumeRolePolicy := getAssumeRolePolicy(file, input)

r.Reporter.Debugf("Creating role '%s'", accRoleName)
Expand Down Expand Up @@ -308,7 +310,7 @@ func (hcp *hcpManagedPoliciesCreator) createRoles(r *rosa.Runtime, input *accoun
func (hcp *hcpManagedPoliciesCreator) printCommands(r *rosa.Runtime, input *accountRolesCreationInput) error {
commands := []string{}
for file, role := range aws.HCPAccountRoles {
accRoleName := aws.GetRoleName(input.prefix, role.Name)
accRoleName := common.GetRoleName(input.prefix, role.Name)
iamTags := hcp.getRoleTags(file, input)

createRole := buildCreateRoleCommand(accRoleName, file, iamTags, input)
Expand All @@ -331,18 +333,18 @@ func (hcp *hcpManagedPoliciesCreator) printCommands(r *rosa.Runtime, input *acco

func (hcp *hcpManagedPoliciesCreator) getRoleTags(roleType string, input *accountRolesCreationInput) map[string]string {
tagsList := getBaseRoleTags(roleType, input)
tagsList[tags.ManagedPolicies] = tags.True
tagsList[common.ManagedPolicies] = tags.True
tagsList[tags.HypershiftPolicies] = tags.True

return tagsList
}

func getBaseRoleTags(roleType string, input *accountRolesCreationInput) map[string]string {
return map[string]string{
tags.OpenShiftVersion: input.defaultPolicyVersion,
tags.RolePrefix: input.prefix,
tags.RoleType: roleType,
tags.RedHatManaged: tags.True,
common.OpenShiftVersion: input.defaultPolicyVersion,
tags.RolePrefix: input.prefix,
tags.RoleType: roleType,
tags.RedHatManaged: tags.True,
}
}

Expand Down
5 changes: 3 additions & 2 deletions cmd/create/machinepool/machinepool.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

awssdk "github.com/aws/aws-sdk-go/aws"
"github.com/briandowns/spinner"
"github.com/openshift-online/ocm-common/pkg"
diskValidator "github.com/openshift-online/ocm-common/pkg/machinepool/validations"
cmv1 "github.com/openshift-online/ocm-sdk-go/clustersmgmt/v1"
"github.com/openshift/rosa/pkg/helper"
Expand Down Expand Up @@ -429,7 +430,7 @@ func addMachinePool(cmd *cobra.Command, clusterKey string, cluster *cmv1.Cluster
os.Exit(1)
}
if spotMaxPrice != "on-demand" {
price, _ := strconv.ParseFloat(spotMaxPrice, 64)
price, _ := strconv.ParseFloat(spotMaxPrice, pkg.MaxByteSize)
maxPrice = &price
}

Expand Down Expand Up @@ -609,7 +610,7 @@ func spotMaxPriceValidator(val interface{}) error {
if spotMaxPrice == "on-demand" {
return nil
}
price, err := strconv.ParseFloat(spotMaxPrice, 64)
price, err := strconv.ParseFloat(spotMaxPrice, pkg.MaxByteSize)
if err != nil {
return fmt.Errorf("Expected a numeric value for spot max price")
}
Expand Down
5 changes: 3 additions & 2 deletions cmd/create/ocmrole/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import (
"github.com/openshift/rosa/pkg/ocm"
"github.com/openshift/rosa/pkg/rosa"

common "github.com/openshift-online/ocm-common/pkg/aws/validations"
cmv1 "github.com/openshift-online/ocm-sdk-go/clustersmgmt/v1"
)

Expand Down Expand Up @@ -343,7 +344,7 @@ func buildCommands(prefix string, roleName string, rolePath string, permissionsB
tags.RedHatManaged: tags.True,
}
if managedPolicies {
iamTags[tags.ManagedPolicies] = tags.True
iamTags[common.ManagedPolicies] = tags.True
}

adminTags := map[string]string{
Expand Down Expand Up @@ -478,7 +479,7 @@ func createRoles(r *rosa.Runtime, prefix string, roleName string, rolePath strin
tags.RedHatManaged: tags.True,
}
if managedPolicies {
iamTags[tags.ManagedPolicies] = tags.True
iamTags[common.ManagedPolicies] = tags.True
}

if !exists {
Expand Down
25 changes: 13 additions & 12 deletions cmd/create/operatorroles/by_clusterkey.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"os"
"strings"

common "github.com/openshift-online/ocm-common/pkg/aws/validations"
cmv1 "github.com/openshift-online/ocm-sdk-go/clustersmgmt/v1"
"github.com/openshift/rosa/pkg/aws"
awscb "github.com/openshift/rosa/pkg/aws/commandbuilder"
Expand Down Expand Up @@ -197,11 +198,11 @@ func createRoles(r *rosa.Runtime,
}

operatorPolicyTags := map[string]string{
tags.OpenShiftVersion: accountRoleVersion,
tags.RolePrefix: prefix,
tags.RedHatManaged: helper.True,
tags.OperatorNamespace: operator.Namespace(),
tags.OperatorName: operator.Name(),
common.OpenShiftVersion: accountRoleVersion,
tags.RolePrefix: prefix,
tags.RedHatManaged: helper.True,
tags.OperatorNamespace: operator.Namespace(),
tags.OperatorName: operator.Name(),
}

if args.forcePolicyCreation || (isSharedVpc && credrequest == aws.IngressOperatorCloudCredentialsRoleType) {
Expand Down Expand Up @@ -235,7 +236,7 @@ func createRoles(r *rosa.Runtime,
tagsList[tags.ClusterID] = cluster.ID()
}
if managedPolicies {
tagsList[tags.ManagedPolicies] = helper.True
tagsList[common.ManagedPolicies] = helper.True
}
if hostedCPPolicies {
tagsList[tags.HypershiftPolicies] = helper.True
Expand Down Expand Up @@ -305,11 +306,11 @@ func buildCommands(r *rosa.Runtime, env string,
policyARN = computePolicyARN(r.Creator.AccountID, prefix, operator.Namespace(), operator.Name(), path)
name := aws.GetOperatorPolicyName(prefix, operator.Namespace(), operator.Name())
iamTags := map[string]string{
tags.OpenShiftVersion: defaultPolicyVersion,
tags.RolePrefix: prefix,
tags.OperatorNamespace: operator.Namespace(),
tags.OperatorName: operator.Name(),
tags.RedHatManaged: helper.True,
common.OpenShiftVersion: defaultPolicyVersion,
tags.RolePrefix: prefix,
tags.OperatorNamespace: operator.Namespace(),
tags.OperatorName: operator.Name(),
tags.RedHatManaged: helper.True,
}
operatorPolicyKey := aws.GetOperatorPolicyKey(credrequest, hostedCPPolicies, isSharedVpc)
fileName := fmt.Sprintf("file://%s.json", operatorPolicyKey)
Expand Down Expand Up @@ -361,7 +362,7 @@ func buildCommands(r *rosa.Runtime, env string,
iamTags[tags.ClusterID] = cluster.ID()
}
if managedPolicies {
iamTags[tags.ManagedPolicies] = helper.True
iamTags[common.ManagedPolicies] = helper.True
}
if hostedCPPolicies {
iamTags[tags.HypershiftPolicies] = helper.True
Expand Down
25 changes: 13 additions & 12 deletions cmd/create/operatorroles/by_prefix.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
cmv1 "github.com/openshift-online/ocm-sdk-go/clustersmgmt/v1"
"github.com/spf13/cobra"

common "github.com/openshift-online/ocm-common/pkg/aws/validations"
"github.com/openshift/rosa/pkg/aws"
awscb "github.com/openshift/rosa/pkg/aws/commandbuilder"
"github.com/openshift/rosa/pkg/aws/tags"
Expand Down Expand Up @@ -312,11 +313,11 @@ func createRolesByPrefix(r *rosa.Runtime, prefix string, permissionsBoundary str
}

operatorPolicyTags := map[string]string{
tags.OpenShiftVersion: defaultPolicyVersion,
tags.RolePrefix: prefix,
tags.RedHatManaged: helper.True,
tags.OperatorNamespace: operator.Namespace(),
tags.OperatorName: operator.Name(),
common.OpenShiftVersion: defaultPolicyVersion,
tags.RolePrefix: prefix,
tags.RedHatManaged: helper.True,
tags.OperatorNamespace: operator.Namespace(),
tags.OperatorName: operator.Name(),
}

if args.forcePolicyCreation || (isSharedVpc && credrequest == aws.IngressOperatorCloudCredentialsRoleType) {
Expand Down Expand Up @@ -348,7 +349,7 @@ func createRolesByPrefix(r *rosa.Runtime, prefix string, permissionsBoundary str
tags.RedHatManaged: helper.True,
}
if managedPolicies {
tagsList[tags.ManagedPolicies] = helper.True
tagsList[common.ManagedPolicies] = helper.True
}
if hostedCPPolicies {
tagsList[tags.HypershiftPolicies] = helper.True
Expand Down Expand Up @@ -407,11 +408,11 @@ func buildCommandsFromPrefix(r *rosa.Runtime, env string,
policyARN = computePolicyARN(r.Creator.AccountID, prefix, operator.Namespace(), operator.Name(), path)
name := aws.GetOperatorPolicyName(prefix, operator.Namespace(), operator.Name())
iamTags := map[string]string{
tags.OpenShiftVersion: defaultPolicyVersion,
tags.RolePrefix: prefix,
tags.OperatorNamespace: operator.Namespace(),
tags.OperatorName: operator.Name(),
tags.RedHatManaged: helper.True,
common.OpenShiftVersion: defaultPolicyVersion,
tags.RolePrefix: prefix,
tags.OperatorNamespace: operator.Namespace(),
tags.OperatorName: operator.Name(),
tags.RedHatManaged: helper.True,
}
operatorPolicyKey := aws.GetOperatorPolicyKey(credrequest, hostedCPPolicies, isSharedVpc)
fileName := fmt.Sprintf("file://%s.json", operatorPolicyKey)
Expand Down Expand Up @@ -461,7 +462,7 @@ func buildCommandsFromPrefix(r *rosa.Runtime, env string,
tags.RedHatManaged: helper.True,
}
if managedPolicies {
iamTags[tags.ManagedPolicies] = helper.True
iamTags[common.ManagedPolicies] = helper.True
}
if hostedCPPolicies {
iamTags[tags.HypershiftPolicies] = helper.True
Expand Down
5 changes: 3 additions & 2 deletions cmd/create/operatorroles/common_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package operatorroles
import (
"fmt"

"github.com/openshift-online/ocm-common/pkg"
"github.com/openshift/rosa/pkg/aws"
"github.com/openshift/rosa/pkg/rosa"
errors "github.com/zgalor/weberr"
Expand All @@ -15,8 +16,8 @@ func computePolicyARN(accountID string, prefix string, namespace string, name st
prefix = aws.DefaultPrefix
}
policy := fmt.Sprintf("%s-%s-%s", prefix, namespace, name)
if len(policy) > 64 {
policy = policy[0:64]
if len(policy) > pkg.MaxByteSize {
policy = policy[0:pkg.MaxByteSize]
}
if path != "" {
return fmt.Sprintf("arn:%s:iam::%s:policy%s%s", aws.GetPartition(), accountID, path, policy)
Expand Down
3 changes: 2 additions & 1 deletion cmd/edit/autoscaler/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
cmv1 "github.com/openshift-online/ocm-sdk-go/clustersmgmt/v1"
"github.com/spf13/cobra"

"github.com/openshift-online/ocm-common/pkg"
"github.com/openshift/rosa/pkg/clusterautoscaler"
"github.com/openshift/rosa/pkg/interactive"
"github.com/openshift/rosa/pkg/ocm"
Expand Down Expand Up @@ -126,7 +127,7 @@ func run(cmd *cobra.Command, _ []string) {
autoscalerArgs.ScaleDown.DelayAfterDelete = autoscaler.ScaleDown().DelayAfterDelete()
autoscalerArgs.ScaleDown.DelayAfterFailure = autoscaler.ScaleDown().DelayAfterFailure()

utilizationThreshold, err := strconv.ParseFloat(autoscaler.ScaleDown().UtilizationThreshold(), 64)
utilizationThreshold, err := strconv.ParseFloat(autoscaler.ScaleDown().UtilizationThreshold(), pkg.MaxByteSize)
if err != nil {
r.Reporter.Errorf("Failed updating autoscaler configuration for cluster '%s': %s",
cluster.ID(), err)
Expand Down
5 changes: 3 additions & 2 deletions cmd/install/addon/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
"github.com/spf13/cobra"
errors "github.com/zgalor/weberr"

"github.com/openshift-online/ocm-common/pkg"
"github.com/openshift/rosa/pkg/arguments"
"github.com/openshift/rosa/pkg/aws"
"github.com/openshift/rosa/pkg/aws/tags"
Expand Down Expand Up @@ -358,8 +359,8 @@ func buildCommand(

func generateRoleName(cr *cmv1.CredentialRequest, prefix string) string {
roleName := fmt.Sprintf("%s-%s-%s", prefix, cr.Namespace(), cr.Name())
if len(roleName) > 64 {
roleName = roleName[0:64]
if len(roleName) > pkg.MaxByteSize {
roleName = roleName[0:pkg.MaxByteSize]
}
return roleName
}
16 changes: 9 additions & 7 deletions cmd/upgrade/accountroles/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ import (
"github.com/openshift/rosa/pkg/ocm"
rprtr "github.com/openshift/rosa/pkg/reporter"
"github.com/openshift/rosa/pkg/rosa"

common "github.com/openshift-online/ocm-common/pkg/aws/validations"
)

var args struct {
Expand Down Expand Up @@ -285,7 +287,7 @@ func LogError(key string, ocmClient *ocm.Client, defaultPolicyVersion string, er
func upgradeAccountRolePolicies(reporter *rprtr.Object, awsClient aws.Client, prefix string, accountID string,
policies map[string]*cmv1.AWSSTSPolicy, policyVersion string, policyPath string, isVersionChosen bool) error {
for file, role := range aws.AccountRoles {
roleName := aws.GetRoleName(prefix, role.Name)
roleName := common.GetRoleName(prefix, role.Name)
promptString := fmt.Sprintf("Upgrade the '%s' role policy latest version ?", roleName)
if isVersionChosen {
promptString = fmt.Sprintf("Upgrade the '%s' role policy to version '%s' ?", roleName, policyVersion)
Expand All @@ -299,10 +301,10 @@ func upgradeAccountRolePolicies(reporter *rprtr.Object, awsClient aws.Client, pr
policyDetails := aws.GetPolicyDetails(policies, filename)
policyARN, err := awsClient.EnsurePolicy(policyARN, policyDetails,
policyVersion, map[string]string{
tags.OpenShiftVersion: policyVersion,
tags.RolePrefix: prefix,
tags.RoleType: file,
tags.RedHatManaged: "true",
common.OpenShiftVersion: policyVersion,
tags.RolePrefix: prefix,
tags.RoleType: file,
tags.RedHatManaged: "true",
}, policyPath)
if err != nil {
return err
Expand Down Expand Up @@ -335,7 +337,7 @@ func buildCommands(prefix string, accountID string, isUpgradeNeedForAccountRoleP
commands := []string{}
if isUpgradeNeedForAccountRolePolicies {
for file, role := range aws.AccountRoles {
accRoleName := aws.GetRoleName(prefix, role.Name)
accRoleName := common.GetRoleName(prefix, role.Name)
policyARN := aws.GetPolicyARN(accountID, accRoleName, policyPath)
_, err := awsClient.IsPolicyExists(policyARN)
hasPolicy := err == nil
Expand Down Expand Up @@ -363,7 +365,7 @@ func buildCommands(prefix string, accountID string, isUpgradeNeedForAccountRoleP

func getAccountPolicyPath(awsClient aws.Client, prefix string) (string, error) {
for _, accountRole := range aws.AccountRoles {
accRoleName := aws.GetRoleName(prefix, accountRole.Name)
accRoleName := common.GetRoleName(prefix, accountRole.Name)
rolePolicies, err := awsClient.GetAttachedPolicy(&accRoleName)
if err != nil {
return "", err
Expand Down
3 changes: 2 additions & 1 deletion cmd/upgrade/cluster/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"strconv"
"strings"

"github.com/openshift-online/ocm-common/pkg"
cmv1 "github.com/openshift-online/ocm-sdk-go/clustersmgmt/v1"
"github.com/openshift/rosa/cmd/upgrade/roles"
"github.com/openshift/rosa/pkg/aws"
Expand Down Expand Up @@ -548,7 +549,7 @@ func buildNodeDrainGracePeriod(r *rosa.Runtime, cmd *cobra.Command, cluster *cmv
os.Exit(1)
}
nodeDrainParsed := strings.Split(nodeDrainGracePeriod, " ")
nodeDrainValue, err := strconv.ParseFloat(nodeDrainParsed[0], 64)
nodeDrainValue, err := strconv.ParseFloat(nodeDrainParsed[0], pkg.MaxByteSize)
if err != nil {
r.Reporter.Errorf("Expected a valid node drain grace period: %s", err)
os.Exit(1)
Expand Down
Loading