Skip to content
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

init httproute controller #345

Merged
merged 8 commits into from
Mar 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,13 @@ preflight: ## Verifies required things like the go version

.PHONY: manifests
manifests: controller-gen ## Generate WebhookConfiguration, ClusterRole and CustomResourceDefinition objects.
$(CONTROLLER_GEN) rbac:roleName=ngrok-ingress-controller-manager-role crd webhook paths="{./api/ingress/v1alpha1/, ./internal/controller/ingress/, ./internal/controller/gateway}" \
$(CONTROLLER_GEN) rbac:roleName=ngrok-ingress-controller-manager-role crd webhook paths="{./api/ingress/v1alpha1/, ./internal/controller/ingress/, ./internal/controller/gateway/}" \
output:crd:artifacts:config=$(HELM_TEMPLATES_DIR)/crds \
output:rbac:artifacts:config=$(HELM_TEMPLATES_DIR)/rbac

.PHONY: generate
generate: controller-gen ## Generate code containing DeepCopy, DeepCopyInto, and DeepCopyObject method implementations.
$(CONTROLLER_GEN) object:headerFile="hack/boilerplate.go.txt" paths="{./api/ingress/v1alpha1/, ./internal/controller/ingress/, ./internal/controller/gateway}"
$(CONTROLLER_GEN) object:headerFile="hack/boilerplate.go.txt" paths="{./api/ingress/v1alpha1/, ./internal/controller/ingress/, ./internal/controller/gateway/}"

.PHONY: fmt
fmt: ## Run go fmt against code.
Expand Down
5 changes: 5 additions & 0 deletions PROJECT

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

11 changes: 11 additions & 0 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,17 @@ func runController(ctx context.Context, opts managerOpts) error {
setupLog.Error(err, "unable to create controller", "controller", "Gateway")
os.Exit(1)
}

if err = (&gatewaycontroller.HTTPRouteReconciler{
Client: mgr.GetClient(),
Log: ctrl.Log.WithName("controllers").WithName("Gateway"),
Scheme: mgr.GetScheme(),
Recorder: mgr.GetEventRecorderFor("gateway-controller"),
Driver: driver,
}).SetupWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "HTTPRoute")
os.Exit(1)
}
}
//+kubebuilder:scaffold:builder

Expand Down
27 changes: 27 additions & 0 deletions helm/ingress-controller/templates/rbac/role.yaml

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

20 changes: 10 additions & 10 deletions internal/controller/gateway/gateway_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,14 @@ type GatewayReconciler struct {
Driver *store.Driver
}

//+kubebuilder:rbac:groups="",resources=events,verbs=create;patch
//+kubebuilder:rbac:groups="",resources=configmaps,verbs=get;list;watch;create;update;delete
//+kubebuilder:rbac:groups="",resources=secrets,verbs=get;list;watch
//+kubebuilder:rbac:groups="",resources=services,verbs=get;list;watch
//+kubebuilder:rbac:groups=gateway.networking.k8s.io,resources=gateways,verbs=get;list;watch;update
//+kubebuilder:rbac:groups=gateway.networking.k8s.io,resources=gateways/status,verbs=get;list;watch;update
//+kubebuilder:rbac:groups=gateway.networking.k8s.io,resources=gatewayclasses,verbs=get;list;watch;update
//+kubebuilder:rbac:groups=gateway.networking.k8s.io,resources=gatewayclasses/status,verbs=get;list;watch;update
// +kubebuilder:rbac:groups="",resources=events,verbs=create;patch
// +kubebuilder:rbac:groups="",resources=configmaps,verbs=get;list;watch;create;update;delete
// +kubebuilder:rbac:groups="",resources=secrets,verbs=get;list;watch
// +kubebuilder:rbac:groups="",resources=services,verbs=get;list;watch
// +kubebuilder:rbac:groups=gateway.networking.k8s.io,resources=gateways,verbs=get;list;watch;update
// +kubebuilder:rbac:groups=gateway.networking.k8s.io,resources=gateways/status,verbs=get;list;watch;update
// +kubebuilder:rbac:groups=gateway.networking.k8s.io,resources=gatewayclasses,verbs=get;list;watch;update
// +kubebuilder:rbac:groups=gateway.networking.k8s.io,resources=gatewayclasses/status,verbs=get;list;watch;update

func (r *GatewayReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
log := r.Log.WithValues("Gateway", req.NamespacedName)
Expand Down Expand Up @@ -135,8 +135,8 @@ func (r *GatewayReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ct
// SetupWithManager sets up the controller with the Manager.
func (r *GatewayReconciler) SetupWithManager(mgr ctrl.Manager) error {
storedResources := []client.Object{
//&gatewayv1.GatewayClass{},
//&gatewayv1.HTTPRoute{},
&gatewayv1.GatewayClass{},
&gatewayv1.HTTPRoute{},
//&corev1.Service{},
&ingressv1alpha1.Domain{},
&ingressv1alpha1.HTTPSEdge{},
Expand Down
141 changes: 141 additions & 0 deletions internal/controller/gateway/httproute_controller.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
/*
MIT License

Copyright (c) 2022 ngrok, Inc.

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/

package gateway

import (
"context"

corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/tools/record"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
gatewayv1 "sigs.k8s.io/gateway-api/apis/v1"

"github.com/go-logr/logr"
ingressv1alpha1 "github.com/ngrok/kubernetes-ingress-controller/api/ingress/v1alpha1"
"github.com/ngrok/kubernetes-ingress-controller/internal/controller/utils"
"github.com/ngrok/kubernetes-ingress-controller/internal/store"
)

// HTTPRouteReconciler reconciles a HTTPRoute object
type HTTPRouteReconciler struct {
client.Client

Log logr.Logger
Scheme *runtime.Scheme
Recorder record.EventRecorder
Driver *store.Driver
}

// +kubebuilder:rbac:groups=gateway.networking.k8s.io,resources=httproutes,verbs=get;list;watch;update
// +kubebuilder:rbac:groups=gateway.networking.k8s.io,resources=httproutes/status,verbs=get;list;watch;update
// +kubebuilder:rbac:groups="",resources=namespaces,verbs=get;list;watch;update

func (r *HTTPRouteReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
log := r.Log.WithValues("HTTPRoute", req.NamespacedName)
ctx = ctrl.LoggerInto(ctx, log)

httproute := new(gatewayv1.HTTPRoute)
err := r.Client.Get(ctx, req.NamespacedName, httproute)
switch {
case err == nil:
// all good, continue
case client.IgnoreNotFound(err) == nil:
if err := r.Driver.DeleteNamedHTTPRoute(req.NamespacedName); err != nil {
log.Error(err, "Failed to delete httproute from store")
return ctrl.Result{}, err
}

err = r.Driver.Sync(ctx, r.Client)
if err != nil {
log.Error(err, "Failed to sync after removing httproute from store")
return ctrl.Result{}, err
}

return ctrl.Result{}, nil
default:
return ctrl.Result{}, err
}

httproute, err = r.Driver.UpdateHTTPRoute(httproute)
if err != nil {
return ctrl.Result{}, err
}

if utils.IsUpsert(httproute) {
// The object is not being deleted, so register and sync finalizer
if err := utils.RegisterAndSyncFinalizer(ctx, r.Client, httproute); err != nil {
log.Error(err, "Failed to register finalizer")
return ctrl.Result{}, err
}
} else {
log.Info("Deleting gateway from store")
if utils.HasFinalizer(httproute) {
if err := utils.RemoveAndSyncFinalizer(ctx, r.Client, httproute); err != nil {
log.Error(err, "Failed to remove finalizer")
return ctrl.Result{}, err
}
}

// Remove it from the store
if err := r.Driver.DeleteHTTPRoute(httproute); err != nil {
return ctrl.Result{}, err
}
}

if err := r.Driver.Sync(ctx, r.Client); err != nil {
log.Error(err, "Faild to sync")
return ctrl.Result{}, err
}

return ctrl.Result{}, nil
}

// SetupWithManager sets up the controller with the Manager.
func (r *HTTPRouteReconciler) SetupWithManager(mgr ctrl.Manager) error {
storedResources := []client.Object{
&gatewayv1.GatewayClass{},
&gatewayv1.Gateway{},
&corev1.Service{},
&ingressv1alpha1.Domain{},
&ingressv1alpha1.HTTPSEdge{},
&ingressv1alpha1.Tunnel{},
//&ingressv1alpha1.NgrokModuleSet{},
}

builder := ctrl.NewControllerManagedBy(mgr).For(&gatewayv1.HTTPRoute{})
for _, obj := range storedResources {
builder = builder.Watches(
obj,
store.NewUpdateStoreHandler(
obj.GetObjectKind().GroupVersionKind().Kind,
r.Driver,
r.Client,
),
)
}
return builder.Complete(r)
}
1 change: 0 additions & 1 deletion internal/store/cachestores.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ type CacheStores struct {
ServiceV1 cache.Store

// Gateway API Stores
//HTTPRoute cache.Store
Gateway cache.Store
HTTPRoute cache.Store

Expand Down
Loading
Loading