github.com/GoogleContainerTools/skaffold@v1.39.18/pkg/skaffold/kubernetes/manifest/util.go (about) 1 /* 2 Copyright 2020 The Skaffold Authors 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package manifest 18 19 import ( 20 "context" 21 "fmt" 22 "io" 23 "io/ioutil" 24 "os" 25 "path/filepath" 26 "strings" 27 28 "github.com/GoogleContainerTools/skaffold/pkg/skaffold/util" 29 ) 30 31 const ( 32 manifestsStagingFolder = "manifest_tmp" 33 renderedManifestsStagingFile = "rendered_manifest.yaml" 34 gcsPrefix = "gs://" 35 ) 36 37 var ManifestTmpDir = filepath.Join(os.TempDir(), manifestsStagingFolder) 38 39 // Write writes manifests to a file, a writer or a GCS bucket. 40 func Write(manifests string, output string, manifestOut io.Writer) error { 41 switch { 42 case output == "": 43 _, err := fmt.Fprintln(manifestOut, manifests) 44 return err 45 case strings.HasPrefix(output, gcsPrefix): 46 tempDir, err := ioutil.TempDir("", manifestsStagingFolder) 47 if err != nil { 48 return writeErr(fmt.Errorf("failed to create the tmp directory: %w", err)) 49 } 50 defer os.RemoveAll(tempDir) 51 tempFile := filepath.Join(tempDir, renderedManifestsStagingFile) 52 if err := dumpToFile(manifests, tempFile); err != nil { 53 return err 54 } 55 gcs := util.Gsutil{} 56 if err := gcs.Copy(context.Background(), tempFile, output, false); err != nil { 57 return writeErr(fmt.Errorf("failed to copy rendered manifests to GCS: %w", err)) 58 } 59 return nil 60 default: 61 return dumpToFile(manifests, output) 62 } 63 } 64 65 func dumpToFile(manifests string, filepath string) error { 66 f, err := os.Create(filepath) 67 if err != nil { 68 return fmt.Errorf("opening file for writing manifests: %w", err) 69 } 70 defer f.Close() 71 _, err = f.WriteString(manifests + "\n") 72 return err 73 }