-
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
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
ad6a743
Migrate Ingress from API extensions/v1beta1 to networking.k8s.io/v1beta1
rubenvp8510 f5bc0eb
Add network.k8s.io ingress api detection
rubenvp8510 861723d
Add client layer for decide which version of API ingress to use
rubenvp8510 1c4b17a
Ingress new/old api tests
rubenvp8510 706e57d
Set used api when the client is created, minor format issues
rubenvp8510 61dac4a
Fix lint issues
rubenvp8510 6c653f1
Change the API detection to run once at start
rubenvp8510 2a5286f
Avoid string assertion on viper.get
rubenvp8510 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
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,172 @@ | ||
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 | ||
extensionAPI bool | ||
} | ||
|
||
// NewIngressClient Creates a new Ingress.client wrapper. | ||
func NewIngressClient(client client.Client, reader client.Reader) *Client { | ||
return &Client{ | ||
client: client, | ||
rClient: reader, | ||
extensionAPI: viper.GetString("ingress-api") == ExtensionAPI, | ||
} | ||
} | ||
|
||
func (c *Client) fromExtToNet(ingress extv1beta.Ingress) netv1beta.Ingress { | ||
oldIngress := netv1beta.Ingress{ | ||
TypeMeta: metav1.TypeMeta{ | ||
Kind: "Ingress", | ||
APIVersion: "extensions/v1beta1", | ||
}, | ||
ObjectMeta: ingress.ObjectMeta, | ||
} | ||
|
||
if ingress.Spec.Backend != nil { | ||
oldIngress.Spec = netv1beta.IngressSpec{ | ||
Backend: &netv1beta.IngressBackend{ | ||
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 c.extensionAPI { | ||
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 c.extensionAPI { | ||
extIngressList := c.fromNetToExt(*obj) | ||
return c.client.Update(ctx, &extIngressList, opts...) | ||
} | ||
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 c.extensionAPI { | ||
extIngressList := c.fromNetToExt(*obj) | ||
return c.client.Delete(ctx, &extIngressList, opts...) | ||
} | ||
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 c.extensionAPI { | ||
extIngressList := c.fromNetToExt(*obj) | ||
return c.client.Create(ctx, &extIngressList, opts...) | ||
} | ||
return c.client.Create(ctx, obj, opts...) | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Whet if backend is not defined? In the
fromNetToExt
ingress.Spec.Backend
is checked to see if nil.