diff --git a/cmd/cpu-device-plugin/cpu-device-plugin.go b/cmd/cpu-device-plugin/cpu-device-plugin.go index d0d1209..3aeb493 100644 --- a/cmd/cpu-device-plugin/cpu-device-plugin.go +++ b/cmd/cpu-device-plugin/cpu-device-plugin.go @@ -175,28 +175,11 @@ func newCPUDeviceManager(poolName string, pool types.Pool, sharedCPUs string) *c } } -func createPluginsForPools() error { - files, err := filepath.Glob(filepath.Join(pluginapi.DevicePluginPath, "cpudp*")) - if err != nil { - glog.Fatal(err) - } - for _, f := range files { - if err := os.Remove(f); err != nil { - glog.Fatal(err) - } - } - poolConf, _, err := types.DeterminePoolConfig() - if err != nil { - glog.Fatal(err) - } - glog.Infof("Pool configuration %v", poolConf) +func validatePools(poolConf types.PoolConfig) (string, error) { var sharedCPUs string + var err error for poolName, pool := range poolConf.Pools { poolType := types.DeterminePoolType(poolName) - //Deault or unrecognizable pools need not be made available to Device Manager as schedulable devices - if poolType == types.DefaultPoolID { - continue - } if poolType == types.SharedPoolID { if sharedCPUs != "" { err = fmt.Errorf("Only one shared pool allowed") @@ -205,7 +188,20 @@ func createPluginsForPools() error { } sharedCPUs = pool.CPUs.String() } + } + return sharedCPUs, err +} + +func createCDMs(poolConf types.PoolConfig, sharedCPUs string) error { + var err error + for poolName, pool := range poolConf.Pools { + poolType := types.DeterminePoolType(poolName) + //Deault or unrecognizable pools need not be made available to Device Manager as schedulable devices + if poolType == types.DefaultPoolID { + continue + } cdm := newCPUDeviceManager(poolName, pool, sharedCPUs) + cdms = append(cdms, cdm) if err := cdm.Start(); err != nil { glog.Errorf("cpuDeviceManager.Start() failed: %v", err) break @@ -218,10 +214,34 @@ func createPluginsForPools() error { glog.Error(err) break } - cdms = append(cdms, cdm) glog.Infof("CPU device plugin registered with the Kubelet") } + return err +} + +func createPluginsForPools() error { + files, err := filepath.Glob(filepath.Join(pluginapi.DevicePluginPath, "cpudp*")) + if err != nil { + glog.Fatal(err) + } + for _, f := range files { + if err := os.Remove(f); err != nil { + glog.Fatal(err) + } + } + poolConf, _, err := types.DeterminePoolConfig() if err != nil { + glog.Fatal(err) + } + glog.Infof("Pool configuration %v", poolConf) + + var sharedCPUs string + sharedCPUs, err = validatePools(poolConf) + if err != nil { + return err + } + + if err := createCDMs(poolConf, sharedCPUs); err != nil { for _, cdm := range cdms { cdm.Stop() } diff --git a/cmd/webhook/webhook.go b/cmd/webhook/webhook.go index 18cef47..d3a7506 100644 --- a/cmd/webhook/webhook.go +++ b/cmd/webhook/webhook.go @@ -123,20 +123,20 @@ func validateAnnotation(poolRequests poolRequestMap, cpuAnnotation types.CPUAnno func patchCPULimit(sharedCPUTime int, patchList []patch, i int, c *corev1.Container) []patch { var patchItem patch - patchItem.Op = "add" + patchItem.Op = "replace" cpuVal := `"` + strconv.Itoa(sharedCPUTime) + `m"` - if len(c.Resources.Limits) > 0 { - patchItem.Path = "/spec/containers/" + strconv.Itoa(i) + "/resources/limits/cpu" - patchItem.Value = - json.RawMessage(cpuVal) - } else { - patchItem.Path = "/spec/containers/" + strconv.Itoa(i) + "/resources" - patchItem.Value = json.RawMessage(`{ "limits": { "cpu":` + cpuVal + ` } }`) - } + patchItem.Path = "/spec/containers/" + strconv.Itoa(i) + "/resources/limits/cpu" + patchItem.Value = json.RawMessage(cpuVal) patchList = append(patchList, patchItem) - return patchList + patchItem.Op = "replace" + cpuVal = `"0m"` + patchItem.Path = "/spec/containers/" + strconv.Itoa(i) + "/resources/requests/cpu" + patchItem.Value = json.RawMessage(cpuVal) + patchList = append(patchList, patchItem) + + return patchList } func patchContainerForPinning(cpuAnnotation types.CPUAnnotation, patchList []patch, i int, c *corev1.Container) ([]patch, error) { diff --git a/pkg/sethandler/controller.go b/pkg/sethandler/controller.go index c844aa8..40335c5 100644 --- a/pkg/sethandler/controller.go +++ b/pkg/sethandler/controller.go @@ -215,6 +215,13 @@ func (setHandler *SetHandler) applyCpusetToContainer(containerID string, cpuset return errors.New("cpuset file does not exist for container:" + trimmedCid + " under the provided cgroupfs hierarchy:" + setHandler.cpusetRoot) } //And for our grand finale, we just "echo" the calculated cpuset to the cpuset cgroupfs "file" of the given container + //Find child cpuset if it exists (kube-proxy) + filepath.Walk(pathToContainerCpusetFile, func(path string, f os.FileInfo, err error) error { + if f.IsDir() { + pathToContainerCpusetFile = path + } + return nil + }) file, err := os.OpenFile(pathToContainerCpusetFile + "/cpuset.cpus", os.O_WRONLY|os.O_SYNC, 0755) if err != nil { return errors.New("Can't open cpuset file:" + pathToContainerCpusetFile + " for container:" + trimmedCid + " because:" + err.Error())