preflight.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /*
  2. Copyright 2019 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 upgrade
  14. import (
  15. "os"
  16. "github.com/coredns/corefile-migration/migration"
  17. "github.com/pkg/errors"
  18. "k8s.io/apimachinery/pkg/util/sets"
  19. clientset "k8s.io/client-go/kubernetes"
  20. "k8s.io/klog"
  21. kubeadmapi "k8s.io/kubernetes/cmd/kubeadm/app/apis/kubeadm"
  22. kubeadmconstants "k8s.io/kubernetes/cmd/kubeadm/app/constants"
  23. "k8s.io/kubernetes/cmd/kubeadm/app/phases/addons/dns"
  24. "k8s.io/kubernetes/cmd/kubeadm/app/preflight"
  25. )
  26. // CoreDNSCheck validates installed kubelet version
  27. type CoreDNSCheck struct {
  28. name string
  29. client clientset.Interface
  30. f func(clientset.Interface) error
  31. }
  32. // Name is part of the preflight.Checker interface
  33. func (c CoreDNSCheck) Name() string {
  34. return c.name
  35. }
  36. // Check is part of the preflight.Checker interface
  37. func (c CoreDNSCheck) Check() (warnings, errors []error) {
  38. if err := c.f(c.client); err != nil {
  39. return nil, []error{err}
  40. }
  41. return nil, nil
  42. }
  43. // RunCoreDNSMigrationCheck initializes checks related to CoreDNS migration.
  44. func RunCoreDNSMigrationCheck(client clientset.Interface, ignorePreflightErrors sets.String, dnsType kubeadmapi.DNSAddOnType) error {
  45. if dnsType != kubeadmapi.CoreDNS {
  46. return nil
  47. }
  48. migrationChecks := []preflight.Checker{
  49. &CoreDNSCheck{
  50. name: "CoreDNSUnsupportedPlugins",
  51. client: client,
  52. f: checkUnsupportedPlugins,
  53. },
  54. &CoreDNSCheck{
  55. name: "CoreDNSMigration",
  56. client: client,
  57. f: checkMigration,
  58. },
  59. }
  60. return preflight.RunChecks(migrationChecks, os.Stderr, ignorePreflightErrors)
  61. }
  62. // checkUnsupportedPlugins checks if there are any plugins included in the current configuration
  63. // that are unsupported for migration.
  64. func checkUnsupportedPlugins(client clientset.Interface) error {
  65. klog.V(1).Infoln("validating if there are any unsupported CoreDNS plugins in the Corefile")
  66. _, corefile, currentInstalledCoreDNSversion, err := dns.GetCoreDNSInfo(client)
  67. if err != nil {
  68. return err
  69. }
  70. unsupportedCoreDNS, err := migration.Unsupported(currentInstalledCoreDNSversion, kubeadmconstants.CoreDNSVersion, corefile)
  71. if err != nil {
  72. return err
  73. }
  74. if unsupportedCoreDNS != nil {
  75. var UnsupportedPlugins, UnsupportedVersion string
  76. for _, unsup := range unsupportedCoreDNS {
  77. UnsupportedPlugins = unsup.Plugin
  78. UnsupportedVersion = unsup.Version
  79. }
  80. if UnsupportedPlugins != "" || UnsupportedVersion != "" {
  81. return errors.New("there are unsupported plugins in the CoreDNS Corefile")
  82. }
  83. }
  84. return nil
  85. }
  86. // checkMigration validates if migration of the current CoreDNS ConfigMap is possible.
  87. func checkMigration(client clientset.Interface) error {
  88. klog.V(1).Infoln("validating if migration can be done for the current CoreDNS release.")
  89. _, corefile, currentInstalledCoreDNSversion, err := dns.GetCoreDNSInfo(client)
  90. if err != nil {
  91. return err
  92. }
  93. _, err = migration.Migrate(currentInstalledCoreDNSversion, kubeadmconstants.CoreDNSVersion, corefile, false)
  94. if err != nil {
  95. return errors.Wrap(err, "CoreDNS will not be upgraded")
  96. }
  97. return nil
  98. }