github.com/GoogleContainerTools/skaffold/v2@v2.13.2/pkg/webhook/gcs/gcs.go (about) 1 /* 2 Copyright 2019 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 gcs 18 19 import ( 20 "bytes" 21 "context" 22 "fmt" 23 "io" 24 "time" 25 26 cstorage "cloud.google.com/go/storage" 27 appsv1 "k8s.io/api/apps/v1" 28 29 "github.com/GoogleContainerTools/skaffold/v2/pkg/webhook/constants" 30 "github.com/GoogleContainerTools/skaffold/v2/pkg/webhook/kubernetes" 31 ) 32 33 // UploadDeploymentLogsToBucket gets logs from d and uploads them to the bucket 34 func UploadDeploymentLogsToBucket(d *appsv1.Deployment, prNumber int) (string, error) { 35 c, err := cstorage.NewClient(context.Background()) 36 if err != nil { 37 return "", fmt.Errorf("creating GCS client: %w", err) 38 } 39 defer c.Close() 40 name := fmt.Sprintf("logs-%d-%d", prNumber, time.Now().UnixNano()) 41 w := c.Bucket(constants.LogsGCSBucket).Object(name).NewWriter(context.Background()) 42 defer w.Close() 43 if _, err := io.Copy(w, bytes.NewBuffer([]byte(kubernetes.Logs(d)))); err != nil { 44 return "", err 45 } 46 return name, nil 47 }