mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-31 07:20:13 +00:00
Merge pull request #83446 from Elias481/fix-pr-49445
Fix behaivour of aws-load-balancer-security-groups annotation
This commit is contained in:
commit
6b13befdfb
@ -3037,11 +3037,6 @@ func isEqualUserGroupPair(l, r *ec2.UserIdGroupPair, compareGroupUserIDs bool) b
|
|||||||
// Returns true if and only if changes were made
|
// Returns true if and only if changes were made
|
||||||
// The security group must already exist
|
// The security group must already exist
|
||||||
func (c *Cloud) setSecurityGroupIngress(securityGroupID string, permissions IPPermissionSet) (bool, error) {
|
func (c *Cloud) setSecurityGroupIngress(securityGroupID string, permissions IPPermissionSet) (bool, error) {
|
||||||
// We do not want to make changes to the Global defined SG
|
|
||||||
if securityGroupID == c.cfg.Global.ElbSecurityGroup {
|
|
||||||
return false, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
group, err := c.findSecurityGroup(securityGroupID)
|
group, err := c.findSecurityGroup(securityGroupID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
klog.Warningf("Error retrieving security group %q", err)
|
klog.Warningf("Error retrieving security group %q", err)
|
||||||
@ -3533,19 +3528,18 @@ func getSGListFromAnnotation(annotatedSG string) []string {
|
|||||||
// Extra groups can be specified via annotation, as can extra tags for any
|
// Extra groups can be specified via annotation, as can extra tags for any
|
||||||
// new groups. The annotation "ServiceAnnotationLoadBalancerSecurityGroups" allows for
|
// new groups. The annotation "ServiceAnnotationLoadBalancerSecurityGroups" allows for
|
||||||
// setting the security groups specified.
|
// setting the security groups specified.
|
||||||
func (c *Cloud) buildELBSecurityGroupList(serviceName types.NamespacedName, loadBalancerName string, annotations map[string]string) ([]string, error) {
|
func (c *Cloud) buildELBSecurityGroupList(serviceName types.NamespacedName, loadBalancerName string, annotations map[string]string) ([]string, bool, error) {
|
||||||
var err error
|
var err error
|
||||||
var securityGroupID string
|
var securityGroupID string
|
||||||
|
// We do not want to make changes to a Global defined SG
|
||||||
|
var setupSg = false
|
||||||
|
|
||||||
sgList := getSGListFromAnnotation(annotations[ServiceAnnotationLoadBalancerSecurityGroups])
|
sgList := getSGListFromAnnotation(annotations[ServiceAnnotationLoadBalancerSecurityGroups])
|
||||||
|
|
||||||
// The below code changes makes sure that when we have Security Groups specified with the ServiceAnnotationLoadBalancerSecurityGroups
|
|
||||||
// annotation we don't create a new default Security Groups
|
|
||||||
|
|
||||||
// If no Security Groups have been specified with the ServiceAnnotationLoadBalancerSecurityGroups annotation, we add the default one.
|
// If no Security Groups have been specified with the ServiceAnnotationLoadBalancerSecurityGroups annotation, we add the default one.
|
||||||
if len(sgList) == 0 {
|
if len(sgList) == 0 {
|
||||||
if c.cfg.Global.ElbSecurityGroup != "" {
|
if c.cfg.Global.ElbSecurityGroup != "" {
|
||||||
securityGroupID = c.cfg.Global.ElbSecurityGroup
|
sgList = append(sgList, c.cfg.Global.ElbSecurityGroup)
|
||||||
} else {
|
} else {
|
||||||
// Create a security group for the load balancer
|
// Create a security group for the load balancer
|
||||||
sgName := "k8s-elb-" + loadBalancerName
|
sgName := "k8s-elb-" + loadBalancerName
|
||||||
@ -3553,16 +3547,17 @@ func (c *Cloud) buildELBSecurityGroupList(serviceName types.NamespacedName, load
|
|||||||
securityGroupID, err = c.ensureSecurityGroup(sgName, sgDescription, getLoadBalancerAdditionalTags(annotations))
|
securityGroupID, err = c.ensureSecurityGroup(sgName, sgDescription, getLoadBalancerAdditionalTags(annotations))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
klog.Errorf("Error creating load balancer security group: %q", err)
|
klog.Errorf("Error creating load balancer security group: %q", err)
|
||||||
return nil, err
|
return nil, setupSg, err
|
||||||
}
|
}
|
||||||
|
sgList = append(sgList, securityGroupID)
|
||||||
|
setupSg = true
|
||||||
}
|
}
|
||||||
sgList = append(sgList, securityGroupID)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
extraSGList := getSGListFromAnnotation(annotations[ServiceAnnotationLoadBalancerExtraSecurityGroups])
|
extraSGList := getSGListFromAnnotation(annotations[ServiceAnnotationLoadBalancerExtraSecurityGroups])
|
||||||
sgList = append(sgList, extraSGList...)
|
sgList = append(sgList, extraSGList...)
|
||||||
|
|
||||||
return sgList, nil
|
return sgList, setupSg, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// buildListener creates a new listener from the given port, adding an SSL certificate
|
// buildListener creates a new listener from the given port, adding an SSL certificate
|
||||||
@ -3871,7 +3866,7 @@ func (c *Cloud) EnsureLoadBalancer(ctx context.Context, clusterName string, apiS
|
|||||||
|
|
||||||
loadBalancerName := c.GetLoadBalancerName(ctx, clusterName, apiService)
|
loadBalancerName := c.GetLoadBalancerName(ctx, clusterName, apiService)
|
||||||
serviceName := types.NamespacedName{Namespace: apiService.Namespace, Name: apiService.Name}
|
serviceName := types.NamespacedName{Namespace: apiService.Namespace, Name: apiService.Name}
|
||||||
securityGroupIDs, err := c.buildELBSecurityGroupList(serviceName, loadBalancerName, annotations)
|
securityGroupIDs, setupSg, err := c.buildELBSecurityGroupList(serviceName, loadBalancerName, annotations)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@ -3879,7 +3874,7 @@ func (c *Cloud) EnsureLoadBalancer(ctx context.Context, clusterName string, apiS
|
|||||||
return nil, fmt.Errorf("[BUG] ELB can't have empty list of Security Groups to be assigned, this is a Kubernetes bug, please report")
|
return nil, fmt.Errorf("[BUG] ELB can't have empty list of Security Groups to be assigned, this is a Kubernetes bug, please report")
|
||||||
}
|
}
|
||||||
|
|
||||||
{
|
if setupSg {
|
||||||
ec2SourceRanges := []*ec2.IpRange{}
|
ec2SourceRanges := []*ec2.IpRange{}
|
||||||
for _, sourceRange := range sourceRanges.StringSlice() {
|
for _, sourceRange := range sourceRanges.StringSlice() {
|
||||||
ec2SourceRanges = append(ec2SourceRanges, &ec2.IpRange{CidrIp: aws.String(sourceRange)})
|
ec2SourceRanges = append(ec2SourceRanges, &ec2.IpRange{CidrIp: aws.String(sourceRange)})
|
||||||
|
@ -1641,11 +1641,12 @@ func TestLBExtraSecurityGroupsAnnotation(t *testing.T) {
|
|||||||
t.Run(test.name, func(t *testing.T) {
|
t.Run(test.name, func(t *testing.T) {
|
||||||
serviceName := types.NamespacedName{Namespace: "default", Name: "myservice"}
|
serviceName := types.NamespacedName{Namespace: "default", Name: "myservice"}
|
||||||
|
|
||||||
sgList, err := c.buildELBSecurityGroupList(serviceName, "aid", test.annotations)
|
sgList, setupSg, err := c.buildELBSecurityGroupList(serviceName, "aid", test.annotations)
|
||||||
assert.NoError(t, err, "buildELBSecurityGroupList failed")
|
assert.NoError(t, err, "buildELBSecurityGroupList failed")
|
||||||
extraSGs := sgList[1:]
|
extraSGs := sgList[1:]
|
||||||
assert.True(t, sets.NewString(test.expectedSGs...).Equal(sets.NewString(extraSGs...)),
|
assert.True(t, sets.NewString(test.expectedSGs...).Equal(sets.NewString(extraSGs...)),
|
||||||
"Security Groups expected=%q , returned=%q", test.expectedSGs, extraSGs)
|
"Security Groups expected=%q , returned=%q", test.expectedSGs, extraSGs)
|
||||||
|
assert.True(t, setupSg, "Security Groups Setup Permissions Flag expected=%t , returned=%t", true, setupSg)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -1674,10 +1675,11 @@ func TestLBSecurityGroupsAnnotation(t *testing.T) {
|
|||||||
t.Run(test.name, func(t *testing.T) {
|
t.Run(test.name, func(t *testing.T) {
|
||||||
serviceName := types.NamespacedName{Namespace: "default", Name: "myservice"}
|
serviceName := types.NamespacedName{Namespace: "default", Name: "myservice"}
|
||||||
|
|
||||||
sgList, err := c.buildELBSecurityGroupList(serviceName, "aid", test.annotations)
|
sgList, setupSg, err := c.buildELBSecurityGroupList(serviceName, "aid", test.annotations)
|
||||||
assert.NoError(t, err, "buildELBSecurityGroupList failed")
|
assert.NoError(t, err, "buildELBSecurityGroupList failed")
|
||||||
assert.True(t, sets.NewString(test.expectedSGs...).Equal(sets.NewString(sgList...)),
|
assert.True(t, sets.NewString(test.expectedSGs...).Equal(sets.NewString(sgList...)),
|
||||||
"Security Groups expected=%q , returned=%q", test.expectedSGs, sgList)
|
"Security Groups expected=%q , returned=%q", test.expectedSGs, sgList)
|
||||||
|
assert.False(t, setupSg, "Security Groups Setup Permissions Flag expected=%t , returned=%t", false, setupSg)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user