@@ -17,19 +17,14 @@ limitations under the License.
17
17
package bootstrap
18
18
19
19
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"
26
21
)
27
22
28
23
// PolicyName defines the name of a managed IAM policy.
29
24
type PolicyName string
30
25
31
26
// 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 }
33
28
34
29
// IsValid will check if a given policy name is valid. That is, it will check if the given policy name is
35
30
// one of the ManagedIAMPolicyNames.
@@ -42,49 +37,21 @@ func (p PolicyName) IsValid() bool {
42
37
return false
43
38
}
44
39
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 ()
54
43
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 ()
62
45
}
63
46
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 ()
73
50
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
83
55
}
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
90
57
}
0 commit comments