当前位置: 首页 > 知识库问答 >
问题:

Fabric8io K8s java客户端是否支持使用YAML片段的patch()或rollingupdate()?

毛德华
2023-03-14

我正在尝试通过将部署片段作为输入来编程k8s应用程序的补丁/滚动升级。我使用patch()方法将片段应用到现有部署上,作为使用Fabric8IO的k8s客户端API的rollingupdate的一部分。fabric8.ioKubernetes-Client版本4.10.1我还使用了Kubernetes-API 3.0.12.中的一些loadYaml帮助器方法

下面是我的示例代码段-adminpatch.yaml文件:

    kind: Deployment   
    spec:
      strategy:
        type: RollingUpdate
        rollingUpdate:
          maxSurge: 1
          maxUnavailable: 0     
      template:
        spec:
          containers:
            - name: ${PATCH_IMAGE_NAME}
              image: ${PATCH_IMAGE_URL}
              imagePullPolicy: Always

我正在将上面的文件内容(替换了所有的占位符)作为字符串发送到patchDeployment()方法。下面是我对fabric8补丁()方法的调用:

     public static String patchDeployment(String deploymentName, String namespace, String deploymentYaml) {
    try {
    Deployment deploymentSnippet = (Deployment) getK8sObject(deploymentYaml);
    if(deploymentSnippet instanceof Deployment) {
            logger.debug("Valid deployment object.");
    Deployment deployment = getK8sClient().apps().deployments().inNamespace(namespace).withName(deploymentName)
        .rolling().patch(deploymentSnippet);
    System.out.println(deployment.toString());
    return getLastConfig(deployment.getMetadata(), deployment);
    }
    } catch (Exception Ex) {
      Ex.printStackTrace();
    }
      return "Failed";
  }

它抛出以下异常

> io.fabric8.kubernetes.client.KubernetesClientException: Failure
> executing: PATCH at:
> https://10.44.4.126:6443/apis/apps/v1/namespaces/default/deployments/patch-demo.
> Message: Deployment.apps "patch-demo" is invalid: spec.selector:
> Invalid value:
> v1.LabelSelector{MatchLabels:map[string]string{"app":"nginx",
> "deployment":"3470574ffdbd6e88d426a77dd951ed45"},
> MatchExpressions:[]v1.LabelSelectorRequirement(nil)}: field is
> immutable. Received status: Status(apiVersion=v1, code=422,
> details=StatusDetails(causes=[StatusCause(field=spec.selector,
> message=Invalid value:
> v1.LabelSelector{MatchLabels:map[string]string{"app":"nginx",
> "deployment":"3470574ffdbd6e88d426a77dd951ed45"},
> MatchExpressions:[]v1.LabelSelectorRequirement(nil)}: field is
> immutable, reason=FieldValueInvalid, additionalProperties={})],
> group=apps, kind=Deployment, name=patch-demo, retryAfterSeconds=null,
> uid=null, additionalProperties={}), kind=Status,
> message=Deployment.apps "patch-demo" is invalid: spec.selector:
> Invalid value:
> v1.LabelSelector{MatchLabels:map[string]string{"app":"nginx",
> "deployment":"3470574ffdbd6e88d426a77dd951ed45"},
> MatchExpressions:[]v1.LabelSelectorRequirement(nil)}: field is
> immutable, metadata=ListMeta(_continue=null, remainingItemCount=null,
> resourceVersion=null, selfLink=null, additionalProperties={}),
> reason=Invalid, status=Failure, additionalProperties={}).

我还使用kubectl patch deployment_name>-n --patch“$(cat adminpatch.yaml) 尝试了原始代码段(带有标签和选择器),这可以很好地应用相同的代码段。

我无法获得关于fabric8io k8s客户端补丁()java API的大量文档。任何帮助都将不胜感激。

共有1个答案

荣德厚
2023-03-14

以下是Fabric8io滚动API中的相关bug:https://github.com/Fabric8io/kubernetes-client/issues/1868

到目前为止,我发现的一种使用fabri8io API进行修补的方法是:

  1. 获取正在运行的部署对象
  2. 用新容器添加/替换其中的容器
  3. 使用createorreplace()API重新部署部署对象

但可以理解的是,您的补丁可能不仅仅是对containers字段的更新。在这种情况下,处理每个可编辑字段变得很混乱。

我继续使用正式K8s客户机的patchNamespacedDeployment()API来实现修补。https://github.com/kubernetes-client/java/blob/356109457499862a581a951a710cd808d0b9c622/examples/src/main/java/io/kubernetes/client/examples/patchexample.java

 类似资料:
  • 我正在编写一个Spring webservice客户机,它调用一个SOAP服务,它返回一个带有附件的SOAP响应(MTOM->XOP include tag in response)。 在我当前的客户机代码中,我使用了SaajSoapMessageFactory并在WebServiceTemplate中注入了相同的内容,我还在封送器中将MtomEnabled设置为true。 客户端的SaajSoa

  • 问题内容: 当我发送此ajax rquest时: 我收到此错误: XMLHttpRequest无法加载http://:// localhost:8080 / wutup / venues / 12。Access- Control-Allow-Methods不允许使用方法PATCH。 但是,使用curl: 问题答案: 该方法 确实 支持HTTP PATCH。 您看到的问题是该方法在选项预检检查的响应

  • 现在我使用的是瘦客户机的ignite ClientCache,我没有找到ClientCache的分布式锁,如果要使用分布式锁,必须使用ignition.start()

  • 第一个twisted支持的诗歌服务器 尽管Twisted大多数情况下用来写服务器代码,但为了一开始尽量从简单处着手,我们首先从简单的客户端讲起。 让我们来试试使用Twisted的客户端。源码在twisted-client-1/get-poetry.py。首先像前面一样要开启三个服务器: python blocking-server/slowpoetry.py --port 10000 poetry

  • 问题内容: 根据这些评论,JSONKit不支持ARC,甚至在ARC环境中都不使用fobjc-no- arc设置运行:https : //github.com/johnezang/JSONKit/issues/37 问题答案: 您仍然可以在ARC应用程序中使用JSONKit。 我自己用的。 在XCode 5中选择项目根目录,在“ 选择应用程序”下,然后选择“ 选项卡”。在JSONKit.m 下,双击

  • Now that you are the proud owner of a Packt book, we have a number of things to help you to get the most from your purchase.