func()

in custom-targets/helm/helm-deployer/render.go [87:144]


func (r *renderer) render(ctx context.Context) (*clouddeploy.RenderResult, error) {
	fmt.Printf("Downloading render input archive to %s and unarchiving to %s\n", srcArchivePath, srcPath)
	inURI, err := r.req.DownloadAndUnarchiveInput(ctx, r.gcsClient, srcArchivePath, srcPath)
	if err != nil {
		return nil, fmt.Errorf("unable to download and unarchive render input: %v", err)
	}
	fmt.Printf("Downloaded render input archive from %s\n", inURI)

	// If template lookup or template validatation is enabled then connect to the cluster at render time.
	if r.params.templateLookup || r.params.templateValidate {
		fmt.Printf("Helm template lookup or validate enabled. Setting up cluster credentials for %s\n", r.params.gkeCluster)
		if _, err := gcloudClusterCredentials(r.params.gkeCluster); err != nil {
			return nil, fmt.Errorf("unable to set up cluster credentials: %v", err)
		}
		fmt.Printf("Finished setting up cluster credentials for %s\n", r.params.gkeCluster)
	}

	// Use the pipeline ID as the helm release since this should be consistent.
	helmRelease := r.req.Pipeline
	chartPath := determineChartPath(r.params)
	hOpts := helmOptions{namespace: r.params.namespace}
	templateOut, err := helmTemplate(helmRelease, chartPath, &helmTemplateOptions{helmOptions: hOpts, lookup: r.params.templateLookup, validate: r.params.templateValidate})
	if err != nil {
		return nil, fmt.Errorf("error running helm template: %v", err)
	}

	tBytes, err := time.Now().MarshalText()
	if err != nil {
		return nil, fmt.Errorf("unable to marshal current time: %v", err)
	}
	// Add a comment at the top of the manifest indicating that it's not used at deploy time.
	manifest := []byte(fmt.Sprintf("# Manifest generated at %s by helm template.\n# This manifest is not used when performing the deploy, instead the same helm chart used to produce this manifest is provided to helm upgrade.\n", tBytes))
	manifest = append(manifest, templateOut...)

	fmt.Println("Uploading manifest from helm template")
	mURI, err := r.req.UploadArtifact(ctx, r.gcsClient, "manifest.yaml", &clouddeploy.GCSUploadContent{Data: manifest})
	if err != nil {
		return nil, fmt.Errorf("error uploading manifest: %v", err)
	}
	fmt.Printf("Uploaded manifest from helm template to %s\n", mURI)

	fmt.Println("Uploading archived helm configuration for use at deploy time")
	ahURI, err := r.req.UploadArtifact(ctx, r.gcsClient, renderedArchiveName, &clouddeploy.GCSUploadContent{LocalPath: srcArchivePath})
	if err != nil {
		return nil, fmt.Errorf("error uploading archived helm configuration: %v", err)
	}
	fmt.Printf("Uploaded archived helm configuration to %s\n", ahURI)

	rr := &clouddeploy.RenderResult{
		ResultStatus: clouddeploy.RenderSucceeded,
		ManifestFile: mURI,
		Metadata: map[string]string{
			clouddeploy.CustomTargetSourceMetadataKey:    helmDeployerSampleName,
			clouddeploy.CustomTargetSourceSHAMetadataKey: clouddeploy.GitCommit,
		},
	}
	return rr, nil
}