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

Migrate Ingress from API extensions/v1beta1 to networking.k8s.io/v1beta1 #1039

Merged
merged 8 commits into from
May 14, 2020
12 changes: 12 additions & 0 deletions deploy/cluster_role.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,18 @@ rules:
- 'update'
- 'delete'
- 'watch'
# Ingress for kubernetes 1.14 or higher
- apiGroups:
- networking.k8s.io
resources:
- ingresses
verbs:
- 'get'
- 'list'
- 'create'
- 'update'
- 'delete'
- 'watch'
- apiGroups:
- batch
resources:
Expand Down
13 changes: 13 additions & 0 deletions deploy/role.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,18 @@ rules:
- 'update'
- 'delete'
- 'watch'
# Ingress for kubernetes 1.14 or higher
- apiGroups:
- networking.k8s.io
resources:
- ingresses
verbs:
- 'get'
- 'list'
- 'create'
- 'update'
- 'delete'
- 'watch'
- apiGroups:
- batch
resources:
Expand Down Expand Up @@ -156,3 +168,4 @@ rules:
- 'update'
- 'delete'
- 'watch'

20 changes: 20 additions & 0 deletions pkg/autodetect/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"sync"
"time"

"github.com/jaegertracing/jaeger-operator/pkg/ingress"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it should go to group import 3


log "github.com/sirupsen/logrus"
"github.com/spf13/viper"
appsv1 "k8s.io/api/apps/v1"
Expand Down Expand Up @@ -102,6 +104,7 @@ func (b *Background) autoDetectCapabilities() {

b.detectElasticsearch(ctx, apiList)
b.detectKafka(ctx, apiList)
b.detectIngressAPI()
}

b.detectClusterRoles(ctx)
Expand Down Expand Up @@ -133,6 +136,23 @@ func (b *Background) detectPlatform(ctx context.Context, apiList *metav1.APIGrou
}
}

func (b *Background) detectIngressAPI() {
apiRes, err := b.dcl.ServerResourcesForGroupVersion("networking.k8s.io/v1beta1")
if err != nil {
viper.Set("ingress-api", ingress.ExtensionAPI)
log.WithField("ingress-api", viper.GetString("ingress-api")).Info("Auto-detected ingress api")
return
}

for _, r := range apiRes.APIResources {
if r.Name == "ingresses" {
viper.Set("ingress-api", ingress.NetworkingAPI)
log.WithField("ingress-api", viper.GetString("ingress-api")).Info("Auto-detected ingress api")
break
}
}
}

func (b *Background) detectElasticsearch(ctx context.Context, apiList *metav1.APIGroupList) {
// detect whether the Elasticsearch operator is available
if b.retryDetectEs {
Expand Down
14 changes: 9 additions & 5 deletions pkg/controller/jaeger/ingress.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ package jaeger
import (
"context"

"github.com/jaegertracing/jaeger-operator/pkg/ingress"

log "github.com/sirupsen/logrus"
"go.opentelemetry.io/otel/global"
"k8s.io/api/extensions/v1beta1"
"k8s.io/api/networking/v1beta1"
"sigs.k8s.io/controller-runtime/pkg/client"

v1 "github.com/jaegertracing/jaeger-operator/pkg/apis/jaegertracing/v1"
Expand All @@ -14,6 +16,8 @@ import (
)

func (r *ReconcileJaeger) applyIngresses(ctx context.Context, jaeger v1.Jaeger, desired []v1beta1.Ingress) error {

ingressClient := ingress.NewIngressClient(r.client, r.rClient)
tracer := global.TraceProvider().GetTracer(v1.ReconciliationTracer)
ctx, span := tracer.Start(ctx, "applyIngresses")
defer span.End()
Expand All @@ -26,7 +30,7 @@ func (r *ReconcileJaeger) applyIngresses(ctx context.Context, jaeger v1.Jaeger,
}),
}
list := &v1beta1.IngressList{}
if err := r.rClient.List(ctx, list, opts...); err != nil {
if err := ingressClient.List(ctx, list, opts...); err != nil {
return tracing.HandleError(err, span)
}

Expand All @@ -36,7 +40,7 @@ func (r *ReconcileJaeger) applyIngresses(ctx context.Context, jaeger v1.Jaeger,
"ingress": d.Name,
"namespace": d.Namespace,
}).Debug("creating ingress")
if err := r.client.Create(ctx, &d); err != nil {
if err := ingressClient.Create(ctx, &d); err != nil {
return tracing.HandleError(err, span)
}
}
Expand All @@ -46,7 +50,7 @@ func (r *ReconcileJaeger) applyIngresses(ctx context.Context, jaeger v1.Jaeger,
"ingress": d.Name,
"namespace": d.Namespace,
}).Debug("updating ingress")
if err := r.client.Update(ctx, &d); err != nil {
if err := ingressClient.Update(ctx, &d); err != nil {
return tracing.HandleError(err, span)
}
}
Expand All @@ -56,7 +60,7 @@ func (r *ReconcileJaeger) applyIngresses(ctx context.Context, jaeger v1.Jaeger,
"ingress": d.Name,
"namespace": d.Namespace,
}).Debug("deleting ingress")
if err := r.client.Delete(ctx, &d); err != nil {
if err := ingressClient.Delete(ctx, &d); err != nil {
return tracing.HandleError(err, span)
}
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/controller/jaeger/ingress_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"testing"

"github.com/stretchr/testify/assert"
"k8s.io/api/extensions/v1beta1"
"k8s.io/api/networking/v1beta1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
Expand Down
178 changes: 178 additions & 0 deletions pkg/ingress/ingress.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
package ingress

import (
"context"

"github.com/spf13/viper"
extv1beta "k8s.io/api/extensions/v1beta1"
netv1beta "k8s.io/api/networking/v1beta1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/controller-runtime/pkg/client"
)

// ExtensionAPI indicate k8s networking.k8s.io/v1beta1 should be used on the current cluster
const ExtensionAPI = "extension"

// NetworkingAPI indicate k8s extensions/v1beta1 should be used on the current cluster
const NetworkingAPI = "networking"

// Client wrap around k8s client, and decide which ingress API should be used, depending on cluster capabilities.
type Client struct {
client client.Client
rClient client.Reader
}

// NewIngressClient Creates a new Ingress.client wrapper.
func NewIngressClient(client client.Client, reader client.Reader) *Client {
return &Client{
client: client,
rClient: reader,
}
}

func (c *Client) fromExtToNet(ingress extv1beta.Ingress) netv1beta.Ingress {
oldIngress := netv1beta.Ingress{
TypeMeta: metav1.TypeMeta{
Kind: "Ingress",
APIVersion: "extensions/v1beta1",
},
ObjectMeta: ingress.ObjectMeta,
Spec: netv1beta.IngressSpec{
Backend: &netv1beta.IngressBackend{
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Whet if backend is not defined? In the fromNetToExt ingress.Spec.Backend is checked to see if nil.

ServiceName: ingress.Spec.Backend.ServiceName,
ServicePort: ingress.Spec.Backend.ServicePort,
},
},
}

for _, tls := range ingress.Spec.TLS {
oldIngress.Spec.TLS = append(oldIngress.Spec.TLS, netv1beta.IngressTLS{
Hosts: tls.Hosts,
SecretName: tls.SecretName,
})
}

for _, rule := range ingress.Spec.Rules {
httpIngressPaths := make([]netv1beta.HTTPIngressPath, len(rule.HTTP.Paths))
for i, path := range rule.HTTP.Paths {
httpIngressPaths[i].Backend.ServicePort = path.Backend.ServicePort
httpIngressPaths[i].Backend.ServiceName = path.Backend.ServiceName
httpIngressPaths[i].Path = path.Path

}

oldIngress.Spec.Rules = append(oldIngress.Spec.Rules, netv1beta.IngressRule{
Host: rule.Host,
IngressRuleValue: netv1beta.IngressRuleValue{
HTTP: &netv1beta.HTTPIngressRuleValue{
Paths: httpIngressPaths,
},
},
})
}

return oldIngress
}

func (c *Client) fromNetToExt(ingress netv1beta.Ingress) extv1beta.Ingress {
oldIngress := extv1beta.Ingress{
TypeMeta: metav1.TypeMeta{
Kind: "Ingress",
APIVersion: "extensions/v1beta1",
},
ObjectMeta: ingress.ObjectMeta,
}

if ingress.Spec.Backend != nil {
oldIngress.Spec = extv1beta.IngressSpec{
Backend: &extv1beta.IngressBackend{
ServiceName: ingress.Spec.Backend.ServiceName,
ServicePort: ingress.Spec.Backend.ServicePort,
},
}
}

for _, tls := range ingress.Spec.TLS {
oldIngress.Spec.TLS = append(oldIngress.Spec.TLS, extv1beta.IngressTLS{
Hosts: tls.Hosts,
SecretName: tls.SecretName,
})
}

for _, rule := range ingress.Spec.Rules {
httpIngressPaths := make([]extv1beta.HTTPIngressPath, len(rule.HTTP.Paths))
for i, path := range rule.HTTP.Paths {
httpIngressPaths[i].Backend.ServicePort = path.Backend.ServicePort
httpIngressPaths[i].Backend.ServiceName = path.Backend.ServiceName
httpIngressPaths[i].Path = path.Path

}

oldIngress.Spec.Rules = append(oldIngress.Spec.Rules, extv1beta.IngressRule{
Host: rule.Host,
IngressRuleValue: extv1beta.IngressRuleValue{
HTTP: &extv1beta.HTTPIngressRuleValue{
Paths: httpIngressPaths,
},
},
})
}

return oldIngress
}

// List is a wrap function that calls k8s client List with extend or networking API.
func (c *Client) List(ctx context.Context, list *netv1beta.IngressList, opts ...client.ListOption) error {
if viper.Get("ingress-api") == ExtensionAPI {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could be at client init time. I guess nobody will enable extensions API dynamically

extIngressList := extv1beta.IngressList{}
err := c.rClient.List(ctx, &extIngressList, opts...)
if err != nil {
return err
}
for _, item := range extIngressList.Items {
list.Items = append(list.Items, c.fromExtToNet(item))
}
return nil
}
return c.rClient.List(ctx, list, opts...)
}

// Update is a wrap function that calls k8s client Update with extend or networking API.
func (c *Client) Update(ctx context.Context, obj *netv1beta.Ingress, opts ...client.UpdateOption) error {
if viper.Get("ingress-api") == ExtensionAPI {
extIngressList := c.fromNetToExt(*obj)
err := c.client.Update(ctx, &extIngressList, opts...)
if err != nil {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not necessary, could just return c.client.Update...

return err
}
return nil
}
return c.client.Update(ctx, obj, opts...)

}

// Delete is a wrap function that calls k8s client Delete with extend or networking API.
func (c *Client) Delete(ctx context.Context, obj *netv1beta.Ingress, opts ...client.DeleteOption) error {
if viper.Get("ingress-api") == ExtensionAPI {
extIngressList := c.fromNetToExt(*obj)
err := c.client.Delete(ctx, &extIngressList, opts...)
if err != nil {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same comment as above.

return err
}
return nil
}
return c.client.Delete(ctx, obj, opts...)
}

// Create is a wrap function that calls k8s client Create with extend or networking API.
func (c *Client) Create(ctx context.Context, obj *netv1beta.Ingress, opts ...client.CreateOption) error {
if viper.Get("ingress-api") == ExtensionAPI {
extIngressList := c.fromNetToExt(*obj)
err := c.client.Create(ctx, &extIngressList, opts...)
if err != nil {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above.

return err
}
return nil
}
return c.client.Create(ctx, obj, opts...)
}
Loading