-
Notifications
You must be signed in to change notification settings - Fork 344
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
Changes from 4 commits
ad6a743
f5bc0eb
861723d
1c4b17a
706e57d
61dac4a
6c653f1
2a5286f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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{ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Whet if backend is not defined? In the |
||
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 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not necessary, could just |
||
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 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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...) | ||
} |
There was a problem hiding this comment.
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