-
Notifications
You must be signed in to change notification settings - Fork 5
Refresh nspawn device config before machine startup #412
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
Draft
Copilot
wants to merge
7
commits into
main
Choose a base branch
from
copilot/add-nspawn-config-hook
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
1873c88
Initial plan
Copilot c0eb92a
Refresh nspawn config before machine start
Copilot 71158be
Rename nspawn refresh unit to config regeneration
Copilot 7edafe8
Merge remote-tracking branch 'origin/main' into copilot/add-nspawn-co…
Copilot df6920b
Merge remote-tracking branch 'origin/main' into copilot/add-nspawn-co…
Copilot 416a1b9
agent: harden nspawn config regeneration
bcho 09e1636
Merge remote-tracking branch 'origin/main' into copilot/add-nspawn-co…
bcho File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,145 @@ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // Licensed under the MIT License. | ||
|
|
||
| package cmd | ||
|
|
||
| import ( | ||
| "context" | ||
| "encoding/json" | ||
| "errors" | ||
| "fmt" | ||
| "log/slog" | ||
| "os" | ||
| "os/signal" | ||
|
|
||
| "github.com/spf13/cobra" | ||
|
|
||
| "github.com/Azure/unbounded/internal/executil" | ||
| "github.com/Azure/unbounded/internal/provision" | ||
| "github.com/Azure/unbounded/pkg/agent/goalstates" | ||
| "github.com/Azure/unbounded/pkg/agent/phases" | ||
| "github.com/Azure/unbounded/pkg/agent/phases/rootfs" | ||
| ) | ||
|
|
||
| func newCmdRegenerateConfig(cmdCtx *CommandContext) *cobra.Command { | ||
| cmd := &cobra.Command{ | ||
| Use: "regenerate-config MACHINE_NAME", | ||
| Short: "Regenerate host-side configuration for a machine", | ||
| Hidden: true, | ||
| Args: cobra.ExactArgs(1), | ||
| RunE: func(cmd *cobra.Command, args []string) error { | ||
| ctx, cancel := signal.NotifyContext(cmd.Context(), os.Interrupt) | ||
| defer cancel() | ||
|
|
||
| cmdCtx.Setup() | ||
|
|
||
| return regenerateConfig(ctx, cmdCtx.Logger, args[0]) | ||
| }, | ||
| } | ||
|
|
||
| return cmd | ||
| } | ||
|
|
||
| func newCmdRegenerateNSpawnConfig(cmdCtx *CommandContext) *cobra.Command { | ||
| cmd := &cobra.Command{ | ||
| Use: "regenerate-nspawn-config MACHINE_NAME", | ||
| Short: "Regenerate host-side nspawn configuration for a machine", | ||
| Hidden: true, | ||
| Args: cobra.ExactArgs(1), | ||
| RunE: func(cmd *cobra.Command, args []string) error { | ||
| ctx, cancel := signal.NotifyContext(cmd.Context(), os.Interrupt) | ||
| defer cancel() | ||
|
|
||
| cmdCtx.Setup() | ||
|
|
||
| return regenerateNSpawnConfig(ctx, cmdCtx.Logger, args[0]) | ||
| }, | ||
| } | ||
|
|
||
| return cmd | ||
| } | ||
|
|
||
| func regenerateConfig(ctx context.Context, log *slog.Logger, machineName string) error { | ||
| return regenerateNSpawnConfig(ctx, log, machineName) | ||
| } | ||
|
|
||
| func regenerateNSpawnConfig(ctx context.Context, log *slog.Logger, machineName string) error { | ||
| cfg, ok, err := loadAppliedConfigForMachine(log, machineName) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| if !ok { | ||
| log.Info("applied config not found, skipping nspawn config regeneration", "machine", machineName) | ||
| return nil | ||
| } | ||
|
|
||
| rootFS, err := goalstates.ResolveNSpawnConfig(cfg, machineName) | ||
| if err != nil { | ||
| return fmt.Errorf("resolve nspawn config goal state: %w", err) | ||
| } | ||
|
|
||
| if err := phases.ExecuteTask(ctx, log, rootfs.EnsureNSpawnConfig(log, rootFS)); err != nil { | ||
| return fmt.Errorf("regenerate nspawn config for %s: %w", machineName, err) | ||
| } | ||
|
|
||
| // systemd loaded the nspawn service drop-in before starting this required | ||
| // oneshot unit. Reload the manager so the pending nspawn start observes the | ||
| // regenerated service properties, including path-specific DeviceAllow entries. | ||
| if err := executil.RunCmd(ctx, log, executil.Systemctl(), "daemon-reload"); err != nil { | ||
| return fmt.Errorf("reload systemd after regenerating config for %s: %w", machineName, err) | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| func loadAppliedConfigForMachine(log *slog.Logger, machineName string) (*provision.AgentConfig, bool, error) { | ||
| if machineName != goalstates.NSpawnMachineKube1 && machineName != goalstates.NSpawnMachineKube2 { | ||
| return nil, false, fmt.Errorf("unsupported nspawn machine %q", machineName) | ||
| } | ||
|
|
||
| return loadAppliedConfig(log, goalstates.AppliedConfigPath(machineName), goalstates.AppliedConfigChecksumPath(machineName)) | ||
| } | ||
|
|
||
| func loadAppliedConfig(log *slog.Logger, path, checksumPath string) (*provision.AgentConfig, bool, error) { | ||
| data, err := os.ReadFile(path) | ||
| if errors.Is(err, os.ErrNotExist) { | ||
| return nil, false, nil | ||
| } | ||
|
|
||
| if err != nil { | ||
| return nil, false, fmt.Errorf("read applied config %s: %w", path, err) | ||
| } | ||
|
|
||
| if err := goalstates.VerifyChecksum(data, checksumPath); err != nil { | ||
| return nil, false, fmt.Errorf("verify applied config checksum for %s: %w", path, err) | ||
| } | ||
|
|
||
| if _, statErr := os.Stat(checksumPath); errors.Is(statErr, os.ErrNotExist) { | ||
| log.Warn( | ||
| "no checksum sidecar found, skipping integrity check", | ||
| "config_path", path, | ||
| "checksum_path", checksumPath, | ||
| ) | ||
| } | ||
|
|
||
| var cfg provision.AgentConfig | ||
| if err := json.Unmarshal(data, &cfg); err != nil { | ||
| return nil, false, fmt.Errorf("decode applied config %s: %w", path, err) | ||
| } | ||
|
|
||
| source, err := provision.ResolveMachineName(&cfg) | ||
| if err != nil { | ||
| return nil, false, fmt.Errorf("resolve applied config machine name %s: %w", path, err) | ||
| } | ||
|
|
||
| if source != "config" { | ||
| log.Info("resolved unbounded MachineName", "name", cfg.MachineName, "source", source) | ||
| } | ||
|
|
||
| if err := cfg.BackfillNodeName(); err != nil { | ||
| return nil, false, fmt.Errorf("backfill applied config node name %s: %w", path, err) | ||
| } | ||
|
|
||
| return &cfg, true, nil | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // Licensed under the MIT License. | ||
|
|
||
| package cmd | ||
|
|
||
| import ( | ||
| "encoding/json" | ||
| "os" | ||
| "path/filepath" | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/require" | ||
|
|
||
| "github.com/Azure/unbounded/internal/provision" | ||
| "github.com/Azure/unbounded/pkg/agent/goalstates" | ||
| ) | ||
|
|
||
| func TestLoadAppliedConfig(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| dir := t.TempDir() | ||
| configPath := filepath.Join(dir, "applied-config.json") | ||
| checksumPath := configPath + ".sha256" | ||
| want := provision.AgentConfig{ | ||
| MachineName: "machine-1", | ||
| NodeName: "node-1", | ||
| } | ||
|
|
||
| data, err := json.Marshal(&want) | ||
| require.NoError(t, err) | ||
| require.NoError(t, os.WriteFile(configPath, data, 0o600)) | ||
| require.NoError(t, os.WriteFile(checksumPath, []byte(goalstates.ComputeChecksum(data)+"\n"), 0o600)) | ||
|
|
||
| got, ok, err := loadAppliedConfig(testLogger(), configPath, checksumPath) | ||
| require.NoError(t, err) | ||
| require.True(t, ok) | ||
| require.Equal(t, want.MachineName, got.MachineName) | ||
| require.Equal(t, want.NodeName, got.NodeName) | ||
| } | ||
|
|
||
| func TestLoadAppliedConfigMissing(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| dir := t.TempDir() | ||
| got, ok, err := loadAppliedConfig( | ||
| testLogger(), | ||
| filepath.Join(dir, "missing.json"), | ||
| filepath.Join(dir, "missing.json.sha256"), | ||
| ) | ||
|
|
||
| require.NoError(t, err) | ||
| require.False(t, ok) | ||
| require.Nil(t, got) | ||
| } | ||
|
|
||
| func TestLoadAppliedConfigChecksumMismatch(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| dir := t.TempDir() | ||
| configPath := filepath.Join(dir, "applied-config.json") | ||
| checksumPath := configPath + ".sha256" | ||
| require.NoError(t, os.WriteFile(configPath, []byte(`{"MachineName":"machine-1"}`), 0o600)) | ||
| require.NoError(t, os.WriteFile(checksumPath, []byte(goalstates.ComputeChecksum([]byte("different"))), 0o600)) | ||
|
|
||
| got, ok, err := loadAppliedConfig(testLogger(), configPath, checksumPath) | ||
| require.ErrorIs(t, err, goalstates.ErrChecksumMismatch) | ||
| require.False(t, ok) | ||
| require.Nil(t, got) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.