Skip to content

Commit

Permalink
alpha: cloud run env, throttling support, image pull auth
Browse files Browse the repository at this point in the history
  • Loading branch information
23doors committed Apr 25, 2022
1 parent a74ac70 commit 2e07ac9
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 19 deletions.
24 changes: 14 additions & 10 deletions deploy/app_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,13 @@ type ServiceAppArgs struct {
}

type ServiceAppDeployOptions struct {
SkipRunsd bool `json:"skip_runsd"`
CPULimit float64 `json:"cpu_limit" default:"1"`
MemoryLimit int `json:"memory_limit" default:"256"`
MinScale int `json:"min_scale" default:"0"`
MaxScale int `json:"max_scale" default:"100"`
SkipRunsd bool `json:"skip_runsd"`
CPULimit float64 `json:"cpu_limit" default:"1"`
MemoryLimit int `json:"memory_limit" default:"256"`
MinScale int `json:"min_scale" default:"0"`
MaxScale int `json:"max_scale" default:"100"`
CPUThrottling bool `json:"cpu_throttling" default:"true"`
ExecutionEnvironment string `json:"execution_environment" default:"gen1"`
}

func NewServiceAppDeployOptions(in map[string]interface{}) (*ServiceAppDeployOptions, error) {
Expand Down Expand Up @@ -319,11 +321,13 @@ func (o *ServiceApp) Plan(ctx context.Context, pctx *config.PluginContext, r *re
IsPublic: fields.Bool(!o.Props.Private),
EnvVars: fields.Map(envVars),

CloudSQLInstances: fields.Sprintf(strings.Join(cloudSQLconnFmt, ","), cloudSQLconnNames...),
MinScale: fields.Int(o.DeployOpts.MinScale),
MaxScale: fields.Int(o.DeployOpts.MaxScale),
MemoryLimit: fields.String(fmt.Sprintf("%dMi", o.DeployOpts.MemoryLimit)),
CPULimit: fields.String(fmt.Sprintf("%dm", int(o.DeployOpts.CPULimit*1000))),
CloudSQLInstances: fields.Sprintf(strings.Join(cloudSQLconnFmt, ","), cloudSQLconnNames...),
MinScale: fields.Int(o.DeployOpts.MinScale),
MaxScale: fields.Int(o.DeployOpts.MaxScale),
MemoryLimit: fields.String(fmt.Sprintf("%dMi", o.DeployOpts.MemoryLimit)),
CPULimit: fields.String(fmt.Sprintf("%dm", int(o.DeployOpts.CPULimit*1000))),
ExecutionEnvironment: fields.String(o.DeployOpts.ExecutionEnvironment),
CPUThrottling: fields.Bool(o.DeployOpts.CPUThrottling),
}

_, err = r.RegisterAppResource(o.App, "cloud_run", o.CloudRun)
Expand Down
23 changes: 18 additions & 5 deletions gcp/cloud_run.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@ type CloudRun struct {
TimeoutSeconds fields.IntInputField `default:"300"`
Port fields.IntInputField `default:"80"`
EnvVars fields.MapInputField
Ingress fields.StringInputField `default:"all"` // options: internal-and-cloud-load-balancing
Ingress fields.StringInputField `default:"all"` // options: internal-and-cloud-load-balancing
ExecutionEnvironment fields.StringInputField `default:"gen1"` // options: gen2
CPUThrottling fields.BoolInputField `default:"true"`
}

func (o *CloudRun) ReferenceID() string {
Expand Down Expand Up @@ -88,6 +90,8 @@ func (o *CloudRun) Read(ctx context.Context, meta interface{}) error { // nolint
o.MaxScale.UnsetCurrent()
o.EnvVars.UnsetCurrent()
o.Ingress.UnsetCurrent()
o.CPUThrottling.UnsetCurrent()
o.ExecutionEnvironment.UnsetCurrent()

return nil
}
Expand Down Expand Up @@ -122,6 +126,8 @@ func (o *CloudRun) Read(ctx context.Context, meta interface{}) error { // nolint
o.TimeoutSeconds.SetCurrent(int(svc.Spec.Template.Spec.TimeoutSeconds))
o.Port.SetCurrent(int(svc.Spec.Template.Spec.Containers[0].Ports[0].ContainerPort))
o.Ingress.SetCurrent(svc.Metadata.Annotations["run.googleapis.com/ingress"])
o.CPUThrottling.SetCurrent(svc.Metadata.Annotations["run.googleapis.com/cpu-throttling"] == "true")
o.ExecutionEnvironment.SetCurrent(svc.Metadata.Annotations["run.googleapis.com/execution-environment"])

v, _ := strconv.Atoi(svc.Spec.Template.Metadata.Annotations["autoscaling.knative.dev/minScale"])
o.MinScale.SetCurrent(v)
Expand Down Expand Up @@ -253,6 +259,11 @@ func (o *CloudRun) makeRunService() *run.Service {
argsStr[i] = v.(string)
}

cpuThrottling := "false"
if o.CPUThrottling.Wanted() {
cpuThrottling = "true"
}

svc := &run.Service{
ApiVersion: "serving.knative.dev/v1",
Kind: "Service",
Expand All @@ -266,10 +277,12 @@ func (o *CloudRun) makeRunService() *run.Service {
Template: &run.RevisionTemplate{
Metadata: &run.ObjectMeta{
Annotations: map[string]string{
"run.googleapis.com/client-name": "outblocks",
"autoscaling.knative.dev/minScale": strconv.Itoa(o.MinScale.Wanted()),
"autoscaling.knative.dev/maxScale": strconv.Itoa(o.MaxScale.Wanted()),
"run.googleapis.com/cloudsql-instances": o.CloudSQLInstances.Wanted(),
"run.googleapis.com/client-name": "outblocks",
"autoscaling.knative.dev/minScale": strconv.Itoa(o.MinScale.Wanted()),
"autoscaling.knative.dev/maxScale": strconv.Itoa(o.MaxScale.Wanted()),
"run.googleapis.com/cloudsql-instances": o.CloudSQLInstances.Wanted(),
"run.googleapis.com/execution-environment": o.ExecutionEnvironment.Wanted(),
"run.googleapis.com/cpu-throttling": cpuThrottling,
},
},
Spec: &run.RevisionSpec{
Expand Down
12 changes: 8 additions & 4 deletions gcp/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ type Image struct {
Source fields.StringInputField
SourceHash fields.StringInputField

Pull bool `state:"-"`
Pull bool `state:"-"`
PullAuth bool `state:"-"`
}

func (o *Image) ReferenceID() string {
Expand Down Expand Up @@ -171,9 +172,12 @@ func (o *Image) push(ctx context.Context, meta interface{}) error {

if o.Pull {
// Pull image from source.
reader, err := cli.ImagePull(ctx, o.Source.Wanted(), dockertypes.ImagePullOptions{
RegistryAuth: authStr,
})
pullOpts := dockertypes.ImagePullOptions{}
if o.PullAuth {
pullOpts.RegistryAuth = authStr
}

reader, err := cli.ImagePull(ctx, o.Source.Wanted(), pullOpts)
if err != nil {
return err
}
Expand Down

0 comments on commit 2e07ac9

Please sign in to comment.