Skip to content

Commit

Permalink
Merge pull request #789 from makhov/ownership-fix
Browse files Browse the repository at this point in the history
Fix machine implementation updating
  • Loading branch information
makhov authored Nov 6, 2024
2 parents 79ae497 + 6564ae6 commit f2eef3d
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 29 deletions.
2 changes: 0 additions & 2 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ import (
"fmt"
"os"

// Import all Kubernetes client auth plugins (e.g. Azure, GCP, OIDC, etc.)
// to ensure that exec-entrypoint and run can make use of them.
_ "k8s.io/client-go/plugin/pkg/client/auth"
"k8s.io/client-go/tools/clientcmd"

Expand Down
49 changes: 45 additions & 4 deletions internal/controller/controlplane/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"strings"
"time"

"github.com/imdario/mergo"
"github.com/k0sproject/version"
corev1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand All @@ -20,7 +22,6 @@ import (
"sigs.k8s.io/controller-runtime/pkg/log"

cpv1beta1 "github.com/k0sproject/k0smotron/api/controlplane/v1beta1"
"github.com/k0sproject/version"
)

func (c *K0sController) createMachine(ctx context.Context, name string, cluster *clusterv1.Cluster, kcp *cpv1beta1.K0sControlPlane, infraRef corev1.ObjectReference) (*clusterv1.Machine, error) {
Expand Down Expand Up @@ -102,12 +103,52 @@ func (c *K0sController) createMachineFromTemplate(ctx context.Context, name stri
return nil, err
}

if err = c.Client.Patch(ctx, machineFromTemplate, client.Apply, &client.PatchOptions{
FieldManager: "k0smotron",
}); err != nil {
existingMachineFromTemplate := &unstructured.Unstructured{}
existingMachineFromTemplate.SetAPIVersion(machineFromTemplate.GetAPIVersion())
existingMachineFromTemplate.SetKind(machineFromTemplate.GetKind())
err = c.Get(ctx, client.ObjectKey{Namespace: machineFromTemplate.GetNamespace(), Name: machineFromTemplate.GetName()}, existingMachineFromTemplate)
if err != nil {
if apierrors.IsNotFound(err) {
if err = c.Client.Patch(ctx, machineFromTemplate, client.Apply, &client.PatchOptions{
FieldManager: "k0smotron",
}); err != nil {
return nil, fmt.Errorf("error apply patching: %w", err)
}
return machineFromTemplate, nil
}

return nil, fmt.Errorf("error getting machine implementation: %w", err)
}

err = mergo.Merge(existingMachineFromTemplate, machineFromTemplate, mergo.WithSliceDeepCopy)
if err != nil {
return nil, err
}

spec, _, _ := unstructured.NestedMap(existingMachineFromTemplate.Object, "spec")
patch := unstructured.Unstructured{Object: map[string]interface{}{
"spec": spec,
}}
data, err := patch.MarshalJSON()
if err != nil {
return nil, err
}

pluralName := ""
resList, _ := c.ClientSet.Discovery().ServerResourcesForGroupVersion(existingMachineFromTemplate.GetAPIVersion())
for _, apiRes := range resList.APIResources {
if apiRes.Kind == existingMachineFromTemplate.GetKind() && !strings.Contains(apiRes.Name, "/") {
pluralName = apiRes.Name
break
}
}
req := c.ClientSet.RESTClient().Patch(types.MergePatchType).
Body(data).
AbsPath("apis", machineFromTemplate.GetAPIVersion(), "namespaces", machineFromTemplate.GetNamespace(), pluralName, machineFromTemplate.GetName())
_, err = req.DoRaw(ctx)
if err != nil {
return nil, fmt.Errorf("error patching: %w", err)
}
return machineFromTemplate, nil
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,7 @@ func (s *CAPIControlPlaneDockerDownScalingSuite) TestCAPIControlPlaneDockerDownS
s.T().Log("cluster objects applied, waiting for cluster to be ready")

var localPort int
// 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) {
localPort, _ = getLBPort("docker-test-lb")
return localPort > 0, nil
})
Expand All @@ -106,8 +105,7 @@ func (s *CAPIControlPlaneDockerDownScalingSuite) TestCAPIControlPlaneDockerDownS
kmcKC, err := util.GetKMCClientSet(s.ctx, s.client, "docker-test", "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 @@ -118,8 +116,7 @@ func (s *CAPIControlPlaneDockerDownScalingSuite) TestCAPIControlPlaneDockerDownS
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) {
err = wait.PollUntilContextCancel(s.ctx, 1*time.Second, true, func(ctx context.Context) (bool, error) {
nodeName := fmt.Sprintf("docker-test-%d", i)
output, err := exec.Command("docker", "exec", nodeName, "k0s", "status").Output()
if err != nil {
Expand All @@ -134,8 +131,7 @@ func (s *CAPIControlPlaneDockerDownScalingSuite) TestCAPIControlPlaneDockerDownS
s.T().Log("waiting for node to be ready")
s.Require().NoError(util.WaitForNodeReadyStatus(s.ctx, kmcKC, "docker-test-worker-0", corev1.ConditionTrue))

// 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) {
result, err := kmcKC.RESTClient().Get().AbsPath("/apis/autopilot.k0sproject.io/v1beta2/controlnodes").DoRaw(ctx)
if err != nil {
return false, err
Expand All @@ -150,28 +146,17 @@ func (s *CAPIControlPlaneDockerDownScalingSuite) TestCAPIControlPlaneDockerDownS
s.Require().NoError(err)

s.T().Log("scaling down control plane")

out, err := exec.Command("docker", "exec", "docker-test-1", "k0s", "etcd", "leave").CombinedOutput()
s.T().Log(string(out))
s.Require().NoError(err)

out, err = exec.Command("docker", "exec", "docker-test-2", "k0s", "etcd", "leave").CombinedOutput()
s.T().Log(string(out))
s.Require().NoError(err)

s.updateClusterObjects()
// nolint:staticcheck
err = wait.PollImmediateUntilWithContext(s.ctx, 1*time.Second, func(ctx context.Context) (bool, error) {
output, err := exec.Command("docker", "exec", "docker-test-0", "k0s", "etcd", "member-list").CombinedOutput()

err = wait.PollUntilContextCancel(s.ctx, 1*time.Second, true, func(ctx context.Context) (bool, error) {
ids, err := util.GetControlPlaneNodesIDs("docker-test")
if err != nil {
return false, nil
}

return !strings.Contains(string(output), "docker-test-1"), nil
return len(ids) == 1, nil
})
s.Require().NoError(err)

s.Require().NoError(util.WaitForNodeReadyStatus(s.ctx, kmcKC, "docker-test-worker-0", corev1.ConditionTrue))
}

func (s *CAPIControlPlaneDockerDownScalingSuite) applyClusterObjects() {
Expand Down

0 comments on commit f2eef3d

Please sign in to comment.