From 18bc71bf6c6ce7872b752942859586be23ce5592 Mon Sep 17 00:00:00 2001 From: killianmuldoon Date: Wed, 21 Jun 2023 15:57:50 +0100 Subject: [PATCH] CAPIM: Enable update for coreDNS and kube-proxy Signed-off-by: killianmuldoon --- .../controllers/inmemorymachine_controller.go | 116 ++++++++++++++++++ .../inmemory/internal/server/api/const.go | 58 +++++++++ .../inmemory/internal/server/api/handler.go | 11 ++ test/infrastructure/inmemory/main.go | 2 + .../clusterclass-in-memory-quick-start.yaml | 5 - 5 files changed, 187 insertions(+), 5 deletions(-) diff --git a/test/infrastructure/inmemory/internal/controllers/inmemorymachine_controller.go b/test/infrastructure/inmemory/internal/controllers/inmemorymachine_controller.go index 5786c0459bdc..de1d7a2e9299 100644 --- a/test/infrastructure/inmemory/internal/controllers/inmemorymachine_controller.go +++ b/test/infrastructure/inmemory/internal/controllers/inmemorymachine_controller.go @@ -26,6 +26,7 @@ import ( "time" "github.com/pkg/errors" + appsv1 "k8s.io/api/apps/v1" corev1 "k8s.io/api/core/v1" rbacv1 "k8s.io/api/rbac/v1" apierrors "k8s.io/apimachinery/pkg/api/errors" @@ -215,6 +216,8 @@ func (r *InMemoryMachineReconciler) reconcileNormal(ctx context.Context, cluster r.reconcileNormalScheduler, r.reconcileNormalControllerManager, r.reconcileNormalKubeadmObjects, + r.reconcileNormalKubeProxy, + r.reconcileNormalCoreDNS, } res := ctrl.Result{} @@ -235,6 +238,119 @@ func (r *InMemoryMachineReconciler) reconcileNormal(ctx context.Context, cluster return res, kerrors.NewAggregate(errs) } +func (r *InMemoryMachineReconciler) reconcileNormalKubeProxy(ctx context.Context, cluster *clusterv1.Cluster, _ *clusterv1.Machine, inMemoryMachine *infrav1.InMemoryMachine) (ctrl.Result, error) { + // No-op if the Node is not provisioned yet + if !conditions.IsTrue(inMemoryMachine, infrav1.NodeProvisionedCondition) { + return ctrl.Result{}, nil + } + + // TODO: Add provisioning time for KubeProxy. + + // Compute the resource group unique name. + // NOTE: We are using reconcilerGroup also as a name for the listener for sake of simplicity. + resourceGroup := klog.KObj(cluster).String() + cloudClient := r.CloudManager.GetResourceGroup(resourceGroup).GetClient() + + // Create the apiserver pod + + kubeProxyDaemonSet := &appsv1.DaemonSet{ + ObjectMeta: metav1.ObjectMeta{ + Namespace: metav1.NamespaceSystem, + Name: "kube-proxy", + Labels: map[string]string{ + "component": "kube-proxy", + }, + }, + Spec: appsv1.DaemonSetSpec{ + Template: corev1.PodTemplateSpec{ + Spec: corev1.PodSpec{ + Containers: []corev1.Container{ + { + Name: "kube-proxy", + Image: "k8s.io/kube-proxy:v1.27.1", + }, + }, + }, + }, + }, + } + if err := cloudClient.Get(ctx, client.ObjectKeyFromObject(kubeProxyDaemonSet), kubeProxyDaemonSet); err != nil { + if !apierrors.IsNotFound(err) { + return ctrl.Result{}, errors.Wrapf(err, "failed to get kube-proxy DaemonSet") + } + + if err := cloudClient.Create(ctx, kubeProxyDaemonSet); err != nil && !apierrors.IsAlreadyExists(err) { + return ctrl.Result{}, errors.Wrapf(err, "failed to create kube-proxy DaemonSet") + } + } + return ctrl.Result{}, nil +} + +func (r *InMemoryMachineReconciler) reconcileNormalCoreDNS(ctx context.Context, cluster *clusterv1.Cluster, _ *clusterv1.Machine, inMemoryMachine *infrav1.InMemoryMachine) (ctrl.Result, error) { + // No-op if the Node is not provisioned yet + if !conditions.IsTrue(inMemoryMachine, infrav1.NodeProvisionedCondition) { + return ctrl.Result{}, nil + } + + // TODO: Add provisioning time for CoreDNS. + + // Compute the resource group unique name. + // NOTE: We are using reconcilerGroup also as a name for the listener for sake of simplicity. + resourceGroup := klog.KObj(cluster).String() + cloudClient := r.CloudManager.GetResourceGroup(resourceGroup).GetClient() + + // Create the apiserver pod + + coreDNSConfigMap := &corev1.ConfigMap{ + ObjectMeta: metav1.ObjectMeta{ + Namespace: metav1.NamespaceSystem, + Name: "coredns", + }, + Data: map[string]string{ + "Corefile": "ANG", + }, + } + coreDNSDeployment := &appsv1.Deployment{ + ObjectMeta: metav1.ObjectMeta{ + Namespace: metav1.NamespaceSystem, + Name: "coredns", + }, + Spec: appsv1.DeploymentSpec{ + Template: corev1.PodTemplateSpec{ + Spec: corev1.PodSpec{ + Containers: []corev1.Container{ + { + Name: "coredns", + Image: "k8s.io/coredns:v1.27.1", + }, + }, + }, + }, + }, + } + if err := cloudClient.Get(ctx, client.ObjectKeyFromObject(coreDNSConfigMap), coreDNSConfigMap); err != nil { + if !apierrors.IsNotFound(err) { + return ctrl.Result{}, errors.Wrapf(err, "failed to get coreDNS configMap") + } + + if err := cloudClient.Create(ctx, coreDNSConfigMap); err != nil && !apierrors.IsAlreadyExists(err) { + return ctrl.Result{}, errors.Wrapf(err, "failed to create coreDNS configMap") + } + } + + if err := cloudClient.Get(ctx, client.ObjectKeyFromObject(coreDNSDeployment), coreDNSDeployment); err != nil { + if !apierrors.IsNotFound(err) { + return ctrl.Result{}, errors.Wrapf(err, "failed to get coreDNS deployment") + } + + if err := cloudClient.Create(ctx, coreDNSDeployment); err != nil && !apierrors.IsAlreadyExists(err) { + return ctrl.Result{}, errors.Wrapf(err, "failed to create coreDNS deployment") + } + } + return ctrl.Result{}, nil + +} + func (r *InMemoryMachineReconciler) reconcileNormalCloudMachine(ctx context.Context, cluster *clusterv1.Cluster, _ *clusterv1.Machine, inMemoryMachine *infrav1.InMemoryMachine) (ctrl.Result, error) { // Compute the resource group unique name. // NOTE: We are using reconcilerGroup also as a name for the listener for sake of simplicity. diff --git a/test/infrastructure/inmemory/internal/server/api/const.go b/test/infrastructure/inmemory/internal/server/api/const.go index b9b57bb6672b..32ea331fde95 100644 --- a/test/infrastructure/inmemory/internal/server/api/const.go +++ b/test/infrastructure/inmemory/internal/server/api/const.go @@ -122,6 +122,19 @@ var ( Version: "v1", }, }, + { + Name: "apps", + Versions: []metav1.GroupVersionForDiscovery{ + { + GroupVersion: "apps/v1", + Version: "v1", + }, + }, + PreferredVersion: metav1.GroupVersionForDiscovery{ + GroupVersion: "apps/v1", + Version: "v1", + }, + }, }, } @@ -200,4 +213,49 @@ var ( }, }, } + appsV1ResourceList = &metav1.APIResourceList{ + GroupVersion: "apps/v1", + APIResources: []metav1.APIResource{ + { + Name: "daemonsets", + SingularName: "daemonset", + Namespaced: true, + Kind: "DaemonSet", + Verbs: []string{ + "create", + "delete", + "deletecollection", + "get", + "list", + "patch", + "update", + "watch", + }, + ShortNames: []string{ + "ds", + }, + StorageVersionHash: "", + }, + { + Name: "deployments", + SingularName: "deployment", + Namespaced: true, + Kind: "Deployment", + Verbs: []string{ + "create", + "delete", + "deletecollection", + "get", + "list", + "patch", + "update", + "watch", + }, + ShortNames: []string{ + "deploy", + }, + StorageVersionHash: "", + }, + }, + } ) diff --git a/test/infrastructure/inmemory/internal/server/api/handler.go b/test/infrastructure/inmemory/internal/server/api/handler.go index 0516ba7eb8f7..009be73248b4 100644 --- a/test/infrastructure/inmemory/internal/server/api/handler.go +++ b/test/infrastructure/inmemory/internal/server/api/handler.go @@ -153,6 +153,14 @@ func (h *apiServerHandler) apisDiscovery(req *restful.Request, resp *restful.Res } return } + if req.PathParameter("group") == "apps" && req.PathParameter("version") == "v1" { + if err := resp.WriteEntity(appsV1ResourceList); err != nil { + _ = resp.WriteErrorString(http.StatusInternalServerError, err.Error()) + return + } + return + } + _ = resp.WriteErrorString(http.StatusInternalServerError, fmt.Sprintf("discovery info not defined for %s/%s", req.PathParameter("group"), req.PathParameter("version"))) return } @@ -552,6 +560,9 @@ func getAPIResourceList(req *restful.Request) *metav1.APIResourceList { if req.PathParameter("group") == "rbac.authorization.k8s.io" && req.PathParameter("version") == "v1" { return rbacv1APIResourceList } + if req.PathParameter("group") == "apps" && req.PathParameter("version") == "v1" { + return appsV1ResourceList + } return nil } return corev1APIResourceList diff --git a/test/infrastructure/inmemory/main.go b/test/infrastructure/inmemory/main.go index dea5a98f9033..43332aba4d60 100644 --- a/test/infrastructure/inmemory/main.go +++ b/test/infrastructure/inmemory/main.go @@ -25,6 +25,7 @@ import ( "time" "github.com/spf13/pflag" + appsv1 "k8s.io/api/apps/v1" corev1 "k8s.io/api/core/v1" rbacv1 "k8s.io/api/rbac/v1" "k8s.io/apimachinery/pkg/runtime" @@ -88,6 +89,7 @@ func init() { // scheme used for operating on the cloud resource. _ = cloudv1.AddToScheme(cloudScheme) _ = corev1.AddToScheme(cloudScheme) + _ = appsv1.AddToScheme(cloudScheme) _ = rbacv1.AddToScheme(cloudScheme) } diff --git a/test/infrastructure/inmemory/templates/clusterclass-in-memory-quick-start.yaml b/test/infrastructure/inmemory/templates/clusterclass-in-memory-quick-start.yaml index 5449ebd14790..c1dfec5292e8 100644 --- a/test/infrastructure/inmemory/templates/clusterclass-in-memory-quick-start.yaml +++ b/test/infrastructure/inmemory/templates/clusterclass-in-memory-quick-start.yaml @@ -6,11 +6,6 @@ spec: controlPlane: metadata: annotations: - # The in-memory provider currently does not support looking up coredns - # and kube-proxy information and leads to reconcile errors in KCP. - # With these annotations KCP will skip processing those steps. - controlplane.cluster.x-k8s.io/skip-coredns: "" - controlplane.cluster.x-k8s.io/skip-kube-proxy: "" machineInfrastructure: ref: apiVersion: infrastructure.cluster.x-k8s.io/v1alpha1