csi_objects.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /*
  2. Copyright 2018 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. // This file is used to deploy the CSI hostPath plugin
  14. // More Information: https://github.com/kubernetes-csi/drivers/tree/master/pkg/hostpath
  15. package drivers
  16. import (
  17. "fmt"
  18. "io/ioutil"
  19. "os"
  20. "path"
  21. "path/filepath"
  22. v1 "k8s.io/api/core/v1"
  23. apierrors "k8s.io/apimachinery/pkg/api/errors"
  24. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  25. "k8s.io/apimachinery/pkg/util/uuid"
  26. clientset "k8s.io/client-go/kubernetes"
  27. "k8s.io/kubernetes/test/e2e/framework"
  28. e2elog "k8s.io/kubernetes/test/e2e/framework/log"
  29. )
  30. func shredFile(filePath string) {
  31. if _, err := os.Stat(filePath); os.IsNotExist(err) {
  32. e2elog.Logf("File %v was not found, skipping shredding", filePath)
  33. return
  34. }
  35. e2elog.Logf("Shredding file %v", filePath)
  36. _, _, err := framework.RunCmd("shred", "--remove", filePath)
  37. if err != nil {
  38. e2elog.Logf("Failed to shred file %v: %v", filePath, err)
  39. }
  40. if _, err := os.Stat(filePath); os.IsNotExist(err) {
  41. e2elog.Logf("File %v successfully shredded", filePath)
  42. return
  43. }
  44. // Shred failed Try to remove the file for good meausure
  45. err = os.Remove(filePath)
  46. framework.ExpectNoError(err, "Failed to remove service account file %s", filePath)
  47. }
  48. // createGCESecrets downloads the GCP IAM Key for the default compute service account
  49. // and puts it in a secret for the GCE PD CSI Driver to consume
  50. func createGCESecrets(client clientset.Interface, ns string) {
  51. saEnv := "E2E_GOOGLE_APPLICATION_CREDENTIALS"
  52. saFile := fmt.Sprintf("/tmp/%s/cloud-sa.json", string(uuid.NewUUID()))
  53. os.MkdirAll(path.Dir(saFile), 0750)
  54. defer os.Remove(path.Dir(saFile))
  55. premadeSAFile, ok := os.LookupEnv(saEnv)
  56. if !ok {
  57. e2elog.Logf("Could not find env var %v, please either create cloud-sa"+
  58. " secret manually or rerun test after setting %v to the filepath of"+
  59. " the GCP Service Account to give to the GCE Persistent Disk CSI Driver", saEnv, saEnv)
  60. return
  61. }
  62. e2elog.Logf("Found CI service account key at %v", premadeSAFile)
  63. // Need to copy it saFile
  64. stdout, stderr, err := framework.RunCmd("cp", premadeSAFile, saFile)
  65. framework.ExpectNoError(err, "error copying service account key: %s\nstdout: %s\nstderr: %s", err, stdout, stderr)
  66. defer shredFile(saFile)
  67. // Create Secret with this Service Account
  68. fileBytes, err := ioutil.ReadFile(saFile)
  69. framework.ExpectNoError(err, "Failed to read file %v", saFile)
  70. s := &v1.Secret{
  71. ObjectMeta: metav1.ObjectMeta{
  72. Name: "cloud-sa",
  73. Namespace: ns,
  74. },
  75. Type: v1.SecretTypeOpaque,
  76. Data: map[string][]byte{
  77. filepath.Base(saFile): fileBytes,
  78. },
  79. }
  80. _, err = client.CoreV1().Secrets(ns).Create(s)
  81. if !apierrors.IsAlreadyExists(err) {
  82. framework.ExpectNoError(err, "Failed to create Secret %v", s.GetName())
  83. }
  84. }