Skip to content

Commit

Permalink
feat: hz.GenericObject to support any fields, not just spec & status
Browse files Browse the repository at this point in the history
Added basic secret object and serviceaccount which creates a secret with nats creds
  • Loading branch information
jlarfors committed Mar 11, 2024
1 parent 9597ed2 commit 9bbd995
Show file tree
Hide file tree
Showing 13 changed files with 551 additions and 119 deletions.
34 changes: 9 additions & 25 deletions cmd/hzctl/table.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package main

import (
"bytes"
"encoding/json"
"fmt"
"os"

"github.com/charmbracelet/lipgloss"
"github.com/charmbracelet/lipgloss/table"
"github.com/verifa/horizon/pkg/hz"
"sigs.k8s.io/yaml"
)

const (
Expand All @@ -18,32 +18,16 @@ const (
)

func printObject(object hz.GenericObject) error {
buf := bytes.Buffer{}
buf.WriteString(
fmt.Sprintf("apiVersion: %s\n", object.APIVersion),
)
buf.WriteString(fmt.Sprintf("kind: %s\n", object.Kind))
buf.WriteString("meta:\n")
buf.WriteString(fmt.Sprintf("\taccount: %s\n", object.Account))
buf.WriteString(fmt.Sprintf("\tname: %s\n", object.Name))
oMF, _ := json.MarshalIndent(object.ManagedFields, "\t", " ")
buf.WriteString(
fmt.Sprintf("\tmanagedFields: %s\n", oMF),
)
buf.WriteString(fmt.Sprintf("\tfinalizers: %s\n", object.Finalizers))
buf.WriteString(fmt.Sprintf("\tlabels: %s\n", object.Labels))
buf.WriteString("spec:\n")
if err := json.Indent(&buf, object.Spec, "", " "); err != nil {
return fmt.Errorf("formatting spec: %w", err)
jb, err := json.Marshal(object)
if err != nil {
return fmt.Errorf("marshalling object: %w", err)
}
buf.WriteString("\n\n")
buf.WriteString("status:\n")
if object.Status != nil {
if err := json.Indent(&buf, object.Status, "", " "); err != nil {
return fmt.Errorf("formatting status: %w", err)
}
yb, err := yaml.JSONToYAML(jb)
if err != nil {
return fmt.Errorf("converting to yaml: %w", err)
}
fmt.Println(buf.String())

fmt.Println(string(yb))
return nil
}

Expand Down
10 changes: 0 additions & 10 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,6 @@ toolchain go1.21.6

require (
cuelang.org/go v0.7.0
github.com/Azure/azure-sdk-for-go/sdk/azcore v1.9.1
github.com/Azure/azure-sdk-for-go/sdk/azidentity v1.5.1
github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/compute/armcompute/v4 v4.2.1
github.com/AzureAD/microsoft-authentication-library-for-go v1.2.1
github.com/a-h/templ v0.2.543
github.com/charmbracelet/huh v0.3.0
github.com/charmbracelet/lipgloss v0.9.1
Expand All @@ -23,8 +19,6 @@ require (
github.com/google/go-cmp v0.6.0
github.com/google/uuid v1.6.0
github.com/gorilla/securecookie v1.1.2
github.com/microsoft/azure-devops-go-api/azuredevops v1.0.0-b5
github.com/microsoft/azure-devops-go-api/azuredevops/v7 v7.1.0
github.com/nats-io/jwt/v2 v2.5.5
github.com/nats-io/nats-server/v2 v2.10.11
github.com/nats-io/nats.go v1.33.1
Expand All @@ -50,7 +44,6 @@ require (
github.com/Antonboom/errname v0.1.12 // indirect
github.com/Antonboom/nilnil v0.1.7 // indirect
github.com/Antonboom/testifylint v1.1.2 // indirect
github.com/Azure/azure-sdk-for-go/sdk/internal v1.5.1 // indirect
github.com/BurntSushi/toml v1.3.2 // indirect
github.com/Djarvur/go-err113 v0.0.0-20210108212216-aea10b59be24 // indirect
github.com/GaijinEntertainment/go-exhaustruct/v3 v3.2.0 // indirect
Expand Down Expand Up @@ -113,7 +106,6 @@ require (
github.com/go-xmlfmt/xmlfmt v1.1.2 // indirect
github.com/gobwas/glob v0.2.3 // indirect
github.com/gofrs/flock v0.8.1 // indirect
github.com/golang-jwt/jwt/v5 v5.2.0 // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/golangci/check v0.0.0-20180506172741-cfe4005ccda2 // indirect
github.com/golangci/dupl v0.0.0-20180902072040-3e9179ac440a // indirect
Expand Down Expand Up @@ -144,7 +136,6 @@ require (
github.com/klauspost/compress v1.17.7 // indirect
github.com/kulti/thelper v0.6.3 // indirect
github.com/kunwardeep/paralleltest v1.0.9 // indirect
github.com/kylelemons/godebug v1.1.0 // indirect
github.com/kyoh86/exportloopref v0.1.11 // indirect
github.com/ldez/gomoddirectives v0.2.3 // indirect
github.com/ldez/tagliatelle v0.5.0 // indirect
Expand Down Expand Up @@ -184,7 +175,6 @@ require (
github.com/opencontainers/image-spec v1.1.0-rc5 // indirect
github.com/pelletier/go-toml v1.9.5 // indirect
github.com/pelletier/go-toml/v2 v2.0.5 // indirect
github.com/pkg/browser v0.0.0-20240102092130-5ac0b6a4141c // indirect
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
github.com/polyfloyd/go-errorlint v1.4.8 // indirect
github.com/prometheus/client_golang v1.12.1 // indirect
Expand Down
29 changes: 0 additions & 29 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -49,22 +49,6 @@ github.com/Antonboom/nilnil v0.1.7 h1:ofgL+BA7vlA1K2wNQOsHzLJ2Pw5B5DpWRLdDAVvvTo
github.com/Antonboom/nilnil v0.1.7/go.mod h1:TP+ScQWVEq0eSIxqU8CbdT5DFWoHp0MbP+KMUO1BKYQ=
github.com/Antonboom/testifylint v1.1.2 h1:IdLRermiLRogxY5AumBL4sP0A+qKHQM/AP1Xd7XOTKc=
github.com/Antonboom/testifylint v1.1.2/go.mod h1:9PFi+vWa8zzl4/B/kqmFJcw85ZUv8ReyBzuQCd30+WI=
github.com/Azure/azure-sdk-for-go/sdk/azcore v1.9.1 h1:lGlwhPtrX6EVml1hO0ivjkUxsSyl4dsiw9qcA1k/3IQ=
github.com/Azure/azure-sdk-for-go/sdk/azcore v1.9.1/go.mod h1:RKUqNu35KJYcVG/fqTRqmuXJZYNhYkBrnC/hX7yGbTA=
github.com/Azure/azure-sdk-for-go/sdk/azidentity v1.5.1 h1:sO0/P7g68FrryJzljemN+6GTssUXdANk6aJ7T1ZxnsQ=
github.com/Azure/azure-sdk-for-go/sdk/azidentity v1.5.1/go.mod h1:h8hyGFDsU5HMivxiS2iYFZsgDbU9OnnJ163x5UGVKYo=
github.com/Azure/azure-sdk-for-go/sdk/internal v1.5.1 h1:6oNBlSdi1QqM1PNW7FPA6xOGA5UNsXnkaYZz9vdPGhA=
github.com/Azure/azure-sdk-for-go/sdk/internal v1.5.1/go.mod h1:s4kgfzA0covAXNicZHDMN58jExvcng2mC/DepXiF1EI=
github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/compute/armcompute/v4 v4.2.1 h1:UPeCRD+XY7QlaGQte2EVI2iOcWvUYA2XY8w5T/8v0NQ=
github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/compute/armcompute/v4 v4.2.1/go.mod h1:oGV6NlB0cvi1ZbYRR2UN44QHxWFyGk+iylgD0qaMXjA=
github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/internal v1.1.2 h1:mLY+pNLjCUeKhgnAJWAKhEUQM+RJQo2H1fuGSw1Ky1E=
github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/internal v1.1.2/go.mod h1:FbdwsQ2EzwvXxOPcMFYO8ogEc9uMMIj3YkmCdXdAFmk=
github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/network/armnetwork v1.0.0 h1:nBy98uKOIfun5z6wx6jwWLrULcM0+cjBalBFZlEZ7CA=
github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/network/armnetwork v1.0.0/go.mod h1:243D9iHbcQXoFUtgHJwL7gl2zx1aDuDMjvBZVGr2uW0=
github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/resources/armresources v1.0.0 h1:ECsQtyERDVz3NP3kvDOTLvbQhqWp/x9EsGKtb4ogUr8=
github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/resources/armresources v1.0.0/go.mod h1:s1tW/At+xHqjNFvWU4G0c0Qv33KOhvbGNj0RCTQDV8s=
github.com/AzureAD/microsoft-authentication-library-for-go v1.2.1 h1:DzHpqpoJVaCgOUdVHxE8QB52S6NiVdDQvGlny1qvPqA=
github.com/AzureAD/microsoft-authentication-library-for-go v1.2.1/go.mod h1:wP83P5OoQ5p6ip3ScPr0BAq0BvuPAvacpEuSzyouqAI=
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/BurntSushi/toml v1.3.2 h1:o7IhLm0Msx3BaB+n3Ag7L8EVlByGnpq14C4YWiu/gL8=
github.com/BurntSushi/toml v1.3.2/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ=
Expand Down Expand Up @@ -180,8 +164,6 @@ github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/denis-tingaikin/go-header v0.4.3 h1:tEaZKAlqql6SKCY++utLmkPLd6K8IBM20Ha7UVm+mtU=
github.com/denis-tingaikin/go-header v0.4.3/go.mod h1:0wOCWuN71D5qIgE2nz9KrKmuYBAC2Mra5RassOIQ2/c=
github.com/dnaeon/go-vcr v1.2.0 h1:zHCHvJYTMh1N7xnV7zf1m1GPBF9Ad0Jk/whtQ1663qI=
github.com/dnaeon/go-vcr v1.2.0/go.mod h1:R4UdLID7HZT3taECzJs4YgbbH6PIGXB6W/sc5OLb6RQ=
github.com/emicklei/proto v1.10.0 h1:pDGyFRVV5RvV+nkBK9iy3q67FBy9Xa7vwrOTE+g5aGw=
github.com/emicklei/proto v1.10.0/go.mod h1:rn1FgRS/FANiZdD2djyH7TMA9jdRDcYQ9IEN9yvjX0A=
github.com/envoyproxy/go-control-plane v0.9.0/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
Expand Down Expand Up @@ -263,8 +245,6 @@ github.com/gobwas/glob v0.2.3/go.mod h1:d3Ez4x06l9bZtSvzIay5+Yzi0fmZzPgnTbPcKjJA
github.com/gofrs/flock v0.8.1 h1:+gYjHKf32LDeiEEFhQaotPbLuUXjY5ZqxKgXy7n59aw=
github.com/gofrs/flock v0.8.1/go.mod h1:F1TvTiK9OcQqauNUHlbJvyl9Qa1QvF/gOUDKA14jxHU=
github.com/gogo/protobuf v1.1.1/go.mod h1:r8qH/GZQm5c6nD/R0oafs1akxWv10x8SbQlK7atdtwQ=
github.com/golang-jwt/jwt/v5 v5.2.0 h1:d/ix8ftRUorsN+5eMIlF4T6J8CAt9rch3My2winC1Jw=
github.com/golang-jwt/jwt/v5 v5.2.0/go.mod h1:pqrtFR0X4osieyHYxtmOUWsAWrfe1Q5UVIyoH402zdk=
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q=
github.com/golang/groupcache v0.0.0-20190702054246-869f871628b6/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc=
github.com/golang/groupcache v0.0.0-20191227052852-215e87163ea7/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc=
Expand Down Expand Up @@ -348,7 +328,6 @@ github.com/google/pprof v0.0.0-20200708004538-1a94d8640e99/go.mod h1:ZgVRPoUq/hf
github.com/google/pprof v0.0.0-20210407192527-94a9f03dee38 h1:yAJXTCF9TqKcTiHJAE8dj7HMvPfh66eeA2JYW7eFpSE=
github.com/google/pprof v0.0.0-20210407192527-94a9f03dee38/go.mod h1:kpwsk12EmLew5upagYY7GY0pfYCcupk39gWOCRROcvE=
github.com/google/renameio v0.1.0/go.mod h1:KWCgfxg9yswjAJkECMjeO8J8rahYeXnNhOm40UhjYkI=
github.com/google/uuid v1.1.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/googleapis/gax-go/v2 v2.0.4/go.mod h1:0Wqv26UfaUD9n4G6kQubkQ+KchISgw+vpHVxEJEs9eg=
Expand Down Expand Up @@ -422,8 +401,6 @@ github.com/kulti/thelper v0.6.3 h1:ElhKf+AlItIu+xGnI990no4cE2+XaSu1ULymV2Yulxs=
github.com/kulti/thelper v0.6.3/go.mod h1:DsqKShOvP40epevkFrvIwkCMNYxMeTNjdWL4dqWHZ6I=
github.com/kunwardeep/paralleltest v1.0.9 h1:3Sr2IfFNcsMmlqPk1cjTUbJ4zofKPGyHxenwPebgTug=
github.com/kunwardeep/paralleltest v1.0.9/go.mod h1:2C7s65hONVqY7Q5Efj5aLzRCNLjw2h4eMc9EcypGjcY=
github.com/kylelemons/godebug v1.1.0 h1:RPNrshWIDI6G2gRW9EHilWtl7Z6Sb1BR0xunSBf0SNc=
github.com/kylelemons/godebug v1.1.0/go.mod h1:9/0rRGxNHcop5bhtWyNeEfOS8JIWk580+fNqagV/RAw=
github.com/kyoh86/exportloopref v0.1.11 h1:1Z0bcmTypkL3Q4k+IDHMWTcnCliEZcaPiIe0/ymEyhQ=
github.com/kyoh86/exportloopref v0.1.11/go.mod h1:qkV4UF1zGl6EkF1ox8L5t9SwyeBAZ3qLMd6up458uqA=
github.com/ldez/gomoddirectives v0.2.3 h1:y7MBaisZVDYmKvt9/l1mjNCiSA1BVn34U0ObUcJwlhA=
Expand Down Expand Up @@ -467,10 +444,6 @@ github.com/mbilski/exhaustivestruct v1.2.0 h1:wCBmUnSYufAHO6J4AVWY6ff+oxWxsVFrwg
github.com/mbilski/exhaustivestruct v1.2.0/go.mod h1:OeTBVxQWoEmB2J2JCHmXWPJ0aksxSUOUy+nvtVEfzXc=
github.com/mgechev/revive v1.3.7 h1:502QY0vQGe9KtYJ9FpxMz9rL+Fc/P13CI5POL4uHCcE=
github.com/mgechev/revive v1.3.7/go.mod h1:RJ16jUbF0OWC3co/+XTxmFNgEpUPwnnA0BRllX2aDNA=
github.com/microsoft/azure-devops-go-api/azuredevops v1.0.0-b5 h1:YH424zrwLTlyHSH/GzLMJeu5zhYVZSx5RQxGKm1h96s=
github.com/microsoft/azure-devops-go-api/azuredevops v1.0.0-b5/go.mod h1:PoGiBqKSQK1vIfQ+yVaFcGjDySHvym6FM1cNYnwzbrY=
github.com/microsoft/azure-devops-go-api/azuredevops/v7 v7.1.0 h1:mmJCWLe63QvybxhW1iBmQWEaCKdc4SKgALfTNZ+OphU=
github.com/microsoft/azure-devops-go-api/azuredevops/v7 v7.1.0/go.mod h1:mDunUZ1IUJdJIRHvFb+LPBUtxe3AYB5MI6BMXNg8194=
github.com/minio/highwayhash v1.0.2 h1:Aak5U0nElisjDCfPSG79Tgzkn2gl66NxOMspRrKnA/g=
github.com/minio/highwayhash v1.0.2/go.mod h1:BQskDq+xkJ12lmlUUi7U0M5Swg3EWR+dLTk+kldvVxY=
github.com/mitchellh/go-homedir v1.1.0 h1:lukF9ziXFxDFPkA1vsr5zpc1XuPDn/wFntq5mG+4E0Y=
Expand Down Expand Up @@ -543,8 +516,6 @@ github.com/pelletier/go-toml v1.9.5 h1:4yBQzkHv+7BHq2PQUZF3Mx0IYxG7LsP222s7Agd3v
github.com/pelletier/go-toml v1.9.5/go.mod h1:u1nR/EPcESfeI/szUZKdtJ0xRNbUoANCkoOuaOx1Y+c=
github.com/pelletier/go-toml/v2 v2.0.5 h1:ipoSadvV8oGUjnUbMub59IDPPwfxF694nG/jwbMiyQg=
github.com/pelletier/go-toml/v2 v2.0.5/go.mod h1:OMHamSCAODeSsVrwwvcJOaoN0LIUIaFVNZzmWyNfXas=
github.com/pkg/browser v0.0.0-20240102092130-5ac0b6a4141c h1:+mdjkGKdHQG3305AYmdv1U2eRNDiU2ErMBj1gwrq8eQ=
github.com/pkg/browser v0.0.0-20240102092130-5ac0b6a4141c/go.mod h1:7rwL4CYBLnjLxUqIJNnCWiEdr3bn6IUYi15bNlnbCCU=
github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
Expand Down
45 changes: 8 additions & 37 deletions pkg/extensions/accounts/account_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@ package accounts_test

import (
"context"
"encoding/json"
"fmt"
"testing"
"time"

"github.com/verifa/horizon/pkg/extensions/accounts"
"github.com/verifa/horizon/pkg/hz"
"github.com/verifa/horizon/pkg/hztest"
"github.com/verifa/horizon/pkg/server"
tu "github.com/verifa/horizon/pkg/testutil"
)
Expand All @@ -34,42 +33,14 @@ func TestAccount(t *testing.T) {
err := accClient.Apply(ctx, account)
tu.AssertNoError(t, err)

// Create a timeout and a done channel.
// Watch until the replicaset is ready.
// If the timeout is reached, fail the test.
timeout := time.After(time.Second * 5)
done := make(chan struct{})
watcher, err := hz.StartWatcher(
hztest.WatchWaitUntil(
t,
ctx,
ti.Conn,
hz.WithWatcherFor(account),
hz.WithWatcherFn(
func(event hz.Event) (hz.Result, error) {
t.Log("watch event for account")
var acc accounts.Account
if err := json.Unmarshal(event.Data, &acc); err != nil {
return hz.Result{}, fmt.Errorf(
"unmarshalling account: %w",
err,
)
}
if acc.Status == nil {
return hz.Result{}, nil
}
t.Log("account ready? ", acc.Status.Ready)
if acc.Status.Ready == true {
close(done)
}
return hz.Result{}, nil
},
),
time.Second*5,
account,
func(acc accounts.Account) bool {
return acc.Status != nil && acc.Status.Ready
},
)
tu.AssertNoError(t, err)
defer watcher.Close()

select {
case <-timeout:
t.Errorf("timed out waiting for account")
case <-done:
}
}
24 changes: 24 additions & 0 deletions pkg/extensions/secrets/secret.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package secrets

import "github.com/verifa/horizon/pkg/hz"

var _ hz.Objecter = (*Secret)(nil)

type Secret struct {
hz.ObjectMeta `json:"metadata" cue:""`
Data SecretData `json:"data" cue:",optional"`
}

func (s Secret) ObjectGroup() string {
return "secrets"
}

func (s Secret) ObjectVersion() string {
return "v1"
}

func (s Secret) ObjectKind() string {
return "Secret"
}

type SecretData map[string]string
55 changes: 55 additions & 0 deletions pkg/extensions/secrets/secret_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package secrets_test

import (
"context"
"encoding/json"
"testing"

"github.com/verifa/horizon/pkg/extensions/secrets"
"github.com/verifa/horizon/pkg/hz"
"github.com/verifa/horizon/pkg/server"
tu "github.com/verifa/horizon/pkg/testutil"
)

func TestSecrets(t *testing.T) {
ctx := context.Background()

ts := server.Test(t, ctx)

ctlr, err := hz.StartController(
ctx,
ts.Conn,
hz.WithControllerFor(secrets.Secret{}),
)
tu.AssertNoError(t, err)
t.Cleanup(func() {
_ = ctlr.Stop()
})

secret := secrets.Secret{
ObjectMeta: hz.ObjectMeta{
Name: "my-secret",
Account: "my-account",
},
Data: secrets.SecretData{
"username": "admin",
"password": "password",
},
}
client := hz.NewClient(
ts.Conn,
hz.WithClientInternal(true),
hz.WithClientDefaultManager(),
)
err = client.Apply(ctx, hz.WithApplyObject(secret))
tu.AssertNoError(t, err)

raw, err := client.Get(ctx, hz.WithGetKey(secret))
tu.AssertNoError(t, err)

getSecret := secrets.Secret{}
err = json.Unmarshal(raw, &getSecret)
tu.AssertNoError(t, err)

tu.AssertEqual(t, secret.Data, getSecret.Data)
}
Loading

0 comments on commit 9bbd995

Please sign in to comment.