certificates.go 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /*
  2. Copyright 2016 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 app implements a server that runs a set of active
  14. // components. This includes replication controllers, service endpoints and
  15. // nodes.
  16. //
  17. package app
  18. import (
  19. "fmt"
  20. "os"
  21. "k8s.io/klog"
  22. "net/http"
  23. "k8s.io/apimachinery/pkg/runtime/schema"
  24. utilfeature "k8s.io/apiserver/pkg/util/feature"
  25. kubeoptions "k8s.io/kubernetes/cmd/kube-controller-manager/app/options"
  26. "k8s.io/kubernetes/pkg/controller/certificates/approver"
  27. "k8s.io/kubernetes/pkg/controller/certificates/cleaner"
  28. "k8s.io/kubernetes/pkg/controller/certificates/rootcacertpublisher"
  29. "k8s.io/kubernetes/pkg/controller/certificates/signer"
  30. "k8s.io/kubernetes/pkg/features"
  31. )
  32. func startCSRSigningController(ctx ControllerContext) (http.Handler, bool, error) {
  33. gvr := schema.GroupVersionResource{Group: "certificates.k8s.io", Version: "v1beta1", Resource: "certificatesigningrequests"}
  34. if !ctx.AvailableResources[gvr] {
  35. klog.Warningf("Resource %s is not available now", gvr.String())
  36. return nil, false, nil
  37. }
  38. if ctx.ComponentConfig.CSRSigningController.ClusterSigningCertFile == "" || ctx.ComponentConfig.CSRSigningController.ClusterSigningKeyFile == "" {
  39. klog.V(2).Info("skipping CSR signer controller because no csr cert/key was specified")
  40. return nil, false, nil
  41. }
  42. // Deprecation warning for old defaults.
  43. //
  44. // * If the signing cert and key are the default paths but the files
  45. // exist, warn that the paths need to be specified explicitly in a
  46. // later release and the defaults will be removed. We don't expect this
  47. // to be the case.
  48. //
  49. // * If the signing cert and key are default paths but the files don't exist,
  50. // bail out of startController without logging.
  51. var keyFileExists, keyUsesDefault, certFileExists, certUsesDefault bool
  52. _, err := os.Stat(ctx.ComponentConfig.CSRSigningController.ClusterSigningCertFile)
  53. certFileExists = !os.IsNotExist(err)
  54. certUsesDefault = (ctx.ComponentConfig.CSRSigningController.ClusterSigningCertFile == kubeoptions.DefaultClusterSigningCertFile)
  55. _, err = os.Stat(ctx.ComponentConfig.CSRSigningController.ClusterSigningKeyFile)
  56. keyFileExists = !os.IsNotExist(err)
  57. keyUsesDefault = (ctx.ComponentConfig.CSRSigningController.ClusterSigningKeyFile == kubeoptions.DefaultClusterSigningKeyFile)
  58. switch {
  59. case (keyFileExists && keyUsesDefault) || (certFileExists && certUsesDefault):
  60. klog.Warningf("You might be using flag defaulting for --cluster-signing-cert-file and" +
  61. " --cluster-signing-key-file. These defaults are deprecated and will be removed" +
  62. " in a subsequent release. Please pass these options explicitly.")
  63. case (!keyFileExists && keyUsesDefault) && (!certFileExists && certUsesDefault):
  64. // This is what we expect right now if people aren't
  65. // setting up the signing controller. This isn't
  66. // actually a problem since the signer is not a
  67. // required controller.
  68. klog.V(2).Info("skipping CSR signer controller because no csr cert/key was specified and the default files are missing")
  69. return nil, false, nil
  70. default:
  71. // Note that '!filesExist && !usesDefaults' is obviously
  72. // operator error. We don't handle this case here and instead
  73. // allow it to be handled by NewCSR... below.
  74. }
  75. c := ctx.ClientBuilder.ClientOrDie("certificate-controller")
  76. signer, err := signer.NewCSRSigningController(
  77. c,
  78. ctx.InformerFactory.Certificates().V1beta1().CertificateSigningRequests(),
  79. ctx.ComponentConfig.CSRSigningController.ClusterSigningCertFile,
  80. ctx.ComponentConfig.CSRSigningController.ClusterSigningKeyFile,
  81. ctx.ComponentConfig.CSRSigningController.ClusterSigningDuration.Duration,
  82. )
  83. if err != nil {
  84. return nil, false, fmt.Errorf("failed to start certificate controller: %v", err)
  85. }
  86. go signer.Run(1, ctx.Stop)
  87. return nil, true, nil
  88. }
  89. func startCSRApprovingController(ctx ControllerContext) (http.Handler, bool, error) {
  90. gvr := schema.GroupVersionResource{Group: "certificates.k8s.io", Version: "v1beta1", Resource: "certificatesigningrequests"}
  91. if !ctx.AvailableResources[gvr] {
  92. klog.Warningf("Resource %s is not available now", gvr.String())
  93. return nil, false, nil
  94. }
  95. approver := approver.NewCSRApprovingController(
  96. ctx.ClientBuilder.ClientOrDie("certificate-controller"),
  97. ctx.InformerFactory.Certificates().V1beta1().CertificateSigningRequests(),
  98. )
  99. go approver.Run(1, ctx.Stop)
  100. return nil, true, nil
  101. }
  102. func startCSRCleanerController(ctx ControllerContext) (http.Handler, bool, error) {
  103. cleaner := cleaner.NewCSRCleanerController(
  104. ctx.ClientBuilder.ClientOrDie("certificate-controller").CertificatesV1beta1().CertificateSigningRequests(),
  105. ctx.InformerFactory.Certificates().V1beta1().CertificateSigningRequests(),
  106. )
  107. go cleaner.Run(1, ctx.Stop)
  108. return nil, true, nil
  109. }
  110. func startRootCACertPublisher(ctx ControllerContext) (http.Handler, bool, error) {
  111. if !utilfeature.DefaultFeatureGate.Enabled(features.BoundServiceAccountTokenVolume) {
  112. return nil, false, nil
  113. }
  114. var (
  115. rootCA []byte
  116. err error
  117. )
  118. if ctx.ComponentConfig.SAController.RootCAFile != "" {
  119. if rootCA, err = readCA(ctx.ComponentConfig.SAController.RootCAFile); err != nil {
  120. return nil, true, fmt.Errorf("error parsing root-ca-file at %s: %v", ctx.ComponentConfig.SAController.RootCAFile, err)
  121. }
  122. } else {
  123. rootCA = ctx.ClientBuilder.ConfigOrDie("root-ca-cert-publisher").CAData
  124. }
  125. sac, err := rootcacertpublisher.NewPublisher(
  126. ctx.InformerFactory.Core().V1().ConfigMaps(),
  127. ctx.InformerFactory.Core().V1().Namespaces(),
  128. ctx.ClientBuilder.ClientOrDie("root-ca-cert-publisher"),
  129. rootCA,
  130. )
  131. if err != nil {
  132. return nil, true, fmt.Errorf("error creating root CA certificate publisher: %v", err)
  133. }
  134. go sac.Run(1, ctx.Stop)
  135. return nil, true, nil
  136. }