client_ca_hook.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /*
  2. Copyright 2017 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. package master
  14. import (
  15. "encoding/json"
  16. "fmt"
  17. "time"
  18. corev1 "k8s.io/api/core/v1"
  19. apiequality "k8s.io/apimachinery/pkg/api/equality"
  20. apierrors "k8s.io/apimachinery/pkg/api/errors"
  21. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  22. utilruntime "k8s.io/apimachinery/pkg/util/runtime"
  23. "k8s.io/apimachinery/pkg/util/wait"
  24. genericapiserver "k8s.io/apiserver/pkg/server"
  25. corev1client "k8s.io/client-go/kubernetes/typed/core/v1"
  26. )
  27. type ClientCARegistrationHook struct {
  28. ClientCA []byte
  29. RequestHeaderUsernameHeaders []string
  30. RequestHeaderGroupHeaders []string
  31. RequestHeaderExtraHeaderPrefixes []string
  32. RequestHeaderCA []byte
  33. RequestHeaderAllowedNames []string
  34. }
  35. func (h ClientCARegistrationHook) PostStartHook(hookContext genericapiserver.PostStartHookContext) error {
  36. // initializing CAs is important so that aggregated API servers can come up with "normal" config.
  37. // We've seen lagging etcd before, so we want to retry this a few times before we decide to crashloop
  38. // the API server on it.
  39. err := wait.Poll(1*time.Second, 30*time.Second, func() (done bool, err error) {
  40. // retry building the config since sometimes the server can be in an in-between state which caused
  41. // some kind of auto detection failure as I recall from other post start hooks.
  42. // TODO see if this is still true and fix the RBAC one too if it isn't.
  43. client, err := corev1client.NewForConfig(hookContext.LoopbackClientConfig)
  44. if err != nil {
  45. utilruntime.HandleError(err)
  46. return false, nil
  47. }
  48. return h.tryToWriteClientCAs(client)
  49. })
  50. // if we're never able to make it through initialization, kill the API server
  51. if err != nil {
  52. return fmt.Errorf("unable to initialize client CA configmap: %v", err)
  53. }
  54. return nil
  55. }
  56. // tryToWriteClientCAs is here for unit testing with a fake client. This is a wait.ConditionFunc so the bool
  57. // indicates if the condition was met. True when its finished, false when it should retry.
  58. func (h ClientCARegistrationHook) tryToWriteClientCAs(client corev1client.CoreV1Interface) (bool, error) {
  59. if err := createNamespaceIfNeeded(client, metav1.NamespaceSystem); err != nil {
  60. utilruntime.HandleError(err)
  61. return false, nil
  62. }
  63. data := map[string]string{}
  64. if len(h.ClientCA) > 0 {
  65. data["client-ca-file"] = string(h.ClientCA)
  66. }
  67. if len(h.RequestHeaderCA) > 0 {
  68. var err error
  69. // encoding errors aren't going to get better, so just fail on them.
  70. data["requestheader-username-headers"], err = jsonSerializeStringSlice(h.RequestHeaderUsernameHeaders)
  71. if err != nil {
  72. return false, err
  73. }
  74. data["requestheader-group-headers"], err = jsonSerializeStringSlice(h.RequestHeaderGroupHeaders)
  75. if err != nil {
  76. return false, err
  77. }
  78. data["requestheader-extra-headers-prefix"], err = jsonSerializeStringSlice(h.RequestHeaderExtraHeaderPrefixes)
  79. if err != nil {
  80. return false, err
  81. }
  82. data["requestheader-client-ca-file"] = string(h.RequestHeaderCA)
  83. data["requestheader-allowed-names"], err = jsonSerializeStringSlice(h.RequestHeaderAllowedNames)
  84. if err != nil {
  85. return false, err
  86. }
  87. }
  88. // write errors may work next time if we retry, so queue for retry
  89. if err := writeConfigMap(client, "extension-apiserver-authentication", data); err != nil {
  90. utilruntime.HandleError(err)
  91. return false, nil
  92. }
  93. return true, nil
  94. }
  95. func jsonSerializeStringSlice(in []string) (string, error) {
  96. out, err := json.Marshal(in)
  97. if err != nil {
  98. return "", err
  99. }
  100. return string(out), err
  101. }
  102. func writeConfigMap(client corev1client.ConfigMapsGetter, name string, data map[string]string) error {
  103. existing, err := client.ConfigMaps(metav1.NamespaceSystem).Get(name, metav1.GetOptions{})
  104. if apierrors.IsNotFound(err) {
  105. _, err := client.ConfigMaps(metav1.NamespaceSystem).Create(&corev1.ConfigMap{
  106. ObjectMeta: metav1.ObjectMeta{Namespace: metav1.NamespaceSystem, Name: name},
  107. Data: data,
  108. })
  109. return err
  110. }
  111. if err != nil {
  112. return err
  113. }
  114. if !apiequality.Semantic.DeepEqual(existing.Data, data) {
  115. existing.Data = data
  116. _, err = client.ConfigMaps(metav1.NamespaceSystem).Update(existing)
  117. }
  118. return err
  119. }