75 lines
2.2 KiB
Go
75 lines
2.2 KiB
Go
package gcp
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
"cloud.google.com/go/compute/apiv1/computepb"
|
|
"google.golang.org/protobuf/proto"
|
|
|
|
"gitea.home.hrajfrisbee.cz/kacerr/egress-proxies-operator/internal/provider"
|
|
)
|
|
|
|
const (
|
|
defaultNetwork = "default"
|
|
defaultNetworkTag = "proxy-operator"
|
|
defaultDiskSizeGB = 10
|
|
userDataKey = "user-data"
|
|
)
|
|
|
|
func withDefaults(cfg provider.GCPConfig) provider.GCPConfig {
|
|
if cfg.Network == "" {
|
|
cfg.Network = defaultNetwork
|
|
}
|
|
if cfg.NetworkTag == "" {
|
|
cfg.NetworkTag = defaultNetworkTag
|
|
}
|
|
if cfg.DiskSizeGB == 0 {
|
|
cfg.DiskSizeGB = defaultDiskSizeGB
|
|
}
|
|
return cfg
|
|
}
|
|
|
|
// buildInsertRequest is pure so the field-by-field unit test needs no fake
|
|
// at all — the plan's primary test for this provider.
|
|
func buildInsertRequest(cfg provider.GCPConfig, req provider.CreateRequest) *computepb.InsertInstanceRequest {
|
|
inst := &computepb.Instance{
|
|
Name: proto.String(req.Name),
|
|
MachineType: proto.String(fmt.Sprintf("zones/%s/machineTypes/%s", req.Placement.Zone, req.Placement.MachineType)),
|
|
Disks: []*computepb.AttachedDisk{{
|
|
Boot: proto.Bool(true),
|
|
AutoDelete: proto.Bool(true),
|
|
InitializeParams: &computepb.AttachedDiskInitializeParams{
|
|
SourceImage: proto.String(req.Placement.Image),
|
|
DiskSizeGb: proto.Int64(cfg.DiskSizeGB),
|
|
},
|
|
}},
|
|
NetworkInterfaces: []*computepb.NetworkInterface{{
|
|
Network: proto.String("global/networks/" + cfg.Network),
|
|
// An ephemeral external IP: exactly this pair, per the API's
|
|
// contract for one-to-one NAT.
|
|
AccessConfigs: []*computepb.AccessConfig{{
|
|
Name: proto.String("External NAT"),
|
|
Type: proto.String("ONE_TO_ONE_NAT"),
|
|
}},
|
|
}},
|
|
// The GC contract: every resource this operator creates carries
|
|
// these two labels, and orphan GC relies on both.
|
|
Labels: map[string]string{
|
|
provider.LabelManaged: provider.LabelManagedYes,
|
|
provider.LabelUID: req.UID,
|
|
},
|
|
Tags: &computepb.Tags{Items: []string{cfg.NetworkTag}},
|
|
}
|
|
if req.CloudInit != "" {
|
|
inst.Metadata = &computepb.Metadata{Items: []*computepb.Items{{
|
|
Key: proto.String(userDataKey),
|
|
Value: proto.String(req.CloudInit),
|
|
}}}
|
|
}
|
|
return &computepb.InsertInstanceRequest{
|
|
Project: cfg.Project,
|
|
Zone: req.Placement.Zone,
|
|
InstanceResource: inst,
|
|
}
|
|
}
|