Skip to content

Commit

Permalink
Support spec.externalIP for externalIP to clusterIP
Browse files Browse the repository at this point in the history
  • Loading branch information
ssup2 committed Mar 25, 2021
1 parent 93156e5 commit d1368d3
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 7 deletions.
11 changes: 6 additions & 5 deletions controllers/service_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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)
Expand All @@ -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 {
Expand Down
24 changes: 22 additions & 2 deletions pkg/rules/rule_external_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
}
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand All @@ -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()
}
}
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down
16 changes: 16 additions & 0 deletions pkg/utils/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down Expand Up @@ -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")
Expand Down

0 comments on commit d1368d3

Please sign in to comment.