Robot Framework中操作K8s V1ConfigMap类对象及Patch的问题求助
问题:在Robot Framework中操作K8s V1ConfigMap对象并完成Patch操作
需求说明
- 使用Python获取指定K8s ConfigMap详情
- 在Robot Framework中修改返回的V1ConfigMap类对象
- 传入修改后的对象调用Patch ConfigMap方法
现有代码
Python库代码
from kubernetes import client, config, utils def get_config_map(self, configmap_name, namespace): try: api = client.CoreV1Api() resp = api.read_namespaced_config_map(name= configmap_name, namespace= namespace) # 返回V1ConfigMap类对象 return(resp) except Exception as e: raise def patch_config_map(self, configmap_name, namespace, body): try: api = client.CoreV1Api() resp = api.patch_namespaced_config_map(name= configmap_name, namespace= namespace, body= body) except Exception as e: raise
Robot Framework测试代码
*** Test Cases *** patch config map [Documentation] Patch the config map [tags] performance test-weekly prom ${configmap_details}= get config map configmap_name=${perf_configmap_name} namespace=${namespace} Log ${configmap_details}['data'] # 此处需要操作返回的类对象 # 执行时出现如下错误
执行错误信息
Variable '${configmap_details}' is V1ConfigMap, which is not subscriptable, and thus accessing item 'data' from it is not possible. To use '[data]' as a literal value, it needs to be escaped like '[data]'
问题核心
能否在Robot Framework中操作V1ConfigMap类对象?如何正确完成修改与Patch操作?
解决方案
1. 可行性结论
Robot Framework完全可以操作V1ConfigMap类对象,但不能用字典下标[]访问类属性,需改用类属性的点.语法。
2. 正确操作步骤
方式一:直接在Robot Framework中修改属性
V1ConfigMap的data属性本身是字典类型,先通过点语法拿到该属性,再用下标修改内部键值:
*** Test Cases *** patch config map [Documentation] Patch the config map [tags] performance test-weekly prom ${configmap_details}= get config map configmap_name=${perf_configmap_name} namespace=${namespace} # 用点语法访问类的data属性,而非下标 Log ${configmap_details.data} # 修改data中的指定键值 ${configmap_details.data}[target_key] = Set Variable new_target_value # 传入修改后的对象执行Patch patch config map configmap_name=${perf_configmap_name} namespace=${namespace} body=${configmap_details}
方式二:封装Python辅助方法(更稳定)
如果担心Robot Framework操作类对象的兼容性,可在Python库中新增修改方法,直接在Python层面处理:
def modify_config_map_data(self, configmap_obj, key, new_value): try: # 处理data为空的情况 if not configmap_obj.data: configmap_obj.data = {} configmap_obj.data[key] = new_value return configmap_obj except Exception as e: raise
然后在Robot Framework中调用:
*** Test Cases *** patch config map [Documentation] Patch the config map [tags] performance test-weekly prom ${configmap_details}= get config map configmap_name=${perf_configmap_name} namespace=${namespace} # 调用Python辅助方法修改ConfigMap ${modified_configmap}= modify config map data configmap_obj=${configmap_details} key=target_key new_value=new_target_value # 执行Patch操作 patch config map configmap_name=${perf_configmap_name} namespace=${namespace} body=${modified_configmap}
3. 轻量化Patch技巧
如果仅需修改部分字段,无需传递完整V1ConfigMap对象,直接构造包含修改内容的字典传入即可:
*** Test Cases *** patch config map [Documentation] Patch the config map [tags] performance test-weekly prom # 构造仅含修改内容的字典 ${patch_body}= Create Dictionary data=${{"target_key": "new_target_value"}} # 传入字典执行Patch patch config map configmap_name=${perf_configmap_name} namespace=${namespace} body=${patch_body}
内容的提问来源于stack exchange,提问作者preeya krish
相关产品推荐
相关产品推荐

