Skip to content

Commit

Permalink
chore(cloud run): Move revision template to separate function
Browse files Browse the repository at this point in the history
  • Loading branch information
mahad-akhter committed Nov 17, 2023
1 parent 65f910f commit 746859d
Showing 1 changed file with 51 additions and 44 deletions.
95 changes: 51 additions & 44 deletions internal/providers/gcp/cloudrun/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,34 @@ func (r *Resource) AllowAccessToAll(ctx context.Context) error {
func (r *Resource) GetServiceTemplate(ctx context.Context, wl *Workload) *runpb.Service {
activity.GetLogger(ctx).Info("setting service template for", "revision", r.Revision)

// unmarshaling the container here assuming that container definition will be specific to a resource
// this can be done at a common location if the container definition turns out to be same for all resources

revisionTemplate := r.GetRevisionTemplate(ctx, wl)

launchStageConfig := r.Config["launch_stage"].(string)
launchStage := api.LaunchStage(api.LaunchStage_value[launchStageConfig])

ingressConfig := r.Config["ingress"].(string)
ingress := runpb.IngressTraffic(runpb.IngressTraffic_value[ingressConfig])

service := &runpb.Service{
Template: revisionTemplate,
LaunchStage: launchStage,
Ingress: ingress,
}

tt := &runpb.TrafficTarget{
Type: runpb.TrafficTargetAllocationType_TRAFFIC_TARGET_ALLOCATION_TYPE_LATEST,
Percent: 100,
}
service.Traffic = []*runpb.TrafficTarget{tt}

return service
}

func (r *Resource) GetRevisionTemplate(ctx context.Context, wl *Workload) *runpb.RevisionTemplate {

templateConfig := r.Config["template"].(map[string]interface{})
templateContainersConfig := templateConfig["containers"].(map[string]interface{})
templateVpcAccessConfig := templateConfig["vpc_access"].(map[string]interface{})
Expand All @@ -232,8 +260,9 @@ func (r *Resource) GetServiceTemplate(ctx context.Context, wl *Workload) *runpb.
CpuIdle: cpuIdle,
}

// unmarshaling the container here assuming that container definition will be specific to a resource
// this can be done at a common location if the container definition turns out to be same for all resources
containerPortConfig := templateContainersConfig["ports"].(map[string]interface{})["container_port"].(string)
containerPort64bit, _ := strconv.ParseInt(containerPortConfig, 10, 32)
containerPort := &runpb.ContainerPort{ContainerPort: int32(containerPort64bit)}

Envs := []*runpb.EnvVar{}

Expand All @@ -247,24 +276,6 @@ func (r *Resource) GetServiceTemplate(ctx context.Context, wl *Workload) *runpb.
})
}

networkInterfaces := templateVpcAccessConfig["network_interfaces"].(map[string]interface{})
networkInterfaceArray := []*runpb.VpcAccess_NetworkInterface{
{
Network: fmt.Sprint(networkInterfaces["network"]),
Subnetwork: fmt.Sprint(networkInterfaces["subnetwork"]),
},
}

egress := templateVpcAccessConfig["egress"].(string)
vpcAccess := &runpb.VpcAccess{
Egress: runpb.VpcAccess_VpcEgress(runpb.VpcAccess_VpcEgress_value[egress]),
NetworkInterfaces: networkInterfaceArray,
}

containerPortConfig := templateContainersConfig["ports"].(map[string]interface{})["container_port"].(string)
containerPort64bit, _ := strconv.ParseInt(containerPortConfig, 10, 32)
containerPort := &runpb.ContainerPort{ContainerPort: int32(containerPort64bit)}

volumeMountsName := templateContainersConfig["volume_mounts"].(map[string]interface{})["name"].(string)
volumeMountsPath := templateContainersConfig["volume_mounts"].(map[string]interface{})["mount_path"].(string)
volumeMounts := &runpb.VolumeMount{
Expand Down Expand Up @@ -293,6 +304,24 @@ func (r *Resource) GetServiceTemplate(ctx context.Context, wl *Workload) *runpb.
MaxInstanceCount: int32(maxInstanceConfig64bit),
}

executionEnvConfig := templateConfig["execution_environment"].(string)
executionEnv := runpb.ExecutionEnvironment(
runpb.ExecutionEnvironment_value[executionEnvConfig])

networkInterfaces := templateVpcAccessConfig["network_interfaces"].(map[string]interface{})
networkInterfaceArray := []*runpb.VpcAccess_NetworkInterface{
{
Network: fmt.Sprint(networkInterfaces["network"]),
Subnetwork: fmt.Sprint(networkInterfaces["subnetwork"]),
},
}

egress := templateVpcAccessConfig["egress"].(string)
vpcAccess := &runpb.VpcAccess{
Egress: runpb.VpcAccess_VpcEgress(runpb.VpcAccess_VpcEgress_value[egress]),
NetworkInterfaces: networkInterfaceArray,
}

volumeName := templateConfig["volumes"].(map[string]interface{})["name"].(string)
volumeInstance := templateConfig["volumes"].(map[string]interface{})["cloud_sql_instance"].(map[string]interface{})["instances"].([]interface{})
VolumeInstanceArray := []string{}
Expand All @@ -310,11 +339,7 @@ func (r *Resource) GetServiceTemplate(ctx context.Context, wl *Workload) *runpb.
}
volumes := []*runpb.Volume{volume}

executionEnvConfig := templateConfig["execution_environment"].(string)
executionEnv := runpb.ExecutionEnvironment(
runpb.ExecutionEnvironment_value[executionEnvConfig])

rt := &runpb.RevisionTemplate{
revisionTemplate := &runpb.RevisionTemplate{
Containers: []*runpb.Container{container},
Scaling: scaling,
ExecutionEnvironment: executionEnv,
Expand All @@ -323,25 +348,7 @@ func (r *Resource) GetServiceTemplate(ctx context.Context, wl *Workload) *runpb.
Volumes: volumes,
}

launchStageConfig := r.Config["launch_stage"].(string)
launchStage := api.LaunchStage(api.LaunchStage_value[launchStageConfig])

ingressConfig := r.Config["ingress"].(string)
ingress := runpb.IngressTraffic(runpb.IngressTraffic_value[ingressConfig])

service := &runpb.Service{
Template: rt,
LaunchStage: launchStage,
Ingress: ingress,
}

tt := &runpb.TrafficTarget{
Type: runpb.TrafficTargetAllocationType_TRAFFIC_TARGET_ALLOCATION_TYPE_LATEST,
Percent: 100,
}
service.Traffic = []*runpb.TrafficTarget{tt}

return service
return revisionTemplate
}

func (r *Resource) GetParent() string {
Expand Down

0 comments on commit 746859d

Please sign in to comment.