Skip to content

Commit

Permalink
[RECUR-186] Deploy versioned API specs (#137)
Browse files Browse the repository at this point in the history
<details open>
<summary><a href="https://amuniversal.atlassian.net/browse/RECUR-186"
title="RECUR-186" target="_blank">RECUR-186</a></summary>
  <br />
  <table>
    <tr>
      <th>Summary</th>
      <td>[DEVOPS] Research how to deploy v1 and v2 APIs separately</td>
    </tr>
    <tr>
      <th>Type</th>
      <td>
<img alt="Story"
src="https://amuniversal.atlassian.net/images/icons/issuetypes/story.png"
/>
        Story
      </td>
    </tr>
    <tr>
      <th>Status</th>
      <td>In Progress</td>
    </tr>
    <tr>
      <th>Points</th>
      <td>N/A</td>
    </tr>
    <tr>
      <th>Labels</th>
      <td>-</td>
    </tr>
  </table>
</details>
<!--
do not remove this marker as it will break action-jira-linter's
functionality.
  added_by_jira_lint
-->
---

<!-- Please make sure you read the contribution guidelines and then fill
out the blanks below.

Please format the PR title appropriately based on the type of change:
  [JIRA-XXX]: <description>
-->

## Description

- Set `ingressWhitelist` input in AKS deploy workflow to the
`KUBERNETES_INGRESS_WHITELIST` repository variable and have it default
to the value in the `values.yaml` and otherwise, default to `0.0.0.0/0`
- Have the API management update workflow run on self-hosted GitHub
Runner in the `amuaksdev101` cluster so that we can have a cloud
VM/container pull down the `swagger.json` files by adding the AKS
cluster node IPs to the `ingressWhitelist` list.
- Remove API spec versions from Swagger JSONs to fix API URL paths
Result:

![image](https://github.com/user-attachments/assets/9f71a738-7208-4611-b054-1c424909905b)

![image](https://github.com/user-attachments/assets/4d135563-aadb-4a85-b58c-4bfe9e435fbf)


## Related Links

<!-- List any links related to this pull request here

Replace "JIRA-XXX" with the your Jira issue key -->

- Jira Issue: RECUR-186
- Testing environment: [![🚀
Deploy](https://github.com/Andrews-McMeel-Universal/subscription_service/actions/workflows/deploy.yml/badge.svg)](https://github.com/Andrews-McMeel-Universal/subscription_service/actions/workflows/deploy.yml)
  • Loading branch information
ebronson68 committed Jul 25, 2024
1 parent 0c18faf commit d9dd811
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 21 deletions.
1 change: 1 addition & 0 deletions .github/actionlint.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ self-hosted-runner:
- boley
- Windows
- self-hosted
- azure
10 changes: 3 additions & 7 deletions .github/workflows/aks-deploy.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ on:
required: false
type: string
description: "IPs to include in the application whitelist"
default: "0.0.0.0/0"
default: ${{ vars.KUBERNETES_INGRESS_WHITELIST }}
adminIngressWhitelist:
required: false
type: string
Expand Down Expand Up @@ -153,13 +153,9 @@ jobs:
$domainName = ($ingress.Split('.') | Select-Object -Last 2) -join '.'
$environmentIngress = "${{ inputs.environmentIngress }}" -replace '"', '' -replace "'", ""
$hostName = $ingress -replace $domainName, '' -replace "\.$", ""
$ingressWhitelist = $appConfig.ingress.annotations."nginx.ingress.kubernetes.io/whitelist-source-range" ?? "${{ inputs.ingressWhitelist }}"
$ingressWhitelist = "${{ inputs.ingressWhitelist }}" ?? $appConfig.ingress.annotations."nginx.ingress.kubernetes.io/whitelist-source-range" ?? '0.0.0.0/0'
if ($appConfig.adminingress) {
$adminIngressWhitelist = $appConfig.adminingress.annotations."nginx.ingress.kubernetes.io/whitelist-source-range" ?? "${{ inputs.adminIngressWhitelist }}"
if (![bool]($adminIngressWhitelist -match "^\d{1,3}(\.\d{1,3}){3}(\/\d{1,2})?(,\d{1,3}(\.\d{1,3}){3}(\/\d{1,2})?)*$")) {
Write-Output "Invalid value set for 'adminingress.annotations.nginx.ingress.kubernetes.io/whitelist-source-range'. Defaulting to ${{ inputs.adminIngressWhitelist }} Value: $adminIngressWhitelist"
$adminIngressWhitelist = "${{ inputs.adminIngressWhitelist }}"
}
$adminIngressWhitelist = "${{ inputs.adminIngressWhitelist }}" ?? $appConfig.adminingress.annotations."nginx.ingress.kubernetes.io/whitelist-source-range"
}
else {
Write-Output "adminingress values not defined. Skipping setting adminIngressWhitelist"
Expand Down
57 changes: 43 additions & 14 deletions .github/workflows/update-azureapimanagement.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ on:
jobs:
update-api-management:
name: Update API Management Service
runs-on: ubuntu-latest
runs-on: azure
steps:
- name: Checkout
uses: actions/checkout@v4
Expand Down Expand Up @@ -111,20 +111,49 @@ jobs:
# Create a context object for API management operations
$Context = New-AzApiManagementContext -ResourceGroupName $ResourceGroup -ServiceName $ServiceName
# Construct the Swagger URL for the default API specification
$SwaggerURL = "https://$serviceFqdn${{ inputs.apiSpecificationPath }}"
Write-Output "Default API Swagger URL: $SwaggerURL"
# Retrieve the API Version Set
$apiVersionSet = Get-AzApiManagementApiVersionSet -Context $Context | Where-Object { $_.DisplayName -eq $ApiId }
if (-not $apiVersionSet) { # Corrected variable name here
$apiVersionSet = New-AzApiManagementApiVersionSet -Context $Context -Name $ApiId -Scheme Segment -Description "$ApiId API"
}
# Import the API using the Swagger URL
Import-AzApiManagementApi -Context $Context -SpecificationUrl $SwaggerURL -SpecificationFormat OpenApi -Path $ApiId -ApiId "$ApiId"
# Update API versions
1..3 | ForEach-Object {
$version = $_
# Retrieve the imported API object
$Api = Get-AzApiManagementApi -Context $Context -ApiId $ApiId
# Check if the versioned API exists
$apiDirectory = Get-ChildItem -Directory -Recurse -Filter "V${version}" -ErrorAction SilentlyContinue | Select-Object -First 1
if ($apiDirectory -and (Test-Path -Path $apiDirectory.FullName)) {
# Construct the Swagger URL for each version
$SwaggerURL = "https://$serviceFqdn/swagger/v$version/swagger.json"
Write-Output $SwaggerURL
# Update the API object with the service URL and subscription requirement
$Api.ServiceURL = $ServiceURL
$Api.SubscriptionRequired = $ApiSubscriptionRequired
# Generate a unique API ID for each version
$versionedApiId = "$ApiId-v$version"
# Update default API version
Set-AzApiManagementApi -InputObject $Api
Add-AzApiManagementApiToProduct -Context $Context -ApiId $ApiId -ProductId $ApiProductId
# Pull down Swagger JSON
$SwaggerJSON = (Invoke-WebRequest -Uri $SwaggerURL).Content
# Remove path prefix from Swagger JSON
$SwaggerJSON = $SwaggerJSON -replace "/v${version}", ""
# Save Swagger JSON to file
$SwaggerJSONFile = "swagger.json"
$SwaggerJSON | Out-File -FilePath $SwaggerJSONFile
# # Import the versioned API
Import-AzApiManagementApi -Context $Context -SpecificationPath $SwaggerJSONFile -SpecificationFormat OpenApi -Path $ApiId -ApiId "$versionedApiId" -ApiVersion "v$version" -ApiVersionSetId $ApiVersionSet.Id
# Retrieve and update the API object
$Api = Get-AzApiManagementApi -Context $Context -ApiId "$versionedApiId"
$Api.ServiceURL = "$ServiceURL/v${version}"
$Api.SubscriptionRequired = $ApiSubscriptionRequired
# Associate the versioned API with a product
Set-AzApiManagementApi -InputObject $Api
Add-AzApiManagementApiToProduct -Context $Context -ApiId "$versionedApiId" -ProductId $ApiProductId
}
else {
Write-Output "Version V$version does not exist in this repository."
}
}

0 comments on commit d9dd811

Please sign in to comment.