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

5/10/2019

I am trying to patch liveness and readiness probes parameters of a kubernetes deployment object. below is my patch.yml.

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

Request:

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

Response:

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

Edit: Below is the output of kubectl version command.

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 is the line from where this error is being thrown. To verify it, i tried creating a sample go program. Below is the code snippet. To my surprise, below code is able to process the yaml file.

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))
    }


}

Output:

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

2 Answers

3/30/2020

You can execute this in pwsh if you use the gc delimiter:

    kubectl patch deployment my-deployment --patch "$(gc my-patch.yaml -Delimiter ?)" -n my-namespace
-- Christian Mikkelsen
Source: StackOverflow

5/14/2019

The issue is with the powershell and double quotes around command substitution. If we add double quotes around command substitution in powershell, it removes new line from the yml due to which, kubectl is not able to convert it into json.

Same does work in bash. Since, I was using powershell, it did not work for me. Correct command that worked is as below.

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

Note: The yml has still some issues. So when I say it worked, I meant the current issue is resolved. The yml has many issues from kubernetes perspective.

-- Manish Bansal
Source: StackOverflow