Skip to content

Commit

Permalink
rename hz-internal api group to core
Browse files Browse the repository at this point in the history
cleanup account reconciler based on server-side apply no-diff-no-op logic
  • Loading branch information
jlarfors committed Apr 16, 2024
1 parent c966403 commit a03df37
Show file tree
Hide file tree
Showing 12 changed files with 37 additions and 42 deletions.
8 changes: 4 additions & 4 deletions pkg/auth/rbac_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func TestRBAC(t *testing.T) {
},
Spec: RoleBindingSpec{
RoleRef: RoleRef{
Group: "hz-internal",
Group: "core",
Kind: "Role",
Name: "role-creator",
},
Expand Down Expand Up @@ -176,7 +176,7 @@ func TestRBAC(t *testing.T) {
},
Spec: RoleBindingSpec{
RoleRef: RoleRef{
Group: "hz-internal",
Group: "core",
Kind: "Role",
Name: "role-runner",
},
Expand Down Expand Up @@ -247,7 +247,7 @@ func TestRBAC(t *testing.T) {
},
Spec: RoleBindingSpec{
RoleRef: RoleRef{
Group: "hz-internal",
Group: "core",
Kind: "Role",
Name: "role-allow-all",
},
Expand All @@ -266,7 +266,7 @@ func TestRBAC(t *testing.T) {
},
Spec: RoleBindingSpec{
RoleRef: RoleRef{
Group: "hz-internal",
Group: "core",
Kind: "Role",
Name: "role-deny-delete",
},
Expand Down
2 changes: 1 addition & 1 deletion pkg/auth/role.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ func (Role) ObjectVersion() string {
}

func (Role) ObjectGroup() string {
return "hz-internal"
return "core"
}

func (Role) ObjectKind() string {
Expand Down
2 changes: 1 addition & 1 deletion pkg/auth/rolebinding.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ func (RoleBinding) ObjectVersion() string {
}

func (RoleBinding) ObjectGroup() string {
return "hz-internal"
return "core"
}

func (RoleBinding) ObjectKind() string {
Expand Down
47 changes: 24 additions & 23 deletions pkg/extensions/accounts/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,11 @@ import (
)

const (
fieldManager = "ctrl-accounts"
Finalizer = "core/account"

ObjectKind = "Account"
ObjectGroup = "hz-internal"
ObjectGroup = "core"
ObjectVersion = "v1"
)

Expand All @@ -38,6 +41,11 @@ func (a Account) ObjectKind() string {
return ObjectKind
}

// Override ObjectAccount because accounts can only exist in the root account.
func (a Account) ObjectAccount() string {
return hz.RootAccount
}

type AccountSpec struct{}

type AccountStatus struct {
Expand All @@ -59,7 +67,6 @@ type AccountStatus struct {
var _ (hz.Reconciler) = (*AccountReconciler)(nil)

type AccountReconciler struct {
hz.Client
Conn *nats.Conn

OpKeyPair nkeys.KeyPair
Expand All @@ -71,7 +78,12 @@ func (r *AccountReconciler) Reconcile(
ctx context.Context,
req hz.Request,
) (hz.Result, error) {
accClient := hz.ObjectClient[Account]{Client: r.Client}
client := hz.NewClient(
r.Conn,
hz.WithClientInternal(true),
hz.WithClientManager(fieldManager),
)
accClient := hz.ObjectClient[Account]{Client: client}
account, err := accClient.Get(
ctx,
hz.WithGetKey(req.Key),
Expand All @@ -80,7 +92,7 @@ func (r *AccountReconciler) Reconcile(
return hz.Result{}, hz.IgnoreNotFound(err)
}

accountApply, err := hz.ExtractManagedFields(account, "ctrl-accounts")
accountApply, err := hz.ExtractManagedFields(account, fieldManager)
if err != nil {
return hz.Result{}, fmt.Errorf("extracting managed fields: %w", err)
}
Expand All @@ -103,8 +115,9 @@ func (r *AccountReconciler) Reconcile(
}
return hz.Result{}, nil
}

ready := true
// If status is non-nil, check if the account exists.
// If it exists, make sure it matches what it should.
// If it doesn't exist, re-create it.
existingClaims, err := jwt.DecodeAccountClaims(account.Status.JWT)
if err != nil {
return hz.Result{}, fmt.Errorf("decoding account claims: %w", err)
Expand All @@ -118,33 +131,21 @@ func (r *AccountReconciler) Reconcile(
if _, err := AccountClaimsUpdate(ctx, r.Conn, r.OpKeyPair, account.Status.JWT); err != nil {
return hz.Result{}, fmt.Errorf("updating account: %w", err)
}
ready = false
}

if ready && !cmp.Equal(claims, existingClaims) {
if !cmp.Equal(claims, existingClaims) {
if _, err := AccountClaimsUpdate(ctx, r.Conn, r.OpKeyPair, account.Status.JWT); err != nil {
return hz.Result{}, fmt.Errorf("updating account: %w", err)
}
ready = false
}

if !ready {
if account.Status.Ready {
accountApply.Status.Ready = false
if _, err := accClient.Apply(ctx, accountApply); err != nil {
return hz.Result{}, fmt.Errorf("updating account: %w", err)
}
return hz.Result{}, nil
}
return hz.Result{}, nil
}

if !account.Status.Ready {
accountApply.Status.Ready = true
if _, err := accClient.Apply(ctx, accountApply); err != nil {
return hz.Result{}, fmt.Errorf("updating account: %w", err)
}
}
accountApply.Status.Ready = true
if _, err := accClient.Apply(ctx, accountApply); err != nil {
return hz.Result{}, fmt.Errorf("updating account: %w", err)
}

return hz.Result{}, nil
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/extensions/accounts/member.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ func (Member) ObjectVersion() string {
}

func (Member) ObjectGroup() string {
return "hz-internal"
return "core"
}

func (Member) ObjectKind() string {
Expand Down
2 changes: 1 addition & 1 deletion pkg/extensions/accounts/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func (u User) ObjectVersion() string {
}

func (u User) ObjectGroup() string {
return "hz-internal"
return "core"
}

func (u User) ObjectKind() string {
Expand Down
1 change: 0 additions & 1 deletion pkg/extensions/accounts/user_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ func TestUser(t *testing.T) {
// In order to publish a user, the account the user references
// must exist in the NATS KV store.
recon := accounts.AccountReconciler{
Client: client,
Conn: ti.Conn,
OpKeyPair: ti.NS.Auth.Operator.SigningKey.KeyPair,
RootAccountPubKey: ti.NS.Auth.RootAccount.PublicKey,
Expand Down
4 changes: 2 additions & 2 deletions pkg/extensions/serviceaccounts/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,8 @@ func (r Reconciler) generateNATSCredentials(
) (string, error) {
accClient := hz.ObjectClient[accounts.Account]{Client: r.Client}
account, err := accClient.Get(ctx, hz.WithGetKey(hz.ObjectKey{
Name: hz.RootAccount,
Account: sa.Account,
Account: hz.RootAccount,
Name: sa.Account,
}))
if err != nil {
return "", fmt.Errorf("getting horizon account: %w", err)
Expand Down
2 changes: 1 addition & 1 deletion pkg/hz/portal.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func (e Portal) ObjectVersion() string {
}

func (e Portal) ObjectGroup() string {
return "hz-internal"
return "core"
}

func (e Portal) ObjectKind() string {
Expand Down
2 changes: 1 addition & 1 deletion pkg/hz/reconciler.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"time"
)

const RootAccount = "hz-root"
const RootAccount = "root"

type Reconciler interface {
Reconcile(context.Context, Request) (Result, error)
Expand Down
2 changes: 1 addition & 1 deletion pkg/hz/testdata/account-test.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
apiVersion: hz-internal/v1 # "core/v1"
apiVersion: core/v1
kind: Account
metadata:
name: test-account
Expand Down
5 changes: 0 additions & 5 deletions pkg/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -266,11 +266,6 @@ func (s *Server) Start(ctx context.Context, opts ...ServerOption) error {
}
if opt.runAccountsController {
recon := accounts.AccountReconciler{
Client: hz.NewClient(
s.Conn,
hz.WithClientInternal(true),
hz.WithClientManager("ctlr-accounts"),
),
Conn: s.Conn,
OpKeyPair: s.NS.Auth.Operator.SigningKey.KeyPair,
RootAccountPubKey: s.NS.Auth.RootAccount.PublicKey,
Expand Down

0 comments on commit a03df37

Please sign in to comment.