Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Preserve node ports on service reconciliation #514

Merged
merged 1 commit into from
Mar 23, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion pkg/router/kubernetes_default.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ func (c *KubernetesDefaultRouter) reconcileService(canary *flaggerv1.Canary, nam
targetPort = canary.Spec.Service.TargetPort
}

// set pod selector and apex port
svcSpec := corev1.ServiceSpec{
Type: corev1.ServiceTypeClusterIP,
Selector: map[string]string{c.labelSelector: podSelector},
Expand All @@ -95,6 +96,7 @@ func (c *KubernetesDefaultRouter) reconcileService(canary *flaggerv1.Canary, nam
},
}

// set additional ports
for n, p := range c.ports {
cp := corev1.ServicePort{
Name: n,
Expand All @@ -109,6 +111,7 @@ func (c *KubernetesDefaultRouter) reconcileService(canary *flaggerv1.Canary, nam
svcSpec.Ports = append(svcSpec.Ports, cp)
}

// create service if it doesn't exists
svc, err := c.kubeClient.CoreV1().Services(canary.Namespace).Get(name, metav1.GetOptions{})
if errors.IsNotFound(err) {
svc = &corev1.Service{
Expand Down Expand Up @@ -140,13 +143,24 @@ func (c *KubernetesDefaultRouter) reconcileService(canary *flaggerv1.Canary, nam
return fmt.Errorf("service %s get query error: %w", name, err)
}

// update existing service pod selector and ports
if svc != nil {
sortPorts := func(a, b interface{}) bool {
return a.(corev1.ServicePort).Port < b.(corev1.ServicePort).Port
}

// copy node ports from existing service
for _, port := range svc.Spec.Ports {
for i, servicePort := range svcSpec.Ports {
if port.Name == servicePort.Name && port.NodePort > 0 {
svcSpec.Ports[i].NodePort = port.NodePort
break
}
}
}

portsDiff := cmp.Diff(svcSpec.Ports, svc.Spec.Ports, cmpopts.SortSlices(sortPorts))
selectorsDiff := cmp.Diff(svcSpec.Selector, svc.Spec.Selector)

if portsDiff != "" || selectorsDiff != "" {
svcClone := svc.DeepCopy()
svcClone.Spec.Ports = svcSpec.Ports
Expand Down