kubectl修补程序请求抛出“在此上下文中不允许使用映射值”

时间:2019-05-10 13:18:06

标签: kubernetes kubectl

我正在尝试修补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}}]}}}}

2 个答案:

答案 0 :(得分:1)

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

Same确实可以使用bash。由于我使用的是Powershell,因此它对我不起作用。正确的命令如下所示。

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

注意:yml仍然存在一些问题。因此,当我说它奏效时,我的意思是当前问题已解决。从kubernetes的角度来看,yml存在许多问题。

答案 1 :(得分:0)

如果使用gc分隔符,则可以在pwsh中执行此操作:

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