-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Synchronize load balancer status from the Kubernetes Service as…
- Loading branch information
Zachary Seguin
authored
Jul 14, 2022
1 parent
c93a316
commit 01418b8
Showing
9 changed files
with
241 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,3 +9,4 @@ deploy/ | |
Makefile | ||
.github/ | ||
bin/ | ||
_out/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,3 +4,4 @@ ingress-istio-controller | |
.vscode | ||
__debug_bin | ||
bin/ | ||
_out/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
package controller | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"reflect" | ||
"strings" | ||
|
||
istionetworkingv1beta1 "istio.io/client-go/pkg/apis/networking/v1beta1" | ||
corev1 "k8s.io/api/core/v1" | ||
networkingv1 "k8s.io/api/networking/v1" | ||
"k8s.io/apimachinery/pkg/api/errors" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/apimachinery/pkg/labels" | ||
"k8s.io/klog" | ||
) | ||
|
||
// handleIngressStatus will synchronize the status of the Load Balancer | ||
// of the Service the Gateway is associated with. | ||
func (c *Controller) handleIngressStatus(ingress *networkingv1.Ingress, vs *istionetworkingv1beta1.VirtualService) (*networkingv1.Ingress, error) { | ||
ctx := context.Background() | ||
|
||
gateways, err := c.getGatewaysForVirtualService(vs) | ||
if err != nil { | ||
return ingress, err | ||
} | ||
|
||
loadBalancerStatus := corev1.LoadBalancerStatus{} | ||
|
||
for _, gateway := range gateways { | ||
services, err := c.getServicesForGateway(gateway) | ||
if err != nil { | ||
return ingress, err | ||
} | ||
|
||
for _, service := range services { | ||
loadBalancerStatus.Ingress = append(loadBalancerStatus.Ingress, service.Status.LoadBalancer.Ingress...) | ||
} | ||
} | ||
|
||
// Compare the current status to the newly generated status | ||
// and if they differ, apply the change. | ||
if !reflect.DeepEqual(ingress.Status.LoadBalancer, loadBalancerStatus) { | ||
klog.Infof("updating ingress status for \"%s/%s\"", ingress.Namespace, ingress.Name) | ||
updatedIngress := ingress.DeepCopy() | ||
updatedIngress.Status.LoadBalancer = loadBalancerStatus | ||
|
||
ingress, err = c.kubeclientset.NetworkingV1().Ingresses(updatedIngress.Namespace).UpdateStatus(ctx, updatedIngress, metav1.UpdateOptions{}) | ||
if err != nil { | ||
return ingress, err | ||
} | ||
} | ||
|
||
return ingress, nil | ||
} | ||
|
||
// getGatewaysForVirtualService will get the gateways associated with the Virtual Service. | ||
func (c *Controller) getGatewaysForVirtualService(vs *istionetworkingv1beta1.VirtualService) ([]*istionetworkingv1beta1.Gateway, error) { | ||
gateways := []*istionetworkingv1beta1.Gateway{} | ||
|
||
for _, gatewayId := range vs.Spec.Gateways { | ||
var gateway *istionetworkingv1beta1.Gateway | ||
var err error | ||
|
||
// Split the gatewayId into [namespace, name] | ||
idParts := strings.Split(gatewayId, "/") | ||
|
||
switch len(idParts) { | ||
case 1: | ||
gateway, err = c.gatewaysListers.Gateways(vs.Namespace).Get(idParts[0]) | ||
case 2: | ||
gateway, err = c.gatewaysListers.Gateways(idParts[0]).Get(idParts[1]) | ||
default: | ||
return nil, fmt.Errorf("unexpected number of parts in Gateway identifier %q: %d", gatewayId, len(idParts)) | ||
} | ||
|
||
// If the Gateway is not found, then ignore the error. | ||
// Otherwise, this is an unexpected error and return it. | ||
if err != nil && errors.IsNotFound(err) { | ||
klog.Errorf("failed to load gateway %q: %v", gatewayId, err) | ||
continue | ||
} else if err != nil { | ||
return nil, err | ||
} | ||
|
||
gateways = append(gateways, gateway) | ||
} | ||
|
||
return gateways, nil | ||
} | ||
|
||
// getServicesForGateway returns Services associated with the given Gateway. | ||
func (c *Controller) getServicesForGateway(gateway *istionetworkingv1beta1.Gateway) ([]*corev1.Service, error) { | ||
selector := labels.SelectorFromSet(gateway.Spec.Selector) | ||
|
||
if c.scopedGateways { | ||
return c.servicesLister.Services(gateway.Namespace).List(selector) | ||
} | ||
|
||
return c.servicesLister.List(selector) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
#!/bin/bash | ||
|
||
set -euxo pipefail | ||
|
||
go run main.go -kubeconfig="$HOME/.kube/config" -default-gateway=istio-ingress/default |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
#!/bin/bash | ||
|
||
set -euxo pipefail | ||
|
||
KIND_VERSION=0.14.0 | ||
KUBERNETES_VERSION=1.21.12 | ||
ISTIO_VERSION=1.14.1 | ||
METALLB_VERSION=0.12.1 | ||
|
||
# Setup temp directory | ||
mkdir -p _out | ||
|
||
# Download kind | ||
curl -Lo _out/kind https://github.com/kubernetes-sigs/kind/releases/download/v$KIND_VERSION/kind-linux-amd64 && chmod +x _out/kind | ||
|
||
# Setup kind cluster | ||
_out/kind create cluster --image kindest/node:v$KUBERNETES_VERSION --name ingress-istio | ||
|
||
# Install metallb (for Load Balancer IPs) | ||
kubectl create namespace metallb-system | ||
kubectl apply -f https://raw.githubusercontent.com/metallb/metallb/v$METALLB_VERSION/manifests/metallb.yaml | ||
cat <<EOF | kubectl apply -f - | ||
apiVersion: v1 | ||
kind: ConfigMap | ||
metadata: | ||
name: config | ||
namespace: metallb-system | ||
data: | ||
config: | | ||
address-pools: | ||
- name: default | ||
protocol: layer2 | ||
addresses: | ||
- 172.19.255.200-172.19.255.250 | ||
EOF | ||
|
||
# Install istio | ||
helm repo add istio https://istio-release.storage.googleapis.com/charts | ||
helm repo update | ||
|
||
kubectl create namespace istio-system | ||
helm install istio-base istio/base -n istio-system --version $ISTIO_VERSION | ||
helm install istiod istio/istiod -n istio-system --wait --version $ISTIO_VERSION | ||
|
||
kubectl create namespace istio-ingress | ||
kubectl label namespace istio-ingress istio-injection=enabled | ||
helm install istio-ingress istio/gateway -n istio-ingress --wait | ||
|
||
echo 'Ready to test!' |
Oops, something went wrong.