Skip to content

Commit

Permalink
Setup spec/ and test/ directories and test task
Browse files Browse the repository at this point in the history
* Add initial node preflight specs
* Update github actions
  • Loading branch information
kthomas committed Jan 22, 2024
1 parent a0f486e commit cd88e0b
Show file tree
Hide file tree
Showing 15 changed files with 399 additions and 135 deletions.
24 changes: 9 additions & 15 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ jobs:
uses: actions/setup-go@v4
with:
go-version: '1.21'
-
name: Symlink go
run: sudo ln -f -s /opt/hostedtoolcache/go/1.21.5/x64/bin/* /usr/bin/
-
name: Install ginkgo
shell: bash --noprofile --norc -x -eo pipefail {0}
Expand All @@ -60,21 +63,13 @@ jobs:
~/.local/share/pnpm/pnpm install
~/.local/share/pnpm/pnpm build
-
name: Test agent
working-directory: ./agent
run: go test -v ./...
name: Run spec suite
working-directory: .
run: sudo -E ~/go/bin/ginkgo run -r --randomize-all --vv -race --trace --keep-going ./spec
-
name: Test nex cli
working-directory: ./nex
run: go test -v ./...
-
name: Test control-api
working-directory: ./internal/control-api
run: ginkgo
-
name: Test nex node
working-directory: ./internal/node
run: go test -v ./...
name: Run test suite
working-directory: .
run: go test -v -race ./test

build:
runs-on: ubuntu-latest
Expand Down Expand Up @@ -102,7 +97,6 @@ jobs:
working-directory: ./nex
run: go build .


goreleaser:
runs-on: ubuntu-latest
steps:
Expand Down
16 changes: 16 additions & 0 deletions Taskfile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,16 @@ tasks:
cmds:
- go build -tags netgo -ldflags '-extldflags "-static"'

clean:
cmds:
- rm -f nex/nex
- sudo rm -rf /opt/cni/bin/*
- sudo rm -rf /var/lib/cni/*
- sudo rm -rf /etc/cni/conf.d/*
- sudo rm -f /tmp/rootfs-*
- sudo rm -f /tmp/.firecracker.*
- sudo rm -rf /tmp/pnats

nex:
dir: nex
sources:
Expand All @@ -30,6 +40,12 @@ tasks:
cmds:
- go build -tags netgo -ldflags '-extldflags "-static"'

test:
deps: [clean]
cmds:
- sudo $GOPATH/bin/ginkgo run -r --randomize-all --vv -race --trace --keep-going ./spec #--cover --coverprofile=.coverage-report.out
- go test -v -race ./test

ui:
dir: ui/web
cmds:
Expand Down
4 changes: 2 additions & 2 deletions internal/control-api/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ func handleLogEntry(api *Client, ch chan EmittedLog) func(m *nats.Msg) {
if len(tokens) != 6 {
return
}
var logEntry rawLog
var logEntry RawLog
err := json.Unmarshal(m.Data, &logEntry)
if err != nil {
api.log.WithError(err).Error("Log entry deserialization failure")
Expand All @@ -243,7 +243,7 @@ func handleLogEntry(api *Client, ch chan EmittedLog) func(m *nats.Msg) {
NodeId: tokens[3],
Workload: tokens[4],
Timestamp: time.Now().UTC().Format(time.RFC3339),
rawLog: logEntry,
RawLog: logEntry,
}
}

Expand Down
4 changes: 2 additions & 2 deletions internal/control-api/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,10 @@ type EmittedLog struct {
NodeId string `json:"node_id"`
Workload string `json:"workload_id"`
Timestamp string `json:"timestamp"`
rawLog
RawLog
}

type rawLog struct {
type RawLog struct {
Text string `json:"text"`
Level logrus.Level `json:"level"`
MachineId string `json:"machine_id"`
Expand Down
31 changes: 15 additions & 16 deletions internal/models/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,23 +76,23 @@ type WatchOptions struct {
// Node configuration is used to configure the node process as well
// as the virtual machines it produces
type NodeOptions struct {
Config string `json:"-"`
ConfigFilepath string `json:"-"`
ForceDepInstall bool `json:"-"`

KernelFile string `json:"kernel_file"`
RootFsFile string `json:"rootfs_file"`
DefaultDir string `json:"default_resource_dir"`
CNI CNIDefinition `json:"cni"`
InternalNodeHost *string `json:"internal_node_host,omitempty"`
InternalNodePort *int `json:"internal_node_port"`
KernelPath *string `json:"kernel_path"`
MachinePoolSize int `json:"machine_pool_size"`
MachineTemplate MachineTemplate `json:"machine_template"`
RateLimiters *Limiters `json:"rate_limiters,omitempty"`
RootFsPath *string `json:"rootfs_path"`
ValidIssuers []string `json:"valid_issuers,omitempty"`
WorkloadTypes []string `json:"workload_types,omitempty"`
Tags map[string]string `json:"tags,omitempty"`
CNI CNIDefinition `json:"cni"`
DefaultResourceDir string `json:"default_resource_dir"`
InternalNodeHost *string `json:"internal_node_host,omitempty"`
InternalNodePort *int `json:"internal_node_port"`
KernelFilepath string `json:"kernel_file"`
KernelPath *string `json:"kernel_path"` // FIXME-- audit json
MachinePoolSize int `json:"machine_pool_size"`
MachineTemplate MachineTemplate `json:"machine_template"`
RateLimiters *Limiters `json:"rate_limiters,omitempty"`
RootFsFilepath string `json:"rootfs_file"` // FIXME-- audit json
RootFsPath *string `json:"rootfs_path"`
Tags map[string]string `json:"tags,omitempty"`
ValidIssuers []string `json:"valid_issuers,omitempty"`
WorkloadTypes []string `json:"workload_types,omitempty"`

Errors []error `json:"errors,omitempty"`
}
Expand Down Expand Up @@ -125,7 +125,6 @@ type MachineTemplate struct {
}

type TokenBucket struct {

// The initial size of a token bucket.
// Minimum: 0
OneTimeBurst *int64 `json:"one_time_burst,omitempty"`
Expand Down
129 changes: 69 additions & 60 deletions internal/node/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,33 +22,70 @@ var (
defaultWorkloadTypes = []string{"elf", "v8", "wasm"}
)

type CliOptions struct {
// Servers is the list of servers to connect to
Servers string
// Creds is nats credentials to authenticate with
Creds string
// TlsCert is the TLS Public Certificate
TlsCert string
// TlsKey is the TLS Private Key
TlsKey string
// TlsCA is the certificate authority to verify the connection with
TlsCA string
// Timeout is how long to wait for operations
Timeout time.Duration
// ConnectionName is the name to use for the underlying NATS connection
ConnectionName string
// Username is the username or token to connect with
Username string
// Password is the password to connect with
Password string
// Nkey is the file holding a nkey to connect with
Nkey string
// TlsFirst configures theTLSHandshakeFirst behavior in nats.go
TlsFirst bool
// Path to the file containing virtual machine management settings and node-wide settings
NodeConfigFile string
// When enabled, the nex node will not clean up logs or rootfs files
ForensicMode bool
// When enabled, preflight will automatically install or reinstall dependencies
ForceDependencyInstall bool
}

// Node configuration is used to configure the node process as well
// as the virtual machines it produces
type NodeConfiguration struct {
KernelFile string `json:"kernel_file"`
RootFsFile string `json:"rootfs_file"`
DefaultDir string `json:"default_resource_dir"`
CNI CNIDefinition `json:"cni"`
InternalNodeHost *string `json:"internal_node_host,omitempty"`
InternalNodePort *int `json:"internal_node_port"`
KernelPath *string `json:"kernel_path"`
MachinePoolSize int `json:"machine_pool_size"`
MachineTemplate MachineTemplate `json:"machine_template"`
RateLimiters *Limiters `json:"rate_limiters,omitempty"`
RootFsPath *string `json:"rootfs_path"`
ValidIssuers []string `json:"valid_issuers,omitempty"`
WorkloadTypes []string `json:"workload_types,omitempty"`
Tags map[string]string `json:"tags,omitempty"`
ForensicMode bool `json:"-"`
ForceDepInstall bool `json:"-"`
KernelFile string `json:"kernel_file"`
RootFsFile string `json:"rootfs_file"`
DefaultResourceDir string `json:"default_resource_dir"`
CNI CNIDefinition `json:"cni"`
InternalNodeHost *string `json:"internal_node_host,omitempty"`
InternalNodePort *int `json:"internal_node_port"`
KernelPath *string `json:"kernel_path"` // FIXME
MachinePoolSize int `json:"machine_pool_size"`
MachineTemplate MachineTemplate `json:"machine_template"`
RateLimiters *Limiters `json:"rate_limiters,omitempty"`
RootFsPath *string `json:"rootfs_path"` // FIXME
ValidIssuers []string `json:"valid_issuers,omitempty"`
WorkloadTypes []string `json:"workload_types,omitempty"`
Tags map[string]string `json:"tags,omitempty"`
ForensicMode bool `json:"-"`
ForceDepInstall bool `json:"-"`

Errors []error `json:"errors,omitempty"`
}

func (c *NodeConfiguration) Validate() bool {
c.Errors = make([]error, 0)

// TODO-- add validation
if _, err := os.Stat(c.KernelFile); errors.Is(err, os.ErrNotExist) {
c.Errors = append(c.Errors, err)
}

if _, err := os.Stat(c.RootFsFile); errors.Is(err, os.ErrNotExist) {
c.Errors = append(c.Errors, err)
}

return len(c.Errors) == 0
}
Expand All @@ -62,8 +99,8 @@ type Limiters struct {
// Defines a reference to the CNI network name, which is defined and configured in a {network}.conflist file, as per
// CNI convention
type CNIDefinition struct {
NetworkName *string `json:"network_name"`
InterfaceName *string `json:"interface_name"`
NetworkName *string `json:"network_name"`
}

// Defines the CPU and memory usage of a machine to be configured when it is added to the pool
Expand All @@ -73,7 +110,6 @@ type MachineTemplate struct {
}

type TokenBucket struct {

// The initial size of a token bucket.
// Minimum: 0
OneTimeBurst *int64 `json:"one_time_burst,omitempty"`
Expand Down Expand Up @@ -114,65 +150,38 @@ func DefaultNodeConfiguration() NodeConfiguration {
}
}

// Retrieves the node configuration from the specified file
// Reads the node configuration from the specified configuration file path
func LoadNodeConfiguration(configFilePath string) (*NodeConfiguration, error) {
bytes, err := os.ReadFile(configFilePath)
if err != nil {
return nil, err
}

config := DefaultNodeConfiguration()
err = json.Unmarshal(bytes, &config)
if len(config.WorkloadTypes) == 0 {
config.WorkloadTypes = defaultWorkloadTypes
}
if err != nil {
return nil, err
}

if config.KernelFile == "" && config.DefaultDir != "" {
config.KernelFile = filepath.Join(config.DefaultDir, "vmlinux")
} else if config.KernelFile == "" && config.DefaultDir == "" {
if len(config.WorkloadTypes) == 0 {
config.WorkloadTypes = defaultWorkloadTypes
}

if config.KernelFile == "" && config.DefaultResourceDir != "" {
config.KernelFile = filepath.Join(config.DefaultResourceDir, "vmlinux")
} else if config.KernelFile == "" && config.DefaultResourceDir == "" {
return nil, errors.New("invalid kernel file setting")
}
if config.RootFsFile == "" && config.DefaultDir != "" {
config.RootFsFile = filepath.Join(config.DefaultDir, "rootfs.ext4")
} else if config.KernelFile == "" && config.DefaultDir == "" {

if config.RootFsFile == "" && config.DefaultResourceDir != "" {
config.RootFsFile = filepath.Join(config.DefaultResourceDir, "rootfs.ext4")
} else if config.KernelFile == "" && config.DefaultResourceDir == "" {
return nil, errors.New("invalid rootfs file setting")
}

if config.Tags == nil {
config.Tags = make(map[string]string)
}
return &config, nil
}

type CliOptions struct {
// Servers is the list of servers to connect to
Servers string
// Creds is nats credentials to authenticate with
Creds string
// TlsCert is the TLS Public Certificate
TlsCert string
// TlsKey is the TLS Private Key
TlsKey string
// TlsCA is the certificate authority to verify the connection with
TlsCA string
// Timeout is how long to wait for operations
Timeout time.Duration
// ConnectionName is the name to use for the underlying NATS connection
ConnectionName string
// Username is the username or token to connect with
Username string
// Password is the password to connect with
Password string
// Nkey is the file holding a nkey to connect with
Nkey string
// TlsFirst configures theTLSHandshakeFirst behavior in nats.go
TlsFirst bool
// Path to the file containing virtual machine management settings and node-wide settings
NodeConfigFile string
// When enabled, the nex node will not clean up logs or rootfs files
ForensicMode bool
// When enabled, preflight will automatically install or reinstall dependencies
ForceDependencyInstall bool
return &config, nil
}
Loading

0 comments on commit cd88e0b

Please sign in to comment.