Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: ACA Updates POC #4362

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions cli/azd/pkg/containerapps/container_app.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"log"
"net/http"
Expand Down Expand Up @@ -63,6 +64,12 @@ type ContainerAppService interface {
imageName string,
options *ContainerAppOptions,
) error
GetActiveContainerImage(ctx context.Context,
subscriptionId string,
resourceGroupName string,
appName string,
options *ContainerAppOptions,
) (string, error)
}

// NewContainerAppService creates a new ContainerAppService
Expand Down Expand Up @@ -262,6 +269,36 @@ func (cas *containerAppService) DeployYaml(
return nil
}

// GetActiveContainerImage returns the image name of the active container in the specified container app
func (cas *containerAppService) GetActiveContainerImage(ctx context.Context,
subscriptionId string,
resourceGroupName string,
appName string,
options *ContainerAppOptions,
) (string, error) {
containerApp, err := cas.getContainerApp(ctx, subscriptionId, resourceGroupName, appName, options)
if err != nil {
return "", fmt.Errorf("getting container app: %w", err)
}

var containers []map[string]any
has, err := containerApp.GetSection(pathTemplateContainers, &containers)
if err != nil {
return "", fmt.Errorf("getting app containers: %w", err)
}

if !has || len(containers) == 0 {
return "", errors.New("no containers found in container app")
}

imageName, ok := containers[0]["image"].(string)
if !ok {
return "", errors.New("no image found in container app")
}

return imageName, nil
}

// Adds and activates a new revision to the specified container app
func (cas *containerAppService) AddRevision(
ctx context.Context,
Expand Down
28 changes: 22 additions & 6 deletions cli/azd/pkg/project/service_target_containerapp.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ package project
import (
"context"
"fmt"
"strconv"
"log"

"github.com/azure/azure-dev/cli/azd/pkg/async"
"github.com/azure/azure-dev/cli/azd/pkg/azapi"
Expand Down Expand Up @@ -171,15 +171,31 @@ func (at *containerAppTarget) addPreProvisionChecks(_ context.Context, serviceCo
// This allows the resource deployment to detect whether or not to pull existing container image during
// provision operation to avoid resetting the container app back to a default image
return serviceConfig.Project.AddHandler("preprovision", func(ctx context.Context, args ProjectLifecycleEventArgs) error {
exists := false
// If we already have a container image name set in the environment then use it.
imageName := at.env.GetServiceProperty(serviceConfig.Name, "IMAGE_NAME")
if imageName != "" {
return nil
}

// Check if the target resource already exists
targetResource, err := at.resourceManager.GetTargetResource(ctx, at.env.GetSubscriptionId(), serviceConfig)
if targetResource != nil && err == nil {
exists = true
if err != nil || targetResource == nil {
log.Printf("Did not find target resource for container app with service name: '%s'", serviceConfig.Name)
return nil
}

imageName, err = at.containerAppService.GetActiveContainerImage(
ctx,
targetResource.SubscriptionId(),
targetResource.ResourceGroupName(),
targetResource.ResourceName(),
nil,
)
if err != nil {
log.Printf("Did not find target resource for container app with service name: '%s'", serviceConfig.Name)
return nil
}

at.env.SetServiceProperty(serviceConfig.Name, "RESOURCE_EXISTS", strconv.FormatBool(exists))
at.env.SetServiceProperty(serviceConfig.Name, "IMAGE_NAME", imageName)
return at.envManager.Save(ctx, at.env)
})
}
Loading