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 1de35f5
Show file tree
Hide file tree
Showing 5 changed files with 85 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 @@ -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,26 @@ 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 {
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
9 changes: 9 additions & 0 deletions internal/controller/controlplane/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,10 @@ 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"
crclient "sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/log"

cpv1beta1 "github.com/k0sproject/k0smotron/api/controlplane/v1beta1"
Expand Down Expand Up @@ -185,6 +187,13 @@ func (c *K0sController) updateStatus(ctx context.Context, kcp *cpv1beta1.K0sCont
kcp.Status.ControlPlaneReady = true
kcp.Status.Inititalized = true

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

return nil

}
20 changes: 18 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,21 @@ 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().
Resource("clusters").
Name("docker-test").
Namespace("default").
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
37 changes: 34 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,34 @@ func (s *CAPIDockerSuite) checkControlPlaneStatus(ctx context.Context, rc *rest.
s.Require().NoError(err)
}

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

crdConfig := *rc
crdConfig.ContentConfig.GroupVersion = &controlplanev1beta1.GroupVersion
crdConfig.APIPath = "/apis"
crdConfig.NegotiatedSerializer = serializer.NewCodecFactory(scheme.Scheme)
crdConfig.UserAgent = rest.DefaultKubernetesUserAgent()
crdRestClient, err := rest.UnversionedRESTClientFor(&crdConfig)
s.Require().NoError(err)

// nolint:staticcheck
err = wait.PollImmediateUntilWithContext(ctx, 1*time.Second, func(ctx context.Context) (bool, error) {
var cluster v1beta1.Cluster
err = crdRestClient.
Get().
Resource("clusters").
Name("docker-test").
Namespace("default").
Do(ctx).
Into(&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 +269,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 +310,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 +329,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 1de35f5

Please sign in to comment.