Skip to content

Commit

Permalink
chore: setup natsgateway reconciler
Browse files Browse the repository at this point in the history
  • Loading branch information
katallaxie authored Dec 3, 2024
1 parent 0bfada2 commit 575f085
Show file tree
Hide file tree
Showing 7 changed files with 125 additions and 41 deletions.
2 changes: 1 addition & 1 deletion api/v1alpha1/nats_gateway_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const (
type NatsGatewaySpec struct {
URL string `json:"url"`
Name string `json:"name,omitempty"`
Username SecretValueFromSource `json:"username"`
Username string `json:"username"`
Password SecretValueFromSource `json:"password"`
}

Expand Down
1 change: 0 additions & 1 deletion api/v1alpha1/zz_generated.deepcopy.go

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

13 changes: 6 additions & 7 deletions controllers/const.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
package controllers

const (
NATZ_OPERATOR_FINALIZER = "natz.zeiss.com/natz-operator"
OPERATOR_SEED_KEY = "seed.nk"
OPERATOR_PUBLIC_KEY = "key.pub"
OPERATOR_JWT = "key.jwt"
OPERATOR_CREDS = "user.creds"
OPERATOR_CONFIG_FILE = "auth.conf"
AUTH_CONFIG_TEMPLATE = `operator: %s
OPERATOR_SEED_KEY = "seed.nk"
OPERATOR_PUBLIC_KEY = "key.pub"
OPERATOR_JWT = "key.jwt"
OPERATOR_CREDS = "user.creds"
OPERATOR_CONFIG_FILE = "auth.conf"
AUTH_CONFIG_TEMPLATE = `operator: %s
system_account: %s
resolver {
type: full
Expand Down
94 changes: 94 additions & 0 deletions controllers/natsgateway_controller.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
package controllers

import (
"context"

corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
"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"
"sigs.k8s.io/controller-runtime/pkg/reconcile"

natsv1alpha1 "github.com/zeiss/natz-operator/api/v1alpha1"
"github.com/zeiss/pkg/k8s/finalizers"
)

const (
EventReasonGatewaySucceeded = "GatewaySucceeded"
EventReasonGatewayFailed = "GatewayFailed"
)

// NatsGatewayReconciler ...
type NatsGatewayReconciler struct {
client.Client
Scheme *runtime.Scheme
Recorder record.EventRecorder
}

// NewNatsGatewayReconciler ...
func NewNatsGatewayReconciler(mgr ctrl.Manager) *NatsGatewayReconciler {
return &NatsGatewayReconciler{
Client: mgr.GetClient(),
Scheme: mgr.GetScheme(),
Recorder: mgr.GetEventRecorderFor(EventRecorderLabel),
}
}

//+kubebuilder:rbac:groups=natz.zeiss.com,resources=natsgateways,verbs=get;list;watch;create;update;patch;delete
//+kubebuilder:rbac:groups=natz.zeiss.com,resources=natsgateways/status,verbs=get;update;patch
//+kubebuilder:rbac:groups=natz.zeiss.com,resources=natsgateways/finalizers,verbs=update

// Reconcile ...
func (r *NatsGatewayReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
log := log.FromContext(ctx)

gateway := &natsv1alpha1.NatsGateway{}
if err := r.Get(ctx, req.NamespacedName, gateway); err != nil {
if errors.IsNotFound(err) {
return ctrl.Result{}, nil
}

return ctrl.Result{}, err
}

if !gateway.ObjectMeta.DeletionTimestamp.IsZero() {
log.Info("processing deletion of gateway")

if finalizers.HasFinalizer(gateway, natsv1alpha1.FinalizerName) {
err := r.reconcileDelete(ctx, gateway)
if err != nil {
return ctrl.Result{}, err
}
}

// Delete
return reconcile.Result{}, nil
}

return reconcile.Result{}, nil
}

func (r *NatsGatewayReconciler) reconcileDelete(ctx context.Context, gateway *natsv1alpha1.NatsGateway) error {
log := log.FromContext(ctx)

log.Info("reconcile delete gateway", "name", gateway.Name, "namespace", gateway.Namespace)

gateway.SetFinalizers(finalizers.RemoveFinalizer(gateway, natsv1alpha1.FinalizerName))
err := r.Update(ctx, gateway)
if err != nil && !errors.IsNotFound(err) {
return err
}

return nil
}

// SetupWithManager sets up the controller with the Manager.
func (r *NatsGatewayReconciler) SetupWithManager(mgr ctrl.Manager) error {
return ctrl.NewControllerManagedBy(mgr).
For(&natsv1alpha1.NatsAccount{}).
Owns(&corev1.Secret{}).
Complete(r)
}
18 changes: 18 additions & 0 deletions examples/gateway.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
apiVersion: v1
kind: Secret
metadata:
name: gateway-north-secret
data:
key: NjJlYjE2NWMwNzBhNDFkNWMxYjU4ZDlkM2Q3MjVjYTE=
---
apiVersion: natz.zeiss.com/v1alpha1
kind: NatsGateway
metadata:
name: harry
spec:
url: nats://nats.north:4222
username: harry
password:
secretKeyRef:
key: key
name: gateway-north-secret
29 changes: 1 addition & 28 deletions manifests/crd/bases/natz.zeiss.com_natsgateways.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -71,34 +71,7 @@ spec:
url:
type: string
username:
description: SecretValueFromSource represents the source of a secret
value
properties:
secretKeyRef:
description: The Secret key to select from.
properties:
key:
description: The key of the secret to select from. Must be
a valid secret key.
type: string
name:
default: ""
description: |-
Name of the referent.
This field is effectively required, but due to backwards compatibility is
allowed to be empty. Instances of this type with an empty value here are
almost certainly wrong.
More info: https://kubernetes.io/docs/concepts/overview/working-with-objects/names/#names
type: string
optional:
description: Specify whether the Secret or its key must be
defined
type: boolean
required:
- key
type: object
x-kubernetes-map-type: atomic
type: object
type: string
required:
- password
- url
Expand Down
9 changes: 5 additions & 4 deletions manifests/crd/kustomization.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@
# since it depends on service name and namespace that are out of this kustomize package.
# It should be run by config/default
resources:
- bases/natz.zeiss.com_natsoperators.yaml
- bases/natz.zeiss.com_natsaccounts.yaml
- bases/natz.zeiss.com_natsusers.yaml
- bases/natz.zeiss.com_natsoperators.yaml
- bases/natz.zeiss.com_natsaccounts.yaml
- bases/natz.zeiss.com_natsusers.yaml
- bases/natz.zeiss.com_natsgateways.yaml
#+kubebuilder:scaffold:crdkustomizeresource

patchesStrategicMerge:
Expand All @@ -24,4 +25,4 @@ patchesStrategicMerge:

# the following config is for teaching kustomize how to do kustomization for CRDs.
configurations:
- kustomizeconfig.yaml
- kustomizeconfig.yaml

0 comments on commit 575f085

Please sign in to comment.