func DetermineRequest()

in custom-targets/util/clouddeploy/clouddeploy.go [295:395]


func DetermineRequest(ctx context.Context, gcsClient *storage.Client, supportedFeatures []string) (interface{}, error) {
	// Values present for render and deploy.
	project := os.Getenv(ProjectEnvKey)
	location := os.Getenv(LocationEnvKey)
	pipeline := os.Getenv(PipelineEnvKey)
	release := os.Getenv(ReleaseEnvKey)
	target := os.Getenv(TargetEnvKey)
	phase := os.Getenv(PhaseEnvKey)
	percentage, err := strconv.Atoi(os.Getenv(PercentageEnvKey))
	if err != nil {
		return nil, fmt.Errorf("failed to parse %q", PercentageEnvKey)
	}
	storageType := os.Getenv(StorageTypeEnvKey)
	inputGCSPath := os.Getenv(InputGCSEnvKey)
	outputGCSPath := os.Getenv(OutputGCSEnvKey)

	workloadType := os.Getenv(WorkloadTypeEnvKey)
	var cbWorkload CloudBuildWorkload
	if workloadType == "CB" {
		cbWorkload = CloudBuildWorkload{
			ServiceAccount: os.Getenv(CloudBuildServiceAccount),
			WorkerPool:     os.Getenv(CloudBuildWorkerPool),
		}
	}

	features := strings.FieldsFunc(os.Getenv(FeaturesEnvKey), func(c rune) bool {
		return c == ','
	})

	reqType := os.Getenv(RequestTypeEnvKey)
	switch reqType {
	case "RENDER":
		rr := &RenderRequest{
			Project:        project,
			Location:       location,
			Pipeline:       pipeline,
			Release:        release,
			Target:         target,
			Phase:          phase,
			Percentage:     percentage,
			StorageType:    storageType,
			InputGCSPath:   inputGCSPath,
			OutputGCSPath:  outputGCSPath,
			WorkloadType:   workloadType,
			WorkloadCBInfo: cbWorkload,
		}

		for _, f := range features {
			if !isFeatureSupported(supportedFeatures, f) {
				msg := fmt.Sprintf("feature %q is not supported", f)
				_, err := rr.UploadResult(ctx, gcsClient, &RenderResult{
					ResultStatus:   RenderNotSupported,
					FailureMessage: msg,
				})
				if err != nil {
					return nil, fmt.Errorf("error uploading render feature not supported results: %v", err)
				}
				return nil, fmt.Errorf(msg)
			}
		}
		return rr, nil

	case "DEPLOY":
		dr := &DeployRequest{
			Project:         project,
			Location:        location,
			Pipeline:        pipeline,
			Release:         release,
			Rollout:         os.Getenv(RolloutEnvKey),
			Target:          target,
			Phase:           phase,
			Percentage:      percentage,
			StorageType:     storageType,
			InputGCSPath:    inputGCSPath,
			SkaffoldGCSPath: os.Getenv(SkaffoldGCSEnvKey),
			ManifestGCSPath: os.Getenv(ManifestGCSEnvKey),
			OutputGCSPath:   outputGCSPath,
			WorkloadType:    workloadType,
			WorkloadCBInfo:  cbWorkload,
		}

		for _, f := range features {
			if !isFeatureSupported(supportedFeatures, f) {
				msg := fmt.Sprintf("feature %q is not supported", f)
				_, err := dr.UploadResult(ctx, gcsClient, &DeployResult{
					ResultStatus:   DeployNotSupported,
					FailureMessage: msg,
				})
				if err != nil {
					return nil, fmt.Errorf("error uploading deploy feature not supported results: %v", err)
				}
				return nil, fmt.Errorf(msg)
			}
		}

		return dr, nil

	default:
		return nil, fmt.Errorf("received unexpected Cloud Deploy request type: %v", reqType)
	}
}