Skip to content

Commit 03530eb

Browse files
beraldoleallittlejawa
authored andcommitted
gcp: fetch disk image size dynamically
Remove the hardcoded disk size and retrieve it from the API instead. Signed-off-by: Beraldo Leal <[email protected]>
1 parent cea15ff commit 03530eb

File tree

5 files changed

+55
-15
lines changed

5 files changed

+55
-15
lines changed

src/cloud-api-adaptor/entrypoint.sh

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -96,10 +96,11 @@ gcp() {
9696

9797
[[ "${PODVM_IMAGE_NAME}" ]] && optionals+="-image-name ${PODVM_IMAGE_NAME} "
9898
[[ "${GCP_PROJECT_ID}" ]] && optionals+="-gcp-project-id ${GCP_PROJECT_ID} "
99-
[[ "${GCP_ZONE}" ]] && optionals+="-zone ${GCP_ZONE} " # if not set retrieved from IMDS
100-
[[ "${GCP_MACHINE_TYPE}" ]] && optionals+="-machine-type ${GCP_MACHINE_TYPE} " # default e2-medium
101-
[[ "${GCP_NETWORK}" ]] && optionals+="-network ${GCP_NETWORK} " # defaults to 'default'
102-
[[ "${GCP_DISK_TYPE}" ]] && optionals+="-disk-type ${GCP_DISK_TYPE} " # defaults to 'pd-standard'
99+
[[ "${GCP_ZONE}" ]] && optionals+="-zone ${GCP_ZONE} " # if not set retrieved from IMDS
100+
[[ "${GCP_MACHINE_TYPE}" ]] && optionals+="-machine-type ${GCP_MACHINE_TYPE} " # default e2-medium
101+
[[ "${GCP_NETWORK}" ]] && optionals+="-network ${GCP_NETWORK} " # defaults to 'default'
102+
[[ "${GCP_DISK_TYPE}" ]] && optionals+="-disk-type ${GCP_DISK_TYPE} " # defaults to 'pd-standard'
103+
[[ "${ROOT_VOLUME_SIZE}" ]] && optionals+="-root-volume-size ${ROOT_VOLUME_SIZE} " # Specify root volume size for pod vm
103104

104105
set -x
105106

src/cloud-api-adaptor/install/overlays/gcp/kustomization.yaml

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,18 @@ configMapGenerator:
1717
namespace: confidential-containers-system
1818
literals:
1919
- CLOUD_PROVIDER="gcp"
20-
#- PAUSE_IMAGE="" # Uncomment and set if you want to use a specific pause image
21-
#- TUNNEL_TYPE="" # Uncomment and set if you want to use a specific tunnel type. Defaults to vxlan
22-
#- VXLAN_PORT="" # Uncomment and set if you want to use a specific vxlan port. Defaults to 4789
2320
- PODVM_IMAGE_NAME="" # set from step "Build Pod VM Image" in gcp/README.md
2421
- GCP_PROJECT_ID="" # set
2522
- GCP_ZONE="" # set e.g. "us-west1-a"
2623
- GCP_MACHINE_TYPE="e2-medium" # replace if needed. caa defaults to e2-medium
2724
- GCP_NETWORK="global/networks/default" # replace if needed.
25+
#- PEERPODS_LIMIT_PER_NODE="10" # Max number of peer pods that can be created per node. Default is 10
26+
#- REMOTE_HYPERVISOR_ENDPOINT="/run/peerpod/hypervisor.sock" # Path to Kata remote hypervisor socket. Default is /run/peerpod/hypervisor.sock
27+
#- PEER_PODS_DIR="/run/peerpod/pods" # Path to peer pods directory. Default is /run/peerpod/pods
28+
#- PAUSE_IMAGE="" # Uncomment and set if you want to use a specific pause image
29+
#- ROOT_VOLUME_SIZE="10" # Uncomment and set if you want to use a specific root volume size. Defaults to 10
30+
#- TUNNEL_TYPE="" # Uncomment and set if you want to use a specific tunnel type. Defaults to vxlan
31+
#- VXLAN_PORT="" # Uncomment and set if you want to use a specific vxlan port. Defaults to 4789
2832
##TLS_SETTINGS
2933
#- CACERT_FILE="/etc/certificates/ca.crt" # for TLS
3034
#- CERT_FILE="/etc/certificates/client.crt" # for TLS

src/cloud-providers/gcp/manager.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ func (_ *Manager) ParseCmd(flags *flag.FlagSet) {
2626
flags.StringVar(&gcpcfg.MachineType, "machine-type", "e2-medium", "Pod VM instance type")
2727
flags.StringVar(&gcpcfg.Network, "network", "", "Network ID to be used for the Pod VMs")
2828
flags.StringVar(&gcpcfg.DiskType, "disk-type", "pd-standard", "Any GCP disk type (pd-standard, pd-ssd, pd-balanced or pd-extreme)")
29+
flags.IntVar(&gcpcfg.RootVolumeSize, "root-volume-size", 10, "Root volume size (in GiB) for the Pod VMs")
2930
}
3031

3132
func (_ *Manager) LoadEnv() {

src/cloud-providers/gcp/provider.go

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,29 @@ func getIPs(instance *computepb.Instance) ([]netip.Addr, error) {
7676
return podNodeIPs, nil
7777
}
7878

79+
func (p *gcpProvider) getImageSizeGB(ctx context.Context, image string) (int64, error) {
80+
client, err := compute.NewImagesRESTClient(ctx)
81+
if err != nil {
82+
return 0, fmt.Errorf("failed to create compute client: %w", err)
83+
}
84+
defer client.Close()
85+
86+
parts := strings.Split(image, "/")
87+
imageName := parts[len(parts)-1]
88+
89+
req := &computepb.GetImageRequest{
90+
Project: p.serviceConfig.ProjectId,
91+
Image: imageName,
92+
}
93+
94+
img, err := client.Get(ctx, req)
95+
if err != nil {
96+
return 0, fmt.Errorf("Failed to get image for %s: %w", image, err)
97+
}
98+
99+
return img.GetDiskSizeGb(), nil
100+
}
101+
79102
func (p *gcpProvider) CreateInstance(ctx context.Context, podName, sandboxID string, cloudConfig cloudinit.CloudConfigGenerator, spec provider.InstanceTypeSpec) (*provider.Instance, error) {
80103

81104
instanceName := util.GenerateInstanceName(podName, sandboxID, maxInstanceNameLen)
@@ -105,6 +128,16 @@ func (p *gcpProvider) CreateInstance(ctx context.Context, podName, sandboxID str
105128
srcImage = proto.String(spec.Image)
106129
}
107130

131+
imageSizeGB, err := p.getImageSizeGB(ctx, *srcImage)
132+
if err != nil {
133+
return nil, fmt.Errorf("Failed to get image size: %w", err)
134+
}
135+
136+
// If user provided RootVolumeSize, use the larger of the two
137+
if p.serviceConfig.RootVolumeSize > 0 && int64(p.serviceConfig.RootVolumeSize) > imageSizeGB {
138+
imageSizeGB = int64(p.serviceConfig.RootVolumeSize)
139+
}
140+
108141
insertReq := &computepb.InsertInstanceRequest{
109142
Project: p.serviceConfig.ProjectId,
110143
Zone: p.serviceConfig.Zone,
@@ -113,7 +146,7 @@ func (p *gcpProvider) CreateInstance(ctx context.Context, podName, sandboxID str
113146
Disks: []*computepb.AttachedDisk{
114147
{
115148
InitializeParams: &computepb.AttachedDiskInitializeParams{
116-
DiskSizeGb: proto.Int64(20),
149+
DiskSizeGb: proto.Int64(imageSizeGB),
117150
SourceImage: srcImage,
118151
DiskType: proto.String(fmt.Sprintf("zones/%s/diskTypes/%s", p.serviceConfig.Zone, p.serviceConfig.DiskType)),
119152
},

src/cloud-providers/gcp/types.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,14 @@ import (
88
)
99

1010
type Config struct {
11-
GcpCredentials string
12-
ProjectId string
13-
Zone string
14-
ImageName string
15-
MachineType string
16-
Network string
17-
DiskType string
11+
GcpCredentials string
12+
ProjectId string
13+
Zone string
14+
ImageName string
15+
MachineType string
16+
Network string
17+
DiskType string
18+
RootVolumeSize int
1819
}
1920

2021
func (c Config) Redact() Config {

0 commit comments

Comments
 (0)