func run()

in gke-deploy/cmd/run/run.go [103:173]


func run(_ *cobra.Command, options *options) error {
	ctx := context.Background()

	var im name.Reference
	if options.image != "" {
		ref, err := name.ParseReference(options.image)
		if err != nil {
			return err
		}
		im = ref
	}

	if options.filename == "" && options.image == "" {
		return fmt.Errorf("omitting -f|--filename requires -i|--image to be set")
	}
	if options.output == "" {
		return fmt.Errorf("value of -o|--output cannot be empty")
	}
	if options.clusterName != "" && options.clusterLocation == "" {
		return fmt.Errorf("you must set -l|--location flag because -c|--cluster flag is set")
	}
	if options.clusterLocation != "" && options.clusterName == "" {
		return fmt.Errorf("you must set -c|--cluster flag because -l|--location flag is set")
	}

	useGcloud := common.GcloudInPath()

	if options.exposePort < 0 {
		return fmt.Errorf("value of -x|--expose must be > 0")
	}
	if options.exposePort > 0 && options.appName == "" {
		return fmt.Errorf("exposing a deployed workload object requires -a|--app to be set")
	}

	if options.createApplicationCR && options.appName == "" {
		return fmt.Errorf("creating an Application CR requires -a|--app to be set")
	}

	labelsMap, err := common.CreateMapFromEqualDelimitedStrings(options.labels)
	if err != nil {
		return err
	}
	annotationsMap, err := common.CreateMapFromEqualDelimitedStrings(options.annotations)
	if err != nil {
		return err
	}
	applicationLinks, err := common.CreateApplicationLinksListFromEqualDelimitedStrings(options.applicationLinks)
	if err != nil {
		return err
	}
	useGsutil := common.UseGsutil(options.filename, options.output)
	d, err := common.CreateDeployer(ctx, useGsutil, useGcloud, options.verbose, options.serverDryRun)
	if err != nil {
		return err
	}

	expandedOutput := common.ExpandedOutputPath(options.output)
	if err := d.Prepare(ctx, im, options.appName, options.appVersion, options.filename, common.SuggestedOutputPath(options.output), expandedOutput, options.namespace, labelsMap, annotationsMap, options.exposePort, options.recursive, options.createApplicationCR, applicationLinks); err != nil {
		return fmt.Errorf("failed to prepare deployment: %v", err)
	}
	applyConfig := expandedOutput
	if strings.HasPrefix(options.output, "gs://") {
		// Without this, gsutil copies the entire expanded output directory, rather than just the files in the directory, which fails applying the deployment if the --recursive flag isn't set.
		applyConfig = applyConfig + "/*"
	}
	if err := d.Apply(ctx, options.clusterName, options.clusterLocation, options.clusterProject, applyConfig, options.namespace, options.waitTimeout, options.recursive); err != nil {
		return fmt.Errorf("failed to apply deployment: %v", err)
	}

	return nil
}