Skip to content

Commit

Permalink
moves nexworkload string newtype to controlapi (#261)
Browse files Browse the repository at this point in the history
* moves nexworkload string newtype to controlapi

* fixing darwin

* adding other platform targets

* windoze
  • Loading branch information
autodidaddict authored Jun 7, 2024
1 parent 4aa9c81 commit c698e97
Show file tree
Hide file tree
Showing 21 changed files with 175 additions and 165 deletions.
5 changes: 3 additions & 2 deletions agent/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"github.com/nats-io/nats.go"
"github.com/nats-io/nkeys"
"github.com/synadia-io/nex/agent/providers"
controlapi "github.com/synadia-io/nex/control-api"
agentapi "github.com/synadia-io/nex/internal/agent-api"
"github.com/synadia-io/nex/internal/models"
"go.opentelemetry.io/otel"
Expand Down Expand Up @@ -171,7 +172,7 @@ func (a *Agent) cacheExecutableArtifact(req *agentapi.DeployRequest) (*string, e
fileName := fmt.Sprintf("workload-%s", *a.md.VmID)
tempFile := path.Join(os.TempDir(), fileName)

if strings.EqualFold(runtime.GOOS, "windows") && req.WorkloadType == models.NexWorkloadNative {
if strings.EqualFold(runtime.GOOS, "windows") && req.WorkloadType == controlapi.NexWorkloadNative {
tempFile = fmt.Sprintf("%s.exe", tempFile)
}

Expand Down Expand Up @@ -274,7 +275,7 @@ func (a *Agent) handleDeploy(m *nats.Msg) {
a.provider = provider

shouldValidate := true
if !a.sandboxed && request.WorkloadType == models.NexWorkloadNative {
if !a.sandboxed && request.WorkloadType == controlapi.NexWorkloadNative {
shouldValidate = false
}

Expand Down
10 changes: 5 additions & 5 deletions agent/providers/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import (
"errors"

"github.com/synadia-io/nex/agent/providers/lib"
controlapi "github.com/synadia-io/nex/control-api"
agentapi "github.com/synadia-io/nex/internal/agent-api"
"github.com/synadia-io/nex/internal/models"
)

// ExecutionProvider implementations provide support for a specific
Expand Down Expand Up @@ -34,14 +34,14 @@ func NewExecutionProvider(params *agentapi.ExecutionProviderParams) (ExecutionPr
// }

switch params.WorkloadType {
case models.NexWorkloadNative:
case controlapi.NexWorkloadNative:
return lib.InitNexExecutionProviderNative(params)
case models.NexWorkloadV8:
case controlapi.NexWorkloadV8:
return lib.InitNexExecutionProviderV8(params)
case models.NexWorkloadOCI:
case controlapi.NexWorkloadOCI:
// TODO-- return lib.InitNexExecutionProviderOCI(params), nil
return nil, errors.New("oci execution provider not yet implemented")
case models.NexWorkloadWasm:
case controlapi.NexWorkloadWasm:
return lib.InitNexExecutionProviderWasm(params)
default:
break
Expand Down
15 changes: 7 additions & 8 deletions control-api/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,14 @@ import (

"github.com/nats-io/jwt/v2"
"github.com/nats-io/nkeys"
"github.com/synadia-io/nex/internal/models"
)

type DeployRequest struct {
Argv []string `json:"argv,omitempty"`
Description *string `json:"description,omitempty"`
WorkloadType models.NexWorkload `json:"type"`
Location *url.URL `json:"location"`
Essential *bool `json:"essential,omitempty"`
Argv []string `json:"argv,omitempty"`
Description *string `json:"description,omitempty"`
WorkloadType NexWorkload `json:"type"`
Location *url.URL `json:"location"`
Essential *bool `json:"essential,omitempty"`

// Contains claims for the workload: name, hash
WorkloadJwt *string `json:"workload_jwt"`
Expand Down Expand Up @@ -146,7 +145,7 @@ func (request *DeployRequest) DecryptRequestEnvironment(recipientXKey nkeys.KeyP
type requestOptions struct {
argv []string
workloadName string
workloadType models.NexWorkload
workloadType NexWorkload
workloadDescription string
location url.URL
env map[string]string
Expand Down Expand Up @@ -179,7 +178,7 @@ func WorkloadName(name string) RequestOption {
}

// Type of the workload, e.g., one of "native", "v8", "oci", "wasm" for this request
func WorkloadType(workloadType models.NexWorkload) RequestOption {
func WorkloadType(workloadType NexWorkload) RequestOption {
return func(o requestOptions) requestOptions {
o.workloadType = workloadType
return o
Expand Down
58 changes: 36 additions & 22 deletions control-api/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"log/slog"

cloudevents "github.com/cloudevents/sdk-go"
"github.com/synadia-io/nex/internal/models"
)

const (
Expand Down Expand Up @@ -33,12 +32,27 @@ type RunResponse struct {
Name string `json:"name"`
}

type NexWorkload string

const (
NexWorkloadNative NexWorkload = "native"
NexWorkloadV8 NexWorkload = "v8"
NexWorkloadOCI NexWorkload = "oci"
NexWorkloadWasm NexWorkload = "wasm"
)

type NodeCapabilities struct {
Sandboxable bool `json:"sandboxable"`
SupportedProviders []NexWorkload `json:"supported_providers"`
NodeTags map[string]string `json:"node_tags"`
}

type AuctionRequest struct {
Arch *string `json:"arch,omitempty"`
OS *string `json:"os,omitempty"`
Sandboxed *bool `json:"sandboxed,omitempty"`
Tags map[string]string `json:"tags,omitempty"`
WorkloadTypes []models.NexWorkload `json:"workload_types,omitempty"`
Arch *string `json:"arch,omitempty"`
OS *string `json:"os,omitempty"`
Sandboxed *bool `json:"sandboxed,omitempty"`
Tags map[string]string `json:"tags,omitempty"`
WorkloadTypes []NexWorkload `json:"workload_types,omitempty"`
}

type AuctionResponse PingResponse
Expand All @@ -64,10 +78,10 @@ type WorkloadPingResponse struct {
}

type WorkloadPingMachineSummary struct {
Id string `json:"id"`
Namespace string `json:"namespace"`
Name string `json:"name"`
WorkloadType models.NexWorkload `json:"type"`
Id string `json:"id"`
Namespace string `json:"namespace"`
Name string `json:"name"`
WorkloadType NexWorkload `json:"type"`
}

type LameDuckResponse struct {
Expand All @@ -82,13 +96,13 @@ type MemoryStat struct {
}

type InfoResponse struct {
Version string `json:"version"`
Uptime string `json:"uptime"`
PublicXKey string `json:"public_xkey"`
Tags map[string]string `json:"tags,omitempty"`
Memory *MemoryStat `json:"memory,omitempty"`
Machines []MachineSummary `json:"machines"`
SupportedWorkloadTypes []models.NexWorkload `json:"supported_workload_types,omitempty"`
Version string `json:"version"`
Uptime string `json:"uptime"`
PublicXKey string `json:"public_xkey"`
Tags map[string]string `json:"tags,omitempty"`
Memory *MemoryStat `json:"memory,omitempty"`
Machines []MachineSummary `json:"machines"`
SupportedWorkloadTypes []NexWorkload `json:"supported_workload_types,omitempty"`
}

type MachineSummary struct {
Expand All @@ -100,11 +114,11 @@ type MachineSummary struct {
}

type WorkloadSummary struct {
Name string `json:"name"`
Description string `json:"description,omitempty"`
Runtime string `json:"runtime"`
WorkloadType models.NexWorkload `json:"type"`
Hash string `json:"hash"`
Name string `json:"name"`
Description string `json:"description,omitempty"`
Runtime string `json:"runtime"`
WorkloadType NexWorkload `json:"type"`
Hash string `json:"hash"`
}

type Envelope struct {
Expand Down
40 changes: 20 additions & 20 deletions internal/agent-api/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (

"github.com/nats-io/jwt/v2"
"github.com/nats-io/nats.go"
"github.com/synadia-io/nex/internal/models"
controlapi "github.com/synadia-io/nex/control-api"
)

// Name of the internal, non-public bucket for sharing files between host and agent
Expand Down Expand Up @@ -44,19 +44,19 @@ type ExecutionProviderParams struct {

// DeployRequest processed by the agent
type DeployRequest struct {
Argv []string `json:"argv,omitempty"`
DecodedClaims jwt.GenericClaims `json:"-"`
Description *string `json:"description"`
Environment map[string]string `json:"environment"`
Essential *bool `json:"essential,omitempty"`
Hash string `json:"hash,omitempty"`
Namespace *string `json:"namespace,omitempty"`
RetriedAt *time.Time `json:"retried_at,omitempty"`
RetryCount *uint `json:"retry_count,omitempty"`
TotalBytes int64 `json:"total_bytes,omitempty"`
TriggerSubjects []string `json:"trigger_subjects"`
WorkloadName *string `json:"workload_name,omitempty"`
WorkloadType models.NexWorkload `json:"workload_type,omitempty"`
Argv []string `json:"argv,omitempty"`
DecodedClaims jwt.GenericClaims `json:"-"`
Description *string `json:"description"`
Environment map[string]string `json:"environment"`
Essential *bool `json:"essential,omitempty"`
Hash string `json:"hash,omitempty"`
Namespace *string `json:"namespace,omitempty"`
RetriedAt *time.Time `json:"retried_at,omitempty"`
RetryCount *uint `json:"retry_count,omitempty"`
TotalBytes int64 `json:"total_bytes,omitempty"`
TriggerSubjects []string `json:"trigger_subjects"`
WorkloadName *string `json:"workload_name,omitempty"`
WorkloadType controlapi.NexWorkload `json:"workload_type,omitempty"`

Stderr io.Writer `json:"-"`
Stdout io.Writer `json:"-"`
Expand All @@ -78,14 +78,14 @@ func (request *DeployRequest) IsEssential() bool {

// Returns true if the run request supports essential flag
func (request *DeployRequest) SupportsEssential() bool {
return request.WorkloadType == models.NexWorkloadNative ||
request.WorkloadType == models.NexWorkloadOCI
return request.WorkloadType == controlapi.NexWorkloadNative ||
request.WorkloadType == controlapi.NexWorkloadOCI
}

// Returns true if the run request supports trigger subjects
func (request *DeployRequest) SupportsTriggerSubjects() bool {
return (request.WorkloadType == models.NexWorkloadV8 ||
request.WorkloadType == models.NexWorkloadWasm) &&
return (request.WorkloadType == controlapi.NexWorkloadV8 ||
request.WorkloadType == controlapi.NexWorkloadWasm) &&
len(request.TriggerSubjects) > 0
}

Expand Down Expand Up @@ -114,8 +114,8 @@ func (r *DeployRequest) Validate() error {

if r.WorkloadType == "" {
err = errors.Join(err, errors.New("workload type is required"))
} else if (r.WorkloadType == models.NexWorkloadV8 ||
r.WorkloadType == models.NexWorkloadWasm) &&
} else if (r.WorkloadType == controlapi.NexWorkloadV8 ||
r.WorkloadType == controlapi.NexWorkloadWasm) &&
len(r.TriggerSubjects) == 0 {
err = errors.Join(err, errors.New("at least one trigger subject is required for this workload type"))
}
Expand Down
3 changes: 2 additions & 1 deletion internal/models/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

"github.com/nats-io/jsm.go/natscontext"
"github.com/nats-io/nats.go"
controlapi "github.com/synadia-io/nex/control-api"
)

type UiOptions struct {
Expand Down Expand Up @@ -72,7 +73,7 @@ type RunOptions struct {
TargetNode string
WorkloadUrl *url.URL
Name string
WorkloadType NexWorkload
WorkloadType controlapi.NexWorkload
Description string
PublisherXkeyFile string
ClaimsIssuerFile string
Expand Down
73 changes: 37 additions & 36 deletions internal/models/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

"github.com/nats-io/nats-server/v2/server"
"github.com/splode/fname"
controlapi "github.com/synadia-io/nex/control-api"
)

const (
Expand All @@ -25,7 +26,7 @@ const (
)

var (
DefaultWorkloadTypes = []NexWorkload{NexWorkloadNative}
DefaultWorkloadTypes = []controlapi.NexWorkload{controlapi.NexWorkloadNative}

DefaultBinPath = append([]string{"/usr/local/bin"}, filepath.SplitList(os.Getenv("PATH"))...)

Expand All @@ -36,32 +37,32 @@ var (
// Node configuration is used to configure the node process as well
// as the virtual machines it produces
type NodeConfiguration struct {
AgentHandshakeTimeoutMillisecond int `json:"agent_handshake_timeout_ms,omitempty"`
AgentPingTimeoutMillisecond int `json:"agent_ping_timeout_ms,omitempty"`
BinPath []string `json:"bin_path"`
CNI CNIDefinition `json:"cni"`
DefaultResourceDir string `json:"default_resource_dir"`
ForceDepInstall bool `json:"-"`
InternalNodeHost *string `json:"internal_node_host,omitempty"`
InternalNodePort *int `json:"internal_node_port"`
KernelFilepath string `json:"kernel_filepath"`
MachinePoolSize int `json:"machine_pool_size"`
MachineTemplate MachineTemplate `json:"machine_template"`
NoSandbox bool `json:"no_sandbox,omitempty"`
OtlpExporterUrl string `json:"otlp_exporter_url,omitempty"`
OtelMetrics bool `json:"otel_metrics"`
OtelMetricsPort int `json:"otel_metrics_port"`
OtelMetricsExporter string `json:"otel_metrics_exporter"`
OtelTraces bool `json:"otel_traces"`
OtelTracesExporter string `json:"otel_traces_exporter"`
PreserveNetwork bool `json:"preserve_network,omitempty"`
RateLimiters *Limiters `json:"rate_limiters,omitempty"`
RootFsFilepath string `json:"rootfs_filepath"`
Tags map[string]string `json:"tags,omitempty"`
ValidIssuers []string `json:"valid_issuers,omitempty"`
WorkloadTypes []NexWorkload `json:"workload_types,omitempty"`
HostServicesConfiguration *HostServicesConfig `json:"host_services,omitempty"`
AutostartConfiguration *AutostartConfig `json:"autostart,omitempty"`
AgentHandshakeTimeoutMillisecond int `json:"agent_handshake_timeout_ms,omitempty"`
AgentPingTimeoutMillisecond int `json:"agent_ping_timeout_ms,omitempty"`
BinPath []string `json:"bin_path"`
CNI CNIDefinition `json:"cni"`
DefaultResourceDir string `json:"default_resource_dir"`
ForceDepInstall bool `json:"-"`
InternalNodeHost *string `json:"internal_node_host,omitempty"`
InternalNodePort *int `json:"internal_node_port"`
KernelFilepath string `json:"kernel_filepath"`
MachinePoolSize int `json:"machine_pool_size"`
MachineTemplate MachineTemplate `json:"machine_template"`
NoSandbox bool `json:"no_sandbox,omitempty"`
OtlpExporterUrl string `json:"otlp_exporter_url,omitempty"`
OtelMetrics bool `json:"otel_metrics"`
OtelMetricsPort int `json:"otel_metrics_port"`
OtelMetricsExporter string `json:"otel_metrics_exporter"`
OtelTraces bool `json:"otel_traces"`
OtelTracesExporter string `json:"otel_traces_exporter"`
PreserveNetwork bool `json:"preserve_network,omitempty"`
RateLimiters *Limiters `json:"rate_limiters,omitempty"`
RootFsFilepath string `json:"rootfs_filepath"`
Tags map[string]string `json:"tags,omitempty"`
ValidIssuers []string `json:"valid_issuers,omitempty"`
WorkloadTypes []controlapi.NexWorkload `json:"workload_types,omitempty"`
HostServicesConfiguration *HostServicesConfig `json:"host_services,omitempty"`
AutostartConfiguration *AutostartConfig `json:"autostart,omitempty"`

// Public NATS server options; when non-nil, a public "userland" NATS server is started during node init
PublicNATSServer *server.Options `json:"public_nats_server,omitempty"`
Expand All @@ -86,15 +87,15 @@ type AutostartConfig struct {
}

type AutostartDeployRequest struct {
Name string `json:"name"`
Namespace string `json:"namespace"`
Argv []string `json:"argv,omitempty"`
Description *string `json:"description,omitempty"`
WorkloadType NexWorkload `json:"type"`
Location string `json:"location"`
JsDomain *string `json:"jsdomain,omitempty"`
Environment map[string]string `json:"environment"`
TriggerSubjects []string `json:"trigger_subjects,omitempty"`
Name string `json:"name"`
Namespace string `json:"namespace"`
Argv []string `json:"argv,omitempty"`
Description *string `json:"description,omitempty"`
WorkloadType controlapi.NexWorkload `json:"type"`
Location string `json:"location"`
JsDomain *string `json:"jsdomain,omitempty"`
Environment map[string]string `json:"environment"`
TriggerSubjects []string `json:"trigger_subjects,omitempty"`
}

func (c *NodeConfiguration) Validate() bool {
Expand Down
15 changes: 0 additions & 15 deletions internal/models/node_capabilities.go
Original file line number Diff line number Diff line change
@@ -1,16 +1 @@
package models

type NexWorkload string

const (
NexWorkloadNative NexWorkload = "native"
NexWorkloadV8 NexWorkload = "v8"
NexWorkloadOCI NexWorkload = "oci"
NexWorkloadWasm NexWorkload = "wasm"
)

type NodeCapabilities struct {
Sandboxable bool `json:"sandboxable"`
SupportedProviders []NexWorkload `json:"supported_providers"`
NodeTags map[string]string `json:"node_tags"`
}
Loading

0 comments on commit c698e97

Please sign in to comment.