-
Notifications
You must be signed in to change notification settings - Fork 143
Add Cilium to ingress2gateway #199
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
Merged
Merged
Changes from 6 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
98e165e
Cilium basic structure
xtineskim 60cc9e3
Add base for Cilium
xtineskim f991928
Add Cilium to IR
xtineskim 4ab193c
Add force-https annotation test
xtineskim 72b3a73
Add README for Cilium
xtineskim fbcdff8
Added missing boilerplate
xtineskim 56f3621
Fix comments
xtineskim 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,21 @@ | ||
| /* | ||
| Copyright 2024 The Kubernetes Authors. | ||
|
|
||
| Licensed under the Apache License, Version 2.0 (the "License"); | ||
| you may not use this file except in compliance with the License. | ||
| You may obtain a copy of the License at | ||
|
|
||
| http://www.apache.org/licenses/LICENSE-2.0 | ||
|
|
||
| Unless required by applicable law or agreed to in writing, software | ||
| distributed under the License is distributed on an "AS IS" BASIS, | ||
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| See the License for the specific language governing permissions and | ||
| limitations under the License. | ||
| */ | ||
|
|
||
| package intermediate | ||
|
|
||
| type CiliumGatewayIR struct{} | ||
| type CiliumHTTPRouteIR struct{} | ||
| type CiliumServiceIR struct{} |
This file contains hidden or 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,8 @@ | ||
| # Cilium Provider | ||
|
|
||
| The project supports translating [Cilium](https://github.com/cilium/cilium) specific annotations. | ||
|
|
||
| ## Supported Annotations | ||
|
|
||
| - `ingress.cilium.io/force-https:`: This annotation redirects HTTP requests to HTTPS with a `301` status code. | ||
|
|
This file contains hidden or 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,27 @@ | ||
| /* | ||
| Copyright 2024 The Kubernetes Authors. | ||
|
|
||
| Licensed under the Apache License, Version 2.0 (the "License"); | ||
| you may not use this file except in compliance with the License. | ||
| You may obtain a copy of the License at | ||
|
|
||
| http://www.apache.org/licenses/LICENSE-2.0 | ||
|
|
||
| Unless required by applicable law or agreed to in writing, software | ||
| distributed under the License is distributed on an "AS IS" BASIS, | ||
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| See the License for the specific language governing permissions and | ||
| limitations under the License. | ||
| */ | ||
|
|
||
| package cilium | ||
|
|
||
| import "fmt" | ||
|
|
||
| const ( | ||
| annotationPrefix = "ingress.cilium.io" | ||
| ) | ||
|
|
||
| func ciliumAnnotation(suffix string) string { | ||
| return fmt.Sprintf("%s/%s", annotationPrefix, suffix) | ||
| } |
This file contains hidden or 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,81 @@ | ||
| /* | ||
| Copyright 2023 The Kubernetes Authors. | ||
|
|
||
| Licensed under the Apache License, Version 2.0 (the "License"); | ||
| you may not use this file except in compliance with the License. | ||
| You may obtain a copy of the License at | ||
|
|
||
| http://www.apache.org/licenses/LICENSE-2.0 | ||
|
|
||
| Unless required by applicable law or agreed to in writing, software | ||
| distributed under the License is distributed on an "AS IS" BASIS, | ||
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| See the License for the specific language governing permissions and | ||
| limitations under the License. | ||
| */ | ||
|
|
||
| package cilium | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
|
|
||
| "github.com/kubernetes-sigs/ingress2gateway/pkg/i2gw" | ||
| "github.com/kubernetes-sigs/ingress2gateway/pkg/i2gw/intermediate" | ||
| "github.com/kubernetes-sigs/ingress2gateway/pkg/i2gw/providers/common" | ||
| "k8s.io/apimachinery/pkg/util/validation/field" | ||
| ) | ||
|
|
||
| // The Name of the provider. | ||
| const Name = "cilium" | ||
| const CiliumIngressClass = "cilium" | ||
|
|
||
| func init() { | ||
| i2gw.ProviderConstructorByName[Name] = NewProvider | ||
| } | ||
|
|
||
| // Provider implements the i2gw.Provider interface. | ||
| type Provider struct { | ||
| storage *storage | ||
| resourceReader *resourceReader | ||
| resourcesToIRConverter *resourcesToIRConverter | ||
| } | ||
|
|
||
| // NewProvider constructs and returns the apisix implementation of i2gw.Provider. | ||
| func NewProvider(conf *i2gw.ProviderConf) i2gw.Provider { | ||
| return &Provider{ | ||
| storage: newResourcesStorage(), | ||
| resourceReader: newResourceReader(conf), | ||
| resourcesToIRConverter: newResourcesToIRConverter(), | ||
| } | ||
| } | ||
|
|
||
| // ToIR converts stored Cilium API entities to intermediate.IR | ||
| // including the cilium specific features. | ||
| func (p *Provider) ToIR() (intermediate.IR, field.ErrorList) { | ||
| return p.resourcesToIRConverter.convertToIR(p.storage) | ||
| } | ||
|
|
||
| func (p *Provider) ToGatewayResources(ir intermediate.IR) (i2gw.GatewayResources, field.ErrorList) { | ||
| return common.ToGatewayResources(ir) | ||
| } | ||
|
|
||
| func (p *Provider) ReadResourcesFromCluster(ctx context.Context) error { | ||
| storage, err := p.resourceReader.readResourcesFromCluster(ctx) | ||
| if err != nil { | ||
| return fmt.Errorf("failed to read resources from cluster: %w", err) | ||
| } | ||
|
|
||
| p.storage = storage | ||
| return nil | ||
| } | ||
|
|
||
| func (p *Provider) ReadResourcesFromFile(_ context.Context, filename string) error { | ||
| storage, err := p.resourceReader.readResourcesFromFile(filename) | ||
| if err != nil { | ||
| return fmt.Errorf("failed to read resources from file: %w", err) | ||
| } | ||
|
|
||
| p.storage = storage | ||
| return nil | ||
| } | ||
This file contains hidden or 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,65 @@ | ||
| /* | ||
| Copyright 2023 The Kubernetes Authors. | ||
|
|
||
| Licensed under the Apache License, Version 2.0 (the "License"); | ||
| you may not use this file except in compliance with the License. | ||
| You may obtain a copy of the License at | ||
|
|
||
| http://www.apache.org/licenses/LICENSE-2.0 | ||
|
|
||
| Unless required by applicable law or agreed to in writing, software | ||
| distributed under the License is distributed on an "AS IS" BASIS, | ||
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| See the License for the specific language governing permissions and | ||
| limitations under the License. | ||
| */ | ||
|
|
||
| package cilium | ||
|
|
||
| import ( | ||
| "github.com/kubernetes-sigs/ingress2gateway/pkg/i2gw" | ||
| "github.com/kubernetes-sigs/ingress2gateway/pkg/i2gw/intermediate" | ||
| "github.com/kubernetes-sigs/ingress2gateway/pkg/i2gw/providers/common" | ||
| networkingv1 "k8s.io/api/networking/v1" | ||
| "k8s.io/apimachinery/pkg/util/validation/field" | ||
| ) | ||
|
|
||
| // resourcesToIRConverter implements the ToIR function of i2gw.ResourcesToIRConverter interface. | ||
| type resourcesToIRConverter struct { | ||
| featureParsers []i2gw.FeatureParser | ||
| implementationSpecificOptions i2gw.ProviderImplementationSpecificOptions | ||
| } | ||
|
|
||
| // newResourcesToIRConverter returns a cilium resourcesToIRConverter instance. | ||
| func newResourcesToIRConverter() *resourcesToIRConverter { | ||
| return &resourcesToIRConverter{ | ||
| featureParsers: []i2gw.FeatureParser{ | ||
| forceHTTPSFeature, | ||
| }, | ||
| implementationSpecificOptions: i2gw.ProviderImplementationSpecificOptions{ | ||
| // The list of the implementationSpecific ingress fields options comes here. | ||
| }, | ||
| } | ||
| } | ||
|
|
||
| func (c *resourcesToIRConverter) convertToIR(storage *storage) (intermediate.IR, field.ErrorList) { | ||
| ingressList := []networkingv1.Ingress{} | ||
| for _, ing := range storage.Ingresses { | ||
| ingressList = append(ingressList, *ing) | ||
| } | ||
| // Convert plain ingress resources to gateway resources, ignoring all | ||
| // provider-specific features. | ||
| ir, errs := common.ToIR(ingressList, c.implementationSpecificOptions) | ||
| if len(errs) > 0 { | ||
| return intermediate.IR{}, errs | ||
| } | ||
|
|
||
| for _, parseFeatureFunc := range c.featureParsers { | ||
| // Apply the feature parsing function to the gateway resources, one by one. | ||
| parseErrs := parseFeatureFunc(ingressList, &ir) | ||
| // Append the parsing errors to the error list. | ||
| errs = append(errs, parseErrs...) | ||
| } | ||
|
|
||
| return ir, errs | ||
| } |
This file contains hidden or 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,70 @@ | ||
| /* | ||
| Copyright 2024 The Kubernetes Authors. | ||
|
|
||
| Licensed under the Apache License, Version 2.0 (the "License"); | ||
| you may not use this file except in compliance with the License. | ||
| You may obtain a copy of the License at | ||
|
|
||
| http://www.apache.org/licenses/LICENSE-2.0 | ||
|
|
||
| Unless required by applicable law or agreed to in writing, software | ||
| distributed under the License is distributed on an "AS IS" BASIS, | ||
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| See the License for the specific language governing permissions and | ||
| limitations under the License. | ||
| */ | ||
|
|
||
| package cilium | ||
|
|
||
| import ( | ||
| "fmt" | ||
|
|
||
| "github.com/kubernetes-sigs/ingress2gateway/pkg/i2gw/intermediate" | ||
| "github.com/kubernetes-sigs/ingress2gateway/pkg/i2gw/notifications" | ||
| "github.com/kubernetes-sigs/ingress2gateway/pkg/i2gw/providers/common" | ||
| networkingv1 "k8s.io/api/networking/v1" | ||
| "k8s.io/apimachinery/pkg/types" | ||
| "k8s.io/apimachinery/pkg/util/validation/field" | ||
| "k8s.io/utils/ptr" | ||
| gatewayv1 "sigs.k8s.io/gateway-api/apis/v1" | ||
| ) | ||
|
|
||
| func forceHTTPSFeature(ingresses []networkingv1.Ingress, ir *intermediate.IR) field.ErrorList { | ||
| var errs field.ErrorList | ||
| forceHTTPSAnnotation := ciliumAnnotation("force-https") | ||
| ruleGroups := common.GetRuleGroups(ingresses) | ||
| for _, rg := range ruleGroups { | ||
|
|
||
| for _, rule := range rg.Rules { | ||
| if val, annotationFound := rule.Ingress.Annotations[forceHTTPSAnnotation]; val == "enabled" || val == "true" { | ||
| if rule.Ingress.Spec.Rules == nil { | ||
| continue | ||
| } | ||
| key := types.NamespacedName{Namespace: rule.Ingress.Namespace, Name: common.RouteName(rg.Name, rg.Host)} | ||
|
|
||
| httpRoute, ok := ir.HTTPRoutes[key] | ||
| if !ok { | ||
| errs = append(errs, field.NotFound(field.NewPath("HTTPRoute"), key)) | ||
| } | ||
|
|
||
| for i, rule := range httpRoute.Spec.Rules { | ||
| rule.Filters = append(rule.Filters, gatewayv1.HTTPRouteFilter{ | ||
| Type: gatewayv1.HTTPRouteFilterRequestRedirect, | ||
| RequestRedirect: &gatewayv1.HTTPRequestRedirectFilter{ | ||
| Scheme: ptr.To("https"), | ||
| StatusCode: ptr.To(int(301)), | ||
| }, | ||
| }) | ||
| rule.BackendRefs = nil | ||
|
|
||
| httpRoute.Spec.Rules[i] = rule | ||
|
|
||
| } | ||
| if annotationFound && ok { | ||
| notify(notifications.InfoNotification, fmt.Sprintf("parsed \"%v\" annotation of ingress and patched %v fields", forceHTTPSAnnotation, field.NewPath("httproute", "spec", "rules").Key("").Child("filters")), &httpRoute) | ||
| } | ||
| } | ||
| } | ||
| } | ||
| return errs | ||
| } |
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.
Uh oh!
There was an error while loading. Please reload this page.