certificates.go 5.7 KB

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