Azure-devops-docs: Azure DevOps Pipeline Environment - Kubernetes resource API

Created on 11 Sep 2019  ·  19Comments  ·  Source: MicrosoftDocs/azure-devops-docs

Is there an API to create Azure DevOps Pipeline Environment - Kubernetes resource?

I have a CI/CD pipeline which automatically creates namespaces. It would be great if I could link these tasks to Kubernetes resource in appropriate environment. Unfortunately I did not find any way to automatically create the resource from CI/CD.


Document Details

Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.

Pri2 devops-cictech devopprod doc-bug

Most helpful comment

We automate everything using REST API, from creating project, repos, policies, pipelines (build/release), entitlements. We are in process of rolling out new version of multi-stage pipeline but we are not able to create Environments, Environment Resources, or Environment Approvals and Checks. Please provide an API for these. This is holding up a major milestone for us.

All 19 comments

@skwasiborski Thank you for the question.

Can you try using the Kubectl task? This should give you access to the create action.

@msebolt unfortunately I think I do not understand your suggestion. I will try to explain my setup more precisely:
I have a Azure DevOps pipeline that builds and deploys my k8s applications. The deployment is done via Kubectl task that executes apply an a single yaml file that was generated earlier in the process.
I want to take advantage of Environments feature with kubernetes namespaces added as resources i.e I want to link my jobs to the namespace as described here. As far as I understand the documentation I need to manually add k8s namespace to every environment before running a deployment job that targets this namespace (if the namespace is not added I am getting an error saying the resource does not exist in an Environment).
This is a bit problematic for normal deployments but I can live with that. The issue is that I implemented an ability to run the pipeline on feature branches (i.e. non develop branch), in such case the app is deployed to dedicated namespace. The namespace name is generated based on branch name. Adding the namespace manually for every branch is problematic especially that the namespace is created from CI/CD. It would be great if I could somehow add the namespace problematically before running the CI/CD pipeline.

No problem, re-assigning to the author.

It would be extra cool, if when you specified the environment like
environment: 'smarthotel-dev.bookings'
that it would not only create the environment 'smarthotel-dev' (if it didn't exist), but also create the namespace 'bookings' as a resource in the environment (if it didn't exist).

@skwasiborski and @yelob we have a feature coming up really soon (around early November) called review apps which lets you create namespaces on the fly for each PR and register them as K8s resources under environments. We are doing this with the intention of allowing the user a way to assess the state of the application in this review environment before merging the PR.

About the request for documentation on APIs for creating K8s resource under environment, the REST APIs for creating environments are not publicly released yet. I'm closing this issue for now as this is not an issue with the documentation. Please open this as an issue on our Azure DevOps Developer Community (which is where we take feature asks) and I'll make sure the PM owner and the team driving environments work are able to triage and act on this issue at the earliest.

@shashankbarsin any update when any API documentation will be available to environments/approval checks?

+1
@shashankbarsin I'd suggest this issue should remain open until API documentation on environments/approval checks is available.

Any update on this issue? I have a partner/customer who is being vocal about this

We automate everything using REST API, from creating project, repos, policies, pipelines (build/release), entitlements. We are in process of rolling out new version of multi-stage pipeline but we are not able to create Environments, Environment Resources, or Environment Approvals and Checks. Please provide an API for these. This is holding up a major milestone for us.

I wanted to see if there were any updates to this. It would be really helpful for us on a current project.

+1

Figured out a way to do this and wanted to pass it along. Please let me know if you have any issues/questions.

Creating an environment via API's in Azure DevOps

@f2calv Thanks for letting us know. These Environments and Kubernetes docs must have been added recently because they were not present before. I am listing below a sample workflow with what I have been able to confirm as working and what is still broken by following the new documentation.

1) List Environments (✔ supported/confirmed)
1) Add Kubernetes Resource (❌ broken - see error below)
1) List Resources in a specified Environment (✔ supported/confirmed)
1) Delete Kubernetes Resource (✔ supported/confirmed)

The documentation you linked for adding a Kubernetes resource only supports 4 request parameters. It seems to be missing parameters for the "Server URL", "Secret", and "Accept untrusted certificates". When I try to create a Kubernetes using the 4 parameters documented, I get the error "_Failed to create resource. Please ensure that all mandatory fields are provided as input._"

https://docs.microsoft.com/en-us/rest/api/azure/devops/distributedtask/environments/get?view=azure-devops-rest-6.0#environmentexpands

Name | Type | Description
-- | -- | --
clusterName | string |  
name | string |  
namespace | string |  
tags | string[] | Tags of the kubernetes resource.
serverUrl ❌missing | string |
secret ❌missing | string |
acceptUntrustedCertificates ❌missing | string |

REQUEST:

POST https://dev.azure.com/{organization}/{project}/_apis/distributedtask/environments/{environmentId}/providers/kubernetes?api-version=6.0-preview.1
{
    "clusterName": "test",
    "name": "test name",
    "namespace": "test-namespace",
    "tags": "foo"
}

RESPONSE:

HTTP/1.1 500 Internal Server Error
{
  "$id": "1",
  "innerException": null,
  "message": "Failed to create resource. Please ensure that all mandatory fields are provided as input.",
  "typeName": "Microsoft.TeamFoundation.DistributedTask.WebApi.KubernetesResourceException, Microsoft.TeamFoundation.DistributedTask.WebApi",
  "typeKey": "KubernetesResourceException",
  "errorCode": 0,
  "eventId": 3000
}

seeing exactly the same error when trying to create resource within a created environment:
rest api post to below url with body:
url = https://XXX/xxxx/xxxx/_apis/distributedtask/environments/17/providers/kubernetes?api-version=6.0-preview.1
```
JSON Payload: [
{
"clusterName": "aks-sandbox",
"tags": "test",
"name": "pr-111",
"namespace": "pr-111",
"serviceEndpointId": "guid of service endpoint"
}
]

Invoke-RestMethod : {"$id":"1","innerException":null,"message":"Failed to create resource. Please ensure that all mandatory fields are provided as input.","typeName":"Microsoft.TeamFoundation.DistributedTask.WebApi.KubernetesResourceException, Microsoft.TeamFoundation.DistributedTask.WebApi","typeKey":"KubernetesResourceException","errorCode":0,"eventId":3000}

You can see the sample payloads in the link below. The blog article above talks about how.

https://github.com/jf781/AzureDevopsEnvironments/tree/master/payload-samples

Hope this helps!!

thanks @jf781 , found the problem with the payload body im passing, it was due to my powershell passing through an array instead of just single json, got it sorted thanks.

you're example is very useful, thanks

https://github.com/jf781/AzureDevopsEnvironments/tree/master/payload-samples

I have the same issue using python.
no problem creating the service endpoint but can't create the kubernetes resource in the environment. Would be nice to have more information about what is missing

{'$id': '1', 'innerException': None, 'message': 'Failed to create resource. Please ensure that all mandatory fields are provided as input.', 'typeName': 'Microsoft.TeamFoundation.DistributedTask.WebApi.KubernetesResourceException, Microsoft.TeamFoundation.DistributedTask.WebApi', 'typeKey': 'KubernetesResourceException', 'errorCode': 0, 'eventId': 3000}

Edit:
OMG I feel stupid, I was missing a 'd' in 'serviceEndpointId'
Working now

Was this page helpful?
0 / 5 - 0 ratings