Skip to content

Commit

Permalink
tests: add worker label through go.
Browse files Browse the repository at this point in the history
libvirt e2e tests did not properly label worker nodes with the worker
label.

This creates a function in test/provisioner/common.go that adds the
worker label to the libvirt e2e createCluster.

Fixes: #1107

Signed-off-by: Derek Lee <derlee@redhat.com>
  • Loading branch information
Derek Lee authored and wainersm committed Jul 26, 2023
1 parent 2d35e53 commit 2e9a872
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 0 deletions.
54 changes: 54 additions & 0 deletions test/provisioner/common.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// (C) Copyright Confidential Containers Contributors
// SPDX-License-Identifier: Apache-2.0

package provisioner

import (
"context"
"encoding/json"
"fmt"
"strings"

corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/e2e-framework/klient/k8s"
"sigs.k8s.io/e2e-framework/pkg/envconf"
)

type patchLabel struct {
Op string `json:"op"`
Path string `json:"path"`
Value string `json:"value"`
}

// Adds the worker label to all workers nodes in a given cluster
func AddNodeRoleWorkerLabel(ctx context.Context, clusterName string, cfg *envconf.Config) error {
fmt.Printf("Adding worker label to nodes belonging to: %s\n", clusterName)
client, err := cfg.NewClient()
if err != nil {
return err
}

nodelist := &corev1.NodeList{}
if err := client.Resources().List(ctx, nodelist); err != nil {
return err
}
// Use full path to avoid overwriting other labels (see RFC 6902)
payload := []patchLabel{{
Op: "add",
// "/" must be written as ~1 (see RFC 6901)
Path: "/metadata/labels/node.kubernetes.io~1worker",
Value: "",
}}
payloadBytes, _ := json.Marshal(payload)
workerStr := clusterName + "-worker"
for _, node := range nodelist.Items {
if strings.Contains(node.Name, workerStr) {
if err := client.Resources().Patch(ctx, &node, k8s.Patch{PatchType: types.JSONPatchType, Data: payloadBytes}); err != nil {
return err
}
}

}
return nil
}
5 changes: 5 additions & 0 deletions test/provisioner/provision_libvirt.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,11 @@ func (l *LibvirtProvisioner) CreateCluster(ctx context.Context, cfg *envconf.Con
kubeconfig := path.Join(home, ".kcli/clusters", clusterName, "auth/kubeconfig")
cfg.WithKubeconfigFile(kubeconfig)

if err := AddNodeRoleWorkerLabel(ctx, clusterName, cfg); err != nil {

return fmt.Errorf("labeling nodes: %w", err)
}

return nil
}

Expand Down

0 comments on commit 2e9a872

Please sign in to comment.