Skip to content

Commit

Permalink
K0s ClusterID annotation added
Browse files Browse the repository at this point in the history
Signed-off-by: Alexey Makhov <amakhov@mirantis.com>
  • Loading branch information
makhov committed Dec 3, 2024
1 parent aef0261 commit 698c494
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 5 deletions.
2 changes: 2 additions & 0 deletions api/controlplane/v1beta1/groupversion_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,5 @@ var (
// AddToScheme adds the types in this group-version to the given scheme.
AddToScheme = SchemeBuilder.AddToScheme
)

const K0sClusterIDAnnotation = "k0sproject.io/cluster-id"
Original file line number Diff line number Diff line change
Expand Up @@ -151,11 +151,18 @@ func (c *K0sController) Reconcile(ctx context.Context, req ctrl.Request) (res ct
}
log.Info("Status updated successfully")

if kcp.Status.ControlPlaneReady {
if perr := c.Client.Patch(ctx, cluster, client.Merge); perr != nil {
err = fmt.Errorf("failed to patch cluster: %w", perr)
}
}

// Requeue the reconciliation if the status is not ready
if !kcp.Status.Ready {
log.Info("Requeuing reconciliation in 20sec since the control plane is not ready")
res = ctrl.Result{RequeueAfter: 20 * time.Second, Requeue: true}
}

}()

log = log.WithValues("cluster", cluster.Name)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,13 @@ import (
"reflect"
"time"

corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
"sigs.k8s.io/cluster-api/controllers/remote"

apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand Down Expand Up @@ -118,6 +120,29 @@ func (c *K0smotronController) Reconcile(ctx context.Context, req ctrl.Request) (
}
}

if ready {
remoteClient, err := remote.NewClusterClient(ctx, "k0smotron", c.Client, util.ObjectKey(cluster))
if err != nil {
return res, fmt.Errorf("failed to create remote client: %w", err)
}

ns := &corev1.Namespace{}
err = remoteClient.Get(ctx, types.NamespacedName{Name: "kube-system", Namespace: ""}, ns)
if err != nil {
if apierrors.IsNotFound(err) {
return ctrl.Result{Requeue: true, RequeueAfter: time.Second * 30}, nil
}
return res, fmt.Errorf("failed to get namespace: %w", err)
}

annotations.AddAnnotations(cluster, map[string]string{
cpv1beta1.K0sClusterIDAnnotation: fmt.Sprintf("kube-system:%s", ns.GetUID()),
})
if err := c.Client.Patch(ctx, cluster, client.Merge); err != nil {
return res, fmt.Errorf("failed to patch cluster: %w", err)
}
}

// TODO: We need to have bit more detailed status and conditions handling
kcp.Status.Ready = ready
kcp.Status.ExternalManagedControlPlane = true
Expand Down
6 changes: 6 additions & 0 deletions internal/controller/controlplane/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
"sigs.k8s.io/cluster-api/controllers/remote"
"sigs.k8s.io/cluster-api/util"
"sigs.k8s.io/cluster-api/util/annotations"
"sigs.k8s.io/cluster-api/util/collections"
"sigs.k8s.io/cluster-api/util/conditions"
"sigs.k8s.io/controller-runtime/pkg/log"
Expand Down Expand Up @@ -185,6 +186,11 @@ func (c *K0sController) updateStatus(ctx context.Context, kcp *cpv1beta1.K0sCont
kcp.Status.ControlPlaneReady = true
kcp.Status.Inititalized = true

// Set the k0s cluster ID annotation
annotations.AddAnnotations(cluster, map[string]string{
cpv1beta1.K0sClusterIDAnnotation: fmt.Sprintf("kube-system:%s", ns.GetUID()),
})

return nil

}
18 changes: 16 additions & 2 deletions inttest/capi-controlplane-docker/capi_controlplane_docker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@ package capicontolplanedocker
import (
"context"
"fmt"
controlplanev1beta1 "github.com/k0sproject/k0smotron/api/controlplane/v1beta1"
"os"
"os/exec"
"sigs.k8s.io/cluster-api/api/v1beta1"
"strconv"
"strings"
"testing"
Expand Down Expand Up @@ -102,8 +104,7 @@ func (s *CAPIControlPlaneDockerSuite) TestCAPIControlPlaneDocker() {
kmcKC, err := util.GetKMCClientSet(s.ctx, s.client, "docker-test-cluster", "default", localPort)
s.Require().NoError(err)

// nolint:staticcheck
err = wait.PollImmediateUntilWithContext(s.ctx, 1*time.Second, func(ctx context.Context) (bool, error) {
err = wait.PollUntilContextCancel(s.ctx, 1*time.Second, true, func(ctx context.Context) (bool, error) {
b, _ := s.client.RESTClient().
Get().
AbsPath("/healthz").
Expand All @@ -113,6 +114,19 @@ func (s *CAPIControlPlaneDockerSuite) TestCAPIControlPlaneDocker() {
})
s.Require().NoError(err)

err = wait.PollUntilContextCancel(s.ctx, 1*time.Second, true, func(ctx context.Context) (bool, error) {
var cluster v1beta1.Cluster
err = s.client.RESTClient().
Get().
AbsPath("/apis/cluster.x-k8s.io/v1beta1/namespaces/default/clusters/docker-test-cluster").
Do(ctx).
Into(&cluster)

clusterIDAnnotation, found := cluster.GetAnnotations()[controlplanev1beta1.K0sClusterIDAnnotation]
return found && strings.Contains(clusterIDAnnotation, "kube-system"), nil
})
s.Require().NoError(err)

for i := 0; i < 3; i++ {
// nolint:staticcheck
err = wait.PollImmediateUntilWithContext(s.ctx, 1*time.Second, func(ctx context.Context) (bool, error) {
Expand Down
27 changes: 24 additions & 3 deletions inttest/capi-docker/capi_docker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"fmt"
"os"
"os/exec"
"sigs.k8s.io/cluster-api/api/v1beta1"
"strings"
"testing"
"time"
Expand Down Expand Up @@ -100,6 +101,7 @@ func (s *CAPIDockerSuite) TestCAPIDocker() {
s.Require().NoError(util.WaitForStatefulSet(s.ctx, s.client, "kmc-docker-test", "default"))

s.checkControlPlaneStatus(s.ctx, s.restConfig)
s.checkClusterIDAnnotation(s.ctx, s.restConfig)

s.T().Log("Starting portforward")
fw, err := util.GetPortForwarder(s.restConfig, "kmc-docker-test-0", "default", 30443)
Expand Down Expand Up @@ -141,6 +143,7 @@ func (s *CAPIDockerSuite) TestCAPIDocker() {
extraFile, err := getDockerNodeFile("docker-test-0", "/tmp/test-file")
s.Require().NoError(err)
s.Require().Equal("test-file", extraFile)

}

func (s *CAPIDockerSuite) prepareCerts() {
Expand Down Expand Up @@ -200,6 +203,24 @@ func (s *CAPIDockerSuite) checkControlPlaneStatus(ctx context.Context, rc *rest.
s.Require().NoError(err)
}

func (s *CAPIDockerSuite) checkClusterIDAnnotation(ctx context.Context, rc *rest.Config) {

Check warning on line 206 in inttest/capi-docker/capi_docker_test.go

View workflow job for this annotation

GitHub Actions / Lint

unused-parameter: parameter 'rc' seems to be unused, consider removing or renaming it as _ (revive)
// nolint:staticcheck
err := wait.PollImmediateUntilWithContext(ctx, 1*time.Second, func(ctx context.Context) (bool, error) {
var cluster v1beta1.Cluster
_ = s.client.RESTClient().
Get().
AbsPath("/apis/cluster.x-k8s.io/v1beta1/namespaces/default/clusters/docker-test").
Do(ctx).
Into(&cluster)

s.T().Log(cluster)
clusterIDAnnotation, found := cluster.GetAnnotations()[controlplanev1beta1.K0sClusterIDAnnotation]
return found && strings.Contains(clusterIDAnnotation, "kube-system"), nil
})

s.Require().NoError(err)
}

func getDockerNodeFile(nodeName string, path string) (string, error) {
output, err := exec.Command("docker", "exec", nodeName, "cat", path).Output()
if err != nil {
Expand Down Expand Up @@ -238,7 +259,7 @@ kind: K0smotronControlPlane
metadata:
name: docker-test-cp
spec:
version: v1.27.2-k0s.0
version: v1.31.2-k0s.0
certificateRefs:
- name: docker-test-ca
type: ca
Expand Down Expand Up @@ -279,7 +300,7 @@ metadata:
name: docker-test-0
namespace: default
spec:
version: v1.27.1
version: v1.31.1
clusterName: docker-test
bootstrap:
configRef:
Expand All @@ -298,7 +319,7 @@ metadata:
namespace: default
spec:
# version is deliberately different to be able to verify we actually pick it up :)
version: v1.27.1+k0s.0
version: v1.31.1+k0s.0
args:
- --labels=k0sproject.io/foo=bar
preStartCommands:
Expand Down

0 comments on commit 698c494

Please sign in to comment.