VMware vSphere Integrated Containers provider (#206)
* Add Virtual Kubelet provider for VIC Initial virtual kubelet provider for VMware VIC. This provider currently handles creating and starting of a pod VM via the VIC portlayer and persona server. Image store handling via the VIC persona server. This provider currently requires the feature/wolfpack branch of VIC. * Added pod stop and delete. Also added node capacity. Added the ability to stop and delete pod VMs via VIC. Also retrieve node capacity information from the VCH. * Cleanup and readme file Some file clean up and added a Readme.md markdown file for the VIC provider. * Cleaned up errors, added function comments, moved operation code 1. Cleaned up error handling. Set standard for creating errors. 2. Added method prototype comments for all interface functions. 3. Moved PodCreator, PodStarter, PodStopper, and PodDeleter to a new folder. * Add mocking code and unit tests for podcache, podcreator, and podstarter Used the unit test framework used in VIC to handle assertions in the provider's unit test. Mocking code generated using OSS project mockery, which is compatible with the testify assertion framework. * Vendored packages for the VIC provider Requires feature/wolfpack branch of VIC and a few specific commit sha of projects used within VIC. * Implementation of POD Stopper and Deleter unit tests (#4) * Updated files for initial PR
This commit is contained in:
179
providers/vic/utils/units.go
Normal file
179
providers/vic/utils/units.go
Normal file
@@ -0,0 +1,179 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
)
|
||||
|
||||
const (
|
||||
BYTE = 1.0 << (10 * iota)
|
||||
KILOBYTE
|
||||
MEGABYTE
|
||||
GIGABYTE
|
||||
TERABYTE
|
||||
PETABYTE
|
||||
)
|
||||
|
||||
const (
|
||||
KILOBYTESUFFIX = "Ki"
|
||||
MEGABYTESUFFIX = "Mi"
|
||||
GIGABYTESUFFIX = "Gi"
|
||||
TERABYTESUFFIX = "Ti"
|
||||
PETABYTESUFFIX = "Pi"
|
||||
)
|
||||
|
||||
const (
|
||||
ONE = 1.0
|
||||
KILO = 1000.0
|
||||
MEGA = KILO * KILO
|
||||
GIGA = MEGA * KILO
|
||||
TERA = GIGA * KILO
|
||||
PETA = TERA * KILO
|
||||
)
|
||||
|
||||
const (
|
||||
KILOSUFFIX = "K"
|
||||
MEGASUFFIX = "M"
|
||||
GIGASUFFIX = "G"
|
||||
TERASUFFIX = "T"
|
||||
PETASUFFIX = "P"
|
||||
)
|
||||
|
||||
const (
|
||||
MEMORYCUTOVER = 100
|
||||
FREQUENCYCUTOVER = 10
|
||||
)
|
||||
|
||||
const (
|
||||
MINPODSIZE = 2 * GIGABYTE
|
||||
)
|
||||
|
||||
func MemsizeToBytesize(size int64, unit string) int64 {
|
||||
u := strings.ToLower(unit)
|
||||
var sizeInBytes int64
|
||||
switch u {
|
||||
case "b":
|
||||
sizeInBytes = size
|
||||
break
|
||||
case "k":
|
||||
case "kb":
|
||||
sizeInBytes = size * KILOBYTE
|
||||
break
|
||||
case "m":
|
||||
case "mb":
|
||||
sizeInBytes = size * MEGABYTE
|
||||
break
|
||||
case "g":
|
||||
case "gb":
|
||||
sizeInBytes = size * GIGABYTE
|
||||
break
|
||||
case "t":
|
||||
case "tb":
|
||||
sizeInBytes = size * TERABYTE
|
||||
break
|
||||
case "p":
|
||||
case "pb":
|
||||
sizeInBytes = size * PETABYTE
|
||||
break
|
||||
default:
|
||||
return 0
|
||||
}
|
||||
|
||||
return sizeInBytes
|
||||
}
|
||||
|
||||
func MemsizeToDecimalString(size int64, unit string) string {
|
||||
sizeInBytes := MemsizeToBytesize(size, unit)
|
||||
|
||||
var res string
|
||||
if sizeInBytes >= PETA*MEMORYCUTOVER {
|
||||
res = fmt.Sprintf("%d%s", sizeInBytes/PETA, PETASUFFIX)
|
||||
} else if sizeInBytes >= TERA*MEMORYCUTOVER {
|
||||
res = fmt.Sprintf("%d%s", sizeInBytes/TERA, TERASUFFIX)
|
||||
} else if sizeInBytes >= GIGA*MEMORYCUTOVER {
|
||||
res = fmt.Sprintf("%d%s", sizeInBytes/GIGA, GIGASUFFIX)
|
||||
} else if sizeInBytes >= MEGA*MEMORYCUTOVER {
|
||||
res = fmt.Sprintf("%d%s", sizeInBytes/MEGA, MEGASUFFIX)
|
||||
} else if sizeInBytes >= KILO*MEMORYCUTOVER {
|
||||
res = fmt.Sprintf("%d%s", sizeInBytes/KILO, KILOSUFFIX)
|
||||
} else {
|
||||
res = fmt.Sprintf("%d", sizeInBytes)
|
||||
}
|
||||
|
||||
return res
|
||||
}
|
||||
|
||||
func MemsizeToBinaryString(size int64, unit string) string {
|
||||
sizeInBytes := MemsizeToBytesize(size, unit)
|
||||
|
||||
var res string
|
||||
if sizeInBytes >= PETABYTE*MEMORYCUTOVER {
|
||||
res = fmt.Sprintf("%d%s", sizeInBytes/PETABYTE, PETABYTESUFFIX)
|
||||
} else if sizeInBytes >= TERABYTE*MEMORYCUTOVER {
|
||||
res = fmt.Sprintf("%d%s", sizeInBytes/TERABYTE, TERABYTESUFFIX)
|
||||
} else if sizeInBytes >= GIGABYTE*MEMORYCUTOVER {
|
||||
res = fmt.Sprintf("%d%s", sizeInBytes/GIGABYTE, GIGABYTESUFFIX)
|
||||
} else if sizeInBytes >= MEGABYTE*MEMORYCUTOVER {
|
||||
res = fmt.Sprintf("%d%s", sizeInBytes/MEGABYTE, MEGABYTESUFFIX)
|
||||
} else if sizeInBytes >= KILOBYTE*MEMORYCUTOVER {
|
||||
res = fmt.Sprintf("%d%s", sizeInBytes/KILOBYTE, KILOBYTESUFFIX)
|
||||
} else {
|
||||
res = fmt.Sprintf("%d", sizeInBytes)
|
||||
}
|
||||
|
||||
return res
|
||||
}
|
||||
|
||||
func MemsizeToMaxPodCount(size int64, unit string) int64 {
|
||||
sizeInBytes := MemsizeToBytesize(size, unit)
|
||||
|
||||
// Divide by minimum pod size
|
||||
return sizeInBytes / MINPODSIZE
|
||||
}
|
||||
|
||||
func FrequencyToHertzFrequency(size int64, unit string) int64 {
|
||||
u := strings.ToLower(unit)
|
||||
var sizeInBytes int64
|
||||
switch u {
|
||||
case "k":
|
||||
case "khz":
|
||||
sizeInBytes = size * KILO
|
||||
break
|
||||
case "m":
|
||||
case "mhz":
|
||||
sizeInBytes = size * MEGA
|
||||
break
|
||||
case "g":
|
||||
case "ghz":
|
||||
sizeInBytes = size * GIGA
|
||||
break
|
||||
default:
|
||||
return 0
|
||||
}
|
||||
|
||||
return sizeInBytes
|
||||
}
|
||||
|
||||
func CpuFrequencyToString(size int64, unit string) string {
|
||||
var res string
|
||||
|
||||
hertz := FrequencyToHertzFrequency(size, unit)
|
||||
if hertz >= GIGA*10 {
|
||||
res = fmt.Sprintf("%d%s", hertz/GIGA, GIGASUFFIX)
|
||||
} else if hertz >= MEGA*FREQUENCYCUTOVER {
|
||||
res = fmt.Sprintf("%d%s", hertz/MEGA, MEGASUFFIX)
|
||||
} else if hertz >= KILO*FREQUENCYCUTOVER {
|
||||
res = fmt.Sprintf("%d%s", hertz/KILO, KILOSUFFIX)
|
||||
} else {
|
||||
res = fmt.Sprintf("%d", hertz)
|
||||
}
|
||||
|
||||
return res
|
||||
}
|
||||
|
||||
func CpuFrequencyToCores(size int64, unit string) int64 {
|
||||
hertz := FrequencyToHertzFrequency(size, unit)
|
||||
// Assume 1G per Core
|
||||
cores := hertz / GIGA
|
||||
return cores
|
||||
}
|
||||
53
providers/vic/utils/units_test.go
Normal file
53
providers/vic/utils/units_test.go
Normal file
@@ -0,0 +1,53 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func TestMemoryConversion(t *testing.T) {
|
||||
memSize := MemsizeToBinaryString(2, "Gb")
|
||||
require.True(t, memSize == "2048Mi")
|
||||
|
||||
memSize = MemsizeToDecimalString(2, "Gb")
|
||||
require.True(t, memSize == "2147M")
|
||||
|
||||
memSize = MemsizeToBinaryString(2048, "Mb")
|
||||
require.True(t, memSize == "2048Mi")
|
||||
|
||||
memSize = MemsizeToDecimalString(2048, "Mb")
|
||||
require.True(t, memSize == "2147M")
|
||||
|
||||
memSize = MemsizeToBinaryString(2048*1024, "Kb")
|
||||
require.True(t, memSize == "2048Mi")
|
||||
|
||||
memSize = MemsizeToDecimalString(2048*1024, "Kb")
|
||||
require.True(t, memSize == "2147M")
|
||||
|
||||
memSize = MemsizeToBinaryString(MEMORYCUTOVER, "Gb")
|
||||
require.True(t, memSize == "100Gi")
|
||||
|
||||
memSize = MemsizeToBinaryString(MEMORYCUTOVER-1, "Gb")
|
||||
strings.HasSuffix(memSize, "Mi")
|
||||
require.True(t, strings.HasSuffix(memSize, "Mi"))
|
||||
|
||||
memSize = MemsizeToBinaryString((MEMORYCUTOVER-1)*1024, "Mb")
|
||||
require.True(t, strings.HasSuffix(memSize, "Mi"))
|
||||
|
||||
memSize = MemsizeToDecimalString(MEMORYCUTOVER*1000000000, "b")
|
||||
require.True(t, memSize == "100G")
|
||||
|
||||
memSize = MemsizeToDecimalString((MEMORYCUTOVER-1)*1000000000, "b")
|
||||
require.True(t, strings.HasSuffix(memSize, "M"))
|
||||
}
|
||||
|
||||
func TestFrequencyConversion(t *testing.T) {
|
||||
feq := CpuFrequencyToString(FREQUENCYCUTOVER, "Ghz")
|
||||
require.True(t, feq == "10G")
|
||||
feq = CpuFrequencyToString(FREQUENCYCUTOVER-1, "Ghz")
|
||||
require.True(t, feq == "9000M")
|
||||
feq = CpuFrequencyToString((FREQUENCYCUTOVER-1)*1000, "Mhz")
|
||||
require.True(t, feq == "9000M")
|
||||
}
|
||||
Reference in New Issue
Block a user