Home > Enterprise >  How to identify/Fail the Azure Devops pipeline with kubernetes
How to identify/Fail the Azure Devops pipeline with kubernetes

Time:11-30

I am using Azure devops to deploy services to AKS. There are few instances that even though the pods weren't started(in crashed state) the pipeline still shows it as success.

I add this task to validate the kubectl pod:

- task: Kubernetes@1
            displayName: 'Deploy ${{ parameters.project_display_name }} to ${{ parameters.cluster }}'
            inputs:
              connectionType: Kubernetes Service Connection
              kubernetesServiceEndpoint: ${{ parameters.cluster }}-${{ parameters.namespace }}
              command: apply
              arguments: -f $(System.ArtifactsDirectory)/k8_manifest/manifest.yml -n ${{ parameters.namespace }}
              outputFormat: 'yaml'

          - task: AzureCLI@2
            displayName: 'Validate Deployment of ${{ parameters.project_display_name }} to ${{ parameters.cluster }}'
            inputs:
              scriptType: 'pscore'
              scriptLocation: 'inlineScript'
              inlineScript: |
                containerStatuses=$(kubectl get deployment loss-limit-api --namespace betting -o=jsonpath='{$.status.conditions}')
                for row in $(echo "${containerStatuses}" | jq -r '.[] | @base64'); do
                    _jq() {
                    echo ${row} | base64 --decode | jq -r ${1}
                    }
                    if [ $(_jq '.status') != "True" ]; then
                        echo "Inactive Pod erron on the deployment"
                        exit 1
                    fi
                done

it returns and error:

Starting: Validate Deployment of Loss Limit API to core-dev00
==============================================================================
Task         : Azure CLI
Description  : Run Azure CLI commands against an Azure subscription in a PowerShell Core/Shell script when running on Linux agent or PowerShell/PowerShell Core/Batch script when running on Windows agent.
Version      : 2.208.0
Author       : Microsoft Corporation
Help         : https://docs.microsoft.com/azure/devops/pipelines/tasks/deploy/azure-cli
==============================================================================
##[error]Script failed with error: Error: Unable to locate executable file: 'pwsh'. Please verify either the file path exists or the file can be found within a directory specified by the PATH environment variable. Also check the file mode to verify the file is executable.
Finishing: Validate Deployment of Loss Limit API to core-dev00

CodePudding user response:

The error message shows that your agent doesn't have PowerShell Core installed. To resolve this:

Method 1: In Azure CLI task, use batch as scriptType instead of pscore.

Method 2: Install PowerShell Core on your agent machine. You can refer to this document for detailed information.

Using Microsoft-hosted linux agent is an option as well. But I don't recommend it because it may cause additional problems, such as not being able to find files hosted on your machine, and so on.

CodePudding user response:

1: Best and most feasible solution in your case is to use wait arguments in your Kubernetes@1 task. You can add condition in wait eg

wait --for=condition=ready pod -l app=netshoot.

More details can be found here. https://kubernetes.io/docs/reference/generated/kubectl/kubectl-commands#apply

kubectl wait --for=condition=complete --timeout=30s

2. Second option I have been using is to use KubernetesManifest@0 rather than Kubernetes@1 as KubernetesManifest@0 includes wait parameters by default and waits until all pods are up and running eventually timesout and fails the pipeline if condition is not met (any pod in failed/ pending state).

  • Related