|
| 1 | +/* |
| 2 | +Copyright 2020 The Kubernetes Authors. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package equality |
| 18 | + |
| 19 | +import ( |
| 20 | + clusterv1 "sigs.k8s.io/cluster-api/api/v1alpha3" |
| 21 | + bootstrapv1 "sigs.k8s.io/cluster-api/bootstrap/kubeadm/api/v1alpha3" |
| 22 | + kubeadmv1 "sigs.k8s.io/cluster-api/bootstrap/kubeadm/types/v1beta1" |
| 23 | + "sigs.k8s.io/cluster-api/util/secret" |
| 24 | +) |
| 25 | + |
| 26 | +// SemanticMerge takes two KubeConfigSpecs and produces a third that is semantically equivalent to the other two |
| 27 | +// by way of merging non-behavior-changing fields from incoming into current. |
| 28 | +func SemanticMerge(current, incoming bootstrapv1.KubeadmConfigSpec, cluster *clusterv1.Cluster) bootstrapv1.KubeadmConfigSpec { |
| 29 | + merged := bootstrapv1.KubeadmConfigSpec{} |
| 30 | + current.DeepCopyInto(&merged) |
| 31 | + |
| 32 | + merged = mergeClusterConfiguration(merged, incoming, cluster) |
| 33 | + |
| 34 | + // Prefer non-nil init configurations: in most cases the init configuration is ignored and so this is purely representative |
| 35 | + if merged.InitConfiguration == nil && incoming.InitConfiguration != nil { |
| 36 | + merged.InitConfiguration = incoming.InitConfiguration.DeepCopy() |
| 37 | + } |
| 38 | + |
| 39 | + // The type meta for embedded types is currently ignored |
| 40 | + if merged.ClusterConfiguration != nil && incoming.ClusterConfiguration != nil { |
| 41 | + merged.ClusterConfiguration.TypeMeta = incoming.ClusterConfiguration.TypeMeta |
| 42 | + } |
| 43 | + |
| 44 | + if merged.InitConfiguration != nil && incoming.InitConfiguration != nil { |
| 45 | + merged.InitConfiguration.TypeMeta = incoming.InitConfiguration.TypeMeta |
| 46 | + } |
| 47 | + |
| 48 | + if merged.JoinConfiguration != nil && incoming.JoinConfiguration != nil { |
| 49 | + merged.JoinConfiguration.TypeMeta = incoming.JoinConfiguration.TypeMeta |
| 50 | + } |
| 51 | + |
| 52 | + // The default discovery injected by the kubeadm bootstrap controller is a unique, time-bounded token. However, any |
| 53 | + // valid token has the same effect of joining the machine to the cluster. We consider the following scenarios: |
| 54 | + // 1. current has no join configuration (meaning it was never reconciled) -> do nothing |
| 55 | + // 2. current has a bootstrap token, and incoming has some configured discovery mechanism -> prefer incoming |
| 56 | + // 3. current has a bootstrap token, and incoming has no discovery set -> delete current's discovery config |
| 57 | + // 4. in all other cases, prefer current's configuration |
| 58 | + if merged.JoinConfiguration == nil || merged.JoinConfiguration.Discovery.BootstrapToken == nil { |
| 59 | + return merged |
| 60 | + } |
| 61 | + |
| 62 | + // current has a bootstrap token, check incoming |
| 63 | + switch { |
| 64 | + case incoming.JoinConfiguration == nil: |
| 65 | + merged.JoinConfiguration.Discovery = kubeadmv1.Discovery{} |
| 66 | + case incoming.JoinConfiguration.Discovery.BootstrapToken != nil: |
| 67 | + fallthrough |
| 68 | + case incoming.JoinConfiguration.Discovery.File != nil: |
| 69 | + incoming.JoinConfiguration.Discovery.DeepCopyInto(&merged.JoinConfiguration.Discovery) |
| 70 | + default: |
| 71 | + // Neither token nor file is set on incoming's Discovery config |
| 72 | + merged.JoinConfiguration.Discovery = kubeadmv1.Discovery{} |
| 73 | + } |
| 74 | + |
| 75 | + return merged |
| 76 | +} |
| 77 | + |
| 78 | +func mergeClusterConfiguration(merged, incoming bootstrapv1.KubeadmConfigSpec, cluster *clusterv1.Cluster) bootstrapv1.KubeadmConfigSpec { |
| 79 | + if merged.ClusterConfiguration == nil && incoming.ClusterConfiguration != nil { |
| 80 | + merged.ClusterConfiguration = incoming.ClusterConfiguration.DeepCopy() |
| 81 | + } else if merged.ClusterConfiguration == nil { |
| 82 | + // incoming's cluster configuration is also nil |
| 83 | + return merged |
| 84 | + } |
| 85 | + |
| 86 | + // Attempt to reconstruct a cluster config in reverse of reconcileTopLevelObjectSettings |
| 87 | + newCfg := incoming.ClusterConfiguration |
| 88 | + if newCfg == nil { |
| 89 | + newCfg = &kubeadmv1.ClusterConfiguration{} |
| 90 | + } |
| 91 | + |
| 92 | + if merged.ClusterConfiguration.ControlPlaneEndpoint == cluster.Spec.ControlPlaneEndpoint.String() && |
| 93 | + newCfg.ControlPlaneEndpoint == "" { |
| 94 | + merged.ClusterConfiguration.ControlPlaneEndpoint = "" |
| 95 | + } |
| 96 | + |
| 97 | + if newCfg.ClusterName == "" { |
| 98 | + merged.ClusterConfiguration.ClusterName = "" |
| 99 | + } |
| 100 | + |
| 101 | + if cluster.Spec.ClusterNetwork != nil { |
| 102 | + if merged.ClusterConfiguration.Networking.DNSDomain == cluster.Spec.ClusterNetwork.ServiceDomain && newCfg.Networking.DNSDomain == "" { |
| 103 | + merged.ClusterConfiguration.Networking.DNSDomain = "" |
| 104 | + } |
| 105 | + |
| 106 | + if merged.ClusterConfiguration.Networking.ServiceSubnet == cluster.Spec.ClusterNetwork.Services.String() && |
| 107 | + newCfg.Networking.ServiceSubnet == "" { |
| 108 | + merged.ClusterConfiguration.Networking.ServiceSubnet = "" |
| 109 | + } |
| 110 | + |
| 111 | + if merged.ClusterConfiguration.Networking.PodSubnet == cluster.Spec.ClusterNetwork.Pods.String() && |
| 112 | + newCfg.Networking.PodSubnet == "" { |
| 113 | + merged.ClusterConfiguration.Networking.PodSubnet = "" |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + // We defer to other sources for the version, such as the Machine |
| 118 | + if newCfg.KubernetesVersion == "" { |
| 119 | + merged.ClusterConfiguration.KubernetesVersion = "" |
| 120 | + } |
| 121 | + |
| 122 | + if merged.ClusterConfiguration.CertificatesDir == secret.DefaultCertificatesDir && |
| 123 | + newCfg.CertificatesDir == "" { |
| 124 | + merged.ClusterConfiguration.CertificatesDir = "" |
| 125 | + } |
| 126 | + |
| 127 | + return merged |
| 128 | +} |
0 commit comments