kubectl 补丁请求抛出 "mapping values are not allowed in this context"

kubectl patch request throwing "mapping values are not allowed in this context"

我正在尝试修补 kubernetes 部署对象的活动性和就绪性探测参数。下面是我的 patch.yml.

--- 
spec: 
  template: 
    spec: 
      containers: 
        - 
          livenessProbe: 
            initialDelaySeconds: 280
          name: notification-service
          readinessProbe: 
            initialDelaySeconds: 220

要求:

kubectl -n my-namespace --kubeconfig=my_config --context=dev patch deployment notification-service --patch "$(cat patch.yml)"

回复:

kubectl : error: unable to parse "spec:   template:     spec:       containers:       - name: notification-service                 
readinessProbe:           initialDelaySeconds: 220         livenessProbe:           initialDelaySeconds: 280": yaml: mapping values are not allowed in this 
context
At line:1 char:1
+ kubectl -n my-namespace --kubeconfig=my_config --context=dev patch  ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (error: unable t...in this context:String) [], RemoteException
    + FullyQualifiedErrorId : NativeCommandError

编辑: 下面是 kubectl version 命令的输出。

Client Version: version.Info{Major:"1", Minor:"10", GitVersion:"v1.10.0", GitCommit:"fc32d2f3698e36b93322a3465f63a14e
9f0eaead", GitTreeState:"clean", BuildDate:"2018-03-26T16:55:54Z", GoVersion:"go1.9.3", Compiler:"gc", Platform:"wind
ows/386"}
Server Version: version.Info{Major:"1", Minor:"13", GitVersion:"v1.13.4", GitCommit:"c27b913fddd1a6c480c229191a087698
aa92f0b1", GitTreeState:"clean", BuildDate:"2019-02-28T13:30:26Z", GoVersion:"go1.11.5", Compiler:"gc", Platform:"lin
ux/amd64"}

This 是引发此错误的行。为了验证它,我尝试创建一个示例 go 程序。下面是代码片段。令我惊讶的是,下面的代码能够处理 yaml 文件。

package main

import (
    "fmt"
    "io/ioutil"
    //"sigs.k8s.io/yaml" // Part of latest master k8s master vendor folder
    yaml2 "github.com/ghodss/yaml" // Part of release 1.10 k8s vendor folder
)


func check(e error) {
    if e != nil {
        panic(e)
    }
}

func main(){
    dat, err := ioutil.ReadFile("D:\EclipseIDEJavaEEDevelopers\Workspace\patch.yaml")
    check(err)
    patch:=string(dat)

    patchBytes, err1 := yaml2.YAMLToJSON([]byte(patch))

    if err != nil {
         fmt.Errorf("unable to parse %q: %v", dat, err1)
    }else{
        fmt.Println("json conversion completed ",string(patchBytes))
    }


}

输出:

json conversion completed  {"spec":{"template":{"spec":{"containers":[{"livenessProbe":{"initialDelaySeconds":280},"name":"notification-service","readinessProbe":{"initialDelaySeconds":220}}]}}}}

问题在于 powershell 和命令替换周围的双引号。如果我们在 powershell 中为命令替换添加双引号,它会从 yml 中删除新行,因此 kubectl 无法将其转换为 json.

同样适用于 bash。因为,我使用的是 powershell,所以它对我不起作用。正确的命令如下。

kubectl -n my-namespace --kubeconfig=my_config --context=dev patch deployment notification-service --patch $(cat patch.yml)

注意:yml还有一些问题。所以当我说它有效时,我的意思是当前的问题已经解决了。从 kubernetes 的角度来看,yml 有很多问题。

如果你使用 gc 定界符,你可以在 pwsh 中执行:

    kubectl patch deployment my-deployment --patch "$(gc my-patch.yaml -Delimiter ?)" -n my-namespace