Skip to content
This repository was archived by the owner on Jan 18, 2023. It is now read-only.

Commit 2295a29

Browse files
committed
Merge ssh://gitlab.devtools.intel.com:29418/OrchSW/CNO/containers_cpu_manager_for_kubernetes
2 parents 5d9d30e + fe9ea32 commit 2295a29

File tree

16 files changed

+192
-97
lines changed

16 files changed

+192
-97
lines changed

cmk.py

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,14 @@
3030
cmk init [--num-exclusive-cores=<num>]
3131
[--num-shared-cores=<num>] [--socket-id=<num>]
3232
[--shared-mode=<mode>] [--exclusive-mode=<mode>]
33-
[--excl-non-isolcpus=<list>]
34-
cmk discover [--no-taint]
33+
[--excl-non-isolcpus=<list>] [--namespace=<name>]
34+
cmk discover [--namespace=<name>] [--no-taint]
3535
cmk describe
36-
cmk reconcile [--publish] [--interval=<seconds>]
36+
cmk reconcile [--publish] [--interval=<seconds>] [--namespace=<name>]
3737
cmk isolate [--socket-id=<num>] --pool=<pool> <command>
38-
[-- <args>...][--no-affinity]
38+
[-- <args>...][--no-affinity] [--namespace=<name>]
3939
cmk install [--install-dir=<dir>]
40-
cmk node-report [--publish] [--interval=<seconds>]
40+
cmk node-report [--publish] [--interval=<seconds>] [--namespace=<name>]
4141
cmk uninstall [--install-dir=<dir>] [--conf-dir=<dir>] [--namespace=<name>]
4242
cmk webhook [--conf-file=<file>] [--cafile=<file>] [--insecure=<bool>]
4343
cmk reconfigure [--node-name=<name>] [--num-exclusive-cores=<num>]
@@ -139,10 +139,10 @@ def main():
139139
int(args["--num-shared-cores"]),
140140
args["--exclusive-mode"],
141141
args["--shared-mode"],
142-
args["--excl-non-isolcpus"])
142+
args["--excl-non-isolcpus"], args["--namespace"])
143143
return
144144
if args["discover"]:
145-
discover.discover(args["--no-taint"])
145+
discover.discover(args["--namespace"], args["--no-taint"])
146146
return
147147
if args["describe"]:
148148
describe.describe()
@@ -152,11 +152,13 @@ def main():
152152
args["--no-affinity"],
153153
args["<command>"],
154154
args["<args>"],
155+
args["--namespace"],
155156
args["--socket-id"])
156157
return
157158
if args["reconcile"]:
158159
reconcile.reconcile(int(args["--interval"]),
159-
args["--publish"])
160+
args["--publish"],
161+
args["--namespace"])
160162
return
161163
if args["install"]:
162164
install.install(args["--install-dir"])
@@ -168,7 +170,8 @@ def main():
168170
return
169171
if args["node-report"]:
170172
nodereport.nodereport(int(args["--interval"]),
171-
args["--publish"])
173+
args["--publish"],
174+
args["--namespace"])
172175
return
173176
if args["webhook"]:
174177
webhook.webhook(args["--conf-file"], args["--cafile"],

intel/clusterinit.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -148,9 +148,9 @@ def run_cmd_pods(cmd_list, cmd_init_list, cmk_img, cmk_img_pol,
148148
for cmd in cmd_list:
149149
args = ""
150150
if cmd == "reconcile":
151-
args = "/cmk/cmk.py isolate --pool=infra /cmk/cmk.py -- reconcile --interval=5 --publish" # noqa: E501
151+
args = ("/cmk/cmk.py isolate --pool=infra --namespace={} /cmk/cmk.py -- reconcile --interval=5 --publish --namespace={}").format(namespace, namespace) # noqa: E501
152152
elif cmd == "nodereport":
153-
args = "/cmk/cmk.py isolate --pool=infra /cmk/cmk.py -- node-report --interval=5 --publish" # noqa: E501
153+
args = ("/cmk/cmk.py isolate --pool=infra --namespace={} /cmk/cmk.py -- node-report --interval=5 --publish --namespace={}").format(namespace, namespace) # noqa: E501
154154

155155
update_pod_with_container(pod, cmd, cmk_img, cmk_img_pol, args)
156156
elif cmd_init_list:
@@ -160,9 +160,10 @@ def run_cmd_pods(cmd_list, cmd_init_list, cmk_img, cmk_img_pol,
160160
if cmd == "init":
161161
args = ("/cmk/cmk.py init --num-exclusive-cores={} "
162162
"--num-shared-cores={} --shared-mode={} "
163-
"--exclusive-mode={} --excl-non-isolcpus={}")\
163+
"--exclusive-mode={} --excl-non-isolcpus={} "
164+
"--namespace={}")\
164165
.format(num_exclusive_cores, num_shared_cores, shared_mode,
165-
exclusive_mode, excl_non_isolcpus)
166+
exclusive_mode, excl_non_isolcpus, namespace)
166167
# If init is the only cmd in cmd_init_list, it should be run
167168
# as regular container as spec.containers is a required field.
168169
# Otherwise, it should be run as init-container.
@@ -174,7 +175,8 @@ def run_cmd_pods(cmd_list, cmd_init_list, cmk_img, cmk_img_pol,
174175
cmk_img_pol, args)
175176
else:
176177
if cmd == "discover":
177-
args = "/cmk/cmk.py discover"
178+
args = ("/cmk/cmk.py discover --namespace={}")\
179+
.format(namespace)
178180
if no_taint:
179181
args = " ".join([args, "--no-taint"])
180182
elif cmd == "install":

intel/config.py

Lines changed: 31 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,12 @@
1818

1919
class Config:
2020

21-
def __init__(self, cm_name, owner):
21+
def __init__(self, cm_name, owner, cm_namespace):
2222
self.c = None
2323
self.c_data = None
2424
self.cm_name = cm_name
2525
self.owner = owner
26+
self.cm_namespace = cm_namespace
2627

2728
def lock(self):
2829
# The lock function is used to avoid two isolate
@@ -40,15 +41,16 @@ def lock(self):
4041
time.sleep(random.random())
4142
while True:
4243
try:
43-
c = k8s.get_config_map(None, self.cm_name, "default")
44+
c = k8s.get_config_map(None, self.cm_name, self.cm_namespace)
4445
owner = c.metadata.annotations["Owner"]
4546
if owner != "":
4647
time.sleep(1)
4748
continue
4849
else:
4950
c.metadata.annotations["Owner"] = self.owner
5051
try:
51-
k8s.patch_config_map(None, self.cm_name, c, "default")
52+
k8s.patch_config_map(None, self.cm_name,
53+
c, self.cm_namespace)
5254
except K8sApiException as err:
5355
logging.error("Error while retreiving configmap {}"
5456
.format(self.cm_name))
@@ -73,7 +75,8 @@ def unlock(self):
7375
}
7476
clusterinit.update_configmap(configmap, self.cm_name, data)
7577
try:
76-
k8s.patch_config_map(None, self.cm_name, configmap, "default")
78+
k8s.patch_config_map(None, self.cm_name,
79+
configmap, self.cm_namespace)
7780
except K8sApiException as err:
7881
logging.error("Error while retreiving configmap {}"
7982
.format(self.cm_name))
@@ -232,7 +235,7 @@ def as_dict(self):
232235
return result
233236

234237

235-
def new(platform, excl_non_isolcpus, name):
238+
def new(platform, excl_non_isolcpus, name, namespace):
236239
# Creates the new CMK configuration for the node. It create a
237240
# configmap object and POSTs it to the K8s API Server
238241

@@ -243,20 +246,32 @@ def new(platform, excl_non_isolcpus, name):
243246
config = update_configmap_exclusive("exclusive-non-isolcpus",
244247
platform, config)
245248
config = update_configmap_shared("infra", platform, config)
246-
247-
configmap = k8sclient.V1ConfigMap()
248249
data = {
249250
"config": yaml.dump(config)
250251
}
251-
clusterinit.update_configmap(configmap, name, data)
252-
253-
try:
254-
k8s.create_config_map(None, configmap, "default")
255-
except K8sApiException as err:
256-
logging.error("Exception when creating config map {}"
257-
.format(name))
258-
logging.error(err.reason)
259-
sys.exit(1)
252+
253+
configmap = k8s.get_config_map(None, name, namespace)
254+
255+
if configmap is None:
256+
configmap = k8sclient.V1ConfigMap()
257+
clusterinit.update_configmap(configmap, name, data)
258+
259+
try:
260+
k8s.create_config_map(None, configmap, namespace)
261+
except K8sApiException as err:
262+
logging.error("Exception when creating config map {}"
263+
.format(name))
264+
logging.error(err.reason)
265+
sys.exit(1)
266+
else:
267+
clusterinit.update_configmap(configmap, name, data)
268+
try:
269+
k8s.patch_config_map(None, name, configmap, namespace)
270+
except K8sApiException as err:
271+
logging.error("Error while patching configmap {}"
272+
.format(name))
273+
logging.error(err.reason)
274+
sys.exit(1)
260275

261276

262277
def update_configmap_exclusive(pool_name, platform, config):

intel/discover.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
# discover reads the CMK configuration file, patches kubernetes nodes with
2828
# appropriate number of CMK Opaque Integer Resource (OIR) slots and applies
2929
# the appropriate CMK node labels and taints.
30-
def discover(no_taint=False):
30+
def discover(namespace, no_taint=False):
3131

3232
version = util.parse_version(k8s.get_kube_version(None))
3333
if version == util.parse_version("v1.8.0"):
@@ -38,11 +38,11 @@ def discover(no_taint=False):
3838
if version >= util.parse_version("v1.8.1"):
3939
# Patch the node with the appropriate CMK ER.
4040
logging.debug("Patching the node with the appropriate CMK ER.")
41-
add_node_er()
41+
add_node_er(namespace)
4242
else:
4343
# Patch the node with the appropriate CMK OIR.
4444
logging.debug("Patching the node with the appropriate CMK OIR.")
45-
add_node_oir()
45+
add_node_oir(namespace)
4646

4747
# Add appropriate CMK label to the node.
4848
logging.debug("Adding appropriate CMK label to the node.")
@@ -55,11 +55,11 @@ def discover(no_taint=False):
5555

5656

5757
# add_node_oir patches the node with the appropriate CMK OIR.
58-
def add_node_oir():
58+
def add_node_oir(namespace):
5959
pod_name = os.environ["HOSTNAME"]
6060
node_name = k8s.get_node_from_pod(None, pod_name)
6161
configmap_name = "cmk-config-{}".format(node_name)
62-
c = config.Config(configmap_name, pod_name)
62+
c = config.Config(configmap_name, pod_name, namespace)
6363
c.lock()
6464

6565
num_excl_non_isolcpus = None
@@ -106,11 +106,11 @@ def add_node_oir():
106106

107107

108108
# add_node_er patches the node with the appropriate CMK extended resources.
109-
def add_node_er():
109+
def add_node_er(namespace):
110110
pod_name = os.environ["HOSTNAME"]
111111
node_name = k8s.get_node_from_pod(None, pod_name)
112112
configmap_name = "cmk-config-{}".format(node_name)
113-
c = config.Config(configmap_name, pod_name)
113+
c = config.Config(configmap_name, pod_name, namespace)
114114
c.lock()
115115

116116
num_excl_non_isolcpus = None

intel/init.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020

2121
def init(num_exclusive_cores, num_shared_cores,
2222
exclusive_allocation_mode, shared_allocation_mode,
23-
excl_non_isolcpus):
23+
excl_non_isolcpus, namespace):
2424
check_hugepages()
2525

2626
logging.info("Requested exclusive cores = {}.".format(num_exclusive_cores))
@@ -38,7 +38,7 @@ def init(num_exclusive_cores, num_shared_cores,
3838
pod_name = os.environ["HOSTNAME"]
3939
node_name = k8s.get_node_from_pod(None, pod_name)
4040
configmap_name = "cmk-config-{}".format(node_name)
41-
config.new(platform, excl_non_isolcpus, configmap_name)
41+
config.new(platform, excl_non_isolcpus, configmap_name, namespace)
4242

4343

4444
def configure(num_exclusive_cores, num_shared_cores,

intel/isolate.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,15 @@
3030
ENV_NUM_CORES = "CMK_NUM_CORES"
3131

3232

33-
def isolate(pool_name, no_affinity, command, args, socket_id=None):
33+
def isolate(pool_name, no_affinity, command, args, namespace, socket_id=None):
3434
pod_name = os.environ["HOSTNAME"]
3535
if not isinstance(pod_name, str):
3636
logging.error("Pod name is not a string, exiting...")
3737
sys.exit(1)
3838
node_name = k8s.get_node_from_pod(None, pod_name)
3939
configmap_name = "cmk-config-{}".format(node_name)
4040

41-
c = config.Config(configmap_name, pod_name)
41+
c = config.Config(configmap_name, pod_name, namespace)
4242
try:
4343
c.lock()
4444
pools = c.c_data.pools

intel/nodereport.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,15 @@
2323
k8s, proc, third_party, topology, util, sst_bf as sst
2424

2525

26-
def nodereport(seconds, publish):
26+
def nodereport(seconds, publish, namespace):
2727
if seconds is None:
2828
seconds = 0
2929
else:
3030
seconds = int(seconds)
3131
should_exit = (seconds <= 0)
3232

3333
while True:
34-
report = generate_report()
34+
report = generate_report(namespace)
3535

3636
print(report.json())
3737

@@ -76,10 +76,10 @@ def nodereport(seconds, publish):
7676
time.sleep(seconds)
7777

7878

79-
def generate_report():
79+
def generate_report(namespace):
8080
report = NodeReport()
81-
check_describe(report)
82-
check_cmk_config(report)
81+
check_describe(report, namespace)
82+
check_cmk_config(report, namespace)
8383
sst_bf = False
8484
try:
8585
sst_bf = bool(discover.get_node_label(sst.NFD_LABEL))
@@ -91,26 +91,26 @@ def generate_report():
9191
return report
9292

9393

94-
def check_describe(report):
94+
def check_describe(report, namespace):
9595
try:
9696
pod_name = os.environ["HOSTNAME"]
9797
node_name = k8s.get_node_from_pod(None, pod_name)
9898
configmap_name = "cmk-config-{}".format(node_name)
99-
c = config.get_config(configmap_name)
99+
c = config.get_config(configmap_name, namespace)
100100
report.add_description(c.as_dict())
101101
except Exception:
102102
pass
103103

104104

105-
def check_cmk_config(report):
105+
def check_cmk_config(report, namespace):
106106
check_conf = report.add_check("configDirectory")
107107

108108
# Verify we can read the config directory
109109
try:
110110
pod_name = os.environ["HOSTNAME"]
111111
node_name = k8s.get_node_from_pod(None, pod_name)
112112
configmap_name = "cmk-config-{}".format(node_name)
113-
c = config.get_config(configmap_name)
113+
c = config.get_config(configmap_name, namespace)
114114
except Exception:
115115
check_conf.add_error("Unable to read CMK configmap")
116116
return # Nothing more we can check for now

intel/reconcile.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,11 @@
2121
from . import config, proc, third_party, custom_resource, k8s, util
2222

2323

24-
def reconcile(seconds, publish):
24+
def reconcile(seconds, publish, namespace):
2525
pod_name = os.environ["HOSTNAME"]
2626
node_name = k8s.get_node_from_pod(None, pod_name)
2727
configmap_name = "cmk-config-{}".format(node_name)
28-
c = config.Config(configmap_name, pod_name)
28+
c = config.Config(configmap_name, pod_name, namespace)
2929
report = None
3030

3131
if seconds is None:

intel/reconfigure.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ def reconfigure(node_name, num_exclusive_cores, num_shared_cores,
5959
node_name = k8s.get_node_from_pod(None, pod_name)
6060
config_cm = "cmk-config-{}".format(node_name)
6161

62-
conf = config.Config(config_cm, pod_name)
62+
conf = config.Config(config_cm, pod_name, namespace)
6363
num_exclusive_cores = int(num_exclusive_cores)
6464
num_shared_cores = int(num_shared_cores)
6565

intel/webhook.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,13 +154,16 @@ def mutate(admission_review, mutations_file):
154154

155155
# apply mutation to containers
156156
for i in range(len(pod['spec']['containers'])):
157+
# Need to reload mutations as they were geting changed
158+
mutations = load_mutations(mutations_file)
157159
container = pod['spec']['containers'][i]
158160

159161
pod['spec']['containers'][i] = apply_mutation(container, mutations)
160162

161163
# apply mutation to initContainers which may not exist
162164
try:
163165
for i in range(len(pod['spec']['initContainers'])):
166+
mutations = load_mutations(mutations_file)
164167
container = pod['spec']['initContainers'][i]
165168

166169
pod['spec']['initContainers'][i] = \

0 commit comments

Comments
 (0)