Skip to content
This repository has been archived by the owner on Dec 7, 2023. It is now read-only.

Add option to change ID prefix #757

Merged
merged 11 commits into from
Feb 1, 2021
9 changes: 9 additions & 0 deletions cmd/ignite/cmd/cmdutil/flags.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package cmdutil

import (
"fmt"

"github.com/spf13/pflag"
"github.com/weaveworks/ignite/pkg/constants"
)
Expand All @@ -10,6 +12,13 @@ func AddNameFlag(fs *pflag.FlagSet, name *string) {
fs.StringVarP(name, "name", "n", *name, "Specify the name")
}

func AddIDPrefixFlag(fs *pflag.FlagSet, idPrefix *string) {
// Note that the flag default is printed for good UX, but it's not implemented by pflag
// We have our own defaulting ComponentConfig logic
fs.StringVar(idPrefix, "id-prefix", "",
fmt.Sprintf("Prefix string for system identifiers (default %v)", constants.IGNITE_PREFIX))
}

func AddConfigFlag(fs *pflag.FlagSet, configFile *string) {
fs.StringVar(configFile, "config", *configFile, "Specify a path to a file with the API resources you want to pass")
}
Expand Down
1 change: 1 addition & 0 deletions cmd/ignite/cmd/vmcmd/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,4 +87,5 @@ func addCreateFlags(fs *pflag.FlagSet, cf *run.CreateFlags) {

runtimeflag.RuntimeVar(fs, &providers.RuntimeName)
networkflag.NetworkPluginVar(fs, &providers.NetworkPluginName)
cmdutil.AddIDPrefixFlag(fs, &providers.IDPrefix)
}
4 changes: 2 additions & 2 deletions cmd/ignite/cmd/vmcmd/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ func NewCmdStart(out io.Writer) *cobra.Command {
addStartFlags(cmd.Flags(), sf)

// NOTE: Since the run command combines the create and start command flags,
// to avoid redefining runtime and network flag in run command, they are
// defined separately here.
// to avoid redefining runtime, network, and id-prefix flags in the run command,
// they are defined separately here, and re-used from addCreateFlags.
runtimeflag.RuntimeVar(cmd.Flags(), &providers.RuntimeName)
networkflag.NetworkPluginVar(cmd.Flags(), &providers.NetworkPluginName)

Expand Down
3 changes: 1 addition & 2 deletions cmd/ignite/run/attach.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
api "github.com/weaveworks/ignite/pkg/apis/ignite"
"github.com/weaveworks/ignite/pkg/config"
"github.com/weaveworks/ignite/pkg/providers"
"github.com/weaveworks/ignite/pkg/util"
)

// checkRunning can be used to skip the running check, this is used by Start and Run
Expand Down Expand Up @@ -37,7 +36,7 @@ func Attach(ao *AttachOptions) error {
fmt.Println(ao.vm.GetUID())

// Attach to the VM in Docker
if err := providers.Runtime.AttachContainer(util.NewPrefixer().Prefix(ao.vm.GetUID())); err != nil {
if err := providers.Runtime.AttachContainer(ao.vm.PrefixedID()); err != nil {
return fmt.Errorf("failed to attach to container for VM %s: %v", ao.vm.GetUID(), err)
}

Expand Down
5 changes: 3 additions & 2 deletions cmd/ignite/run/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,9 @@ func (cf *CreateFlags) NewCreateOptions(args []string, fs *flag.FlagSet) (*Creat
baseVM.Spec = providers.ComponentConfig.Spec.VMDefaults
}

// Set the runtime and network-plugin on the VM. This overrides the global
// config.
// Initialize the VM's Prefixer
baseVM.Status.IDPrefix = providers.IDPrefix
// Set the runtime and network-plugin on the VM, then override the global config.
baseVM.Status.Runtime.Name = providers.RuntimeName
baseVM.Status.Network.Plugin = providers.NetworkPluginName
// Populate the runtime and network-plugin providers.
Expand Down
3 changes: 1 addition & 2 deletions cmd/ignite/run/logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
api "github.com/weaveworks/ignite/pkg/apis/ignite"
"github.com/weaveworks/ignite/pkg/config"
"github.com/weaveworks/ignite/pkg/providers"
"github.com/weaveworks/ignite/pkg/util"
)

type LogsOptions struct {
Expand All @@ -32,7 +31,7 @@ func Logs(lo *LogsOptions) error {
}

// Fetch the VM logs
rc, err := providers.Runtime.ContainerLogs(util.NewPrefixer().Prefix(lo.vm.GetUID()))
rc, err := providers.Runtime.ContainerLogs(lo.vm.PrefixedID())
if err != nil {
return fmt.Errorf("failed to get logs for VM %q: %v", lo.vm.GetUID(), err)
}
Expand Down
3 changes: 2 additions & 1 deletion cmd/ignite/run/testdata/output/apply-vm-config-empty.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
"kernel": {
"id": null,
"size": "0B"
}
},
"idPrefix": ""
}
}
3 changes: 2 additions & 1 deletion cmd/ignite/run/testdata/output/apply-vm-config-json.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
"kernel": {
"id": null,
"size": "0B"
}
},
"idPrefix": ""
}
}
3 changes: 2 additions & 1 deletion cmd/ignite/run/testdata/output/apply-vm-config-yaml.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
"kernel": {
"id": null,
"size": "0B"
}
},
"idPrefix": ""
}
}
3 changes: 2 additions & 1 deletion cmd/ignite/run/testdata/output/inspect-json.txt
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
"kernel": {
"id": null,
"size": "0B"
}
},
"idPrefix": ""
}
}
1 change: 1 addition & 0 deletions cmd/ignite/run/testdata/output/inspect-yaml.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ spec:
oci: foo/bar:latest
storage: {}
status:
idPrefix: ""
image:
id: null
size: 0B
Expand Down
2 changes: 2 additions & 0 deletions cmd/ignited/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/spf13/cobra"
"github.com/spf13/pflag"

"github.com/weaveworks/ignite/cmd/ignite/cmd/cmdutil"
"github.com/weaveworks/ignite/pkg/config"
"github.com/weaveworks/ignite/pkg/logs"
logflag "github.com/weaveworks/ignite/pkg/logs/flag"
Expand Down Expand Up @@ -63,5 +64,6 @@ func addGlobalFlags(fs *pflag.FlagSet) {
logflag.LogLevelFlagVar(fs, &logLevel)
runtimeflag.RuntimeVar(fs, &providers.RuntimeName)
networkflag.NetworkPluginVar(fs, &providers.NetworkPluginName)
cmdutil.AddIDPrefixFlag(fs, &providers.IDPrefix)
fs.StringVar(&configPath, "ignite-config", "", "Ignite configuration path; refer to the 'Ignite Configuration' docs for more details")
}
8 changes: 5 additions & 3 deletions docs/api/ignite_v1alpha3.md
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ type BlockDeviceVolume struct {

BlockDeviceVolume defines a block device on the host

## <a name="Configuration">type</a> [Configuration](https://github.com/weaveworks/ignite/tree/master/pkg/apis/ignite/v1alpha3/types.go?s=9231:9374#L252)
## <a name="Configuration">type</a> [Configuration](https://github.com/weaveworks/ignite/tree/master/pkg/apis/ignite/v1alpha3/types.go?s=9275:9418#L253)

``` go
type Configuration struct {
Expand All @@ -166,13 +166,14 @@ type Configuration struct {
Configuration represents the ignite runtime configuration.
+k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object

## <a name="ConfigurationSpec">type</a> [ConfigurationSpec](https://github.com/weaveworks/ignite/tree/master/pkg/apis/ignite/v1alpha3/types.go?s=9431:9674#L260)
## <a name="ConfigurationSpec">type</a> [ConfigurationSpec](https://github.com/weaveworks/ignite/tree/master/pkg/apis/ignite/v1alpha3/types.go?s=9475:9786#L261)

``` go
type ConfigurationSpec struct {
Runtime igniteRuntime.Name `json:"runtime,omitempty"`
NetworkPlugin igniteNetwork.PluginName `json:"networkPlugin,omitempty"`
VMDefaults VMSpec `json:"vmDefaults,omitempty"`
IDPrefix string `json:"idPrefix,omitempty"`
}
```

Expand Down Expand Up @@ -495,7 +496,7 @@ type VMSpec struct {

VMSpec describes the configuration of a VM

## <a name="VMStatus">type</a> [VMStatus](https://github.com/weaveworks/ignite/tree/master/pkg/apis/ignite/v1alpha3/types.go?s=8785:9096#L241)
## <a name="VMStatus">type</a> [VMStatus](https://github.com/weaveworks/ignite/tree/master/pkg/apis/ignite/v1alpha3/types.go?s=8785:9140#L241)

``` go
type VMStatus struct {
Expand All @@ -505,6 +506,7 @@ type VMStatus struct {
Network *Network `json:"network,omitempty"`
Image OCIImageSource `json:"image"`
Kernel OCIImageSource `json:"kernel"`
IDPrefix string `json:"idPrefix"`
}
```

Expand Down
1 change: 1 addition & 0 deletions docs/cli/ignite/ignite_create.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ ignite create <OCI image> [flags]
-f, --copy-files strings Copy files/directories from the host to the created VM
--cpus uint VM vCPU count, 1 or even numbers between 1 and 32 (default 1)
-h, --help help for create
--id-prefix string Prefix string for system identifiers (default ignite)
--kernel-args string Set the command line for the kernel (default "console=ttyS0 reboot=k panic=1 pci=off ip=dhcp")
-k, --kernel-image oci-image Specify an OCI image containing the kernel at /boot/vmlinux and optionally, modules (default weaveworks/ignite-kernel:4.19.125)
-l, --label stringArray Set a label (foo=bar)
Expand Down
1 change: 1 addition & 0 deletions docs/cli/ignite/ignite_run.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ ignite run <OCI image> [flags]
--cpus uint VM vCPU count, 1 or even numbers between 1 and 32 (default 1)
-d, --debug Debug mode, keep container after VM shutdown
-h, --help help for run
--id-prefix string Prefix string for system identifiers (default ignite)
--ignore-preflight-checks strings A list of checks whose errors will be shown as warnings. Example: 'BinaryInPath,Port,ExistingFile'. Value 'all' ignores errors from all checks.
-i, --interactive Attach to the VM after starting
--kernel-args string Set the command line for the kernel (default "console=ttyS0 reboot=k panic=1 pci=off ip=dhcp")
Expand Down
1 change: 1 addition & 0 deletions docs/cli/ignite/ignite_vm_create.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ ignite vm create <OCI image> [flags]
-f, --copy-files strings Copy files/directories from the host to the created VM
--cpus uint VM vCPU count, 1 or even numbers between 1 and 32 (default 1)
-h, --help help for create
--id-prefix string Prefix string for system identifiers (default ignite)
--kernel-args string Set the command line for the kernel (default "console=ttyS0 reboot=k panic=1 pci=off ip=dhcp")
-k, --kernel-image oci-image Specify an OCI image containing the kernel at /boot/vmlinux and optionally, modules (default weaveworks/ignite-kernel:4.19.125)
-l, --label stringArray Set a label (foo=bar)
Expand Down
1 change: 1 addition & 0 deletions docs/cli/ignite/ignite_vm_run.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ ignite vm run <OCI image> [flags]
--cpus uint VM vCPU count, 1 or even numbers between 1 and 32 (default 1)
-d, --debug Debug mode, keep container after VM shutdown
-h, --help help for run
--id-prefix string Prefix string for system identifiers (default ignite)
--ignore-preflight-checks strings A list of checks whose errors will be shown as warnings. Example: 'BinaryInPath,Port,ExistingFile'. Value 'all' ignores errors from all checks.
-i, --interactive Attach to the VM after starting
--kernel-args string Set the command line for the kernel (default "console=ttyS0 reboot=k panic=1 pci=off ip=dhcp")
Expand Down
1 change: 1 addition & 0 deletions docs/cli/ignited/ignited.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ TODO: ignited documentation

```
-h, --help help for ignited
--id-prefix string Prefix string for system identifiers (default ignite)
--ignite-config string Ignite configuration path; refer to the 'Ignite Configuration' docs for more details
--log-level loglevel Specify the loglevel for the program (default info)
--network-plugin plugin Network plugin to use. Available options are: [cni docker-bridge] (default cni)
Expand Down
1 change: 1 addition & 0 deletions docs/cli/ignited/ignited_completion.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ ignited completion [flags]
### Options inherited from parent commands

```
--id-prefix string Prefix string for system identifiers (default ignite)
--ignite-config string Ignite configuration path; refer to the 'Ignite Configuration' docs for more details
--log-level loglevel Specify the loglevel for the program (default info)
--network-plugin plugin Network plugin to use. Available options are: [cni docker-bridge] (default cni)
Expand Down
1 change: 1 addition & 0 deletions docs/cli/ignited/ignited_daemon.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ ignited daemon [flags]
### Options inherited from parent commands

```
--id-prefix string Prefix string for system identifiers (default ignite)
--ignite-config string Ignite configuration path; refer to the 'Ignite Configuration' docs for more details
--log-level loglevel Specify the loglevel for the program (default info)
--network-plugin plugin Network plugin to use. Available options are: [cni docker-bridge] (default cni)
Expand Down
1 change: 1 addition & 0 deletions docs/cli/ignited/ignited_gitops.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ ignited gitops <repo-url> [flags]
### Options inherited from parent commands

```
--id-prefix string Prefix string for system identifiers (default ignite)
--ignite-config string Ignite configuration path; refer to the 'Ignite Configuration' docs for more details
--log-level loglevel Specify the loglevel for the program (default info)
--network-plugin plugin Network plugin to use. Available options are: [cni docker-bridge] (default cni)
Expand Down
1 change: 1 addition & 0 deletions docs/cli/ignited/ignited_version.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ ignited version [flags]
### Options inherited from parent commands

```
--id-prefix string Prefix string for system identifiers (default ignite)
--ignite-config string Ignite configuration path; refer to the 'Ignite Configuration' docs for more details
--log-level loglevel Specify the loglevel for the program (default info)
--network-plugin plugin Network plugin to use. Available options are: [cni docker-bridge] (default cni)
Expand Down
36 changes: 35 additions & 1 deletion e2e/run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func TestIgniteRunWithContainerdAndCNI(t *testing.T) {

// runCurl is a helper for testing network connectivity
// vmName should be unique for each test
func runCurl(t *testing.T, vmName, runtime, networkPlugin string) {
func runCurl(t *testing.T, vmName, runtime, networkPlugin string, extraRunArgs []string) {
assert.Assert(t, e2eHome != "", "IGNITE_E2E_HOME should be set")

igniteCmd := util.NewCommand(t, igniteBin)
Expand All @@ -82,6 +82,7 @@ func runCurl(t *testing.T, vmName, runtime, networkPlugin string) {
With("run", "--name="+vmName).
With(util.DefaultVMImage).
With("--ssh").
With(extraRunArgs...).
Run()

igniteCmd.New().
Expand All @@ -96,6 +97,7 @@ func TestCurlWithDockerAndDockerBridge(t *testing.T) {
"e2e-test-curl-docker-and-docker-bridge",
"docker",
"docker-bridge",
[]string{},
)
}

Expand All @@ -105,6 +107,7 @@ func TestCurlWithDockerAndCNI(t *testing.T) {
"e2e-test-curl-docker-and-cni",
"docker",
"cni",
[]string{},
)
}

Expand All @@ -114,6 +117,37 @@ func TestCurlWithContainerdAndCNI(t *testing.T) {
"e2e-test-curl-containerd-and-cni",
"containerd",
"cni",
[]string{},
)
}

func TestIDPrefixCurlWithDockerAndDockerBridge(t *testing.T) {
runCurl(
t,
"e2e-test-curl-docker-and-docker-bridge",
"docker",
"docker-bridge",
[]string{"--id-prefix=e2e"},
)
}

func TestIDPrefixCurlWithDockerAndCNI(t *testing.T) {
runCurl(
t,
"e2e-test-curl-docker-and-cni",
"docker",
"cni",
[]string{"--id-prefix=e2e"},
)
}

func TestIDPrefixCurlWithContainerdAndCNI(t *testing.T) {
runCurl(
t,
"e2e-test-curl-containerd-and-cni",
"containerd",
"cni",
[]string{"--id-prefix=e2e"},
)
}

Expand Down
18 changes: 17 additions & 1 deletion pkg/apis/ignite/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,25 @@ func (vm *VM) SetKernel(kernel *Kernel) {
vm.Status.Kernel = kernel.Status.OCISource
}

// NewPrefixer returns a util.Prefixer specific to the VM
func (vm *VM) NewPrefixer() *util.Prefixer {
if vm.Status.IDPrefix == "" {
// default for backwards compatibility /w previous VM's with no status set
// note, we need to be careful that the VM.Status.IDPrefix is set as soon as possible
// to avoid non-matching prefixedID's
return util.NewPrefixer(constants.IGNITE_PREFIX)
}
return util.NewPrefixer(vm.Status.IDPrefix)
}

// PrefixedID returns the VM's prefixed ID for system references
func (vm *VM) PrefixedID() string {
return vm.NewPrefixer().Prefix(vm.GetUID())
}

// SnapshotDev returns the path where the (legacy) DM snapshot exists
func (vm *VM) SnapshotDev() string {
return path.Join("/dev/mapper", util.NewPrefixer().Prefix(vm.GetUID()))
return path.Join("/dev/mapper", vm.PrefixedID())
}

// Running returns true if the VM is running, otherwise false
Expand Down
2 changes: 2 additions & 0 deletions pkg/apis/ignite/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,7 @@ type VMStatus struct {
Network *Network `json:"network,omitempty"`
Image OCIImageSource `json:"image"`
Kernel OCIImageSource `json:"kernel"`
IDPrefix string `json:"idPrefix"`
}

// Configuration represents the ignite runtime configuration.
Expand All @@ -261,4 +262,5 @@ type ConfigurationSpec struct {
Runtime igniteRuntime.Name `json:"runtime,omitempty"`
NetworkPlugin igniteNetwork.PluginName `json:"networkPlugin,omitempty"`
VMDefaults VMSpec `json:"vmDefaults,omitempty"`
IDPrefix string `json:"idPrefix,omitempty"`
}
1 change: 1 addition & 0 deletions pkg/apis/ignite/v1alpha2/zz_generated.conversion.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions pkg/apis/ignite/v1alpha3/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,7 @@ type VMStatus struct {
Network *Network `json:"network,omitempty"`
Image OCIImageSource `json:"image"`
Kernel OCIImageSource `json:"kernel"`
IDPrefix string `json:"idPrefix"`
}

// Configuration represents the ignite runtime configuration.
Expand All @@ -261,4 +262,5 @@ type ConfigurationSpec struct {
Runtime igniteRuntime.Name `json:"runtime,omitempty"`
NetworkPlugin igniteNetwork.PluginName `json:"networkPlugin,omitempty"`
VMDefaults VMSpec `json:"vmDefaults,omitempty"`
IDPrefix string `json:"idPrefix,omitempty"`
}
Loading