Skip to content

Commit

Permalink
Add support for Google GCE provider (#148)
Browse files Browse the repository at this point in the history
* Initial commit for gce provider.

* Add README

* Add resource reader for gce

* Add converter in gce
  • Loading branch information
sawsa307 authored Jun 7, 2024
1 parent 21f9c46 commit 143ae87
Show file tree
Hide file tree
Showing 19 changed files with 1,164 additions and 20 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ Ingress2gateway also supports translating provider-specific resources and ingres
- [kong](pkg/i2gw/providers/kong/README.md)
- [istio](pkg/i2gw/providers/istio/README.md)
- [apisix](pkg/i2gw/providers/apisix/README.md)
- [gce](pkg/i2gw/providers/gce/README.md)

If your provider, or a specific feature, is not currently supported, please open an issue and describe your use case.

Expand Down
1 change: 1 addition & 0 deletions cmd/print.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import (

// Call init function for the providers
_ "github.com/kubernetes-sigs/ingress2gateway/pkg/i2gw/providers/apisix"
_ "github.com/kubernetes-sigs/ingress2gateway/pkg/i2gw/providers/gce"
_ "github.com/kubernetes-sigs/ingress2gateway/pkg/i2gw/providers/ingressnginx"
_ "github.com/kubernetes-sigs/ingress2gateway/pkg/i2gw/providers/istio"
_ "github.com/kubernetes-sigs/ingress2gateway/pkg/i2gw/providers/kong"
Expand Down
5 changes: 3 additions & 2 deletions pkg/i2gw/providers/apisix/resource_reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (

"github.com/kubernetes-sigs/ingress2gateway/pkg/i2gw"
"github.com/kubernetes-sigs/ingress2gateway/pkg/i2gw/providers/common"
"k8s.io/apimachinery/pkg/util/sets"
)

// converter implements the i2gw.CustomResourceReader interface.
Expand All @@ -39,7 +40,7 @@ func (r *resourceReader) readResourcesFromCluster(ctx context.Context) (*storage
// read apisix related resources from cluster.
storage := newResourcesStorage()

ingresses, err := common.ReadIngressesFromCluster(ctx, r.conf.Client, ApisixIngressClass)
ingresses, err := common.ReadIngressesFromCluster(ctx, r.conf.Client, sets.New(ApisixIngressClass))
if err != nil {
return nil, err
}
Expand All @@ -51,7 +52,7 @@ func (r *resourceReader) readResourcesFromFile(filename string) (*storage, error
// read apisix related resources from file.
storage := newResourcesStorage()

ingresses, err := common.ReadIngressesFromFile(filename, r.conf.Namespace, ApisixIngressClass)
ingresses, err := common.ReadIngressesFromFile(filename, r.conf.Namespace, sets.New[string](ApisixIngressClass))
if err != nil {
return nil, err
}
Expand Down
10 changes: 1 addition & 9 deletions pkg/i2gw/providers/common/converter.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (

"github.com/kubernetes-sigs/ingress2gateway/pkg/i2gw"
networkingv1 "k8s.io/api/networking/v1"
networkingv1beta1 "k8s.io/api/networking/v1beta1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/types"
Expand Down Expand Up @@ -135,14 +134,7 @@ type ingressPath struct {
}

func (a *ingressAggregator) addIngress(ingress networkingv1.Ingress) {
var ingressClass string
if ingress.Spec.IngressClassName != nil && *ingress.Spec.IngressClassName != "" {
ingressClass = *ingress.Spec.IngressClassName
} else if _, ok := ingress.Annotations[networkingv1beta1.AnnotationIngressClass]; ok {
ingressClass = ingress.Annotations[networkingv1beta1.AnnotationIngressClass]
} else {
ingressClass = ingress.Name
}
ingressClass := GetIngressClass(ingress)
for _, rule := range ingress.Spec.Rules {
a.addIngressRule(ingress.Namespace, ingress.Name, ingressClass, rule, ingress.Spec)
}
Expand Down
2 changes: 2 additions & 0 deletions pkg/i2gw/providers/common/converter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ func Test_ingresses2GatewaysAndHttpRoutes(t *testing.T) {
},
},
}},
IngressClassName: PtrTo("simple"),
},
}},
expectedGatewayResources: i2gw.GatewayResources{
Expand Down Expand Up @@ -150,6 +151,7 @@ func Test_ingresses2GatewaysAndHttpRoutes(t *testing.T) {
},
},
}},
IngressClassName: PtrTo("with-tls"),
},
}},
expectedGatewayResources: i2gw.GatewayResources{
Expand Down
9 changes: 5 additions & 4 deletions pkg/i2gw/providers/common/resource_reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,12 @@ import (
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/sets"
kubeyaml "k8s.io/apimachinery/pkg/util/yaml"
"sigs.k8s.io/controller-runtime/pkg/client"
)

func ReadIngressesFromCluster(ctx context.Context, client client.Client, ingressClass string) (map[types.NamespacedName]*networkingv1.Ingress, error) {
func ReadIngressesFromCluster(ctx context.Context, client client.Client, ingressClasses sets.Set[string]) (map[types.NamespacedName]*networkingv1.Ingress, error) {
var ingressList networkingv1.IngressList
err := client.List(ctx, &ingressList)
if err != nil {
Expand All @@ -41,7 +42,7 @@ func ReadIngressesFromCluster(ctx context.Context, client client.Client, ingress

ingresses := map[types.NamespacedName]*networkingv1.Ingress{}
for i, ingress := range ingressList.Items {
if GetIngressClass(ingress) != ingressClass {
if !ingressClasses.Has(GetIngressClass(ingress)) {
continue
}
ingresses[types.NamespacedName{Namespace: ingress.Namespace, Name: ingress.Name}] = &ingressList.Items[i]
Expand All @@ -50,7 +51,7 @@ func ReadIngressesFromCluster(ctx context.Context, client client.Client, ingress
return ingresses, nil
}

func ReadIngressesFromFile(filename, namespace, ingressClass string) (map[types.NamespacedName]*networkingv1.Ingress, error) {
func ReadIngressesFromFile(filename, namespace string, ingressClasses sets.Set[string]) (map[types.NamespacedName]*networkingv1.Ingress, error) {
stream, err := os.ReadFile(filename)
if err != nil {
return nil, fmt.Errorf("failed to read file %v: %w", filename, err)
Expand All @@ -70,7 +71,7 @@ func ReadIngressesFromFile(filename, namespace, ingressClass string) (map[types.
if err != nil {
return nil, err
}
if GetIngressClass(ingress) != ingressClass {
if !ingressClasses.Has(GetIngressClass(ingress)) {
continue
}
ingresses[types.NamespacedName{Namespace: ingress.Namespace, Name: ingress.Name}] = &ingress
Expand Down
2 changes: 1 addition & 1 deletion pkg/i2gw/providers/common/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func GetIngressClass(ingress networkingv1.Ingress) string {
} else if _, ok := ingress.Annotations[networkingv1beta1.AnnotationIngressClass]; ok {
ingressClass = ingress.Annotations[networkingv1beta1.AnnotationIngressClass]
} else {
ingressClass = ingress.Name
ingressClass = ""
}

return ingressClass
Expand Down
46 changes: 46 additions & 0 deletions pkg/i2gw/providers/gce/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# GCE Provider

The project supports translating ingress-gce specific annotations.

Currently supported annotations:
`kubernetes.io/ingress.class`: Though it is a deprecated annotation for most providers, GCE still uses this annotation to specify the specific type of load balancers created by GKE Ingress.

## Implementation-specific features

The following implementation-specific features are supported:

- Ingress path with type `ImplementationSpecific` will:
- Translate to equivalent Gateway Prefix path but dropping `/*`, if `/*` exists
- Translate to equivalent Exact path otherwise.

Examples:
| Ingress `ImplementationSpecific` Path | map to Gateway Path |
| ------------------------------------- | -------------------------------------- |
| /* | / Prefix |
| /v1 | /v1 Exact |
| /v1/ | /v1/ Exact |
| /v1/* | /v1 Prefix |

Note: For Ingress `ImplementationSpecific` path with `/v1/*`, it will map to
`/v1/` or `/v1/v2` but not `/v1`. If you want to avoid such behavior,
please consider switching to `Prefix`, `Exact`, or an `ImplementationSpecific`
path without `*` before converting Ingress to Gateway.

## Feature list
Currently supported:
- [Basic Internal Ingress](https://github.com/GoogleCloudPlatform/gke-networking-recipes/tree/main/ingress/single-cluster/ingress-internal-basic)
- [Basic external Ingress](https://github.com/GoogleCloudPlatform/gke-networking-recipes/tree/main/ingress/single-cluster/ingress-external-basic)
- [Ingress with custom default backend](https://github.com/GoogleCloudPlatform/gke-networking-recipes/tree/main/ingress/single-cluster/ingress-custom-default-backend)

To be supported:
- [Ingress with custom HTTP health check](https://github.com/GoogleCloudPlatform/gke-networking-recipes/tree/main/ingress/single-cluster/ingress-custom-http-health-check)
- [IAP enabled ingress](https://github.com/GoogleCloudPlatform/gke-networking-recipes/tree/main/ingress/single-cluster/ingress-iap)
- [Google Cloud Armor enabled ingress](https://github.com/GoogleCloudPlatform/gke-networking-recipes/blob/main/ingress/single-cluster/ingress-cloudarmor/README.md)
- [Ingress with HTTPS redirect](https://github.com/GoogleCloudPlatform/gke-networking-recipes/tree/main/ingress/single-cluster/ingress-https)

## Summary of GKE Ingress annotation
External Ingress:
https://cloud.google.com/kubernetes-engine/docs/how-to/load-balance-ingress#summary_of_external_ingress_annotations

Internal Ingress:
https://cloud.google.com/kubernetes-engine/docs/how-to/internal-load-balance-ingress#summary_of_internal_ingress_annotations
71 changes: 71 additions & 0 deletions pkg/i2gw/providers/gce/converter.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
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 gce

import (
"github.com/kubernetes-sigs/ingress2gateway/pkg/i2gw"
"github.com/kubernetes-sigs/ingress2gateway/pkg/i2gw/providers/common"
networkingv1 "k8s.io/api/networking/v1"
"k8s.io/apimachinery/pkg/util/validation/field"
)

// converter implements the ToGatewayAPI function of i2gw.ResourceConverter interface.
type converter struct {
conf *i2gw.ProviderConf

featureParsers []i2gw.FeatureParser
implementationSpecificOptions i2gw.ProviderImplementationSpecificOptions
}

// newConverter returns an ingress-gce converter instance.
func newConverter(conf *i2gw.ProviderConf) converter {
return converter{
conf: conf,
featureParsers: []i2gw.FeatureParser{},
implementationSpecificOptions: i2gw.ProviderImplementationSpecificOptions{
ToImplementationSpecificHTTPPathTypeMatch: implementationSpecificHTTPPathTypeMatch,
},
}
}

func (c *converter) convert(storage *storage) (i2gw.GatewayResources, 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.
gatewayResources, errs := common.ToGateway(ingressList, c.implementationSpecificOptions)
if len(errs) > 0 {
return i2gw.GatewayResources{}, errs
}

errs = setGCEGatewayClasses(ingressList, &gatewayResources)
if len(errs) > 0 {
return i2gw.GatewayResources{}, errs
}

for _, parseFeatureFunc := range c.featureParsers {
// Apply the feature parsing function to the gateway resources, one by one.
parseErrs := parseFeatureFunc(ingressList, &gatewayResources)
// Append the parsing errors to the error list.
errs = append(errs, parseErrs...)
}

return gatewayResources, errs
}
Loading

0 comments on commit 143ae87

Please sign in to comment.