Skip to content

Commit b3e384f

Browse files
Add cluster label management feat (#111)
* add label management * Fix CI issues * fix lint * fix Ci;s --------- Co-authored-by: Rishi Mondal <[email protected]>
1 parent 097046c commit b3e384f

File tree

3 files changed

+104
-197
lines changed

3 files changed

+104
-197
lines changed

src/shared/functions/__init__.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@
22

33
from src.shared.base_functions import function_registry
44
from src.shared.functions.binding_policy_management import BindingPolicyManagement
5+
from src.shared.functions.check_cluster_upgrades import CheckClusterUpgradesFunction
6+
from src.shared.functions.cluster_label_management import ClusterLabelManagement
57
from src.shared.functions.deploy_to import DeployToFunction
68
from src.shared.functions.describe_resource import DescribeResourceFunction
79
from src.shared.functions.edit_resource import EditResourceFunction
8-
from src.shared.functions.get_cluster_labels import GetClusterLabelsFunction
910
from src.shared.functions.gvrc_discovery import GVRCDiscoveryFunction
1011
from src.shared.functions.helm.list import HelmListFunction
1112
from src.shared.functions.helm.repo import HelmRepoFunction
@@ -15,7 +16,6 @@
1516
from src.shared.functions.multicluster_create import MultiClusterCreateFunction
1617
from src.shared.functions.multicluster_logs import MultiClusterLogsFunction
1718
from src.shared.functions.namespace_utils import NamespaceUtilsFunction
18-
from src.shared.functions.check_cluster_upgrades import CheckClusterUpgradesFunction
1919

2020

2121
def initialize_functions():
@@ -44,7 +44,7 @@ def initialize_functions():
4444
function_registry.register(HelmDeployFunction())
4545

4646
# Register cluster labels helper function
47-
function_registry.register(GetClusterLabelsFunction())
47+
function_registry.register(ClusterLabelManagement())
4848

4949
# Register GVRC and namespace utilities
5050
function_registry.register(GVRCDiscoveryFunction())
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
from __future__ import annotations
2+
3+
import asyncio
4+
from typing import Any, Dict, List
5+
6+
from src.shared.base_functions import BaseFunction
7+
8+
9+
class ClusterLabelManagement(BaseFunction):
10+
"""
11+
Add / update labels on an Open-Cluster-Management ManagedCluster.
12+
13+
Typical call:
14+
• cluster_name – managedcluster resource name
15+
• labels – dict of key → value
16+
• kube_context – context of the OCM Hub / ITS (default: its1)
17+
"""
18+
19+
def __init__(self) -> None:
20+
super().__init__(
21+
name="cluster_label_management",
22+
description="Add or update labels on a ManagedCluster object."
23+
)
24+
25+
# ────────────────────────── public entry ──────────────────────────
26+
async def execute(
27+
self,
28+
cluster_name: str,
29+
labels: Dict[str, str] | None = None,
30+
remove_labels: List[str] | None = None,
31+
kube_context: str = "its1", # default is ITS / OCM hub
32+
kubeconfig: str = "",
33+
**_: Any,
34+
) -> Dict[str, Any]:
35+
if not cluster_name:
36+
return {"status": "error", "error": "cluster_name is required"}
37+
if not labels and not remove_labels:
38+
return {"status": "error", "error": "labels or remove_labels must be provided"}
39+
40+
label_args: List[str] = []
41+
if labels:
42+
label_args += [f"{k}={v}" for k, v in labels.items()]
43+
if remove_labels:
44+
label_args += [f"{key}-" for key in remove_labels]
45+
cmd = [
46+
"kubectl", "--context", kube_context,
47+
"label", "managedcluster", cluster_name,
48+
*label_args, "--overwrite",
49+
]
50+
if kubeconfig:
51+
cmd += ["--kubeconfig", kubeconfig]
52+
53+
proc = await asyncio.create_subprocess_exec(
54+
*cmd, stdout=asyncio.subprocess.PIPE, stderr=asyncio.subprocess.PIPE
55+
)
56+
stdout, stderr = await proc.communicate()
57+
58+
if proc.returncode != 0:
59+
# Surface full stderr so the LLM can show the exact cause
60+
return {
61+
"status": "error",
62+
"stderr": stderr.decode().strip(),
63+
"cmd": " ".join(cmd),
64+
}
65+
66+
return {
67+
"status": "success",
68+
"stdout": stdout.decode().strip(),
69+
"cmd": " ".join(cmd),
70+
}
71+
72+
# ────────────────────────── JSON schema ──────────────────────────
73+
def get_schema(self) -> Dict[str, Any]:
74+
return {
75+
"type": "object",
76+
"properties": {
77+
"cluster_name": {
78+
"type": "string",
79+
"description": "Name of the ManagedCluster resource",
80+
},
81+
"labels": {
82+
"type": "object",
83+
"description": "Dictionary of label key/value pairs to add/update",
84+
},
85+
"remove_labels": {
86+
"type": "array",
87+
"items": {"type": "string"},
88+
"description": "List of label keys to delete",
89+
},
90+
"kube_context": {
91+
"type": "string",
92+
"description": "kubectl context of the OCM hub",
93+
"default": "its1",
94+
},
95+
"kubeconfig": {
96+
"type": "string",
97+
"description": "Path to alternate kubeconfig (optional)",
98+
},
99+
},
100+
"required": ["cluster_name"],
101+
}

src/shared/functions/get_cluster_labels.py

Lines changed: 0 additions & 194 deletions
This file was deleted.

0 commit comments

Comments
 (0)