Skip to content

Commit

Permalink
Add VirtletForceDHCPNetworkConfig annotation
Browse files Browse the repository at this point in the history
  • Loading branch information
Ivan Shvedunov committed Mar 5, 2019
1 parent 3bcd031 commit 7931d8c
Show file tree
Hide file tree
Showing 8 changed files with 66 additions and 3 deletions.
15 changes: 15 additions & 0 deletions docs/docs/reference/cloud-init.md
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,21 @@ use the following construct in the pod annotations:
VirtletCloudInitUserDataScript: "@virtlet-mount-script@"
```

# Disabling cloud-init based network configuration

As the network configuration is applied only upon the first startup,
it's not used when
[persistent root filesystem](../volumes/#persistent-root-filesystem)
is used, so new network configuration can be passed using DHCP to a
new pod with the same rootfs. Moreover, in some cases it may be
desirable to force Virtlet to use DHCP instead of cloud-init based
network config. To do so, you need to add the following annotation
to the pod:

```yaml
VirtletForceDHCPNetworkConfig: "true"
```

# Additional links

These links may help to understand some basics about cloud-init:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
meta-data:
instance-id: foo.default
local-hostname: foo
network-config: null
user-data: null
3 changes: 3 additions & 0 deletions pkg/libvirttools/TestContainerLifecycle.out.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@
CPUModel: ""
CPUSetting: null
DiskDriver: scsi
ForceDHCPNetworkConfig: false
InjectedFiles: null
MetaData: null
RootVolumeSize: 0
Expand Down Expand Up @@ -173,6 +174,7 @@
CPUModel: ""
CPUSetting: null
DiskDriver: scsi
ForceDHCPNetworkConfig: false
InjectedFiles: null
MetaData: null
RootVolumeSize: 0
Expand Down Expand Up @@ -226,6 +228,7 @@
CPUModel: ""
CPUSetting: null
DiskDriver: scsi
ForceDHCPNetworkConfig: false
InjectedFiles: null
MetaData: null
RootVolumeSize: 0
Expand Down
1 change: 1 addition & 0 deletions pkg/libvirttools/TestDomainForcedShutdown.out.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@
CPUModel: ""
CPUSetting: null
DiskDriver: scsi
ForceDHCPNetworkConfig: false
InjectedFiles: null
MetaData: null
RootVolumeSize: 0
Expand Down
8 changes: 5 additions & 3 deletions pkg/libvirttools/cloudinit.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,9 +177,11 @@ func (g *CloudInitGenerator) generateUserData(volumeMap diskPathMap) ([]byte, er
}

func (g *CloudInitGenerator) generateNetworkConfiguration() ([]byte, error) {
if g.config.RootVolumeDevice() != nil {
// We don't use network config with persistent rootfs
// for now because with some cloud-init
if g.config.ParsedAnnotations.ForceDHCPNetworkConfig || g.config.RootVolumeDevice() != nil {
// Don't use cloud-init network config if asked not
// to do so.
// Also, we don't use network config with persistent
// rootfs for now because with some cloud-init
// implementations it's applied only once
return nil, nil
}
Expand Down
12 changes: 12 additions & 0 deletions pkg/libvirttools/cloudinit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -388,6 +388,18 @@ func TestCloudInitGenerator(t *testing.T) {
// make sure network config is null for the persistent rootfs case
verifyNetworkConfig: true,
},
{
name: "pod with forced dhcp network config",
config: &types.VMConfig{
PodName: "foo",
PodNamespace: "default",
ParsedAnnotations: &types.VirtletAnnotations{ForceDHCPNetworkConfig: true},
},
verifyMetaData: true,
verifyUserData: true,
// make sure network config is null
verifyNetworkConfig: true,
},
{
name: "injecting mount script into user data script",
config: &types.VMConfig{
Expand Down
13 changes: 13 additions & 0 deletions pkg/metadata/types/annotations.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ const (
sshKeysKeyName = "VirtletSSHKeys"
chown9pfsMountsKeyName = "VirtletChown9pfsMounts"
systemUUIDKeyName = "VirtletSystemUUID"
forceDHCPNetworkConfigKeyName = "VirtletForceDHCPNetworkConfig"
// CloudInitUserDataSourceKeyName is the name of user data source key in the pod annotations.
CloudInitUserDataSourceKeyName = "VirtletCloudInitUserDataSource"
// SSHKeySourceKeyName is the name of ssh key source key in the pod annotations.
Expand Down Expand Up @@ -119,6 +120,10 @@ type VirtletAnnotations struct {
// SystemUUID specifies fixed SMBIOS UUID to be used for the domain.
// If not set, the SMBIOS UUID will be automatically generated from the Pod ID.
SystemUUID *uuid.UUID
// ForceDHCPNetworkConfig prevents Virtlet from using Cloud-Init based network
// configuration and makes it only provide DHCP. Note that this will
// not work for multi-CNI configuration.
ForceDHCPNetworkConfig bool
}

// ExternalDataLoader is used to load extra pod data from
Expand Down Expand Up @@ -318,5 +323,13 @@ func (va *VirtletAnnotations) parsePodAnnotations(ns string, podAnnotations map[
}
}

if podAnnotations[chown9pfsMountsKeyName] == "true" {
va.VirtletChown9pfsMounts = true
}

if podAnnotations[forceDHCPNetworkConfigKeyName] == "true" {
va.ForceDHCPNetworkConfig = true
}

return nil
}
12 changes: 12 additions & 0 deletions pkg/metadata/types/annotations_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,18 @@ func TestVirtletAnnotations(t *testing.T) {
CDImageType: "nocloud",
},
},
{
name: "force DHCP network config",
annotations: map[string]string{
"VirtletForceDHCPNetworkConfig": "true",
},
va: &VirtletAnnotations{
VCPUCount: 1,
DiskDriver: "scsi",
CDImageType: "nocloud",
ForceDHCPNetworkConfig: true,
},
},
// bad metadata items follow
{
name: "bad vcpu count",
Expand Down

0 comments on commit 7931d8c

Please sign in to comment.