Add "app.starlingx.io/component=platform" label

Set the default label value for app.starlingx.io/component
label to 'platform'

Test Plan:
PASSED: build-pkgs
PASSED: Upload using 'system application-upload
        <intel-device-plugins-operator-version.tgz>'
PASSED: Check the status of charts using below command, it should
        be disabled.
        "system helm-override-list app_name --long"
PASSED: Enable the charts using below command:
        "system helm-chart-attribute-modify --enabled true app_name
        chart_name namespace"
PASSED: Apply the app using "system application-apply" command
PASSED: Testing on QAT supported hardware:
        After apply, QAT pod should be running. Verify using below
        command.
        "kubectl get pods -A | grep qat"
PASSED: Check all pods are running in platform label using command
        "sudo kube-cpusets | grep intel-device-plugin"

        Note: List the core numbers using command
        "system host-cpu-list 1"

PASSED: Change label to application using command "system
        helm-override-update --values values.yaml
        intel-device-plugins-operator intel-device-plugins-operator
        intel-device-plugins-operator"
        and check the pods are running in application core using
        "sudo kube-cpusets | grep intel-device-plugin"

        Note: cat values.yaml
        app.starlingx.io/component: application

PASSED: Change label to invalid label then it raises error:
        ValueError: Value invalid for label:namespace
        app.starlingx.io/component:invalid is not supported

        Note: cat values.yaml
        app.starlingx.io/component: invalid

Story: 2010604
Task: 51027

Change-Id: I0498efc77457a03ab56695227a67488072bef4c7
Signed-off-by: Md Irshad Sheikh <mdirshad.sheikh@windriver.com>
This commit is contained in:
Md Irshad Sheikh 2024-09-17 08:15:21 -04:00
parent 5a14570691
commit 9420bf9662

View File

@ -45,7 +45,7 @@ class IntelDevicePluginsAppLifecycleOperator(base.AppLifecycleOperator):
lifecycle_utils.create_local_registry_secrets(app_op, app, hook_info)
lifecycle_utils.add_pod_security_admission_controller_labels(
app_op, app, hook_info)
return self.add_component_label_in_pods(app_op, app)
return self._update_component_label(app_op, app)
elif hook_info.operation == constants.APP_REMOVE_OP and \
hook_info.relative_timing == constants.APP_LIFECYCLE_TIMING_POST:
return lifecycle_utils.delete_local_registry_secrets(app_op, app, hook_info)
@ -119,43 +119,49 @@ class IntelDevicePluginsAppLifecycleOperator(base.AppLifecycleOperator):
return val
def add_component_label_in_pods(self, app_op, app):
def _update_component_label(self, app_op, app):
"""
Adding component label in pods.
Adding component label in namespace.
:param app_op: AppOperator object
:param app: AppOperator.Application object
"""
dbapi_instance = app_op._dbapi
db_app_id = dbapi_instance.kube_app_get(app.name).id
component_constant = app_constants.HELM_COMPONENT_LABEL_INTEL_DEVICE_PLUGINS_OPERATOR
# List all charts enabled
charts = self._get_charts_enabled(dbapi_instance, db_app_id)
user_overrides = self._get_user_overrides(app._kube_app, app_op._dbapi)
for chart in charts:
# Loading user-overrides
user_overrides = chart['user_overrides']
# If user-overrides exists, checking if label was set by user.
if user_overrides and component_constant in user_overrides:
dict_chart_overrides = yaml.safe_load(user_overrides)
label = dict_chart_overrides[component_constant]
# Checking if it's a supported label. If not, set platform as default label.
if label not in app_constants.HELM_COMPONENT_SUPPORTED_LABELS:
dict_chart_overrides[component_constant] = 'platform'
LOG.warn(f'User override for core affinity {label} is not supported,' +
'using platform as default label.')
component_label = user_overrides.get(component_constant, 'platform')
chart['user_overrides'] = yaml.safe_dump(dict_chart_overrides)
if component_label in ['application', 'platform']:
# Get namespace attributes
k8s_client_core = app_op._kube._get_kubernetesclient_core()
namespace = k8s_client_core.read_namespace(
app_constants.HELM_NS_INTEL_DEVICE_PLUGINS_OPERATOR)
dbapi_instance.helm_override_update(
db_app_id,
chart['name'],
app_constants.HELM_NS_INTEL_DEVICE_PLUGINS_OPERATOR,
chart)
# Previous value
previous_component_label = namespace.metadata.labels.get(
component_constant)
# Set label in namespace
namespace.metadata.labels.update(
{
component_constant:
component_label
})
app_op._kube.kube_patch_namespace(
app_constants.HELM_NS_INTEL_DEVICE_PLUGINS_OPERATOR,
namespace)
# Restarts all pods in namespace if label has changed
if (previous_component_label is None or
previous_component_label != component_label):
self._delete_pods(app_op, k8s_client_core)
else:
raise ValueError(f"Value {component_label} for label:namespace "
f"{component_constant}: {component_label}"
" is not supported")
def _get_charts_enabled(self, dbapi_instance, db_app_id):
# Listing all helm charts from db_app_id
@ -169,3 +175,45 @@ class IntelDevicePluginsAppLifecycleOperator(base.AppLifecycleOperator):
"system_overrides": i.system_overrides
} for i in overrides if i.system_overrides['enabled'] is True]
return charts
def _get_user_overrides(self, kube_app, dbapi):
"""Get user overrides from db
:param kube_app: Kubernetes Application instance
:param dbapi: dbapi
:return User overrides in dict format
"""
user_overrides = {}
overrides = dbapi.helm_override_get(
kube_app.id,
app_constants.HELM_CHART_INTEL_DEVICE_PLUGINS_OPERATOR,
app_constants.HELM_NS_INTEL_DEVICE_PLUGINS_OPERATOR)
if overrides.user_overrides:
user_overrides = yaml.safe_load(overrides.user_overrides)
return user_overrides
def _delete_pods(self, app_op, k8s_client_core):
"""Delete all pods within the application namespace
:param app_op: AppOperator object
:param k8s_client_core: Kubernetes client object
"""
try:
# pod list
pods = k8s_client_core.list_namespaced_pod(
app_constants.HELM_NS_INTEL_DEVICE_PLUGINS_OPERATOR)
# On namespace label change, delete pods to force restart
for pod in pods.items:
app_op._kube.kube_delete_pod(
name=pod.metadata.name,
namespace=app_constants.HELM_NS_INTEL_DEVICE_PLUGINS_OPERATOR,
grace_periods_seconds=0
)
except Exception:
LOG.error("Failed to delete pods in namespace %s",
app_constants.HELM_NS_INTEL_DEVICE_PLUGINS_OPERATOR)