Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allow agent to pull its metadata from environment #156

Merged
merged 4 commits into from
Apr 2, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 10 additions & 7 deletions agent/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,21 @@ func HaltVM(err error) {

// Initialize a new agent to facilitate communications with the host
func NewAgent(ctx context.Context, cancelF context.CancelFunc) (*Agent, error) {
metadata, err := GetMachineMetadata()
var metadata *agentapi.MachineMetadata
var err error

if os.Getenv(nexEnvSandbox) == "false" {
autodidaddict marked this conversation as resolved.
Show resolved Hide resolved
metadata, err = GetMachineDataFromEnv()
autodidaddict marked this conversation as resolved.
Show resolved Hide resolved
} else {
metadata, err = GetMachineMetadata()
}
if err != nil {
fmt.Fprintf(os.Stderr, "failed to get mmds data: %s", err)
fmt.Fprintf(os.Stderr, "failed to get machine metadata: %s", err)
return nil, err
}

if !metadata.Validate() {
return nil, fmt.Errorf("invalid metadata retrieved from mmds; %v", metadata.Errors)
return nil, fmt.Errorf("invalid metadata: %v", metadata.Errors)
}

nc, err := nats.Connect(fmt.Sprintf("nats://%s:%d", *metadata.NodeNatsHost, *metadata.NodeNatsPort))
Expand Down Expand Up @@ -105,8 +112,6 @@ func (a *Agent) FullVersion() string {
// Start the agent
// NOTE: agent process will request vm shutdown if this fails
func (a *Agent) Start() {
// n.log.Info("starting agent")

err := a.init()
if err != nil {
panic(err)
Expand All @@ -129,8 +134,6 @@ func (a *Agent) Start() {
time.Sleep(runloopSleepInterval)
}
}

// a.log.Info("exiting agent")
a.cancelF()
}

Expand Down
29 changes: 27 additions & 2 deletions agent/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"fmt"
"io"
"net/http"
"os"
"strconv"
"time"

agentapi "github.com/synadia-io/nex/internal/agent-api"
Expand All @@ -15,6 +17,11 @@ import (
// (see https://github.com/firecracker-microvm/firecracker/blob/main/docs/mmds/mmds-user-guide.md#version-2)
const MmdsAddress = "169.254.169.254"

const nexEnvSandbox = "NEX_SANDBOX"
const nexEnvWorkloadID = "NEX_WORKLOADID"
const nexEnvNodeNatsHost = "NEX_NODENATSHOST"
autodidaddict marked this conversation as resolved.
Show resolved Hide resolved
const nexEnvNodeNatsPort = "NEX_NODENATSPORT"

const metadataClientTimeoutMillis = 50
const metadataPollingTimeoutMillis = 5000

Expand Down Expand Up @@ -45,7 +52,7 @@ func GetMachineMetadata() (*agentapi.MachineMetadata, error) {
timeoutAt := time.Now().UTC().Add(metadataPollingTimeoutMillis * time.Millisecond)

for {
metadata, err := performMetadataQuery(url, req, client)
metadata, err := performMetadataQuery(req, client)
if err != nil {
if time.Now().UTC().After(timeoutAt) {
break
Expand All @@ -60,7 +67,25 @@ func GetMachineMetadata() (*agentapi.MachineMetadata, error) {
return nil, fmt.Errorf("failed to obtain metadata after %dms", metadataPollingTimeoutMillis)
}

func performMetadataQuery(url string, req *http.Request, client *http.Client) (*agentapi.MachineMetadata, error) {
func GetMachineDataFromEnv() (*agentapi.MachineMetadata, error) {
vmid := os.Getenv(nexEnvWorkloadID)
host := os.Getenv(nexEnvNodeNatsHost)
port := os.Getenv(nexEnvNodeNatsPort)
msg := "Metadata obtained from no-sandbox environment"
p, err := strconv.Atoi(port)
if err != nil {
return nil, err
}

return &agentapi.MachineMetadata{
VmID: &vmid,
NodeNatsHost: &host,
NodeNatsPort: &p,
Message: &msg,
}, nil
}

func performMetadataQuery(req *http.Request, client *http.Client) (*agentapi.MachineMetadata, error) {
resp, err := client.Do(req)
if err != nil {
return nil, err
Expand Down
Loading