Skip to content

Commit 45d93b1

Browse files
author
carflo
committed
adds endpoint-security-group-tags flag
1 parent ee82b3e commit 45d93b1

File tree

4 files changed

+35
-10
lines changed

4 files changed

+35
-10
lines changed

main.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ func main() {
108108
tgbResManager := targetgroupbinding.NewDefaultResourceManager(mgr.GetClient(), cloud.ELBV2(), cloud.EC2(),
109109
podInfoRepo, sgManager, sgReconciler, vpcInfoProvider,
110110
cloud.VpcID(), controllerCFG.ClusterName, controllerCFG.FeatureGates.Enabled(config.EndpointsFailOpen), controllerCFG.EnableEndpointSlices, controllerCFG.DisableRestrictedSGRules,
111-
mgr.GetEventRecorderFor("targetGroupBinding"), ctrl.Log)
111+
controllerCFG.EndpointSGTags, mgr.GetEventRecorderFor("targetGroupBinding"), ctrl.Log)
112112
backendSGProvider := networking.NewBackendSGProvider(controllerCFG.ClusterName, controllerCFG.BackendSecurityGroup,
113113
cloud.VpcID(), cloud.EC2(), mgr.GetClient(), controllerCFG.DefaultTags, ctrl.Log.WithName("backend-sg-provider"))
114114
ingGroupReconciler := ingress.NewGroupReconciler(cloud, mgr.GetClient(), mgr.GetEventRecorderFor("ingress"),

pkg/config/controller_config.go

+6-1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ const (
1818
flagDefaultTags = "default-tags"
1919
flagDefaultTargetType = "default-target-type"
2020
flagExternalManagedTags = "external-managed-tags"
21+
flagEndpointSGTags = "endpoint-security-group-tags"
2122
flagServiceMaxConcurrentReconciles = "service-max-concurrent-reconciles"
2223
flagTargetGroupBindingMaxConcurrentReconciles = "targetgroupbinding-max-concurrent-reconciles"
2324
flagTargetGroupBindingMaxExponentialBackoffDelay = "targetgroupbinding-max-exponential-backoff-delay"
@@ -74,6 +75,9 @@ type ControllerConfig struct {
7475
// List of Tag keys on AWS resources that will be managed externally.
7576
ExternalManagedTags []string
7677

78+
// AWS Tags that will be used by the controller to find the worker node security group to add inbound rules from NLBs.
79+
EndpointSGTags map[string]string
80+
7781
// Default SSL Policy that will be applied to all ingresses or services that do not have
7882
// the SSL Policy annotation.
7983
DefaultSSLPolicy string
@@ -128,7 +132,8 @@ func (cfg *ControllerConfig) BindFlags(fs *pflag.FlagSet) {
128132
"Enable EndpointSlices for IP targets instead of Endpoints")
129133
fs.BoolVar(&cfg.DisableRestrictedSGRules, flagDisableRestrictedSGRules, defaultDisableRestrictedSGRules,
130134
"Disable the usage of restricted security group rules")
131-
135+
fs.StringToStringVar(&cfg.EndpointSGTags, flagEndpointSGTags, nil,
136+
"AWS Tags that will be used by the controller to find the worker node security group to add inbound rules from NLBs")
132137
cfg.FeatureGates.BindFlags(fs)
133138
cfg.AWSConfig.BindFlags(fs)
134139
cfg.RuntimeConfig.BindFlags(fs)

pkg/targetgroupbinding/networking_manager.go

+26-7
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ type NetworkingManager interface {
4545

4646
// NewDefaultNetworkingManager constructs defaultNetworkingManager.
4747
func NewDefaultNetworkingManager(k8sClient client.Client, podENIResolver networking.PodENIInfoResolver, nodeENIResolver networking.NodeENIInfoResolver,
48-
sgManager networking.SecurityGroupManager, sgReconciler networking.SecurityGroupReconciler, vpcID string, clusterName string, logger logr.Logger, disabledRestrictedSGRulesFlag bool) *defaultNetworkingManager {
48+
sgManager networking.SecurityGroupManager, sgReconciler networking.SecurityGroupReconciler, vpcID string, clusterName string, endpointSGTags map[string]string, logger logr.Logger, disabledRestrictedSGRulesFlag bool) *defaultNetworkingManager {
4949

5050
return &defaultNetworkingManager{
5151
k8sClient: k8sClient,
@@ -55,6 +55,7 @@ func NewDefaultNetworkingManager(k8sClient client.Client, podENIResolver network
5555
sgReconciler: sgReconciler,
5656
vpcID: vpcID,
5757
clusterName: clusterName,
58+
endpointSGTags: endpointSGTags,
5859
logger: logger,
5960

6061
mutex: sync.Mutex{},
@@ -74,6 +75,7 @@ type defaultNetworkingManager struct {
7475
sgReconciler networking.SecurityGroupReconciler
7576
vpcID string
7677
clusterName string
78+
endpointSGTags map[string]string
7779
logger logr.Logger
7880

7981
// mutex will serialize our TargetGroup's networking reconcile requests.
@@ -518,19 +520,36 @@ func (m *defaultNetworkingManager) resolveEndpointSGForENI(ctx context.Context,
518520
return "", err
519521
}
520522
clusterResourceTagKey := fmt.Sprintf("kubernetes.io/cluster/%s", m.clusterName)
521-
sgIDsWithClusterTag := sets.NewString()
523+
sgIDsWithMatchingEndpointSGTags := sets.NewString()
522524
for sgID, sgInfo := range sgInfoByID {
525+
isMatch := true
523526
if _, ok := sgInfo.Tags[clusterResourceTagKey]; ok {
524-
sgIDsWithClusterTag.Insert(sgID)
527+
for endpointSGTagKey, endpointSGTagValue := range m.endpointSGTags {
528+
if sgInfo.Tags[endpointSGTagKey] != endpointSGTagValue {
529+
isMatch = false
530+
break
531+
}
532+
}
533+
} else {
534+
continue
535+
}
536+
537+
if isMatch {
538+
sgIDsWithMatchingEndpointSGTags.Insert(sgID)
525539
}
526540
}
527-
if len(sgIDsWithClusterTag) != 1 {
541+
542+
if len(sgIDsWithMatchingEndpointSGTags) != 1 {
528543
// user may provide incorrect `--cluster-name` at bootstrap or modify the tag key unexpectedly, it is hard to find out if no clusterName included in error message.
529544
// having `clusterName` included in error message might be helpful for shorten the troubleshooting time spent.
530-
return "", errors.Errorf("expect exactly one securityGroup tagged with %v for eni %v, got: %v (clusterName: %v)",
531-
clusterResourceTagKey, eniInfo.NetworkInterfaceID, sgIDsWithClusterTag.List(), m.clusterName)
545+
if len(m.endpointSGTags) == 0 {
546+
return "", errors.Errorf("expect exactly one securityGroup tagged with %v for eni %v, got: %v (clusterName: %v)",
547+
clusterResourceTagKey, eniInfo.NetworkInterfaceID, sgIDsWithMatchingEndpointSGTags.List(), m.clusterName)
548+
}
549+
return "", errors.Errorf("expect exactly one securityGroup tagged with %v and %v for eni %v, got: %v (clusterName: %v)",
550+
clusterResourceTagKey, m.endpointSGTags, eniInfo.NetworkInterfaceID, sgIDsWithMatchingEndpointSGTags.List(), m.clusterName)
532551
}
533-
sgID, _ := sgIDsWithClusterTag.PopAny()
552+
sgID, _ := sgIDsWithMatchingEndpointSGTags.PopAny()
534553
return sgID, nil
535554
}
536555

pkg/targetgroupbinding/resource_manager.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ func NewDefaultResourceManager(k8sClient client.Client, elbv2Client services.ELB
4242
podInfoRepo k8s.PodInfoRepo, sgManager networking.SecurityGroupManager, sgReconciler networking.SecurityGroupReconciler,
4343
vpcInfoProvider networking.VPCInfoProvider,
4444
vpcID string, clusterName string, failOpenEnabled bool, endpointSliceEnabled bool, disabledRestrictedSGRulesFlag bool,
45+
endpointSGTags map[string]string,
4546
eventRecorder record.EventRecorder, logger logr.Logger) *defaultResourceManager {
4647
targetsManager := NewCachedTargetsManager(elbv2Client, logger)
4748
endpointResolver := backend.NewDefaultEndpointResolver(k8sClient, podInfoRepo, failOpenEnabled, endpointSliceEnabled, logger)
@@ -50,7 +51,7 @@ func NewDefaultResourceManager(k8sClient client.Client, elbv2Client services.ELB
5051
podENIResolver := networking.NewDefaultPodENIInfoResolver(k8sClient, ec2Client, nodeInfoProvider, vpcID, logger)
5152
nodeENIResolver := networking.NewDefaultNodeENIInfoResolver(nodeInfoProvider, logger)
5253

53-
networkingManager := NewDefaultNetworkingManager(k8sClient, podENIResolver, nodeENIResolver, sgManager, sgReconciler, vpcID, clusterName, logger, disabledRestrictedSGRulesFlag)
54+
networkingManager := NewDefaultNetworkingManager(k8sClient, podENIResolver, nodeENIResolver, sgManager, sgReconciler, vpcID, clusterName, endpointSGTags, logger, disabledRestrictedSGRulesFlag)
5455
return &defaultResourceManager{
5556
k8sClient: k8sClient,
5657
targetsManager: targetsManager,

0 commit comments

Comments
 (0)