Skip to content

Commit

Permalink
Merge pull request #364 from ngrok/del/28005-policy-controller
Browse files Browse the repository at this point in the history
initial policy controller update
  • Loading branch information
OfTheDelmer authored Apr 26, 2024
2 parents 1451f9a + eef2cd8 commit 6de5646
Show file tree
Hide file tree
Showing 12 changed files with 64 additions and 142 deletions.
8 changes: 0 additions & 8 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -106,14 +106,6 @@ ifndef ignore-not-found
ignore-not-found = false
endif

.PHONY: install
install: manifests kustomize ## Install CRDs into the K8s cluster specified in ~/.kube/config.
$(KUSTOMIZE) build config/crd | kubectl apply -f -

.PHONY: uninstall
uninstall: manifests kustomize ## Uninstall CRDs from the K8s cluster specified in ~/.kube/config. Call with ignore-not-found=true to ignore resource not found errors during deletion.
$(KUSTOMIZE) build config/crd | kubectl delete --ignore-not-found=$(ignore-not-found) -f -

.PHONY: deploy
deploy: _deploy-check-env-vars docker-build manifests kustomize _helm_setup ## Deploy controller to the K8s cluster specified in ~/.kube/config.
helm upgrade ngrok-ingress-controller $(HELM_CHART_DIR) --install \
Expand Down
11 changes: 9 additions & 2 deletions api/ngrok/v1alpha1/ngroktrafficpolicy_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,20 @@ type NgrokTrafficPolicySpec struct {
// INSERT ADDITIONAL SPEC FIELDS - desired state of cluster
// Important: Run "make" to regenerate code after modifying this file

// The raw json encoded policy that was applied to the ngrok API
// +kubebuilder:validation:Schemaless
// +kubebuilder:pruning:PreserveUnknownFields
// +kubebuilder:validation:Type=object
Policy json.RawMessage `json:"policy,omitempty"`
}

// NgrokTrafficPolicyStatus defines the observed state of NgrokTrafficPolicy
type NgrokTrafficPolicyStatus struct {
// INSERT ADDITIONAL STATUS FIELD - define observed state of cluster
// Important: Run "make" to regenerate code after modifying this file

// The raw json encoded policy that was applied to the ngrok API
// +kubebuilder:validation:Schemaless
// +kubebuilder:pruning:PreserveUnknownFields
// +kubebuilder:validation:Type=object
Policy json.RawMessage `json:"policy,omitempty"`
}

Expand Down
12 changes: 12 additions & 0 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ import (
"github.com/ngrok/kubernetes-ingress-controller/internal/annotations"
gatewaycontroller "github.com/ngrok/kubernetes-ingress-controller/internal/controller/gateway"
controllers "github.com/ngrok/kubernetes-ingress-controller/internal/controller/ingress"
ngrokctr "github.com/ngrok/kubernetes-ingress-controller/internal/controller/ngrok"
"github.com/ngrok/kubernetes-ingress-controller/internal/ngrokapi"
"github.com/ngrok/kubernetes-ingress-controller/internal/store"
"github.com/ngrok/kubernetes-ingress-controller/internal/version"
Expand Down Expand Up @@ -318,6 +319,17 @@ func runController(ctx context.Context, opts managerOpts) error {
os.Exit(1)
}
}

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

if err := mgr.AddReadyzCheck("readyz", func(req *http.Request) error {
Expand Down
23 changes: 0 additions & 23 deletions config/crd/kustomization.yaml

This file was deleted.

19 changes: 0 additions & 19 deletions config/crd/kustomizeconfig.yaml

This file was deleted.

31 changes: 0 additions & 31 deletions config/rbac/ngrok_ngroktrafficpolicy_editor_role.yaml

This file was deleted.

27 changes: 0 additions & 27 deletions config/rbac/ngrok_ngroktrafficpolicy_viewer_role.yaml

This file was deleted.

4 changes: 0 additions & 4 deletions config/samples/kustomization.yaml

This file was deleted.

12 changes: 0 additions & 12 deletions config/samples/ngrok_v1alpha1_ngroktrafficpolicy.yaml

This file was deleted.

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

17 changes: 11 additions & 6 deletions internal/controller/ngrok/ngroktrafficpolicy_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,23 @@ package ngrok
import (
"context"

"github.com/go-logr/logr"
ngrokv1alpha1 "github.com/ngrok/kubernetes-ingress-controller/api/ngrok/v1alpha1"
"github.com/ngrok/kubernetes-ingress-controller/internal/store"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/tools/record"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/log"

ngrokv1alpha1 "github.com/ngrok/kubernetes-ingress-controller/api/ngrok/v1alpha1"
)

// NgrokTrafficPolicyReconciler reconciles a NgrokTrafficPolicy object
type NgrokTrafficPolicyReconciler struct {
client.Client
Scheme *runtime.Scheme
Log logr.Logger
Scheme *runtime.Scheme
Recorder record.EventRecorder
Driver *store.Driver
}

//+kubebuilder:rbac:groups=ngrok.k8s.ngrok.com,resources=ngroktrafficpolicies,verbs=get;list;watch;create;update;patch;delete
Expand All @@ -57,14 +62,14 @@ type NgrokTrafficPolicyReconciler struct {
func (r *NgrokTrafficPolicyReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
_ = log.FromContext(ctx)

// TODO(user): your logic here

return ctrl.Result{}, nil
err := r.Driver.SyncEdges(ctx, r.Client)
return ctrl.Result{}, err
}

// SetupWithManager sets up the controller with the Manager.
func (r *NgrokTrafficPolicyReconciler) SetupWithManager(mgr ctrl.Manager) error {
return ctrl.NewControllerManagedBy(mgr).
For(&ngrokv1alpha1.NgrokTrafficPolicy{}).
WithEventFilter(commonPredicateFilters).
Complete(r)
}
24 changes: 24 additions & 0 deletions internal/controller/ngrok/policy_predicates.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package ngrok

import (
"sigs.k8s.io/controller-runtime/pkg/event"
"sigs.k8s.io/controller-runtime/pkg/predicate"
)

// common update func predicate filter
func updateFuncPredicateFilter(ue event.UpdateEvent) bool {
// First check if there are any annotations present that aren't in the old version
oldAnnotations := ue.ObjectOld.GetAnnotations()
for newKey, newValue := range ue.ObjectNew.GetAnnotations() {
if oldAnnotations[newKey] != newValue {
return true
}
}
// No change to spec, so we can ignore. This does not filter out updates
// that set metadata.deletionTimestamp, so this won't undermine finalizer.
return ue.ObjectNew.GetGeneration() != ue.ObjectOld.GetGeneration()
}

var commonPredicateFilters = predicate.Funcs{
UpdateFunc: updateFuncPredicateFilter,
}

0 comments on commit 6de5646

Please sign in to comment.