【k8s client-fabric8异常:Namespace mismatch Item namespace: xx1 Operation namespace: xx2】

在尝试从测试环境中删除TF-job时遇到Namespacemismatch错误,原因是项目命名空间与操作命名空间不匹配。解决方案包括在创建KubernetesClient时指定命名空间或在删除操作上使用inNamespace方法确保正确命名空间。
摘要由CSDN通过智能技术生成

测试环境 删除TF-job时报错,Namespace mismatch ,Item namespace xxxxx1,Operation namespace: xxxxxx2

代码如下:

public AIPRet<String> deleteTFJob(String projectName, String tfJobName) {
        try (final KubernetesClient client = AIClusterQuery.connectToCluster("")) {
            CustomResourceDefinitionContext customResourceDefinition =
                    new CustomResourceDefinitionContext.Builder().withGroup("kubeflow.org")
                            .withScope("Namespaced")
                            .withVersion("v1")
                            .withKind("TFJob")
                            .build();
            MixedOperation<GenericKubernetesResource, GenericKubernetesResourceList, Resource<GenericKubernetesResource>>
                    tfJob = client.genericKubernetesResources(customResourceDefinition);

            GenericKubernetesResource created = tfJob.inNamespace(projectName)
                    .withName(tfJobName)
                    .get();
            if (created != null) {
                tfJob.delete(created);
                log.info("删除TFJob:" + tfJobName + "成功");
            } else {
                log.error("没有待删除的TFJob:" + tfJobName);
            }
        } catch (Exception e) {
            log.error("Could not delete TFJob resource: {}", e.getMessage(), e);
            return AIPRet.<String>builder()
                    .success(false)
                    .message("删除TFJob失败")
                    .build();
        }
        return AIPRet.<String>builder()
                .success(true)
                .data("项目:" + projectName + " TFJob:" + tfJobName)
                .message("删除TFJob成功")
                .build();
    }

其中client获取代码 大概如下:

 config = new ConfigBuilder()
                    .withTrustCerts(true)
                    .withMasterUrl(masterUrl)
                    .withOauthToken(base64Token)
                    .build();
KubernetesClient client = new DefaultKubernetesClient(config);
return client ;

解决办法

方法一:
修改client ,增加namespace指定

 config = new ConfigBuilder()
                    .withTrustCerts(true)
                    .withMasterUrl(masterUrl)
                    .withOauthToken(base64Token)
                    .withNamespace(nameSpace)
                    .build();
KubernetesClient client = new DefaultKubernetesClient(config);
return client ;

方法二:

修改删除方法,在operation(job)上使用 inNamespace,返回NonNamespaceOperation

在这里插入图片描述

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

祺稷

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值