Cyberithub

Solved "error loading config file: yaml : line: did not find expected key"

Advertisements

In this article, we will see how to solve "error loading config file: yaml : line : did not find expected key". Many times you might have noticed that whenever you add a new cluster information in kubeconfig file and then try to switch to that context based on the information given then you always end up with "error loading config file: yaml : line : did not find expected key" on the output. This error basically means it is not able to detect and load the given context configuration details from kubeconfig file. This error required you to mandatorily check the validity of given cluster, user, namespace and authentication information in kubeconfig file.

But the real frustration happens when you are giving all the details correctly but still showing the same error again and again. Something similar happened to me as well. So I started checking this error and after a while I understood the problem. But before solving this problem, I thought to write an article about this so that it will help you folks as well in case you are also facing similar error.

 

Solved "error loading config file: yaml : line: did not find expected key"

Solved "error loading config file: yaml : line: did not find expected key"

Also Read: Krew - Plugin Manager for Kubectl Command Line Tool

To explain the error further, after adding context and namespace details in kubeconfig file along with the certificate authentication details, I tried to switch to the added context by using kubectl config use-context pr-hcm-u01 command but then I noticed error loading config file: yaml : line: did not find expected key on the output as shown below.

cyberithub@macos4723 ~ % kubectl config use-context pr-hcm-u01
error: error loading config file "/Users/cyberithub/.kube/config": yaml: line 98: did not find expected key

While the above error could error due to multiple reasons but as I stated earlier even after giving the correct certificate authentication and cluster details as shown below, I was observing error loading config file "/Users/cyberithub/.kube/config": yaml: line 98: did not find expected key.

cyberithub@macos4723 ~ % vi ~/.kube/config
............................
- context:
    cluster: rs326
    namespace: portal-hcm-u01
    user: kube_user
  name: pr-hcm-u01
.............................

So the next thing I checked is the indentation of the details given in YAML format. Then I found out that copy pasting the details in kubeconfig file using vi editor introduced indentation error in the config file. This I have verified by testing the indentation of the file in online YAML format checker.

So, to solve this error I had to remove all the spaces and then format it again manually using my local system arrow cursor by giving proper indentation and then saved the file. After performing this step, I again tried to switch the context using same kubectl config use-context pr-hcm-u01 command and this time I noticed that I am able to switch without any error.

cyberithub@macos4723 ~ % kubectl config use-context pr-hcm-u01
Switched to context "pr-hcm-u01"

It is necessarily that you might be facing the error while trying to switch the context. The mentioned error can occur in other YAML files as well but the important point is that the cause of "error loading config file: yaml : line : did not find expected key" remains same. Most of the time I observed this error is due to incorrect indentation introduced in the YAML file. So you need to be always careful with the YAML Indentation.

Hope, above solution gave you a good understanding about the error and solution steps that you need to follow to solve this kind of error. If after following above solution, you are still getting the same error then I request you to kindly share your feedback in the comment box so that I can look into your error as well.

Leave a Comment