Skip to content

Commit 8ef5ea2

Browse files
authored
helm: fix kubeadm bugs caused by CoreDNS installation (#3353)
* helm: rename CoreDNS configmap * upgrade-agent: ignore CoreDNS preflight errors * fixup! helm: rename CoreDNS configmap
1 parent e077eaf commit 8ef5ea2

File tree

10 files changed

+77
-10
lines changed

10 files changed

+77
-10
lines changed

cli/internal/cmd/apply.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -428,6 +428,9 @@ func (a *applyCmd) apply(
428428
if err := a.runHelmApply(cmd, conf, stateFile, upgradeDir); err != nil {
429429
return err
430430
}
431+
if err := a.applier.CleanupCoreDNSResources(cmd.Context()); err != nil {
432+
return fmt.Errorf("cleaning up CoreDNS: %w", err)
433+
}
431434
}
432435

433436
// Upgrade node image
@@ -847,6 +850,7 @@ type applier interface {
847850
// methods required to install/upgrade Helm charts
848851

849852
AnnotateCoreDNSResources(context.Context) error
853+
CleanupCoreDNSResources(context.Context) error
850854
PrepareHelmCharts(
851855
flags helm.Options, state *state.State, serviceAccURI string, masterSecret uri.MasterSecret,
852856
) (helm.Applier, bool, error)

cli/internal/cmd/apply_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -554,6 +554,7 @@ func (s *stubConstellApplier) Init(context.Context, atls.Validator, *state.State
554554

555555
type helmApplier interface {
556556
AnnotateCoreDNSResources(context.Context) error
557+
CleanupCoreDNSResources(ctx context.Context) error
557558
PrepareHelmCharts(
558559
flags helm.Options, stateFile *state.State, serviceAccURI string, masterSecret uri.MasterSecret,
559560
) (

cli/internal/cmd/init_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,10 @@ func (s stubHelmApplier) AnnotateCoreDNSResources(_ context.Context) error {
282282
return nil
283283
}
284284

285+
func (s stubHelmApplier) CleanupCoreDNSResources(_ context.Context) error {
286+
return nil
287+
}
288+
285289
func (s stubHelmApplier) PrepareHelmCharts(
286290
_ helm.Options, _ *state.State, _ string, _ uri.MasterSecret,
287291
) (helm.Applier, bool, error) {

cli/internal/cmd/upgradeapply_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -379,6 +379,10 @@ func (m *mockApplier) AnnotateCoreDNSResources(_ context.Context) error {
379379
return nil
380380
}
381381

382+
func (m *mockApplier) CleanupCoreDNSResources(_ context.Context) error {
383+
return nil
384+
}
385+
382386
func (m *mockApplier) PrepareHelmCharts(
383387
helmOpts helm.Options, stateFile *state.State, str string, masterSecret uri.MasterSecret,
384388
) (helm.Applier, bool, error) {

internal/constellation/helm.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,21 @@ func (a *Applier) AnnotateCoreDNSResources(ctx context.Context) error {
6060
return nil
6161
}
6262

63+
// CleanupCoreDNSResources removes CoreDNS resources that are not managed by Helm.
64+
//
65+
// This is only required when CoreDNS was installed by kubeadm directly.
66+
// TODO(burgerdev): remove after v2.19 is released.
67+
func (a *Applier) CleanupCoreDNSResources(ctx context.Context) error {
68+
err := a.dynamicClient.
69+
Resource(schema.GroupVersionResource{Group: "", Version: "v1", Resource: "configmaps"}).
70+
Namespace("kube-system").
71+
Delete(ctx, "coredns", v1.DeleteOptions{})
72+
if !k8serrors.IsNotFound(err) {
73+
return err
74+
}
75+
return nil
76+
}
77+
6378
// PrepareHelmCharts loads Helm charts for Constellation and returns an executor to apply them.
6479
func (a *Applier) PrepareHelmCharts(
6580
flags helm.Options, state *state.State, serviceAccURI string, masterSecret uri.MasterSecret,

internal/constellation/helm/charts/coredns/templates/configmap.yaml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,4 @@
1-
21
apiVersion: v1
3-
kind: ConfigMap
4-
metadata:
5-
name: coredns
6-
namespace: kube-system
72
data:
83
Corefile: |
94
.:53 {
@@ -26,3 +21,8 @@ data:
2621
reload
2722
loadbalance
2823
}
24+
kind: ConfigMap
25+
metadata:
26+
creationTimestamp: null
27+
name: edg-coredns
28+
namespace: kube-system

internal/constellation/helm/charts/coredns/templates/deployment.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,6 @@ spec:
104104
items:
105105
- key: Corefile
106106
path: Corefile
107-
name: coredns
107+
name: edg-coredns
108108
name: config-volume
109109
status: {}

internal/constellation/helm/corednsgen/corednsgen.go

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ import (
2929
"sigs.k8s.io/yaml"
3030
)
3131

32+
const configMapName = "edg-coredns"
33+
3234
var chartDir = flag.String("charts", "./charts", "target directory to create charts in")
3335

3436
func main() {
@@ -44,9 +46,9 @@ func main() {
4446
writeTemplate(kubedns.CoreDNSServiceAccount, "serviceaccount.yaml")
4547
writeTemplate(kubedns.CoreDNSClusterRole, "clusterrole.yaml")
4648
writeTemplate(kubedns.CoreDNSClusterRoleBinding, "clusterrolebinding.yaml")
47-
writeTemplate(kubedns.CoreDNSConfigMap, "configmap.yaml")
4849
writeTemplate(kubedns.CoreDNSService, "service.yaml")
4950

51+
writeFileRelativeToChartDir(patchedConfigMap(), "templates", "configmap.yaml")
5052
writeFileRelativeToChartDir(patchedDeployment(), "templates", "deployment.yaml")
5153
}
5254

@@ -92,7 +94,25 @@ func valuesYAML() []byte {
9294
return data
9395
}
9496

95-
// patchedDeployment extracts the CoreDNS deployment from kubeadm and adds necessary tolerations.
97+
// patchedConfigMap renames the CoreDNS ConfigMap such that kubeadm does not find it.
98+
//
99+
// See https://github.com/kubernetes/kubeadm/issues/2846#issuecomment-1899942683.
100+
func patchedConfigMap() []byte {
101+
var cm corev1.ConfigMap
102+
if err := yaml.Unmarshal(parseTemplate(kubedns.CoreDNSConfigMap), &cm); err != nil {
103+
log.Fatalf("Could not parse configmap: %v", err)
104+
}
105+
106+
cm.Name = configMapName
107+
108+
out, err := yaml.Marshal(cm)
109+
if err != nil {
110+
log.Fatalf("Could not marshal patched deployment: %v", err)
111+
}
112+
return out
113+
}
114+
115+
// patchedDeployment extracts the CoreDNS Deployment from kubeadm, adds necessary tolerations and updates the ConfigMap reference.
96116
func patchedDeployment() []byte {
97117
var d appsv1.Deployment
98118
if err := yaml.Unmarshal(parseTemplate(kubedns.CoreDNSDeployment), &d); err != nil {
@@ -104,6 +124,14 @@ func patchedDeployment() []byte {
104124
{Key: "node.kubernetes.io/unreachable", Operator: corev1.TolerationOpExists, Effect: corev1.TaintEffectNoExecute, TolerationSeconds: toPtr(int64(10))},
105125
}
106126
d.Spec.Template.Spec.Tolerations = append(d.Spec.Template.Spec.Tolerations, tolerations...)
127+
128+
for i, vol := range d.Spec.Template.Spec.Volumes {
129+
if vol.ConfigMap != nil {
130+
vol.ConfigMap.Name = configMapName
131+
}
132+
d.Spec.Template.Spec.Volumes[i] = vol
133+
}
134+
107135
out, err := yaml.Marshal(d)
108136
if err != nil {
109137
log.Fatalf("Could not marshal patched deployment: %v", err)

terraform-provider-constellation/internal/provider/cluster_resource.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1311,6 +1311,11 @@ func (r *ClusterResource) applyHelmCharts(ctx context.Context, applier *constell
13111311
diags.AddError("Applying Helm charts", err.Error())
13121312
return diags
13131313
}
1314+
1315+
if err := applier.CleanupCoreDNSResources(ctx); err != nil {
1316+
diags.AddError("Cleaning up CoreDNS resources", err.Error())
1317+
return diags
1318+
}
13141319
return diags
13151320
}
13161321

upgrade-agent/internal/server/server.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,12 +111,18 @@ func (s *Server) ExecuteUpdate(ctx context.Context, updateRequest *upgradeproto.
111111
return nil, status.Errorf(codes.Internal, "unable to install the kubeadm binary: %s", err)
112112
}
113113

114-
upgradeCmd := exec.CommandContext(ctx, "kubeadm", "upgrade", "plan", updateRequest.WantedKubernetesVersion)
114+
// CoreDNS addon status is checked even though we did not install it.
115+
// TODO(burgerdev): Use kubeadm phases once supported: https://github.com/kubernetes/kubeadm/issues/1318.
116+
commonArgs := []string{"--ignore-preflight-errors", "CoreDNSMigration,CoreDNSUnsupportedPlugins", updateRequest.WantedKubernetesVersion}
117+
planArgs := append([]string{"upgrade", "plan"}, commonArgs...)
118+
applyArgs := append([]string{"upgrade", "apply", "--yes", "--patches", constants.KubeadmPatchDir}, commonArgs...)
119+
120+
upgradeCmd := exec.CommandContext(ctx, "kubeadm", planArgs...)
115121
if out, err := upgradeCmd.CombinedOutput(); err != nil {
116122
return nil, status.Errorf(codes.Internal, "unable to execute kubeadm upgrade plan %s: %s: %s", updateRequest.WantedKubernetesVersion, err, string(out))
117123
}
118124

119-
applyCmd := exec.CommandContext(ctx, "kubeadm", "upgrade", "apply", "--yes", "--patches", constants.KubeadmPatchDir, updateRequest.WantedKubernetesVersion)
125+
applyCmd := exec.CommandContext(ctx, "kubeadm", applyArgs...)
120126
if out, err := applyCmd.CombinedOutput(); err != nil {
121127
return nil, status.Errorf(codes.Internal, "unable to execute kubeadm upgrade apply: %s: %s", err, string(out))
122128
}

0 commit comments

Comments
 (0)