Skip to content

Commit

Permalink
CAPIM: Enable update for coreDNS and kube-proxy
Browse files Browse the repository at this point in the history
Signed-off-by: killianmuldoon <[email protected]>
  • Loading branch information
killianmuldoon committed Jun 23, 2023
1 parent 6073e52 commit c85ce8e
Show file tree
Hide file tree
Showing 6 changed files with 185 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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{}
Expand Down Expand Up @@ -753,6 +756,117 @@ func (r *InMemoryMachineReconciler) reconcileNormalKubeadmObjects(ctx context.Co
return ctrl.Result{}, nil
}

func (r *InMemoryMachineReconciler) reconcileNormalKubeProxy(ctx context.Context, cluster *clusterv1.Cluster, machine *clusterv1.Machine, _ *infrav1.InMemoryMachine) (ctrl.Result, error) {
// No-op if the machine is not a control plane machine.
if !util.IsControlPlaneMachine(machine) {
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 kube-proxy-daemonset
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: fmt.Sprintf("registry.k8s.io/kube-proxy:%s", *machine.Spec.Version),
},
},
},
},
},
}
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, machine *clusterv1.Machine, _ *infrav1.InMemoryMachine) (ctrl.Result, error) {
// No-op if the machine is not a control plane machine.
if !util.IsControlPlaneMachine(machine) {
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 coredns configMap.
corednsConfigMap := &corev1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{
Namespace: metav1.NamespaceSystem,
Name: "coredns",
},
Data: map[string]string{
"Corefile": "ANG",
},
}
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")
}
}
// Create the coredns deployment.
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: "registry.k8s.io/coredns/coredns:v1.10.1",
},
},
},
},
},
}

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) reconcileDelete(ctx context.Context, cluster *clusterv1.Cluster, machine *clusterv1.Machine, inMemoryMachine *infrav1.InMemoryMachine) (ctrl.Result, error) {
// Call the inner reconciliation methods.
phases := []func(ctx context.Context, cluster *clusterv1.Cluster, machine *clusterv1.Machine, inMemoryMachine *infrav1.InMemoryMachine) (ctrl.Result, error){
Expand Down
58 changes: 58 additions & 0 deletions test/infrastructure/inmemory/internal/server/api/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
},
},
},
}

Expand Down Expand Up @@ -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: "",
},
},
}
)
11 changes: 11 additions & 0 deletions test/infrastructure/inmemory/internal/server/api/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions test/infrastructure/inmemory/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit c85ce8e

Please sign in to comment.