From 971db3c0c3d862638e29633038c560c256e83baa Mon Sep 17 00:00:00 2001 From: Rolf Neugebauer Date: Thu, 6 Apr 2017 23:16:32 +0100 Subject: [PATCH] infrakit: Add LogicalID -> IP -> UUID mapping If the LogicalID passed in is a IP address, turn this into a UUID and pass it to HyperKit. This will cause VPNKit to assign the IP address the VM. Note: This currently requires a custom version of VPNKit Signed-off-by: Rolf Neugebauer --- src/cmd/infrakit-instance-hyperkit/instance.go | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/cmd/infrakit-instance-hyperkit/instance.go b/src/cmd/infrakit-instance-hyperkit/instance.go index b8c28378c..6b1d4d1f7 100644 --- a/src/cmd/infrakit-instance-hyperkit/instance.go +++ b/src/cmd/infrakit-instance-hyperkit/instance.go @@ -4,6 +4,7 @@ import ( "errors" "fmt" "io/ioutil" + "net" "os" "path" @@ -70,11 +71,23 @@ func (p hyperkitPlugin) Provision(spec instance.Spec) (*instance.ID, error) { id := instance.ID(path.Base(instanceDir)) log.Infof("[%s] New instance", id) + // The LogicalID may be a IP address. If so, translate it into a + // magic UUID which cause VPNKit to assign a fixed IP address logicalID := string(id) + uuidStr := "" if spec.LogicalID != nil { logicalID = string(*spec.LogicalID) + if ip := net.ParseIP(logicalID); len(ip) > 0 { + uuid := make([]byte, 16) + uuid[12] = ip.To4()[0] + uuid[13] = ip.To4()[1] + uuid[14] = ip.To4()[2] + uuid[15] = ip.To4()[3] + uuidStr = fmt.Sprintf("%x-%x-%x-%x-%x", uuid[0:4], uuid[4:6], uuid[6:8], uuid[8:10], uuid[10:]) + } } log.Infof("[%s] LogicalID: %s", id, logicalID) + log.Debugf("[%s] UUID: %s", id, uuidStr) // Start a HyperKit instance h, err := hyperkit.New(p.HyperKit, instanceDir, p.VPNKitSock, "") @@ -86,6 +99,7 @@ func (p hyperkitPlugin) Provision(spec instance.Spec) (*instance.ID, error) { h.CPUs = int(properties["CPUs"].(float64)) h.Memory = int(properties["Memory"].(float64)) h.DiskSize = int(properties["Disk"].(float64)) + h.UUID = uuidStr h.UserData = spec.Init h.Console = hyperkit.ConsoleFile log.Infof("[%s] Booting: %s/%s", id, h.Kernel, h.Initrd)