From 5d81a3c6c3e48f301973733fd92c87f6b429b3e9 Mon Sep 17 00:00:00 2001 From: Victor Vazquez Date: Sat, 25 May 2024 00:35:44 +0000 Subject: [PATCH 1/4] Adding alpha feature `alpha.aca.persistDomains` which would use the current custom Domains from the existing ACA and use it for creating a new revision --- cli/azd/pkg/containerapps/container_app.go | 48 +++++++++++++++++----- cli/azd/resources/alpha_features.yaml | 2 + 2 files changed, 40 insertions(+), 10 deletions(-) diff --git a/cli/azd/pkg/containerapps/container_app.go b/cli/azd/pkg/containerapps/container_app.go index 5c67c13aa44..e5959b8d714 100644 --- a/cli/azd/pkg/containerapps/container_app.go +++ b/cli/azd/pkg/containerapps/container_app.go @@ -16,6 +16,7 @@ import ( "github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/appcontainers/armappcontainers/v3" azdinternal "github.com/azure/azure-dev/cli/azd/internal" "github.com/azure/azure-dev/cli/azd/pkg/account" + "github.com/azure/azure-dev/cli/azd/pkg/alpha" "github.com/azure/azure-dev/cli/azd/pkg/convert" "github.com/azure/azure-dev/cli/azd/pkg/httputil" "github.com/benbjohnson/clock" @@ -59,22 +60,25 @@ func NewContainerAppService( httpClient httputil.HttpClient, clock clock.Clock, armClientOptions *arm.ClientOptions, + alphaFeatureManager *alpha.FeatureManager, ) ContainerAppService { return &containerAppService{ - credentialProvider: credentialProvider, - httpClient: httpClient, - userAgent: azdinternal.UserAgent(), - clock: clock, - armClientOptions: armClientOptions, + credentialProvider: credentialProvider, + httpClient: httpClient, + userAgent: azdinternal.UserAgent(), + clock: clock, + armClientOptions: armClientOptions, + alphaFeatureManager: alphaFeatureManager, } } type containerAppService struct { - credentialProvider account.SubscriptionCredentialProvider - httpClient httputil.HttpClient - userAgent string - clock clock.Clock - armClientOptions *arm.ClientOptions + credentialProvider account.SubscriptionCredentialProvider + httpClient httputil.HttpClient + userAgent string + clock clock.Clock + armClientOptions *arm.ClientOptions + alphaFeatureManager *alpha.FeatureManager } type ContainerAppIngressConfiguration struct { @@ -112,6 +116,8 @@ func (cas *containerAppService) GetIngressConfiguration( // or updating the container app. When unset, we use the default API version of the armappcontainers.ContainerAppsClient. const apiVersionKey = "api-version" +var persistCustomDomainsFeature = alpha.MustFeatureKey("aca.persistDomains") + func (cas *containerAppService) DeployYaml( ctx context.Context, subscriptionId string, @@ -124,6 +130,28 @@ func (cas *containerAppService) DeployYaml( return fmt.Errorf("decoding yaml: %w", err) } + if shouldPersist := cas.alphaFeatureManager.IsEnabled(persistCustomDomainsFeature); shouldPersist { + aca, err := cas.getContainerApp(ctx, subscriptionId, resourceGroupName, appName) + if err == nil { + containerAppJson, err := json.Marshal(obj) + if err != nil { + panic("marshalling container app json failed") + } + var containerApp armappcontainers.ContainerApp + if err := json.Unmarshal(containerAppJson, &containerApp); err != nil { + return fmt.Errorf("converting to container app type: %w", err) + } + containerApp.Properties.Configuration.Ingress.CustomDomains = aca.Properties.Configuration.Ingress.CustomDomains + backToByte, err := json.Marshal(containerApp) + if err != nil { + panic("marshalling container app json failed") + } + if err := yaml.Unmarshal(backToByte, &obj); err != nil { + return fmt.Errorf("decoding yaml: %w", err) + } + } + } + var poller *runtime.Poller[armappcontainers.ContainerAppsClientCreateOrUpdateResponse] // The way we make the initial request depends on whether the apiVersion is specified in the YAML. diff --git a/cli/azd/resources/alpha_features.yaml b/cli/azd/resources/alpha_features.yaml index 6344086cd80..39988d7d9a3 100644 --- a/cli/azd/resources/alpha_features.yaml +++ b/cli/azd/resources/alpha_features.yaml @@ -6,3 +6,5 @@ description: "Enable Helm support for AKS deployments." - id: aks.kustomize description: "Enable Kustomize support for AKS deployments." +- id: aca.persistDomains + description: "Do not change custom domains when deploying Azure Container Apps." From 6d2ce366501ae5b3b22ccc3d5cfbbb8170263d74 Mon Sep 17 00:00:00 2001 From: Victor Vazquez Date: Sat, 25 May 2024 00:44:08 +0000 Subject: [PATCH 2/4] test and lint --- cli/azd/pkg/containerapps/container_app_test.go | 2 ++ cli/azd/pkg/project/service_target_containerapp_test.go | 1 + 2 files changed, 3 insertions(+) diff --git a/cli/azd/pkg/containerapps/container_app_test.go b/cli/azd/pkg/containerapps/container_app_test.go index 352aef697ae..19b3ba8ef64 100644 --- a/cli/azd/pkg/containerapps/container_app_test.go +++ b/cli/azd/pkg/containerapps/container_app_test.go @@ -42,6 +42,7 @@ func Test_ContainerApp_GetIngressConfiguration(t *testing.T) { mockContext.HttpClient, clock.NewMock(), mockContext.ArmClientOptions, + mockContext.AlphaFeaturesManager, ) ingressConfig, err := cas.GetIngressConfiguration(*mockContext.Context, subscriptionId, resourceGroup, appName) require.NoError(t, err) @@ -137,6 +138,7 @@ func Test_ContainerApp_AddRevision(t *testing.T) { mockContext.HttpClient, clock.NewMock(), mockContext.ArmClientOptions, + mockContext.AlphaFeaturesManager, ) err := cas.AddRevision(*mockContext.Context, subscriptionId, resourceGroup, appName, updatedImageName) require.NoError(t, err) diff --git a/cli/azd/pkg/project/service_target_containerapp_test.go b/cli/azd/pkg/project/service_target_containerapp_test.go index 92e85c77103..cd13f4530d8 100644 --- a/cli/azd/pkg/project/service_target_containerapp_test.go +++ b/cli/azd/pkg/project/service_target_containerapp_test.go @@ -139,6 +139,7 @@ func createContainerAppServiceTarget( mockContext.HttpClient, clock.NewMock(), mockContext.ArmClientOptions, + mockContext.AlphaFeaturesManager, ) containerRegistryService := azcli.NewContainerRegistryService( credentialProvider, From efec056fe7c7b9728a2cae9bb7c341b38ce19e76 Mon Sep 17 00:00:00 2001 From: Victor Vazquez Date: Sat, 25 May 2024 02:20:28 +0000 Subject: [PATCH 3/4] use config to be type-less --- cli/azd/pkg/containerapps/container_app.go | 21 +++++---------------- 1 file changed, 5 insertions(+), 16 deletions(-) diff --git a/cli/azd/pkg/containerapps/container_app.go b/cli/azd/pkg/containerapps/container_app.go index e5959b8d714..156e59b594c 100644 --- a/cli/azd/pkg/containerapps/container_app.go +++ b/cli/azd/pkg/containerapps/container_app.go @@ -17,6 +17,7 @@ import ( azdinternal "github.com/azure/azure-dev/cli/azd/internal" "github.com/azure/azure-dev/cli/azd/pkg/account" "github.com/azure/azure-dev/cli/azd/pkg/alpha" + "github.com/azure/azure-dev/cli/azd/pkg/config" "github.com/azure/azure-dev/cli/azd/pkg/convert" "github.com/azure/azure-dev/cli/azd/pkg/httputil" "github.com/benbjohnson/clock" @@ -133,22 +134,10 @@ func (cas *containerAppService) DeployYaml( if shouldPersist := cas.alphaFeatureManager.IsEnabled(persistCustomDomainsFeature); shouldPersist { aca, err := cas.getContainerApp(ctx, subscriptionId, resourceGroupName, appName) if err == nil { - containerAppJson, err := json.Marshal(obj) - if err != nil { - panic("marshalling container app json failed") - } - var containerApp armappcontainers.ContainerApp - if err := json.Unmarshal(containerAppJson, &containerApp); err != nil { - return fmt.Errorf("converting to container app type: %w", err) - } - containerApp.Properties.Configuration.Ingress.CustomDomains = aca.Properties.Configuration.Ingress.CustomDomains - backToByte, err := json.Marshal(containerApp) - if err != nil { - panic("marshalling container app json failed") - } - if err := yaml.Unmarshal(backToByte, &obj); err != nil { - return fmt.Errorf("decoding yaml: %w", err) - } + acaAsConfig := config.NewConfig(obj) + acaAsConfig.Set( + "properties.configuration.ingress.customDomains", aca.Properties.Configuration.Ingress.CustomDomains) + obj = acaAsConfig.Raw() } } From 10b7070829658167b355d5c6e6a577a82146e971 Mon Sep 17 00:00:00 2001 From: Victor Vazquez Date: Sat, 25 May 2024 02:23:58 +0000 Subject: [PATCH 4/4] lint --- cli/azd/pkg/containerapps/container_app.go | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/cli/azd/pkg/containerapps/container_app.go b/cli/azd/pkg/containerapps/container_app.go index 156e59b594c..39ed6a49c31 100644 --- a/cli/azd/pkg/containerapps/container_app.go +++ b/cli/azd/pkg/containerapps/container_app.go @@ -135,9 +135,14 @@ func (cas *containerAppService) DeployYaml( aca, err := cas.getContainerApp(ctx, subscriptionId, resourceGroupName, appName) if err == nil { acaAsConfig := config.NewConfig(obj) - acaAsConfig.Set( + err := acaAsConfig.Set( "properties.configuration.ingress.customDomains", aca.Properties.Configuration.Ingress.CustomDomains) - obj = acaAsConfig.Raw() + + if err == nil { + obj = acaAsConfig.Raw() + } else { + log.Printf("failed to set custom domains: %v. Domains will be ignored.", err) + } } }