Update external-dns to use v1 ingress

This commit is contained in:
Andrew Stuart 2021-09-08 09:29:05 -07:00
parent 5977d09a70
commit 0e27f41fa0
No known key found for this signature in database
GPG Key ID: D409317C5B5ACD4D
4 changed files with 58 additions and 43 deletions

View File

@ -237,7 +237,7 @@ spec:
``` ```
- Then, create service file called 'expose-apple-banana-app.yaml' to expose the services. For more information to deploy ingress controller, refer to (https://kubernetes.github.io/ingress-nginx/deploy/) - Then, create service file called 'expose-apple-banana-app.yaml' to expose the services. For more information to deploy ingress controller, refer to (https://kubernetes.github.io/ingress-nginx/deploy/)
```yaml ```yaml
apiVersion: extensions/v1beta1 apiVersion: networking.k8s.io/v1
kind: Ingress kind: Ingress
metadata: metadata:
name: example-ingress name: example-ingress
@ -252,8 +252,10 @@ spec:
paths: paths:
- path: /apple - path: /apple
backend: backend:
serviceName: example-service service:
servicePort: 5678 name: example-service
port:
number: 5678
``` ```
- Then, create the deployment and service: - Then, create the deployment and service:
```console ```console
@ -298,7 +300,7 @@ $ kubectl delete -f external-dns.yaml
ports: ports:
- port: 5678 # Default port for image - port: 5678 # Default port for image
--- ---
apiVersion: extensions/v1beta1 apiVersion: networking.k8s.io/v1
kind: Ingress kind: Ingress
metadata: metadata:
name: example-ingress name: example-ingress
@ -313,8 +315,10 @@ $ kubectl delete -f external-dns.yaml
paths: paths:
- path: /apple - path: /apple
backend: backend:
serviceName: example-service service:
servicePort: 5678 name: example-service
port:
number: 5678
``` ```
- _Config File Example Kubernetes cluster service from different cloud vendors_ - _Config File Example Kubernetes cluster service from different cloud vendors_
```yaml ```yaml
@ -434,7 +438,7 @@ $ kubectl delete -f external-dns.yaml
ports: ports:
- port: 5680 # Default port for image - port: 5680 # Default port for image
--- ---
apiVersion: extensions/v1beta1 apiVersion: networking.k8s.io/v1
kind: Ingress kind: Ingress
metadata: metadata:
name: example-ingress name: example-ingress
@ -449,10 +453,12 @@ $ kubectl delete -f external-dns.yaml
paths: paths:
- path: /apple - path: /apple
backend: backend:
serviceName: example-service service:
servicePort: 5678 name: example-service
port:
number: 5678
--- ---
apiVersion: extensions/v1beta1 apiVersion: networking.k8s.io/v1
kind: Ingress kind: Ingress
metadata: metadata:
name: example-ingress1 name: example-ingress1
@ -467,10 +473,12 @@ $ kubectl delete -f external-dns.yaml
paths: paths:
- path: /apple - path: /apple
backend: backend:
serviceName: example-service1 service:
servicePort: 5679 name: example-service1
port:
number: 5679
--- ---
apiVersion: extensions/v1beta1 apiVersion: networking.k8s.io/v1
kind: Ingress kind: Ingress
metadata: metadata:
name: example-ingress2 name: example-ingress2
@ -485,8 +493,10 @@ $ kubectl delete -f external-dns.yaml
paths: paths:
- path: /apple - path: /apple
backend: backend:
serviceName: example-service2 service:
servicePort: 5680 name: example-service2
port:
number: 5680
``` ```
- _Config File Example Kubernetes cluster service from different cloud vendors_ - _Config File Example Kubernetes cluster service from different cloud vendors_
```yaml ```yaml
@ -572,6 +582,7 @@ $ kubectl delete -f external-dns.yaml
ports: ports:
- port: 5679 # Default port for image - port: 5679 # Default port for image
--- ---
apiVersion: networking.k8s.io/v1
kind: Ingress kind: Ingress
metadata: metadata:
name: example-ingress name: example-ingress
@ -586,10 +597,12 @@ $ kubectl delete -f external-dns.yaml
paths: paths:
- path: /apple - path: /apple
backend: backend:
serviceName: example-service service:
servicePort: 5678 name: example-service
port:
number: 5678
--- ---
apiVersion: extensions/v1beta1 apiVersion: networking.k8s.io/v1
kind: Ingress kind: Ingress
metadata: metadata:
name: example-ingress1 name: example-ingress1
@ -604,8 +617,10 @@ $ kubectl delete -f external-dns.yaml
paths: paths:
- path: /apple - path: /apple
backend: backend:
serviceName: example-service1 service:
servicePort: 5679 name: example-service1
port:
number: 5679
``` ```
- Then, create the deployment and service: - Then, create the deployment and service:
```console ```console

View File

@ -54,7 +54,7 @@ if external_dns_manages_services:
k8s_domains.extend(annotations['domainName'].split(',')) k8s_domains.extend(annotations['domainName'].split(','))
if external_dns_manages_ingresses: if external_dns_manages_ingresses:
ev1 = client.ExtensionsV1beta1Api() ev1 = client.NetworkingV1Api()
ings = ev1.list_ingress_for_all_namespaces() ings = ev1.list_ingress_for_all_namespaces()
for i in ings.items: for i in ings.items:
for r in i.spec.rules: for r in i.spec.rules:

View File

@ -24,11 +24,11 @@ import (
"text/template" "text/template"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
"k8s.io/api/extensions/v1beta1" networkv1 "k8s.io/api/networking/v1"
"k8s.io/apimachinery/pkg/labels" "k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/util/wait" "k8s.io/apimachinery/pkg/util/wait"
kubeinformers "k8s.io/client-go/informers" kubeinformers "k8s.io/client-go/informers"
extinformers "k8s.io/client-go/informers/extensions/v1beta1" netinformers "k8s.io/client-go/informers/networking/v1"
"k8s.io/client-go/kubernetes" "k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/cache" "k8s.io/client-go/tools/cache"
@ -57,7 +57,7 @@ type ingressSource struct {
fqdnTemplate *template.Template fqdnTemplate *template.Template
combineFQDNAnnotation bool combineFQDNAnnotation bool
ignoreHostnameAnnotation bool ignoreHostnameAnnotation bool
ingressInformer extinformers.IngressInformer ingressInformer netinformers.IngressInformer
ignoreIngressTLSSpec bool ignoreIngressTLSSpec bool
ignoreIngressRulesSpec bool ignoreIngressRulesSpec bool
} }
@ -72,7 +72,7 @@ func NewIngressSource(kubeClient kubernetes.Interface, namespace, annotationFilt
// Use shared informer to listen for add/update/delete of ingresses in the specified namespace. // Use shared informer to listen for add/update/delete of ingresses in the specified namespace.
// Set resync period to 0, to prevent processing when nothing has changed. // Set resync period to 0, to prevent processing when nothing has changed.
informerFactory := kubeinformers.NewSharedInformerFactoryWithOptions(kubeClient, 0, kubeinformers.WithNamespace(namespace)) informerFactory := kubeinformers.NewSharedInformerFactoryWithOptions(kubeClient, 0, kubeinformers.WithNamespace(namespace))
ingressInformer := informerFactory.Extensions().V1beta1().Ingresses() ingressInformer := informerFactory.Networking().V1().Ingresses()
// Add default resource event handlers to properly initialize informer. // Add default resource event handlers to properly initialize informer.
ingressInformer.Informer().AddEventHandler( ingressInformer.Informer().AddEventHandler(
@ -161,7 +161,7 @@ func (sc *ingressSource) Endpoints(ctx context.Context) ([]*endpoint.Endpoint, e
return endpoints, nil return endpoints, nil
} }
func (sc *ingressSource) endpointsFromTemplate(ing *v1beta1.Ingress) ([]*endpoint.Endpoint, error) { func (sc *ingressSource) endpointsFromTemplate(ing *networkv1.Ingress) ([]*endpoint.Endpoint, error) {
hostnames, err := execTemplate(sc.fqdnTemplate, ing) hostnames, err := execTemplate(sc.fqdnTemplate, ing)
if err != nil { if err != nil {
return nil, err return nil, err
@ -187,7 +187,7 @@ func (sc *ingressSource) endpointsFromTemplate(ing *v1beta1.Ingress) ([]*endpoin
} }
// filterByAnnotations filters a list of ingresses by a given annotation selector. // filterByAnnotations filters a list of ingresses by a given annotation selector.
func (sc *ingressSource) filterByAnnotations(ingresses []*v1beta1.Ingress) ([]*v1beta1.Ingress, error) { func (sc *ingressSource) filterByAnnotations(ingresses []*networkv1.Ingress) ([]*networkv1.Ingress, error) {
selector, err := getLabelSelector(sc.annotationFilter) selector, err := getLabelSelector(sc.annotationFilter)
if err != nil { if err != nil {
return nil, err return nil, err
@ -198,7 +198,7 @@ func (sc *ingressSource) filterByAnnotations(ingresses []*v1beta1.Ingress) ([]*v
return ingresses, nil return ingresses, nil
} }
filteredList := []*v1beta1.Ingress{} filteredList := []*networkv1.Ingress{}
for _, ingress := range ingresses { for _, ingress := range ingresses {
// include ingress if its annotations match the selector // include ingress if its annotations match the selector
@ -210,13 +210,13 @@ func (sc *ingressSource) filterByAnnotations(ingresses []*v1beta1.Ingress) ([]*v
return filteredList, nil return filteredList, nil
} }
func (sc *ingressSource) setResourceLabel(ingress *v1beta1.Ingress, endpoints []*endpoint.Endpoint) { func (sc *ingressSource) setResourceLabel(ingress *networkv1.Ingress, endpoints []*endpoint.Endpoint) {
for _, ep := range endpoints { for _, ep := range endpoints {
ep.Labels[endpoint.ResourceLabelKey] = fmt.Sprintf("ingress/%s/%s", ingress.Namespace, ingress.Name) ep.Labels[endpoint.ResourceLabelKey] = fmt.Sprintf("ingress/%s/%s", ingress.Namespace, ingress.Name)
} }
} }
func (sc *ingressSource) setDualstackLabel(ingress *v1beta1.Ingress, endpoints []*endpoint.Endpoint) { func (sc *ingressSource) setDualstackLabel(ingress *networkv1.Ingress, endpoints []*endpoint.Endpoint) {
val, ok := ingress.Annotations[ALBDualstackAnnotationKey] val, ok := ingress.Annotations[ALBDualstackAnnotationKey]
if ok && val == ALBDualstackAnnotationValue { if ok && val == ALBDualstackAnnotationValue {
log.Debugf("Adding dualstack label to ingress %s/%s.", ingress.Namespace, ingress.Name) log.Debugf("Adding dualstack label to ingress %s/%s.", ingress.Namespace, ingress.Name)
@ -227,7 +227,7 @@ func (sc *ingressSource) setDualstackLabel(ingress *v1beta1.Ingress, endpoints [
} }
// endpointsFromIngress extracts the endpoints from ingress object // endpointsFromIngress extracts the endpoints from ingress object
func endpointsFromIngress(ing *v1beta1.Ingress, ignoreHostnameAnnotation bool, ignoreIngressTLSSpec bool, ignoreIngressRulesSpec bool) []*endpoint.Endpoint { func endpointsFromIngress(ing *networkv1.Ingress, ignoreHostnameAnnotation bool, ignoreIngressTLSSpec bool, ignoreIngressRulesSpec bool) []*endpoint.Endpoint {
ttl, err := getTTLFromAnnotations(ing.Annotations) ttl, err := getTTLFromAnnotations(ing.Annotations)
if err != nil { if err != nil {
log.Warn(err) log.Warn(err)
@ -290,7 +290,7 @@ func endpointsFromIngress(ing *v1beta1.Ingress, ignoreHostnameAnnotation bool, i
return endpoints return endpoints
} }
func targetsFromIngressStatus(status v1beta1.IngressStatus) endpoint.Targets { func targetsFromIngressStatus(status networkv1.IngressStatus) endpoint.Targets {
var targets endpoint.Targets var targets endpoint.Targets
for _, lb := range status.LoadBalancer.Ingress { for _, lb := range status.LoadBalancer.Ingress {

View File

@ -24,7 +24,7 @@ import (
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite" "github.com/stretchr/testify/suite"
v1 "k8s.io/api/core/v1" v1 "k8s.io/api/core/v1"
"k8s.io/api/extensions/v1beta1" networkv1 "k8s.io/api/networking/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes/fake" "k8s.io/client-go/kubernetes/fake"
@ -37,7 +37,7 @@ var _ Source = &ingressSource{}
type IngressSuite struct { type IngressSuite struct {
suite.Suite suite.Suite
sc Source sc Source
fooWithTargets *v1beta1.Ingress fooWithTargets *networkv1.Ingress
} }
func (suite *IngressSuite) SetupTest() { func (suite *IngressSuite) SetupTest() {
@ -51,7 +51,7 @@ func (suite *IngressSuite) SetupTest() {
hostnames: []string{"v1"}, hostnames: []string{"v1"},
annotations: map[string]string{ALBDualstackAnnotationKey: ALBDualstackAnnotationValue}, annotations: map[string]string{ALBDualstackAnnotationKey: ALBDualstackAnnotationValue},
}).Ingress() }).Ingress()
_, err := fakeClient.ExtensionsV1beta1().Ingresses(suite.fooWithTargets.Namespace).Create(context.Background(), suite.fooWithTargets, metav1.CreateOptions{}) _, err := fakeClient.NetworkingV1().Ingresses(suite.fooWithTargets.Namespace).Create(context.Background(), suite.fooWithTargets, metav1.CreateOptions{})
suite.NoError(err, "should succeed") suite.NoError(err, "should succeed")
suite.sc, err = NewIngressSource( suite.sc, err = NewIngressSource(
@ -1177,7 +1177,7 @@ func testIngressEndpoints(t *testing.T) {
fakeClient := fake.NewSimpleClientset() fakeClient := fake.NewSimpleClientset()
for _, item := range ti.ingressItems { for _, item := range ti.ingressItems {
ingress := item.Ingress() ingress := item.Ingress()
_, err := fakeClient.ExtensionsV1beta1().Ingresses(ingress.Namespace).Create(context.Background(), ingress, metav1.CreateOptions{}) _, err := fakeClient.NetworkingV1().Ingresses(ingress.Namespace).Create(context.Background(), ingress, metav1.CreateOptions{})
require.NoError(t, err) require.NoError(t, err)
} }
source, _ := NewIngressSource( source, _ := NewIngressSource(
@ -1213,29 +1213,29 @@ type fakeIngress struct {
annotations map[string]string annotations map[string]string
} }
func (ing fakeIngress) Ingress() *v1beta1.Ingress { func (ing fakeIngress) Ingress() *networkv1.Ingress {
ingress := &v1beta1.Ingress{ ingress := &networkv1.Ingress{
ObjectMeta: metav1.ObjectMeta{ ObjectMeta: metav1.ObjectMeta{
Namespace: ing.namespace, Namespace: ing.namespace,
Name: ing.name, Name: ing.name,
Annotations: ing.annotations, Annotations: ing.annotations,
}, },
Spec: v1beta1.IngressSpec{ Spec: networkv1.IngressSpec{
Rules: []v1beta1.IngressRule{}, Rules: []networkv1.IngressRule{},
}, },
Status: v1beta1.IngressStatus{ Status: networkv1.IngressStatus{
LoadBalancer: v1.LoadBalancerStatus{ LoadBalancer: v1.LoadBalancerStatus{
Ingress: []v1.LoadBalancerIngress{}, Ingress: []v1.LoadBalancerIngress{},
}, },
}, },
} }
for _, dnsname := range ing.dnsnames { for _, dnsname := range ing.dnsnames {
ingress.Spec.Rules = append(ingress.Spec.Rules, v1beta1.IngressRule{ ingress.Spec.Rules = append(ingress.Spec.Rules, networkv1.IngressRule{
Host: dnsname, Host: dnsname,
}) })
} }
for _, hosts := range ing.tlsdnsnames { for _, hosts := range ing.tlsdnsnames {
ingress.Spec.TLS = append(ingress.Spec.TLS, v1beta1.IngressTLS{ ingress.Spec.TLS = append(ingress.Spec.TLS, networkv1.IngressTLS{
Hosts: hosts, Hosts: hosts,
}) })
} }