Skip to content

Commit

Permalink
fix: adding lookup of accounts
Browse files Browse the repository at this point in the history
  • Loading branch information
katallaxie authored Sep 17, 2024
1 parent 4259468 commit 58e6adb
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 24 deletions.
34 changes: 24 additions & 10 deletions cmd/account-server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"
"os"
"strings"

"github.com/nats-io/nats.go"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -79,13 +80,35 @@ func run(ctx context.Context) error {
if err != nil {
return err
}
defer nc.Drain()
defer nc.Close()

err = setupControllers(mgr, nc)
ac := controllers.NewNatsAccountServer(mgr, nc)
err = ac.SetupWithManager(mgr)
if err != nil {
return err
}

sub, err := nc.Subscribe("$SYS.REQ.ACCOUNT.*.CLAIMS.LOOKUP", func(msg *nats.Msg) {
accountId := strings.TrimSuffix(strings.TrimPrefix(msg.Subject, "$SYS.REQ.ACCOUNT."), ".CLAIMS.LOOKUP")
setupLog.Info("account lookup", "accountId", accountId)

accountToken, ok := ac.GetJWT(accountId)
if !ok {
setupLog.Info("account not found", "accountId", accountId)
return
}

if err := msg.Respond([]byte(accountToken)); err != nil {
setupLog.Error(err, "failed to respond to account lookup", "accountId", accountId)
}
})
if err != nil {
return err
}
defer sub.Drain()
defer sub.Unsubscribe()

//+kubebuilder:scaffold:builders

if err := mgr.AddHealthzCheck("healthz", healthz.Ping); err != nil {
Expand All @@ -106,15 +129,6 @@ func run(ctx context.Context) error {
return nil
}

func setupControllers(mgr ctrl.Manager, nc *nats.Conn) error {
err := controllers.NewNatsAccountServer(mgr, nc).SetupWithManager(mgr)
if err != nil {
return err
}

return nil
}

func main() {
if err := rootCmd.Execute(); err != nil {
setupLog.Error(err, "unable to run operator")
Expand Down
36 changes: 22 additions & 14 deletions controllers/account_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ package controllers

import (
"context"
"sync"

natsv1alpha1 "github.com/zeiss/natz-operator/api/v1alpha1"

"github.com/nats-io/nats.go"
"github.com/zeiss/pkg/conv"
"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/runtime"
ctrl "sigs.k8s.io/controller-runtime"
Expand All @@ -17,7 +19,7 @@ import (
type NatsAccountServer struct {
client.Client
Scheme *runtime.Scheme
accounts map[string]string
accounts sync.Map
nc *nats.Conn
}

Expand All @@ -26,13 +28,22 @@ type NatsAccountServer struct {
// NewNatsAccountServer ...
func NewNatsAccountServer(mgr ctrl.Manager, nc *nats.Conn) *NatsAccountServer {
return &NatsAccountServer{
Client: mgr.GetClient(),
Scheme: mgr.GetScheme(),
accounts: make(map[string]string),
nc: nc,
Client: mgr.GetClient(),
Scheme: mgr.GetScheme(),
nc: nc,
}
}

// GetJWT ...
func (r *NatsAccountServer) GetJWT(publicKey string) (string, bool) {
jwt, ok := r.accounts.Load(publicKey)
if !ok {
return "", false
}

return conv.String(jwt), true
}

// Reconcile ...
func (r *NatsAccountServer) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
logger := log.FromContext(ctx)
Expand All @@ -49,19 +60,16 @@ func (r *NatsAccountServer) Reconcile(ctx context.Context, req ctrl.Request) (ct
logger.Info("reconciling account", "account", account.Name)

if account.DeletionTimestamp != nil {
delete(r.accounts, account.Status.PublicKey)
r.accounts.Delete(account.Status.PublicKey)
return ctrl.Result{}, nil
}

if account.Status.JWT != "" && account.Status.PublicKey != "" {
r.accounts[account.Status.PublicKey] = account.Status.JWT

if r.nc != nil {
go func() {
if err := r.nc.Publish("$SYS.REQ.CLAIMS.UPDATE", []byte(account.Status.JWT)); err != nil {
logger.Info("failed to publish claims update", "account", account.Name, "err", err)
}
}()
r.accounts.Store(account.Status.PublicKey, account.Status.JWT)

if err := r.nc.Publish("$SYS.REQ.CLAIMS.UPDATE", []byte(account.Status.JWT)); err != nil {
logger.Info("failed to publish claims update", "account", account.Name, "err", err)
return ctrl.Result{}, err
}
}

Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ require (
github.com/samber/lo v1.47.0
github.com/spf13/cobra v1.8.1
github.com/spf13/pflag v1.0.5
github.com/zeiss/pkg v0.1.8
golang.org/x/mod v0.21.0
helm.sh/helm v2.17.0+incompatible
k8s.io/api v0.31.1
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -478,6 +478,8 @@ github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9dec
github.com/yuin/goldmark v1.3.5/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k=
github.com/yuin/goldmark v1.4.1/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k=
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
github.com/zeiss/pkg v0.1.8 h1:7SHrVlXaC63pyQxiWHtZE6rRWSyulLOu/qLvVrL5y7M=
github.com/zeiss/pkg v0.1.8/go.mod h1:QVWTdonZRsTedmLwUg1YTJj66aBexDP2SA6ZxyfxSmg=
gitlab.com/bosi/decorder v0.4.2 h1:qbQaV3zgwnBZ4zPMhGLW4KZe7A7NwxEhJx39R3shffo=
gitlab.com/bosi/decorder v0.4.2/go.mod h1:muuhHoaJkA9QLcYHq4Mj8FJUwDZ+EirSHRiaTcTf6T8=
go-simpler.org/assert v0.9.0 h1:PfpmcSvL7yAnWyChSjOz6Sp6m9j5lyK8Ok9pEL31YkQ=
Expand Down

0 comments on commit 58e6adb

Please sign in to comment.