forked from kubernetes/kubernetes
-
Notifications
You must be signed in to change notification settings - Fork 102
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
UPSTREAM: <carry>: advertise shared cpus for mixed cpus feature
Kubelet should advertise the shared cpus as extedned resources. This has the benefit of limiting the amount of containers that can request an access to the shared cpus. For more information see - openshift/enhancements#1396 Signed-off-by: Talor Itzhak <titzhak@redhat.com>
- Loading branch information
1 parent
7b9421a
commit d204876
Showing
4 changed files
with
152 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
/* | ||
Copyright 2023 The Kubernetes Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package sharedcpus | ||
|
||
import ( | ||
"encoding/json" | ||
"errors" | ||
"os" | ||
|
||
corev1 "k8s.io/api/core/v1" | ||
"k8s.io/klog/v2" | ||
) | ||
|
||
const ( | ||
configFileName = "/etc/kubernetes/openshift-workload-mixed-cpus" | ||
sharedCpusResourceName = "workload.openshift.io/enable-shared-cpus" | ||
) | ||
|
||
var ( | ||
config Config | ||
sharedCpusEnabled bool | ||
) | ||
|
||
type Config struct { | ||
sharedCpus `json:"shared_cpus"` | ||
} | ||
|
||
type sharedCpus struct { | ||
// ContainersLimit specify the number of containers that are allowed to access the shared CPU pool` | ||
ContainersLimit int64 `json:"containers_limit"` | ||
} | ||
|
||
func init() { | ||
parseConfig() | ||
} | ||
|
||
func IsEnabled() bool { | ||
return sharedCpusEnabled | ||
} | ||
|
||
func GetResourceName() corev1.ResourceName { | ||
return sharedCpusResourceName | ||
} | ||
|
||
func GetConfig() Config { | ||
return config | ||
} | ||
|
||
func parseConfig() { | ||
b, err := os.ReadFile(configFileName) | ||
if err != nil { | ||
if errors.Is(err, os.ErrNotExist) { | ||
return | ||
} | ||
klog.ErrorS(err, "Failed to read configuration file for shared cpus", "fileName", configFileName) | ||
return | ||
} | ||
cfg, err := parseConfigData(b) | ||
if err != nil { | ||
return | ||
} | ||
config = *cfg | ||
sharedCpusEnabled = true | ||
} | ||
|
||
func parseConfigData(data []byte) (*Config, error) { | ||
cfg := &Config{} | ||
err := json.Unmarshal(data, cfg) | ||
if err != nil { | ||
klog.ErrorS(err, "Failed to parse configuration file for shared cpus", "fileContent", string(data)) | ||
} | ||
return cfg, err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package sharedcpus | ||
|
||
import "testing" | ||
|
||
func TestParseConfigData(t *testing.T) { | ||
testCases := []struct { | ||
data []byte | ||
expectedToBeParsed bool | ||
containerLimitValue int64 | ||
}{ | ||
{ | ||
data: []byte(`{ | ||
"shared_cpus": { | ||
"containers_limit": 15 | ||
} | ||
}`), | ||
expectedToBeParsed: true, | ||
containerLimitValue: 15, | ||
}, | ||
{ | ||
data: []byte(`{ | ||
"shared_cpus": { | ||
"abc": "25" | ||
} | ||
}`), | ||
expectedToBeParsed: false, | ||
containerLimitValue: 0, | ||
}, | ||
} | ||
for _, tc := range testCases { | ||
cfg, err := parseConfigData(tc.data) | ||
if err != nil && tc.expectedToBeParsed { | ||
t.Errorf("shared cpus data expected to be parsed") | ||
} | ||
if cfg.ContainersLimit != tc.containerLimitValue { | ||
t.Errorf("shared cpus ContainersLimit is different than expected: want: %d; got %d", tc.containerLimitValue, cfg.ContainersLimit) | ||
} | ||
} | ||
} |