Skip to content

Commit

Permalink
Merge pull request #370 from ngrok/del-bob/29099-modules-to-traffic-p…
Browse files Browse the repository at this point in the history
…olicy

modules to traffic policy
  • Loading branch information
OfTheDelmer authored May 1, 2024
2 parents 9d4be0d + 635f943 commit e820323
Show file tree
Hide file tree
Showing 18 changed files with 170 additions and 128 deletions.
4 changes: 3 additions & 1 deletion api/ingress/v1alpha1/httpsedge_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ SOFTWARE.
package v1alpha1

import (
"encoding/json"

"github.com/ngrok/ngrok-api-go/v5"
"golang.org/x/exp/slices"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand Down Expand Up @@ -78,7 +80,7 @@ type HTTPSEdgeRouteSpec struct {
// +kubebuilder:validation:Schemaless
// +kubebuilder:pruning:PreserveUnknownFields
// +kubebuilder:validation:Type=object
Policy EndpointTrafficPolicy `json:"policy,omitempty"`
Policy json.RawMessage `json:"policy,omitempty"`
}

// HTTPSEdgeSpec defines the desired state of HTTPSEdge
Expand Down
1 change: 0 additions & 1 deletion api/ingress/v1alpha1/ngrok_common.go
Original file line number Diff line number Diff line change
Expand Up @@ -431,7 +431,6 @@ func (amazon *EndpointOAuthAmazon) ToNgrok(clientSecret *string) *ngrok.Endpoint
return mod
}

type EndpointTrafficPolicy json.RawMessage
type EndpointPolicy struct {
// Determines if the rule will be applied to traffic
Enabled *bool `json:"enabled,omitempty"`
Expand Down
4 changes: 3 additions & 1 deletion api/ingress/v1alpha1/tcpedge_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ SOFTWARE.
package v1alpha1

import (
"encoding/json"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

Expand All @@ -47,7 +49,7 @@ type TCPEdgeSpec struct {
// +kubebuilder:validation:Schemaless
// +kubebuilder:pruning:PreserveUnknownFields
// +kubebuilder:validation:Type=object
Policy EndpointTrafficPolicy `json:"policy,omitempty"`
Policy json.RawMessage `json:"policy,omitempty"`
}

// TCPEdgeStatus defines the observed state of TCPEdge
Expand Down
4 changes: 3 additions & 1 deletion api/ingress/v1alpha1/tlsedge_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ SOFTWARE.
package v1alpha1

import (
"encoding/json"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

Expand Down Expand Up @@ -55,7 +57,7 @@ type TLSEdgeSpec struct {
// +kubebuilder:validation:Schemaless
// +kubebuilder:pruning:PreserveUnknownFields
// +kubebuilder:validation:Type=object
Policy EndpointTrafficPolicy `json:"policy,omitempty"`
Policy json.RawMessage `json:"policy,omitempty"`
}

// TLSEdgeStatus defines the observed state of TLSEdge
Expand Down
25 changes: 3 additions & 22 deletions api/ingress/v1alpha1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -322,12 +322,12 @@ func runController(ctx context.Context, opts managerOpts) error {

if err = (&ngrokctr.NgrokTrafficPolicyReconciler{
Client: mgr.GetClient(),
Log: ctrl.Log.WithName("controllers").WithName("Policy"),
Log: ctrl.Log.WithName("controllers").WithName("traffic-policy"),
Scheme: mgr.GetScheme(),
Recorder: mgr.GetEventRecorderFor("policy-controller"),
Driver: driver,
}).SetupWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "Policy")
setupLog.Error(err, "unable to create controller", "controller", "TrafficPolicy")
os.Exit(1)
}
//+kubebuilder:scaffold:builder
Expand Down
6 changes: 3 additions & 3 deletions helm/ingress-controller/Chart.lock
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
dependencies:
- name: common
repository: https://charts.bitnami.com/bitnami
version: 2.19.1
digest: sha256:8b1a425b039469d9fd0b0b48bd825df70bdcbe6f62e1f83b8c06653bfc472943
generated: "2024-03-27T10:54:59.497298868-05:00"
version: 2.19.2
digest: sha256:36ee26c48abc4337e3fbba948307c1f39201ba45f654b6eeeee5d96c664bf049
generated: "2024-04-30T12:50:41.520529-05:00"

This file was deleted.

24 changes: 23 additions & 1 deletion internal/annotations/annotations.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ limitations under the License.
package annotations

import (
"fmt"

"github.com/imdario/mergo"
ingressv1alpha1 "github.com/ngrok/kubernetes-ingress-controller/api/ingress/v1alpha1"
"github.com/ngrok/kubernetes-ingress-controller/internal/annotations/compression"
Expand Down Expand Up @@ -98,8 +100,28 @@ func (e Extractor) Extract(ing *networking.Ingress) *RouteModules {
return pia
}

// Extracts a list of moudule set names from the annotation
// Extracts a list of module set names from the annotation
// k8s.ngrok.com/modules: "module1,module2"
func ExtractNgrokModuleSetsFromAnnotations(ing *networking.Ingress) ([]string, error) {
return parser.GetStringSliceAnnotation("modules", ing)
}

// Extracts a single traffic policy str from the annotation
// k8s.ngrok.com/traffic-policy: "module1"
func ExtractNgrokTrafficPolicyFromAnnotations(ing *networking.Ingress) (string, error) {
policies, err := parser.GetStringSliceAnnotation("traffic-policy", ing)

if err != nil {
return "", err
}

if len(policies) > 1 {
return "", fmt.Errorf("multiple traffic policies are not supported: %v", policies)
}

if len(policies) != 0 {
return policies[0], nil
}

return "", nil
}
3 changes: 1 addition & 2 deletions internal/controller/ingress/httpsedge_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ package controllers

import (
"context"
"encoding/json"
"errors"
"fmt"
"reflect"
Expand Down Expand Up @@ -1052,7 +1051,7 @@ func (u *edgeRouteModuleUpdater) setEdgeRoutePolicy(ctx context.Context, route *
_, err := client.Replace(ctx, &ngrokapi.EdgeRoutePolicyRawReplace{
EdgeID: route.EdgeID,
ID: route.ID,
Module: json.RawMessage(policy),
Module: policy,
})
return err
}
3 changes: 3 additions & 0 deletions internal/controller/ingress/ingress_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

"github.com/go-logr/logr"
ingressv1alpha1 "github.com/ngrok/kubernetes-ingress-controller/api/ingress/v1alpha1"
ngrokv1alpha1 "github.com/ngrok/kubernetes-ingress-controller/api/ngrok/v1alpha1"
"github.com/ngrok/kubernetes-ingress-controller/internal/annotations"
"github.com/ngrok/kubernetes-ingress-controller/internal/controller/controllers"
internalerrors "github.com/ngrok/kubernetes-ingress-controller/internal/errors"
Expand Down Expand Up @@ -37,6 +38,7 @@ func (r *IngressReconciler) SetupWithManager(mgr ctrl.Manager) error {
&ingressv1alpha1.HTTPSEdge{},
&ingressv1alpha1.Tunnel{},
&ingressv1alpha1.NgrokModuleSet{},
&ngrokv1alpha1.NgrokTrafficPolicy{},
}

builder := ctrl.NewControllerManagedBy(mgr).For(&netv1.Ingress{})
Expand All @@ -57,6 +59,7 @@ func (r *IngressReconciler) SetupWithManager(mgr ctrl.Manager) error {
// +kubebuilder:rbac:groups="networking.k8s.io",resources=ingresses/status,verbs=get;list;watch;update
// +kubebuilder:rbac:groups="networking.k8s.io",resources=ingressclasses,verbs=get;list;watch
// +kubebuilder:rbac:groups=ingress.k8s.ngrok.com,resources=ngrokmodulesets,verbs=get;list;watch
// +kubebuilder:rbac:groups=ngrok.k8s.ngrok.com,resources=ngroktrafficpolicies,verbs=get;list;watch

// This reconcile function is called by the controller-runtime manager.
// It is invoked whenever there is an event that occurs for a resource
Expand Down
3 changes: 1 addition & 2 deletions internal/controller/ingress/tcpedge_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ package controllers

import (
"context"
"encoding/json"
"fmt"

"golang.org/x/exp/maps"
Expand Down Expand Up @@ -406,7 +405,7 @@ func (r *TCPEdgeReconciler) updatePolicyModule(ctx context.Context, edge *ingres
r.Log.Info("Updating Policy module")
_, err := client.Replace(ctx, &ngrokapi.EdgeRawTCPPolicyReplace{
ID: remoteEdge.ID,
Module: json.RawMessage(policy),
Module: policy,
})

return err
Expand Down
3 changes: 1 addition & 2 deletions internal/controller/ingress/tlsedge_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ package controllers

import (
"context"
"encoding/json"
"errors"

"golang.org/x/exp/maps"
Expand Down Expand Up @@ -417,7 +416,7 @@ func (r *TLSEdgeReconciler) updatePolicyModule(ctx context.Context, edge *ingres
r.Log.Info("Updating Policy module")
_, err := client.Replace(ctx, &ngrokapi.EdgeRawTLSPolicyReplace{
ID: remoteEdge.ID,
Module: json.RawMessage(policy),
Module: policy,
})
return err
}
29 changes: 19 additions & 10 deletions internal/store/cachestores.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (

"github.com/go-logr/logr"
ingressv1alpha1 "github.com/ngrok/kubernetes-ingress-controller/api/ingress/v1alpha1"
ngrokv1alpha1 "github.com/ngrok/kubernetes-ingress-controller/api/ngrok/v1alpha1"
corev1 "k8s.io/api/core/v1"
netv1 "k8s.io/api/networking/v1"
"k8s.io/apimachinery/pkg/runtime"
Expand All @@ -40,10 +41,11 @@ type CacheStores struct {
HTTPRoute cache.Store

// Ngrok Stores
DomainV1 cache.Store
TunnelV1 cache.Store
HTTPSEdgeV1 cache.Store
NgrokModuleV1 cache.Store
DomainV1 cache.Store
TunnelV1 cache.Store
HTTPSEdgeV1 cache.Store
NgrokModuleV1 cache.Store
NgrokTrafficPolicyV1 cache.Store

log logr.Logger
l *sync.RWMutex
Expand All @@ -60,12 +62,13 @@ func NewCacheStores(logger logr.Logger) CacheStores {
Gateway: cache.NewStore(keyFunc),
HTTPRoute: cache.NewStore(keyFunc),
// Ngrok Stores
DomainV1: cache.NewStore(keyFunc),
TunnelV1: cache.NewStore(keyFunc),
HTTPSEdgeV1: cache.NewStore(keyFunc),
NgrokModuleV1: cache.NewStore(keyFunc),
l: &sync.RWMutex{},
log: logger,
DomainV1: cache.NewStore(keyFunc),
TunnelV1: cache.NewStore(keyFunc),
HTTPSEdgeV1: cache.NewStore(keyFunc),
NgrokModuleV1: cache.NewStore(keyFunc),
NgrokTrafficPolicyV1: cache.NewStore(keyFunc),
l: &sync.RWMutex{},
log: logger,
}
}

Expand Down Expand Up @@ -121,6 +124,8 @@ func (c CacheStores) Get(obj runtime.Object) (item interface{}, exists bool, err
return c.HTTPSEdgeV1.Get(obj)
case *ingressv1alpha1.NgrokModuleSet:
return c.NgrokModuleV1.Get(obj)
case *ngrokv1alpha1.NgrokTrafficPolicy:
return c.NgrokTrafficPolicyV1.Get(obj)
default:
return nil, false, fmt.Errorf("unsupported object type: %T", obj)
}
Expand Down Expand Up @@ -162,6 +167,8 @@ func (c CacheStores) Add(obj runtime.Object) error {
return c.HTTPSEdgeV1.Add(obj)
case *ingressv1alpha1.NgrokModuleSet:
return c.NgrokModuleV1.Add(obj)
case *ngrokv1alpha1.NgrokTrafficPolicy:
return c.NgrokTrafficPolicyV1.Add(obj)

default:
return fmt.Errorf("unsupported object type: %T", obj)
Expand Down Expand Up @@ -204,6 +211,8 @@ func (c CacheStores) Delete(obj runtime.Object) error {
return c.HTTPSEdgeV1.Delete(obj)
case *ingressv1alpha1.NgrokModuleSet:
return c.NgrokModuleV1.Delete(obj)
case *ngrokv1alpha1.NgrokTrafficPolicy:
return c.NgrokTrafficPolicyV1.Delete(obj)
default:
return fmt.Errorf("unsupported object type: %T", obj)
}
Expand Down
Loading

0 comments on commit e820323

Please sign in to comment.