Skip to content
Draft
Show file tree
Hide file tree
Changes from all 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
2 changes: 2 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ package-all: package-linux-amd64 package-linux-arm64
test:
@echo "Running tests..."
@go test -v ./...
@echo "Running local_e2e build-tag tests..."
@go test -v -tags local_e2e ./pkg/aksmachine ./pkg/cmd/daemon

.PHONY: test-coverage
test-coverage:
Expand Down
85 changes: 65 additions & 20 deletions cmd/e2ehelper/localmachine/localmachine.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,21 @@ package localmachine
import (
"context"
"encoding/json"
"errors"
"fmt"
"io"
"os"
"path/filepath"

"github.com/spf13/cobra"

"github.com/Azure/AKSFlexNode/pkg/aksmachine"
"github.com/Azure/AKSFlexNode/pkg/aksmachine/local"
)

const fileMode = 0o600

const localResourceID = "local-test-machine"

var (
flagPath string
flagKubernetesVersion string
Expand Down Expand Up @@ -78,36 +83,45 @@ func init() {
Command.AddCommand(createCmd, getCmd, statusCmd, deleteCmd)
}

func client() (*local.Client, error) {
return local.NewClient(flagPath)
}

func runCreate(ctx context.Context, out io.Writer) error {
c, err := client()
if err != nil {
return err
select {
case <-ctx.Done():
return ctx.Err()
default:
}
machine, err := c.Create(ctx, aksmachine.GoalState{KubernetesVersion: flagKubernetesVersion, SettingsVersion: flagSettingsVersion})
if err != nil {
machine := &aksmachine.Machine{
ID: localResourceID,
Goal: aksmachine.GoalState{
KubernetesVersion: flagKubernetesVersion,
SettingsVersion: flagSettingsVersion,
},
}
if err := writeLocalMachine(flagPath, machine); err != nil {
return err
}
return writeMachine(out, machine)
}

func runGet(ctx context.Context, out io.Writer) error {
c, err := client()
if err != nil {
return err
select {
case <-ctx.Done():
return ctx.Err()
default:
}
machine, err := c.Get(ctx)
machine, err := readLocalMachine(flagPath)
if err != nil {
return err
}
return writeMachine(out, machine)
}

func runStatus(ctx context.Context, out io.Writer) error {
c, err := client()
select {
case <-ctx.Done():
return ctx.Err()
default:
}
machine, err := readLocalMachine(flagPath)
if err != nil {
return err
}
Expand All @@ -116,11 +130,8 @@ func runStatus(ctx context.Context, out io.Writer) error {
ObservedSettingsVersion: flagObservedSettingsVersion,
Message: flagMessage,
}
if err := c.PatchStatus(ctx, status); err != nil {
return err
}
machine, err := c.Get(ctx)
if err != nil {
machine.Status = status
if err := writeLocalMachine(flagPath, machine); err != nil {
return err
}
return writeMachine(out, machine)
Expand All @@ -139,3 +150,37 @@ func writeMachine(out io.Writer, machine *aksmachine.Machine) error {
enc.SetIndent("", " ")
return enc.Encode(machine)
}

func readLocalMachine(path string) (*aksmachine.Machine, error) {
data, err := os.ReadFile(filepath.Clean(path))
if errors.Is(err, os.ErrNotExist) {
return nil, &aksmachine.NotFoundError{Resource: path}
}
if err != nil {
return nil, fmt.Errorf("read machine file %s: %w", path, err)
}

var machine aksmachine.Machine
if err := json.Unmarshal(data, &machine); err != nil {
return nil, fmt.Errorf("decode machine file %s: %w", path, err)
}
return &machine, nil
}

func writeLocalMachine(path string, machine *aksmachine.Machine) error {
if machine == nil {
return fmt.Errorf("machine is nil")
}
if err := os.MkdirAll(filepath.Dir(path), 0o700); err != nil {
return fmt.Errorf("create local machine file directory: %w", err)
}
data, err := json.MarshalIndent(machine, "", " ")
if err != nil {
return fmt.Errorf("marshal machine: %w", err)
}
data = append(data, '\n')
if err := os.WriteFile(filepath.Clean(path), data, fileMode); err != nil {
return fmt.Errorf("write machine file %s: %w", path, err)
}
return nil
}
2 changes: 1 addition & 1 deletion hack/e2e/lib/common.sh
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@ ensure_binary() {
E2E_BINARY="${E2E_WORK_DIR}/aks-flex-node"
(
cd "${REPO_ROOT}"
GOOS=linux GOARCH=amd64 go build -ldflags "${ldflags}" -o "${E2E_BINARY}" ./cmd/aks-flex-node
GOOS=linux GOARCH=amd64 go build -tags local_e2e -ldflags "${ldflags}" -o "${E2E_BINARY}" ./cmd/aks-flex-node
)
chmod +x "${E2E_BINARY}"

Expand Down
15 changes: 15 additions & 0 deletions pkg/aksmachine/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package aksmachine

import (
"log/slog"

"github.com/Azure/AKSFlexNode/pkg/config"
)

func newMachineClientFromConfig(cfg *config.Config, logger *slog.Logger) (MachineClient, error) {
if cfg.Agent.ARMProxyURLOverrideForE2E != "" {
logger.Warn("using ARM proxy machine client for dev-test")
return newARMProxyClient(cfg, logger)
}
return newARMClient(cfg, logger)
}
Loading
Loading