|
| 1 | +package builder |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + _ "embed" |
| 6 | + "fmt" |
| 7 | + "os" |
| 8 | + "strings" |
| 9 | + |
| 10 | + "github.com/aws/aws-sdk-go-v2/service/sts" |
| 11 | + |
| 12 | + "github.com/weaveworks/eksctl/pkg/awsapi" |
| 13 | + cft "github.com/weaveworks/eksctl/pkg/cfn/template" |
| 14 | + "github.com/weaveworks/eksctl/pkg/goformation" |
| 15 | + gfn "github.com/weaveworks/eksctl/pkg/goformation/cloudformation" |
| 16 | + "github.com/weaveworks/eksctl/pkg/goformation/cloudformation/cloudformation" |
| 17 | + gfneks "github.com/weaveworks/eksctl/pkg/goformation/cloudformation/eks" |
| 18 | + "github.com/weaveworks/eksctl/pkg/goformation/cloudformation/lambda" |
| 19 | + gfnt "github.com/weaveworks/eksctl/pkg/goformation/cloudformation/types" |
| 20 | +) |
| 21 | + |
| 22 | +//go:embed templates/beta-resources.yaml |
| 23 | +var betaResourcesTemplate []byte |
| 24 | + |
| 25 | +//go:embed templates/beta.py |
| 26 | +var lambdaBetaPy []byte |
| 27 | + |
| 28 | +func addBetaResources(stsAPI awsapi.STS, stackName string, clusterTemplate *gfn.Template, g *gfneks.Cluster) error { |
| 29 | + |
| 30 | + identity, err := stsAPI.GetCallerIdentity(context.TODO(), &sts.GetCallerIdentityInput{}) |
| 31 | + if err != nil { |
| 32 | + return fmt.Errorf("unable to get identity: %w", err) |
| 33 | + } |
| 34 | + userArn := *identity.Arn |
| 35 | + baseArn := userArn[:strings.LastIndex(userArn, "/")] |
| 36 | + roleArn := fmt.Sprintf("%s%s", baseArn, "/{{SessionName}}") |
| 37 | + iamARN := strings.Replace( |
| 38 | + strings.Replace(baseArn, "assumed-role", "role", 1), |
| 39 | + "sts", "iam", 1) |
| 40 | + |
| 41 | + clusterName := "eksctl-" + stackName + "-cluster" |
| 42 | + |
| 43 | + template, err := goformation.ParseYAML(betaResourcesTemplate) |
| 44 | + if err != nil { |
| 45 | + return err |
| 46 | + } |
| 47 | + for resourceName, resource := range template.Resources { |
| 48 | + clusterTemplate.Resources[resourceName] = resource |
| 49 | + } |
| 50 | + for key, output := range template.Outputs { |
| 51 | + clusterTemplate.Outputs[key] = output |
| 52 | + } |
| 53 | + customResource := clusterTemplate.Resources["ControlPlane"].(*gfn.CustomResource) |
| 54 | + if g.AccessConfig != nil { |
| 55 | + customResource.Properties["AccessConfig"] = g.AccessConfig |
| 56 | + } |
| 57 | + if g.BootstrapSelfManagedAddons != nil { |
| 58 | + customResource.Properties["BootstrapSelfManagedAddons"] = g.BootstrapSelfManagedAddons |
| 59 | + } |
| 60 | + if g.ComputeConfig != nil { |
| 61 | + customResource.Properties["ComputeConfig"] = g.ComputeConfig |
| 62 | + } |
| 63 | + if g.EncryptionConfig != nil { |
| 64 | + customResource.Properties["EncryptionConfig"] = g.EncryptionConfig |
| 65 | + } |
| 66 | + if g.KubernetesNetworkConfig != nil { |
| 67 | + customResource.Properties["KubernetesNetworkConfig"] = g.KubernetesNetworkConfig |
| 68 | + } |
| 69 | + if g.Logging != nil { |
| 70 | + customResource.Properties["Logging"] = g.Logging |
| 71 | + } |
| 72 | + if g.Name != nil { |
| 73 | + customResource.Properties["Name"] = g.Name |
| 74 | + } |
| 75 | + if g.OutpostConfig != nil { |
| 76 | + customResource.Properties["OutpostConfig"] = g.OutpostConfig |
| 77 | + } |
| 78 | + if g.RemoteNetworkConfig != nil { |
| 79 | + customResource.Properties["RemoteNetworkConfig"] = g.RemoteNetworkConfig |
| 80 | + } |
| 81 | + if g.ResourcesVpcConfig != nil { |
| 82 | + customResource.Properties["ResourcesVpcConfig"] = g.ResourcesVpcConfig |
| 83 | + } |
| 84 | + if g.RoleArn != nil { |
| 85 | + customResource.Properties["RoleArn"] = g.RoleArn |
| 86 | + } |
| 87 | + if g.StorageConfig != nil { |
| 88 | + customResource.Properties["StorageConfig"] = g.StorageConfig |
| 89 | + } |
| 90 | + if g.Tags != nil { |
| 91 | + g.Tags = append(g.Tags, cloudformation.Tag{ |
| 92 | + Key: gfnt.NewString("Name"), |
| 93 | + Value: gfnt.NewString(clusterName + "/ControlPlane"), |
| 94 | + }) |
| 95 | + customResource.Properties["Tags"] = g.Tags |
| 96 | + } else { |
| 97 | + customResource.Properties["Tags"] = []cloudformation.Tag{ |
| 98 | + { |
| 99 | + Key: gfnt.NewString("Name"), |
| 100 | + Value: gfnt.NewString(clusterName + "/ControlPlane"), |
| 101 | + }, |
| 102 | + } |
| 103 | + } |
| 104 | + if g.UpgradePolicy != nil { |
| 105 | + customResource.Properties["UpgradePolicy"] = g.UpgradePolicy |
| 106 | + } |
| 107 | + if g.Version != nil { |
| 108 | + customResource.Properties["Version"] = g.Version |
| 109 | + } |
| 110 | + if g.ZonalShiftConfig != nil { |
| 111 | + customResource.Properties["ZonalShiftConfig"] = g.ZonalShiftConfig |
| 112 | + } |
| 113 | + |
| 114 | + customResource.Properties["IAMPrincipalArn"] = gfnt.NewString(iamARN) |
| 115 | + customResource.Properties["STSRoleArn"] = gfnt.NewString(roleArn) |
| 116 | + |
| 117 | + customFunction := clusterTemplate.Resources["CustomEKSFunction"].(*lambda.Function) |
| 118 | + customFunction.Code = &lambda.Function_Code{ |
| 119 | + ZipFile: gfnt.NewString(string(lambdaBetaPy)), |
| 120 | + } |
| 121 | + |
| 122 | + clusterTemplate.Outputs["EKSFunctionArn"] = gfn.Output{ |
| 123 | + Value: gfnt.MakeFnGetAttString("CustomEKSFunction", "Arn"), |
| 124 | + Export: &gfn.Export{ |
| 125 | + Name: gfnt.MakeFnSubString(fmt.Sprintf("${%s}::EKSFunctionArn", gfnt.StackName)), |
| 126 | + }, |
| 127 | + } |
| 128 | + |
| 129 | + clusterTemplate.Parameters["EksEndpointUrl"] = gfn.Parameter{ |
| 130 | + Type: "String", |
| 131 | + Description: "The endpoint URL for the EKS service", |
| 132 | + Default: gfnt.NewString(os.Getenv("AWS_ENDPOINT_URL_EKS")), |
| 133 | + } |
| 134 | + return nil |
| 135 | +} |
| 136 | + |
| 137 | +func addBetaManagedNodeGroupResources(managedResource *gfneks.Nodegroup, stackName string) *gfn.CustomResource { |
| 138 | + customResource := &gfn.CustomResource{ |
| 139 | + Type: "Custom::EksManagedNodeGroup", |
| 140 | + } |
| 141 | + customResource.Properties = make(map[string]interface{}) |
| 142 | + functionArn := gfnt.MakeFnImportValueString(fmt.Sprintf("eksctl-%s-cluster::EKSFunctionArn", stackName)) |
| 143 | + customResource.Properties["ServiceToken"] = functionArn |
| 144 | + |
| 145 | + if managedResource.AmiType != nil { |
| 146 | + customResource.Properties["AmiType"] = managedResource.AmiType |
| 147 | + } |
| 148 | + if managedResource.CapacityType != nil { |
| 149 | + customResource.Properties["CapacityType"] = managedResource.CapacityType |
| 150 | + } |
| 151 | + if managedResource.ClusterName != nil { |
| 152 | + customResource.Properties["ClusterName"] = managedResource.ClusterName |
| 153 | + } |
| 154 | + if managedResource.DiskSize != nil { |
| 155 | + customResource.Properties["DiskSize"] = managedResource.DiskSize |
| 156 | + } |
| 157 | + if managedResource.ForceUpdateEnabled != nil { |
| 158 | + customResource.Properties["ForceUpdateEnabled"] = managedResource.ForceUpdateEnabled |
| 159 | + } |
| 160 | + if managedResource.InstanceTypes != nil { |
| 161 | + customResource.Properties["InstanceTypes"] = managedResource.InstanceTypes |
| 162 | + } |
| 163 | + if managedResource.Labels != nil { |
| 164 | + customResource.Properties["Labels"] = managedResource.Labels |
| 165 | + } |
| 166 | + if managedResource.LaunchTemplate != nil { |
| 167 | + customResource.Properties["LaunchTemplate"] = managedResource.LaunchTemplate |
| 168 | + } |
| 169 | + if managedResource.NodeRepairConfig != nil { |
| 170 | + customResource.Properties["NodeRepairConfig"] = managedResource.NodeRepairConfig |
| 171 | + } |
| 172 | + if managedResource.NodeRole != nil { |
| 173 | + customResource.Properties["NodeRole"] = managedResource.NodeRole |
| 174 | + } |
| 175 | + if managedResource.NodegroupName != nil { |
| 176 | + customResource.Properties["NodegroupName"] = managedResource.NodegroupName |
| 177 | + } |
| 178 | + if managedResource.ReleaseVersion != nil { |
| 179 | + customResource.Properties["ReleaseVersion"] = managedResource.ReleaseVersion |
| 180 | + } |
| 181 | + if managedResource.RemoteAccess != nil { |
| 182 | + customResource.Properties["RemoteAccess"] = managedResource.RemoteAccess |
| 183 | + } |
| 184 | + if managedResource.ScalingConfig != nil { |
| 185 | + customResource.Properties["ScalingConfig"] = managedResource.ScalingConfig |
| 186 | + } |
| 187 | + if managedResource.Subnets != nil { |
| 188 | + customResource.Properties["Subnets"] = managedResource.Subnets |
| 189 | + } |
| 190 | + if managedResource.Tags != nil { |
| 191 | + customResource.Properties["Tags"] = managedResource.Tags |
| 192 | + } |
| 193 | + if managedResource.Taints != nil { |
| 194 | + customResource.Properties["Taints"] = managedResource.Taints |
| 195 | + } |
| 196 | + if managedResource.UpdateConfig != nil { |
| 197 | + customResource.Properties["UpdateConfig"] = managedResource.UpdateConfig |
| 198 | + } |
| 199 | + if managedResource.Version != nil { |
| 200 | + customResource.Properties["Version"] = managedResource.Version |
| 201 | + } |
| 202 | + |
| 203 | + return customResource |
| 204 | +} |
| 205 | + |
| 206 | +func createBetaAssumeRolePolicy() interface{} { |
| 207 | + statements := []cft.MapOfInterfaces{ |
| 208 | + { |
| 209 | + "Effect": "Allow", |
| 210 | + "Principal": cft.MapOfInterfaces{ |
| 211 | + "Service": "eks.amazonaws.com", |
| 212 | + }, |
| 213 | + "Action": []string{ |
| 214 | + "sts:AssumeRole", |
| 215 | + "sts:TagSession", |
| 216 | + }, |
| 217 | + }, |
| 218 | + { |
| 219 | + "Effect": "Allow", |
| 220 | + "Principal": cft.MapOfInterfaces{ |
| 221 | + "Service": "eks-beta.aws.internal", |
| 222 | + }, |
| 223 | + "Action": []string{ |
| 224 | + "sts:AssumeRole", |
| 225 | + "sts:TagSession", |
| 226 | + }, |
| 227 | + }, |
| 228 | + { |
| 229 | + "Effect": "Allow", |
| 230 | + "Principal": cft.MapOfInterfaces{ |
| 231 | + "Service": "eks-gamma.aws.internal", |
| 232 | + }, |
| 233 | + "Action": []string{ |
| 234 | + "sts:AssumeRole", |
| 235 | + "sts:TagSession", |
| 236 | + }, |
| 237 | + }, |
| 238 | + } |
| 239 | + return cft.MakePolicyDocument(statements...) |
| 240 | +} |
| 241 | + |
| 242 | +func addBetaAccessEntry(stackName string, accessEntryType string) *gfn.CustomResource { |
| 243 | + customResource := &gfn.CustomResource{ |
| 244 | + Type: "Custom::EksAccessEntry", |
| 245 | + } |
| 246 | + customResource.Properties = make(map[string]interface{}) |
| 247 | + functionArn := gfnt.MakeFnImportValueString(fmt.Sprintf("eksctl-%s-cluster::EKSFunctionArn", stackName)) |
| 248 | + customResource.Properties["ServiceToken"] = functionArn |
| 249 | + customResource.Properties["PrincipalArn"] = gfnt.MakeFnGetAttString(cfnIAMInstanceRoleName, "Arn") |
| 250 | + customResource.Properties["ClusterName"] = gfnt.NewString(stackName) |
| 251 | + customResource.Properties["Type"] = gfnt.NewString(accessEntryType) |
| 252 | + return customResource |
| 253 | +} |
0 commit comments