Skip to content

Commit 60649f3

Browse files
authored
Merge pull request #5654 from dlipovetsky/fix-print-policy
🐛 Make print-policy consistent with print-cloudformation-template
2 parents c02551b + cde8f38 commit 60649f3

File tree

2 files changed

+28
-60
lines changed

2 files changed

+28
-60
lines changed

cmd/clusterawsadm/cloudformation/bootstrap/iam.go

Lines changed: 14 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,14 @@ limitations under the License.
1717
package bootstrap
1818

1919
import (
20-
"fmt"
21-
"os"
22-
"path"
23-
24-
"sigs.k8s.io/cluster-api-provider-aws/v2/cmd/clusterawsadm/converters"
25-
iamv1 "sigs.k8s.io/cluster-api-provider-aws/v2/iam/api/v1beta1"
20+
"github.com/awslabs/goformation/v4/cloudformation/iam"
2621
)
2722

2823
// PolicyName defines the name of a managed IAM policy.
2924
type PolicyName string
3025

3126
// ManagedIAMPolicyNames slice of managed IAM policies.
32-
var ManagedIAMPolicyNames = [5]PolicyName{ControllersPolicy, ControllersPolicyEKS, ControlPlanePolicy, NodePolicy, CSIPolicy}
27+
var ManagedIAMPolicyNames = []PolicyName{ControllersPolicy, ControllersPolicyEKS, ControlPlanePolicy, NodePolicy, CSIPolicy}
3328

3429
// IsValid will check if a given policy name is valid. That is, it will check if the given policy name is
3530
// one of the ManagedIAMPolicyNames.
@@ -42,49 +37,21 @@ func (p PolicyName) IsValid() bool {
4237
return false
4338
}
4439

45-
// GenerateManagedIAMPolicyDocuments generates JSON representation of policy documents for all ManagedIAMPolicy.
46-
func (t Template) GenerateManagedIAMPolicyDocuments(policyDocDir string) error {
47-
for _, pn := range ManagedIAMPolicyNames {
48-
pd := t.GetPolicyDocFromPolicyName(pn)
49-
50-
pds, err := converters.IAMPolicyDocumentToJSON(*pd)
51-
if err != nil {
52-
return fmt.Errorf("failed to marshal policy document for ManagedIAMPolicy %q: %w", pn, err)
53-
}
40+
// RenderManagedIAMPolicies returns all the managed IAM Policies that would be rendered by the template.
41+
func (t Template) RenderManagedIAMPolicies() map[string]*iam.ManagedPolicy {
42+
cft := t.RenderCloudFormation()
5443

55-
fn := path.Join(policyDocDir, fmt.Sprintf("%s.json", pn))
56-
err = os.WriteFile(fn, []byte(pds), 0o600)
57-
if err != nil {
58-
return fmt.Errorf("failed to generate policy document for ManagedIAMPolicy %q: %w", pn, err)
59-
}
60-
}
61-
return nil
44+
return cft.GetAllIAMManagedPolicyResources()
6245
}
6346

64-
func (t Template) policyFunctionMap() map[PolicyName]func() *iamv1.PolicyDocument {
65-
return map[PolicyName]func() *iamv1.PolicyDocument{
66-
ControlPlanePolicy: t.cloudProviderControlPlaneAwsPolicy,
67-
ControllersPolicy: t.ControllersPolicy,
68-
ControllersPolicyEKS: t.ControllersPolicyEKS,
69-
NodePolicy: t.cloudProviderNodeAwsPolicy,
70-
CSIPolicy: t.csiControllerPolicy,
71-
}
72-
}
47+
// RenderManagedIAMPolicy returns a specific managed IAM Policy by name, or nil if the policy is not found.
48+
func (t Template) RenderManagedIAMPolicy(name PolicyName) *iam.ManagedPolicy {
49+
cft := t.RenderCloudFormation()
7350

74-
// PrintPolicyDocs prints the JSON representation of policy documents for all ManagedIAMPolicy.
75-
func (t Template) PrintPolicyDocs() error {
76-
for _, name := range ManagedIAMPolicyNames {
77-
policyDoc := t.GetPolicyDocFromPolicyName(name)
78-
value, err := converters.IAMPolicyDocumentToJSON(*policyDoc)
79-
if err != nil {
80-
return err
81-
}
82-
fmt.Println(name, value)
51+
p, err := cft.GetIAMManagedPolicyWithName(string(name))
52+
if err != nil {
53+
// Return error only if the policy is not found.
54+
return nil
8355
}
84-
return nil
85-
}
86-
87-
// GetPolicyDocFromPolicyName returns a Template's policy document.
88-
func (t Template) GetPolicyDocFromPolicyName(policyName PolicyName) *iamv1.PolicyDocument {
89-
return t.policyFunctionMap()[policyName]()
56+
return p
9057
}

cmd/clusterawsadm/cmd/bootstrap/iam/iam_doc.go

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,13 @@ package iam
1818

1919
import (
2020
"fmt"
21+
"os"
2122

2223
"github.com/spf13/cobra"
2324
"k8s.io/kubectl/pkg/util/templates"
2425

2526
"sigs.k8s.io/cluster-api-provider-aws/v2/cmd/clusterawsadm/cloudformation/bootstrap"
26-
"sigs.k8s.io/cluster-api-provider-aws/v2/cmd/clusterawsadm/converters"
27+
cmdout "sigs.k8s.io/cluster-api-provider-aws/v2/cmd/clusterawsadm/printers"
2728
)
2829

2930
var errInvalidDocumentName = fmt.Errorf("invalid document name, use one of: %+v", bootstrap.ManagedIAMPolicyNames)
@@ -53,31 +54,31 @@ func printPolicyCmd() *cobra.Command {
5354
clusterawsadm bootstrap iam print-policy --document AWSIAMManagedPolicyCloudProviderNodes
5455
5556
# Print out the IAM policy for the Kubernetes AWS EBS CSI Driver Controller.
56-
clusterawsadm bootstrap iam print-policy --document AWSEBSCSIPolicyController
57+
# Note that this is available only when 'spec.controlPlane.enableCSIPolicy' is set to 'true' in the configuration file.
58+
clusterawsadm bootstrap iam print-policy --document AWSEBSCSIPolicyControllerc
5759
`),
5860
Args: cobra.NoArgs,
5961
RunE: func(cmd *cobra.Command, args []string) error {
60-
template, err := getBootstrapTemplate(cmd)
62+
printer, err := cmdout.New("json", os.Stdout)
6163
if err != nil {
62-
return err
64+
return fmt.Errorf("failed creating output printer: %w", err)
6365
}
6466

65-
policyName, err := getDocumentName(cmd)
67+
t, err := getBootstrapTemplate(cmd)
6668
if err != nil {
6769
return err
6870
}
6971

70-
if policyName == "" {
71-
return template.PrintPolicyDocs()
72-
}
73-
74-
policyDocument := template.GetPolicyDocFromPolicyName(policyName)
75-
str, err := converters.IAMPolicyDocumentToJSON(*policyDocument)
72+
specificPolicyName, err := getPolicyName(cmd)
7673
if err != nil {
7774
return err
7875
}
76+
if specificPolicyName != "" {
77+
printer.Print(t.RenderManagedIAMPolicy(specificPolicyName))
78+
return nil
79+
}
7980

80-
fmt.Println(str)
81+
printer.Print(t.RenderManagedIAMPolicies())
8182
return nil
8283
},
8384
}
@@ -86,7 +87,7 @@ func printPolicyCmd() *cobra.Command {
8687
return newCmd
8788
}
8889

89-
func getDocumentName(cmd *cobra.Command) (bootstrap.PolicyName, error) {
90+
func getPolicyName(cmd *cobra.Command) (bootstrap.PolicyName, error) {
9091
val := bootstrap.PolicyName(cmd.Flags().Lookup("document").Value.String())
9192

9293
if val == "" {

0 commit comments

Comments
 (0)