Skip to content

Commit

Permalink
Make system namespace a const (#469)
Browse files Browse the repository at this point in the history
* makes 'system' a const

Signed-off-by: Jordan Rash <jordan@synadia.com>
  • Loading branch information
jordan-rash authored Dec 27, 2024
1 parent caa0440 commit c89cd77
Show file tree
Hide file tree
Showing 13 changed files with 35 additions and 23 deletions.
2 changes: 1 addition & 1 deletion cmd/nex/cli_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ type Globals struct {

Config kong.ConfigFlag `help:"Configuration file to load" placeholder:"./nex.config.json"`
Version kong.VersionFlag `help:"Print version information"`
Namespace string `env:"NEX_NAMESPACE" default:"system" help:"Specifies namespace when running nex commands"`
Namespace string `env:"NEX_NAMESPACE" default:"${adminNamespace}" help:"Specifies namespace when running nex commands"`
Check bool `help:"Print the current values of all options without running a command"`
DisableUpgradeCheck bool `env:"NEX_DISABLE_UPGRADE_CHECK" name:"disable-upgrade-check" help:"Disable the upgrade check"`
AutoUpgrade bool `env:"NEX_AUTO_UPGRADE" name:"auto-upgrade" help:"Automatically upgrade the nex CLI when a new version is available"`
Expand Down
2 changes: 1 addition & 1 deletion cmd/nex/cli_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ type Globals struct {

Config kong.ConfigFlag `help:"Configuration file to load" placeholder:"./nex.config.json"`
Version kong.VersionFlag `help:"Print version information"`
Namespace string `env:"NEX_NAMESPACE" default:"system" help:"Specifies namespace when running nex commands"`
Namespace string `env:"NEX_NAMESPACE" default:"${adminNamespace}" help:"Specifies namespace when running nex commands"`
Check bool `help:"Print the current values of all options without running a command"`
DevMode bool `name:"dev" default:"false" help:"Enable development mode"`
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/nex/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func configureLogger(cfg *Globals, nc *nats.Conn, serverPublicKey string, showSy

filter := []string{}
if !showSystemLogs {
filter = append(filter, "system")
filter = append(filter, "actor_system")
}
if hideWorkloadLogs {
filter = append(filter, "workload")
Expand Down
1 change: 1 addition & 0 deletions cmd/nex/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ func main() {
"version": fmt.Sprintf("%s [%s] | Built: %s", VERSION, COMMIT, BUILDDATE),
"versionOnly": VERSION,
"defaultResourcePath": userResourcePath,
"adminNamespace": "system",
},
kong.BindTo(context.Background(), (*context.Context)(nil)),
kong.Bind(&nex.Globals),
Expand Down
4 changes: 2 additions & 2 deletions cmd/nex/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ func TestCLISimple(t *testing.T) {
nex := NexCLI{}

parser := kong.Must(&nex,
kong.Vars(map[string]string{"versionOnly": "testing", "defaultResourcePath": "."}),
kong.Vars(map[string]string{"versionOnly": "testing", "defaultResourcePath": ".", "adminNamespace": "system"}),
kong.Bind(&nex.Globals),
)
kp, err := nkeys.CreatePair(nkeys.PrefixByteServer)
Expand Down Expand Up @@ -66,7 +66,7 @@ func TestCLIWithConfig(t *testing.T) {

nex := NexCLI{}
parser := kong.Must(&nex,
kong.Vars(map[string]string{"versionOnly": "testing", "defaultResourcePath": "."}),
kong.Vars(map[string]string{"versionOnly": "testing", "defaultResourcePath": ".", "adminNamespace": "system"}),
kong.Configuration(kong.JSON, f.Name()),
kong.Bind(&nex.Globals),
)
Expand Down
3 changes: 2 additions & 1 deletion cmd/nex/monitor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/nats-io/nats-server/v2/server"
"github.com/nats-io/nats.go"
"github.com/nats-io/nats.go/jetstream"
"github.com/synadia-io/nex/models"
)

func startNatsServer(t testing.TB) (*server.Server, error) {
Expand Down Expand Up @@ -79,7 +80,7 @@ func TestLogSubject(t *testing.T) {

globals := &Globals{}
globals.NatsServers = []string{natsServer.ClientURL()}
globals.Namespace = "system"
globals.Namespace = models.NodeSystemNamespace

ctx, cancel := context.WithCancel(context.Background())
go func() {
Expand Down
2 changes: 1 addition & 1 deletion cmd/nex/upgrade_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func TestDisableAutoUpgradeFlags(t *testing.T) {
nex := NexCLI{}

parser := kong.Must(&nex,
kong.Vars(map[string]string{"versionOnly": "testing", "defaultResourcePath": "."}),
kong.Vars(map[string]string{"versionOnly": "testing", "defaultResourcePath": ".", "adminNamespace": "system"}),
kong.Bind(&nex.Globals),
)

Expand Down
10 changes: 10 additions & 0 deletions models/node.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package models

const (
// This is a special namespace that is used for "admin" only requests
// The node listens on a handful of topics for and typically the
// 3rd position is the namespace. There are some endpoints that can be seen in
// `node/internal/actors/subjects.go` that have "system" hardcoded in
// the third position. These are considered privledged endpoints
NodeSystemNamespace string = "system"
)
22 changes: 11 additions & 11 deletions models/subjects.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,47 +10,47 @@ const (

// System only subjects
func PingSubject() string {
return ControlAPIPrefix + ".system.PING"
return fmt.Sprintf("%s.%s.PING", ControlAPIPrefix, NodeSystemNamespace)
}

func DirectDeploySubject(inNodeId string) string {
return fmt.Sprintf(ControlAPIPrefix+".system.DDEPLOY.%s", inNodeId)
return fmt.Sprintf("%s.%s.DDEPLOY.%s", ControlAPIPrefix, NodeSystemNamespace, inNodeId)
}

func LameduckSubject(inNodeId string) string {
return fmt.Sprintf(ControlAPIPrefix+".system.LAMEDUCK.%s", inNodeId)
return fmt.Sprintf("%s.%s.LAMEDUCK.%s", ControlAPIPrefix, NodeSystemNamespace, inNodeId)
}

func DirectPingSubject(inNodeId string) string {
return fmt.Sprintf(ControlAPIPrefix+".system.PING.%s", inNodeId)
return fmt.Sprintf("%s.%s.PING.%s", ControlAPIPrefix, NodeSystemNamespace, inNodeId)
}

func InfoSubject(inNodeId string) string {
return fmt.Sprintf(ControlAPIPrefix+".system.INFO.%s", inNodeId)
return fmt.Sprintf("%s.%s.INFO.%s", ControlAPIPrefix, NodeSystemNamespace, inNodeId)
}

// WPING subjects
func NamespacePingRequestSubject(inNS string) string {
return fmt.Sprintf(ControlAPIPrefix+".%s.WPING", inNS)
return fmt.Sprintf("%s.%s.WPING", ControlAPIPrefix, inNS)
}

func WorkloadPingRequestSubject(inNS, inWorkload string) string {
return fmt.Sprintf(ControlAPIPrefix+".%s.WPING.%s", inNS, inWorkload)
return fmt.Sprintf("%s.%s.WPING.%s", ControlAPIPrefix, inNS, inWorkload)
}

// Request subjects
func AuctionRequestSubject(inNS string) string {
return fmt.Sprintf(ControlAPIPrefix+".%s.AUCTION", inNS)
return fmt.Sprintf("%s.%s.AUCTION", ControlAPIPrefix, inNS)
}

func AuctionDeployRequestSubject(inNS, inBidId string) string {
return fmt.Sprintf(ControlAPIPrefix+".%s.ADEPLOY.%s", inNS, inBidId)
return fmt.Sprintf("%s.%s.ADEPLOY.%s", ControlAPIPrefix, inNS, inBidId)
}

func UndeployRequestSubject(inNS, inWorkloadID string) string {
return fmt.Sprintf(ControlAPIPrefix+".%s.UNDEPLOY.%s", inNS, inWorkloadID)
return fmt.Sprintf("%s.%s.UNDEPLOY.%s", ControlAPIPrefix, inNS, inWorkloadID)
}

func CloneWorkloadRequestSubject(inNS, inWorkloadID string) string {
return fmt.Sprintf(ControlAPIPrefix+".%s.CLONE.%s", inNS, inWorkloadID)
return fmt.Sprintf("%s.%s.CLONE.%s", ControlAPIPrefix, inNS, inWorkloadID)
}
2 changes: 1 addition & 1 deletion node/internal/actors/control_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -626,7 +626,7 @@ func (api *ControlAPI) handleNamespacePing(m *nats.Msg) {
}

for _, workload := range workloadResp.Workloads {
if namespace == "system" || workload.Namespace == namespace {
if namespace == models.NodeSystemNamespace || workload.Namespace == namespace {
workloads = append(workloads, nodegen.WorkloadSummary{
Id: workload.Id,
Name: workload.Name,
Expand Down
2 changes: 1 addition & 1 deletion node/internal/actors/direct_start_agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ func (a *DirectStartAgent) stopWorkload(m *actorproto.StopWorkload) (*actorproto
return nil, err
}

if m.Namespace != "system" || m.Namespace != wl.Namespace {
if m.Namespace != models.NodeSystemNamespace || m.Namespace != wl.Namespace {
a.logger.Warn("stop workload namespace mismatch", slog.String("name", a.self.Name()), slog.String("workload", m.WorkloadId), slog.String("request_namespace", m.Namespace), slog.String("actual_namespace", wl.Namespace))
return nil, errors.New("namespace mismatch")
}
Expand Down
2 changes: 1 addition & 1 deletion node/internal/actors/direct_start_agent_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func TestPingAgent(t *testing.T) {

probe := tk.NewProbe(ctx)

probe.SendSync(models.DirectStartActorName, &actorproto.PingAgent{Namespace: "system"}, time.Second*3)
probe.SendSync(models.DirectStartActorName, &actorproto.PingAgent{Namespace: models.NodeSystemNamespace}, time.Second*3)
respEnv, ok := probe.ExpectAnyMessage().(*actorproto.Envelope)
if !ok {
t.Fatalf("unexpected message type: %T", respEnv)
Expand Down
4 changes: 2 additions & 2 deletions node/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ func (nn *nexNode) initializeSupervisionTree() error {
var err error

nn.actorSystem, err = goakt.NewActorSystem("nexnode",
goakt.WithLogger(logger.NewSlog(nn.options.Logger.Handler().WithGroup("system"))),
goakt.WithLogger(logger.NewSlog(nn.options.Logger.Handler().WithGroup("actor_system"))),
goakt.WithPassivationDisabled(),
// In the non-v2 version of goakt, these functions were supported.
// TODO: figure out why they're gone or how we can plug in our own impls
Expand Down Expand Up @@ -507,7 +507,7 @@ func (nn *nexNode) GetInfo(namespace string) (*actorproto.NodeInfo, error) {
return nil, errors.New("failed to unmarshal agent response")
}
for _, w := range wL.Workloads {
if namespace == "system" || w.Namespace == namespace {
if namespace == models.NodeSystemNamespace || w.Namespace == namespace {
resp.Workloads = append(resp.Workloads, &actorproto.WorkloadSummary{
Id: w.Id,
Name: w.Name,
Expand Down

0 comments on commit c89cd77

Please sign in to comment.