Skip to content

Commit

Permalink
name the status KV identically to ConditionOrc (#26)
Browse files Browse the repository at this point in the history
  • Loading branch information
DoctorVin authored Jun 13, 2023
1 parent 7e9c86e commit aaf4dfa
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 15 deletions.
13 changes: 8 additions & 5 deletions internal/worker/kv_status.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"time"

cotyp "github.com/metal-toolbox/conditionorc/pkg/types"
sm "github.com/metal-toolbox/flasher/internal/statemachine"
"github.com/metal-toolbox/flasher/types"
"github.com/nats-io/nats.go"
Expand All @@ -15,7 +16,7 @@ import (
)

var (
statusKVName = "flasher-status"
statusKVName = string(cotyp.FirmwareInstall)
defaultKVOpts = []kv.Option{
kv.WithReplicas(3),
kv.WithDescription("flasher condition status tracking"),
Expand Down Expand Up @@ -53,10 +54,12 @@ func (s *statusKVPublisher) Publish(hCtx *sm.HandlerContext) {

func statusFromContext(hCtx *sm.HandlerContext) []byte {
sv := &types.StatusValue{
WorkerID: hCtx.WorkerID.String(),
Target: hCtx.Asset.ID.String(),
State: string(hCtx.Task.State()),
Status: statusInfoJSON(hCtx.Task.Status),
WorkerID: hCtx.WorkerID.String(),
Target: hCtx.Asset.ID.String(),
State: string(hCtx.Task.State()),
Status: statusInfoJSON(hCtx.Task.Status),
// ResourceVersion: XXX: the handler context has no concept of this! does this make
// sense at the controller-level?
UpdatedAt: time.Now(),
}
return sv.MustBytes()
Expand Down
4 changes: 2 additions & 2 deletions internal/worker/kv_status_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func TestPublisher(t *testing.T) {
pub := NewStatusKVPublisher(evJS, logrus.New(), kv.WithReplicas(1))
require.NotNil(t, pub, "publisher constructor")

readHandle, err := js.KeyValue("flasher-status")
readHandle, err := js.KeyValue("firmwareInstall")
require.NoError(t, err, "read handle")

taskID := uuid.New()
Expand Down Expand Up @@ -96,7 +96,7 @@ func TestPublisher(t *testing.T) {
err = json.Unmarshal(entry.Value(), sv)
require.NoError(t, err, "unmarshal")

require.Equal(t, types.Version, sv.Version, "version check")
require.Equal(t, types.Version, sv.MsgVersion, "version check")
require.Equal(t, assetID.String(), sv.Target, "sv Target")
require.Equal(t, json.RawMessage(`{"msg":"some-status"}`), sv.Status, "sv Status")

Expand Down
17 changes: 9 additions & 8 deletions types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,22 @@ const (
Version int32 = 1
)

// StatusValue is the canonical structure
// StatusValue is the canonical structure for reporting status of an ongoing firmware install
type StatusValue struct {
UpdatedAt time.Time `json:"updated"`
WorkerID string `json:"worker"`
Target string `json:"target"`
State string `json:"state"`
Status json.RawMessage `json:"status"`
Version int32 `json:"version"`
UpdatedAt time.Time `json:"updated"`
WorkerID string `json:"worker"`
Target string `json:"target"`
State string `json:"state"`
Status json.RawMessage `json:"status"`
ResourceVersion int64 `json:"resourceVersion"` // for updates to server-service
MsgVersion int32 `json:"msgVersion"`
// WorkSpec json.RawMessage `json:"spec"` XXX: for re-publish use-cases
}

// MustBytes sets the version field of the StatusValue so any callers don't have
// to deal with it. It will panic if we cannot serialize to JSON for some reason.
func (v *StatusValue) MustBytes() []byte {
v.Version = Version
v.MsgVersion = Version
byt, err := json.Marshal(v)
if err != nil {
panic("unable to serialize status value: " + err.Error())
Expand Down

0 comments on commit aaf4dfa

Please sign in to comment.