k8s.io/kubernetes@v1.29.3/test/e2e/storage/drivers/csi_objects.go (about) 1 /* 2 Copyright 2018 The Kubernetes 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 // This file is used to deploy the CSI hostPath plugin 18 // More Information: https://github.com/kubernetes-csi/drivers/tree/master/pkg/hostpath 19 20 package drivers 21 22 import ( 23 "context" 24 "fmt" 25 "os" 26 "path" 27 "path/filepath" 28 29 v1 "k8s.io/api/core/v1" 30 apierrors "k8s.io/apimachinery/pkg/api/errors" 31 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 32 "k8s.io/apimachinery/pkg/util/uuid" 33 34 clientset "k8s.io/client-go/kubernetes" 35 "k8s.io/kubernetes/test/e2e/framework" 36 ) 37 38 func shredFile(filePath string) { 39 if _, err := os.Stat(filePath); os.IsNotExist(err) { 40 framework.Logf("File %v was not found, skipping shredding", filePath) 41 return 42 } 43 framework.Logf("Shredding file %v", filePath) 44 _, _, err := framework.RunCmd("shred", "--remove", filePath) 45 if err != nil { 46 framework.Logf("Failed to shred file %v: %v", filePath, err) 47 } 48 if _, err := os.Stat(filePath); os.IsNotExist(err) { 49 framework.Logf("File %v successfully shredded", filePath) 50 return 51 } 52 // Shred failed Try to remove the file for good measure 53 err = os.Remove(filePath) 54 framework.ExpectNoError(err, "Failed to remove service account file %s", filePath) 55 56 } 57 58 // createGCESecrets downloads the GCP IAM Key for the default compute service account 59 // and puts it in a secret for the GCE PD CSI Driver to consume 60 func createGCESecrets(client clientset.Interface, ns string) { 61 saEnv := "E2E_GOOGLE_APPLICATION_CREDENTIALS" 62 saFile := fmt.Sprintf("/tmp/%s/cloud-sa.json", string(uuid.NewUUID())) 63 64 os.MkdirAll(path.Dir(saFile), 0750) 65 defer os.Remove(path.Dir(saFile)) 66 67 premadeSAFile, ok := os.LookupEnv(saEnv) 68 if !ok { 69 framework.Logf("Could not find env var %v, please either create cloud-sa"+ 70 " secret manually or rerun test after setting %v to the filepath of"+ 71 " the GCP Service Account to give to the GCE Persistent Disk CSI Driver", saEnv, saEnv) 72 return 73 } 74 75 framework.Logf("Found CI service account key at %v", premadeSAFile) 76 // Need to copy it saFile 77 stdout, stderr, err := framework.RunCmd("cp", premadeSAFile, saFile) 78 framework.ExpectNoError(err, "error copying service account key: %s\nstdout: %s\nstderr: %s", err, stdout, stderr) 79 defer shredFile(saFile) 80 // Create Secret with this Service Account 81 fileBytes, err := os.ReadFile(saFile) 82 framework.ExpectNoError(err, "Failed to read file %v", saFile) 83 84 s := &v1.Secret{ 85 ObjectMeta: metav1.ObjectMeta{ 86 Name: "cloud-sa", 87 Namespace: ns, 88 }, 89 Type: v1.SecretTypeOpaque, 90 Data: map[string][]byte{ 91 filepath.Base(saFile): fileBytes, 92 }, 93 } 94 95 _, err = client.CoreV1().Secrets(ns).Create(context.TODO(), s, metav1.CreateOptions{}) 96 if !apierrors.IsAlreadyExists(err) { 97 framework.ExpectNoError(err, "Failed to create Secret %v", s.GetName()) 98 } 99 }