Skip to content

Commit

Permalink
Remove redundant code
Browse files Browse the repository at this point in the history
  • Loading branch information
cPu1 committed Feb 11, 2021
1 parent 1d92b70 commit ea15d83
Showing 1 changed file with 22 additions and 56 deletions.
78 changes: 22 additions & 56 deletions pkg/cfn/builder/managed_launch_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,43 +46,7 @@ func (m *ManagedNodeGroupResourceSet) makeLaunchTemplateData() (*gfnec2.LaunchTe
if mng.SSH != nil && api.IsSetAndNonEmptyString(mng.SSH.PublicKeyName) {
launchTemplateData.KeyName = gfnt.NewString(*mng.SSH.PublicKeyName)

var sgIngressRules []gfnec2.SecurityGroup_Ingress
if *mng.SSH.Allow {
if len(mng.SSH.SourceSecurityGroupIDs) > 0 {
for _, sgID := range mng.SSH.SourceSecurityGroupIDs {
sgIngressRules = append(sgIngressRules, gfnec2.SecurityGroup_Ingress{
FromPort: sgPortSSH,
ToPort: sgPortSSH,
IpProtocol: sgProtoTCP,
SourceSecurityGroupId: gfnt.NewString(sgID),
})
}
} else {
makeSSHIngress := func(cidrIP *gfnt.Value) gfnec2.SecurityGroup_Ingress {
return gfnec2.SecurityGroup_Ingress{
FromPort: sgPortSSH,
ToPort: sgPortSSH,
IpProtocol: sgProtoTCP,
CidrIp: cidrIP,
}
}

if mng.PrivateNetworking {
allInternalIPv4 := gfnt.NewString(m.clusterConfig.VPC.CIDR.String())
sgIngressRules = []gfnec2.SecurityGroup_Ingress{makeSSHIngress(allInternalIPv4)}
} else {
sgIngressRules = []gfnec2.SecurityGroup_Ingress{
makeSSHIngress(sgSourceAnywhereIPv4),
{
FromPort: sgPortSSH,
ToPort: sgPortSSH,
IpProtocol: sgProtoTCP,
CidrIpv6: sgSourceAnywhereIPv6,
},
}
}
}

sshRef := m.newResource("SSH", &gfnec2.SecurityGroup{
GroupName: gfnt.MakeFnSubString(fmt.Sprintf("${%s}-remoteAccess", gfnt.StackName)),
VpcId: makeImportValue(m.clusterStackName, outputs.ClusterVPC),
Expand Down Expand Up @@ -142,7 +106,6 @@ func (m *ManagedNodeGroupResourceSet) makeLaunchTemplateData() (*gfnec2.LaunchTe
}

func makeSSHIngressRules(n *api.NodeGroupBase, vpcCIDR, description string) []gfnec2.SecurityGroup_Ingress {
allInternalIPv4 := gfnt.NewString(vpcCIDR)
var sgIngressRules []gfnec2.SecurityGroup_Ingress
if *n.SSH.Allow {
if len(n.SSH.SourceSecurityGroupIDs) > 0 {
Expand All @@ -155,28 +118,31 @@ func makeSSHIngressRules(n *api.NodeGroupBase, vpcCIDR, description string) []gf
})
}
} else {
if n.PrivateNetworking {
sgIngressRules = append(sgIngressRules, gfnec2.SecurityGroup_Ingress{
CidrIp: allInternalIPv4,
Description: gfnt.NewString("Allow SSH access to " + description + " (private, only inside VPC)"),
IpProtocol: sgProtoTCP,
makeSSHIngress := func(cidrIP *gfnt.Value, sshDesc string) gfnec2.SecurityGroup_Ingress {
return gfnec2.SecurityGroup_Ingress{
FromPort: sgPortSSH,
ToPort: sgPortSSH,
})
} else {
sgIngressRules = append(sgIngressRules, gfnec2.SecurityGroup_Ingress{
CidrIp: sgSourceAnywhereIPv4,
Description: gfnt.NewString("Allow SSH access to " + description),
IpProtocol: sgProtoTCP,
FromPort: sgPortSSH,
ToPort: sgPortSSH,
}, gfnec2.SecurityGroup_Ingress{
CidrIpv6: sgSourceAnywhereIPv6,
Description: gfnt.NewString("Allow SSH access to " + description),
IpProtocol: sgProtoTCP,
FromPort: sgPortSSH,
ToPort: sgPortSSH,
})
CidrIp: cidrIP,
Description: gfnt.NewString(sshDesc),
}
}

sshDesc := "Allow SSH access to " + description

if n.PrivateNetworking {
allInternalIPv4 := gfnt.NewString(vpcCIDR)
sgIngressRules = []gfnec2.SecurityGroup_Ingress{makeSSHIngress(allInternalIPv4, sshDesc+" (private, only inside VPC)")}
} else {
sgIngressRules = append(sgIngressRules,
makeSSHIngress(sgSourceAnywhereIPv4, sshDesc),
gfnec2.SecurityGroup_Ingress{
CidrIpv6: sgSourceAnywhereIPv6,
Description: gfnt.NewString(sshDesc),
IpProtocol: sgProtoTCP,
FromPort: sgPortSSH,
ToPort: sgPortSSH,
})
}
}
}
Expand Down

0 comments on commit ea15d83

Please sign in to comment.