You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

secret_injector.go 3.3 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /*
  2. Copyright 2021 The KubeEdge 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. package runtime
  14. import (
  15. "context"
  16. "encoding/json"
  17. "fmt"
  18. v1 "k8s.io/api/core/v1"
  19. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  20. "k8s.io/client-go/kubernetes"
  21. )
  22. const (
  23. S3EndpointKey = "s3-endpoint"
  24. S3UseHTTPSKey = "s3-usehttps"
  25. // env name
  26. S3EndpointURLEnv = "S3_ENDPOINT_URL"
  27. AccessKeyID = "ACCESS_KEY_ID"
  28. // env name
  29. AccessKeyIDEnv = "ACCESS_KEY_ID"
  30. SecretAccessKey = "SECRET_ACCESS_KEY"
  31. // env name
  32. SecretAccessKeyEnv = "SECRET_ACCESS_KEY"
  33. SecretAnnotationKey = "sedna.io/credential"
  34. )
  35. func buildS3SecretEnvs(secret *v1.Secret) (envs []v1.EnvVar, err error) {
  36. if secret == nil {
  37. return
  38. }
  39. var s3Endpoint string
  40. if s3Endpoint = secret.Annotations[S3EndpointKey]; s3Endpoint == "" {
  41. err = fmt.Errorf("empty endpoint in secret %s", secret.Name)
  42. return
  43. }
  44. var s3EndpointURL string
  45. useHTTPS := true
  46. if httpsConf, ok := secret.Annotations[S3UseHTTPSKey]; ok && httpsConf == "0" {
  47. useHTTPS = false
  48. }
  49. if useHTTPS {
  50. s3EndpointURL = "https://" + s3Endpoint
  51. } else {
  52. s3EndpointURL = "http://" + s3Endpoint
  53. }
  54. envs = append(envs, v1.EnvVar{
  55. Name: S3EndpointURLEnv,
  56. Value: s3EndpointURL,
  57. })
  58. // Better to use secretKeyRef or EnvFrom for this.
  59. // But now(2021/4/28) kubeedge does not support secretKeyRef for env value,
  60. // there is a open pr for this, https://github.com/kubeedge/kubeedge/pull/2230
  61. envs = append(envs,
  62. v1.EnvVar{
  63. Name: AccessKeyIDEnv,
  64. Value: string(secret.Data[AccessKeyID]),
  65. },
  66. )
  67. envs = append(envs,
  68. v1.EnvVar{
  69. Name: SecretAccessKeyEnv,
  70. Value: string(secret.Data[SecretAccessKey]),
  71. },
  72. )
  73. return
  74. }
  75. // MergeSecretEnvs merges two EnvVar list
  76. func MergeSecretEnvs(nowE, newE []v1.EnvVar, overwrite bool) []v1.EnvVar {
  77. existEnvNames := make(map[string]int)
  78. for i, e := range nowE {
  79. existEnvNames[e.Name] = i
  80. }
  81. for _, e := range newE {
  82. if idx, exist := existEnvNames[e.Name]; !exist {
  83. nowE = append(nowE, e)
  84. existEnvNames[e.Name] = len(nowE) - 1
  85. } else {
  86. if overwrite {
  87. nowE[idx] = e
  88. }
  89. }
  90. }
  91. return nowE
  92. }
  93. func InjectSecretAnnotations(client kubernetes.Interface, obj CommonInterface, secretName string) (err error) {
  94. if len(secretName) == 0 {
  95. return
  96. }
  97. secret, err := client.CoreV1().Secrets(obj.GetNamespace()).Get(context.TODO(), secretName, metav1.GetOptions{})
  98. if err != nil {
  99. return
  100. }
  101. return injectSecretObj(obj, secret)
  102. }
  103. func injectSecretObj(obj CommonInterface, secret *v1.Secret) (err error) {
  104. secretData := secret.GetAnnotations()
  105. for k, v := range secret.Data {
  106. // v already decoded
  107. secretData[k] = string(v)
  108. }
  109. b, _ := json.Marshal(secretData)
  110. ann := obj.GetAnnotations()
  111. if ann == nil {
  112. ann = make(map[string]string)
  113. }
  114. ann[SecretAnnotationKey] = string(b)
  115. obj.SetAnnotations(ann)
  116. return nil
  117. }