From d1368d3047d8f0b940acfefabd7f69357ac9faa6 Mon Sep 17 00:00:00 2001 From: ssup2 Date: Thu, 25 Mar 2021 16:57:22 +0000 Subject: [PATCH] Support spec.externalIP for externalIP to clusterIP --- controllers/service_controller.go | 11 ++++++----- pkg/rules/rule_external_cluster.go | 24 ++++++++++++++++++++++-- pkg/utils/utils_test.go | 16 ++++++++++++++++ 3 files changed, 44 insertions(+), 7 deletions(-) diff --git a/controllers/service_controller.go b/controllers/service_controller.go index c526e72..c9de93c 100644 --- a/controllers/service_controller.go +++ b/controllers/service_controller.go @@ -186,6 +186,9 @@ func (r *ServiceReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ct for _, ingress := range svc.Status.LoadBalancer.Ingress { oldExternalIPs = append(oldExternalIPs, ingress.IP) } + for _, externalIP := range svc.Spec.ExternalIPs { + oldExternalIPs = append(oldExternalIPs, externalIP) + } // Delete rules for _, oldExternalIP := range oldExternalIPs { @@ -207,11 +210,6 @@ func (r *ServiceReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ct } } - // Check service is LoadBalancer type - if svc.Spec.Type != corev1.ServiceTypeLoadBalancer { - return ctrl.Result{}, nil - } - // Get service's clusterIPs for each family clusterIPv4 := utils.GetClusterIPByFamily(corev1.IPv4Protocol, svc) clusterIPv6 := utils.GetClusterIPByFamily(corev1.IPv6Protocol, svc) @@ -225,6 +223,9 @@ func (r *ServiceReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ct for _, ingress := range svc.Status.LoadBalancer.Ingress { externalIPs = append(externalIPs, ingress.IP) } + for _, externalIP := range svc.Spec.ExternalIPs { + externalIPs = append(externalIPs, externalIP) + } // Create rules for _, externalIP := range externalIPs { diff --git a/pkg/rules/rule_external_cluster.go b/pkg/rules/rule_external_cluster.go index 250a4c8..5013ef7 100644 --- a/pkg/rules/rule_external_cluster.go +++ b/pkg/rules/rule_external_cluster.go @@ -144,7 +144,7 @@ func CleanupRulesExternalCluster(logger logr.Logger, svcs *corev1.ServiceList) e // Make up service map svcMap := make(map[string]*corev1.Service) for _, svc := range svcs.Items { - if ip.IsIPv4Addr(utils.GetClusterIPByFamily(corev1.IPv4Protocol, &svc)) && svc.Spec.Type == corev1.ServiceTypeLoadBalancer { + if ip.IsIPv4Addr(utils.GetClusterIPByFamily(corev1.IPv4Protocol, &svc)) { svcMap[svc.Namespace+"/"+svc.Name] = svc.DeepCopy() } } @@ -181,6 +181,11 @@ func CleanupRulesExternalCluster(logger logr.Logger, svcs *corev1.ServiceList) e externalIPs = append(externalIPs, ingress.IP) } } + for _, externalIP := range svc.Spec.ExternalIPs { + if ip.IsIPv4Addr(externalIP) { + externalIPs = append(externalIPs, externalIP) + } + } // Compare service info and delete iptables rules for _, externalIP := range externalIPs { @@ -230,6 +235,11 @@ func CleanupRulesExternalCluster(logger logr.Logger, svcs *corev1.ServiceList) e externalIPs = append(externalIPs, ingress.IP) } } + for _, externalIP := range svc.Spec.ExternalIPs { + if ip.IsIPv4Addr(externalIP) { + externalIPs = append(externalIPs, externalIP) + } + } // Compare service info and delete diff iptables rules for _, externalIP := range externalIPs { @@ -252,7 +262,7 @@ func CleanupRulesExternalCluster(logger logr.Logger, svcs *corev1.ServiceList) e // Make up service map svcMap := make(map[string]*corev1.Service) for _, svc := range svcs.Items { - if ip.IsIPv6Addr(utils.GetClusterIPByFamily(corev1.IPv6Protocol, &svc)) && svc.Spec.Type == corev1.ServiceTypeLoadBalancer { + if ip.IsIPv6Addr(utils.GetClusterIPByFamily(corev1.IPv6Protocol, &svc)) { svcMap[svc.Namespace+"/"+svc.Name] = svc.DeepCopy() } } @@ -289,6 +299,11 @@ func CleanupRulesExternalCluster(logger logr.Logger, svcs *corev1.ServiceList) e externalIPs = append(externalIPs, ingress.IP) } } + for _, externalIP := range svc.Spec.ExternalIPs { + if ip.IsIPv6Addr(externalIP) { + externalIPs = append(externalIPs, externalIP) + } + } // Compare service info and delete iptables rules for _, externalIP := range externalIPs { @@ -338,6 +353,11 @@ func CleanupRulesExternalCluster(logger logr.Logger, svcs *corev1.ServiceList) e externalIPs = append(externalIPs, ingress.IP) } } + for _, externalIP := range svc.Spec.ExternalIPs { + if ip.IsIPv6Addr(externalIP) { + externalIPs = append(externalIPs, externalIP) + } + } // Compare service info and delete diff iptables rules for _, externalIP := range externalIPs { diff --git a/pkg/utils/utils_test.go b/pkg/utils/utils_test.go index 9d7ff96..8e9c938 100644 --- a/pkg/utils/utils_test.go +++ b/pkg/utils/utils_test.go @@ -24,6 +24,12 @@ var ( }, } + noneSvc = corev1.Service{ + Spec: corev1.ServiceSpec{ + ClusterIP: corev1.ClusterIPNone, + }, + } + ipv4SvcFamily = corev1.Service{ Spec: corev1.ServiceSpec{ IPFamilies: []corev1.IPFamily{corev1.IPv4Protocol}, @@ -57,6 +63,16 @@ func TestGetClusterIPByFamily(t *testing.T) { t.Errorf("wrong result - ipv6Svc") } + clusterIP = GetClusterIPByFamily(corev1.IPv4Protocol, &noneSvc) + if clusterIP != "" { + t.Errorf("wrong result - noneSvc - ipv4") + } + + clusterIP = GetClusterIPByFamily(corev1.IPv6Protocol, &noneSvc) + if clusterIP != "" { + t.Errorf("wrong result - noneSvc - ipv6") + } + clusterIP = GetClusterIPByFamily(corev1.IPv4Protocol, &ipv4SvcFamily) if clusterIP != ipv4Local { t.Errorf("wrong result - ipv4SvcFamily")