@@ -35,13 +35,15 @@ import (
35
35
clientset "k8s.io/client-go/kubernetes"
36
36
clientsetscheme "k8s.io/client-go/kubernetes/scheme"
37
37
"k8s.io/klog/v2"
38
+ "sigs.k8s.io/yaml"
38
39
39
40
kubeadmapi "k8s.io/kubernetes/cmd/kubeadm/app/apis/kubeadm"
40
41
kubeadmconstants "k8s.io/kubernetes/cmd/kubeadm/app/constants"
41
42
"k8s.io/kubernetes/cmd/kubeadm/app/images"
42
43
kubeadmutil "k8s.io/kubernetes/cmd/kubeadm/app/util"
43
44
"k8s.io/kubernetes/cmd/kubeadm/app/util/apiclient"
44
45
"k8s.io/kubernetes/cmd/kubeadm/app/util/image"
46
+ "k8s.io/kubernetes/cmd/kubeadm/app/util/patches"
45
47
)
46
48
47
49
const (
@@ -85,7 +87,7 @@ func deployedDNSReplicas(client clientset.Interface, replicas int32) (*int32, er
85
87
}
86
88
87
89
// EnsureDNSAddon creates the CoreDNS addon
88
- func EnsureDNSAddon (cfg * kubeadmapi.ClusterConfiguration , client clientset.Interface , out io.Writer , printManifest bool ) error {
90
+ func EnsureDNSAddon (cfg * kubeadmapi.ClusterConfiguration , client clientset.Interface , patchesDir string , out io.Writer , printManifest bool ) error {
89
91
var replicas * int32
90
92
var err error
91
93
if ! printManifest {
@@ -97,10 +99,10 @@ func EnsureDNSAddon(cfg *kubeadmapi.ClusterConfiguration, client clientset.Inter
97
99
var defaultReplicas int32 = coreDNSReplicas
98
100
replicas = & defaultReplicas
99
101
}
100
- return coreDNSAddon (cfg , client , replicas , out , printManifest )
102
+ return coreDNSAddon (cfg , client , replicas , patchesDir , out , printManifest )
101
103
}
102
104
103
- func coreDNSAddon (cfg * kubeadmapi.ClusterConfiguration , client clientset.Interface , replicas * int32 , out io.Writer , printManifest bool ) error {
105
+ func coreDNSAddon (cfg * kubeadmapi.ClusterConfiguration , client clientset.Interface , replicas * int32 , patchesDir string , out io.Writer , printManifest bool ) error {
104
106
// Get the YAML manifest
105
107
coreDNSDeploymentBytes , err := kubeadmutil .ParseTemplate (CoreDNSDeployment , struct {
106
108
DeploymentName , Image , ControlPlaneTaintKey string
@@ -115,6 +117,14 @@ func coreDNSAddon(cfg *kubeadmapi.ClusterConfiguration, client clientset.Interfa
115
117
return errors .Wrap (err , "error when parsing CoreDNS deployment template" )
116
118
}
117
119
120
+ // Apply patches to the CoreDNS Deployment
121
+ if len (patchesDir ) != 0 {
122
+ coreDNSDeploymentBytes , err = applyCoreDNSDeploymentPatches (coreDNSDeploymentBytes , patchesDir , out )
123
+ if err != nil {
124
+ return errors .Wrap (err , "could not apply patches to the CoreDNS Deployment" )
125
+ }
126
+ }
127
+
118
128
// Get the config file for CoreDNS
119
129
coreDNSConfigMapBytes , err := kubeadmutil .ParseTemplate (CoreDNSConfigMap , struct { DNSDomain , UpstreamNameserver , StubDomain string }{
120
130
DNSDomain : cfg .Networking .DNSDomain ,
@@ -377,3 +387,27 @@ func setCorefile(client clientset.Interface, coreDNSCorefileName string) error {
377
387
}
378
388
return nil
379
389
}
390
+
391
+ // applyCoreDNSDeploymentPatches reads patches from a directory and applies them over the input coreDNSDeploymentBytes
392
+ func applyCoreDNSDeploymentPatches (coreDNSDeploymentBytes []byte , patchesDir string , output io.Writer ) ([]byte , error ) {
393
+ patchManager , err := patches .GetPatchManagerForPath (patchesDir , patches .KnownTargets (), output )
394
+ if err != nil {
395
+ return nil , err
396
+ }
397
+
398
+ patchTarget := & patches.PatchTarget {
399
+ Name : patches .CoreDNSDeployment ,
400
+ StrategicMergePatchObject : apps.Deployment {},
401
+ Data : coreDNSDeploymentBytes ,
402
+ }
403
+ if err := patchManager .ApplyPatchesToTarget (patchTarget ); err != nil {
404
+ return nil , err
405
+ }
406
+
407
+ coreDNSDeploymentBytes , err = yaml .JSONToYAML (patchTarget .Data )
408
+ if err != nil {
409
+ return nil , err
410
+ }
411
+
412
+ return coreDNSDeploymentBytes , nil
413
+ }
0 commit comments