microsoft / azure-pipelines-tasks

Tasks for Azure Pipelines
https://aka.ms/tfbuild
MIT License
3.47k stars 2.61k forks source link

[Question]: How to patch a singke key-value pair in an existing ConfigMap #20447

Open sdg002 opened 2 hours ago

sdg002 commented 2 hours ago

Task name

Kubernetes@1

Task version

1

Environment type (Please select at least one enviroment where you face this issue)

Azure DevOps Server type

dev.azure.com (formerly visualstudio.com)

Azure DevOps Server Version (if applicable)

No response

Operation system

Ubuntu

Question

I am trying to use the `Kubernetes@1` Azure Devops Task to `patch` a single name-vaue pair in an already existing `ConfigMap`

## YAML snippet

```yaml
  - task: Kubernetes@1
    displayName: 'update configmap with version'
    inputs:
      connectionType: 'Azure Resource Manager'
      azureSubscriptionEndpoint: ${{ parameters.azserviceconnection }} # name of the Azure Service connection
      azureResourceGroup: ${{ parameters.aksresourcegroup }} # the resource group which has the AKS cluster
      kubernetesCluster: aks001-dev
      namespace: powertrading-dev
      configMapName: shared-configmap
      useConfigMapFile: false
      configMapArguments: --from-literal=key1=some_value_from_yaml
    continueOnError: false      

My intent is to replicate the following command:

kubectl --namespace powertrading-dev patch configmap shared-configmap -p  "{ \"data\" : { \"key1\" : \"some_value_from_yaml\" }}"

I can't get the Devops task to update a single key-value pair. Any suggestions ?

sdg002 commented 2 hours ago

Option-1-Using the configMapName option

I have explained above. This doe not work.


Option-2-Using the patch command

I have also tried doing a patch

  - task: Kubernetes@1
    displayName: 'update configmap with version'
    inputs:
      connectionType: 'Azure Resource Manager'
      azureSubscriptionEndpoint: ${{ parameters.azserviceconnection }} 
      azureResourceGroup: ${{ parameters.aksresourcegroup }}
      kubernetesCluster: ${{ parameters.aksresourcename }}
      namespace: powertrading-dev
      command: patch
      arguments: "configmap shared-configmap --patch  '{\"data\": {\"key1\": \"newvalue1 from yaml\" }' "

But, this fails with the following error:


2024-09-19T20:40:02.7681122Z [command]/opt/hostedtoolcache/kubectl/1.31.0/x64/kubectl patch -n powertrading-dev configmap shared-configmap --patch '{data: {key1: newvalue1 from yaml }' -o json
2024-09-19T20:40:02.7731977Z error: unable to parse "'{data:": yaml: found unexpected end of stream
2024-09-19T20:40:02.7807376Z ##[error]error: unable to parse "'{data:": yaml: found unexpected end of stream
2024-09-19T20:40:02.7816495Z commandOutput

The double quotations are being stripped off.

Can somebody suggest how I may get the above to work?