Search code examples
azure-devopsazure-pipelineskubectl

Azure DevOps release pipeline fails on kubectl command


I'm trying to build an ADO release pipeline to delete running pods from our on-prem Kubernetes cluster. I already have a self-hosted agent with kubectl installed and and am able to run the build pipeline on it (which builds the Docker image and pushes it to our on-prem Harbor repo). And now I'm trying to create the release pipeline to delete running pods so they are recreated with the latest image from Harbor. I've also created an ADO service connection for the Kubernetes cluster with the correct kubeconfig info.

I'm using the Kubectl task and am just trying to show the running pods (to test getting this working). However, when the pipeline runs this task, it seems to say that "get pods" is not a valid kubectl command:

2020-05-15T19:23:33.1905346Z [command]/usr/bin/kubectl --kubeconfig /myagent/_work/_temp/kubectlTask/1589570612549/config get pods -n rpaapis
2020-05-15T19:23:33.1906131Z Error: unknown command "get pods" for "kubectl"
2020-05-15T19:23:33.1907408Z Run 'kubectl --help' for usage.
2020-05-15T19:23:33.1944451Z ##[error]Error: unknown command "get pods" for "kubectl"
2020-05-15T19:23:33.1956294Z ##[error]Run 'kubectl --help' for usage.
2020-05-15T19:23:33.1957880Z ##[error]/usr/bin/kubectl failed with return code: 1

The YAML produced by the task seems fairly straightforward:

steps:
- task: Kubernetes@0
  displayName: 'kubectl get pods'
  inputs:
    kubernetesServiceConnection: 'On-prem K8s - staging'
    namespace: rpaapis
    command: 'get pods'
    versionOrLocation: location
    specifyLocation: /usr/bin/kubectl

I can log onto the self-hosted agent system and run the kubectl command with the same kubeconfig data as in the service connection:

/usr/bin/kubectl --kubeconfig /tmp/config get pods -n rpaapis
NAME                       READY   STATUS    RESTARTS   AGE
rpaapis-65c96b8dcb-khzzb   2/2     Running   0          30h
rpaapis-65c96b8dcb-lhzzj   2/2     Running   0          30h

So it seems that it's executing the correct kubectl executable, so not sure why the odd error. In fact, the only successful command I can seem to make the task run is "kubectl --help". Any thoughts would be appreciated!


Solution

  • Try specifying both the command and arguments:

    - task: Kubernetes@1
      displayName: 'kubectl get pods'
      inputs:
        kubernetesServiceConnection: 'On-prem K8s - staging'
        namespace: rpaapis
        command: 'get'
        arguments: 'pods'
        versionOrLocation: location
        specifyLocation: /usr/bin/kubectl
    

    I bumped into this too! Not immediately obvious from the Kubernetes@1 task documentation, feels like there's a lack of examples there.