Skip to content

Commit

Permalink
gcp: handle image in two formats
Browse files Browse the repository at this point in the history
This allow users to use public images shared outside the project scope.

Signed-off-by: Beraldo Leal <bleal@redhat.com>
  • Loading branch information
beraldoleal committed Feb 5, 2025
1 parent 63e7228 commit 5011f26
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions src/cloud-providers/gcp/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"fmt"
"log"
"net/netip"
"strings"

compute "cloud.google.com/go/compute/apiv1"
computepb "cloud.google.com/go/compute/apiv1/computepb"
Expand Down Expand Up @@ -89,8 +90,15 @@ func (p *gcpProvider) CreateInstance(ctx context.Context, podName, sandboxID str
userDataEnc := base64.StdEncoding.EncodeToString([]byte(userData))
logger.Printf("userDataEnc: %s", userDataEnc)

// It's expected that the image from the annotation will follow the format "projects/<project>/global/images/<image>"
srcImage := proto.String(fmt.Sprintf("projects/%s/global/images/%s", p.serviceConfig.ProjectId, p.serviceConfig.ImageName))
// It's expected that the image from the annotation will follow the format
// "projects/<project>/global/images/<imageid>" or just the "<imageid>" if the
// image is present on the same project.
var srcImage *string
if strings.HasPrefix(p.serviceConfig.ImageName, "projects/") {
srcImage = proto.String(p.serviceConfig.ImageName)
} else {
srcImage = proto.String(fmt.Sprintf("projects/%s/global/images/%s", p.serviceConfig.ProjectId, p.serviceConfig.ImageName))
}

if spec.Image != "" {
logger.Printf("Choosing %s from annotation as the GCP image for the PodVM image", spec.Image)
Expand Down

0 comments on commit 5011f26

Please sign in to comment.