-
Notifications
You must be signed in to change notification settings - Fork 295
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: use a daemonset and kubectl exec to provide images to remote cl…
…usters
- Loading branch information
Showing
9 changed files
with
299 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 0 additions & 8 deletions
8
test/e2e/data/infrastructure-vsphere-govmomi/main/remote-management/image-injection.yaml
This file was deleted.
Oops, something went wrong.
8 changes: 0 additions & 8 deletions
8
test/e2e/data/infrastructure-vsphere-govmomi/main/remote-management/kustomization.yaml
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
/* | ||
Copyright 2024 The Kubernetes Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package framework | ||
|
||
import ( | ||
"context" | ||
|
||
. "github.com/onsi/gomega" | ||
appsv1 "k8s.io/api/apps/v1" | ||
"k8s.io/klog/v2" | ||
"sigs.k8s.io/cluster-api/test/framework" | ||
. "sigs.k8s.io/cluster-api/test/framework/ginkgoextensions" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
) | ||
|
||
// waitForDaemonSetAvailableInput is the input for WaitForDeploymentsAvailable. | ||
type waitForDaemonSetAvailableInput struct { | ||
Getter framework.Getter | ||
Daemonset *appsv1.DaemonSet | ||
} | ||
|
||
// waitForDaemonSetAvailable waits until the Deployment has status.Available = True, that signals that | ||
// all the desired replicas are in place. | ||
// This can be used to check if Cluster API controllers installed in the management cluster are working. | ||
// xref: https://github.com/kubernetes/kubernetes/blob/bfa4188/staging/src/k8s.io/kubectl/pkg/polymorphichelpers/rollout_status.go#L95 | ||
func waitForDaemonSetAvailable(ctx context.Context, input waitForDaemonSetAvailableInput, intervals ...interface{}) { | ||
Byf("Waiting for daemonset %s to be available", klog.KObj(input.Daemonset)) | ||
daemon := &appsv1.DaemonSet{} | ||
Eventually(func() bool { | ||
key := client.ObjectKey{ | ||
Namespace: input.Daemonset.GetNamespace(), | ||
Name: input.Daemonset.GetName(), | ||
} | ||
if err := input.Getter.Get(ctx, key, daemon); err != nil { | ||
return false | ||
} | ||
if daemon.Generation <= daemon.Status.ObservedGeneration { | ||
if daemon.Status.UpdatedNumberScheduled < daemon.Status.DesiredNumberScheduled { | ||
return false | ||
} | ||
if daemon.Status.NumberAvailable < daemon.Status.DesiredNumberScheduled { | ||
return false | ||
} | ||
return true | ||
} | ||
return false | ||
}, intervals...).Should(BeTrue()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,219 @@ | ||
/* | ||
Copyright 2024 The Kubernetes Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package framework | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"io" | ||
"os" | ||
"time" | ||
|
||
. "github.com/onsi/gomega" | ||
"github.com/pkg/errors" | ||
appsv1 "k8s.io/api/apps/v1" | ||
corev1 "k8s.io/api/core/v1" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
kerrors "k8s.io/apimachinery/pkg/util/errors" | ||
"k8s.io/apimachinery/pkg/util/httpstream" | ||
"k8s.io/client-go/kubernetes/scheme" | ||
"k8s.io/client-go/tools/remotecommand" | ||
"k8s.io/klog/v2" | ||
"k8s.io/utils/ptr" | ||
"sigs.k8s.io/cluster-api/test/framework" | ||
. "sigs.k8s.io/cluster-api/test/framework/ginkgoextensions" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil" | ||
) | ||
|
||
func LoadImagesFunc(ctx context.Context) func(clusterProxy framework.ClusterProxy) { | ||
sourceFile := os.Getenv("DOCKER_IMAGE_TAR") | ||
Expect(sourceFile).ToNot(BeEmpty(), "DOCKER_IMAGE_TAR must be set") | ||
|
||
loader := imagePreloader{ | ||
sourceFile: sourceFile, | ||
} | ||
|
||
return func(clusterProxy framework.ClusterProxy) { | ||
loader.loadImagesToCluster(ctx, clusterProxy) | ||
} | ||
} | ||
|
||
type imagePreloader struct { | ||
sourceFile string | ||
} | ||
|
||
// loadImagesToCluster deploys a privileged daemonset and uses it to stream-load container images. | ||
func (i imagePreloader) loadImagesToCluster(ctx context.Context, clusterProxy framework.ClusterProxy) { | ||
daemon, daemonMutateFn, daemonLabels := getPreloadDaemonset() | ||
ctrlClient := clusterProxy.GetClient() | ||
|
||
// Create Daemonset | ||
_, err := controllerutil.CreateOrPatch(ctx, ctrlClient, daemon, daemonMutateFn) | ||
Expect(err).ToNot(HaveOccurred()) | ||
|
||
// Wait for DaemonSet to be available. | ||
waitForDaemonSetAvailable(ctx, waitForDaemonSetAvailableInput{Getter: ctrlClient, Daemonset: daemon}, time.Minute*3, time.Second*10) | ||
|
||
// List all pods and load images via each found pod. | ||
pods := &corev1.PodList{} | ||
Expect(ctrlClient.List( | ||
ctx, | ||
pods, | ||
client.InNamespace(daemon.Namespace), | ||
client.MatchingLabels(daemonLabels), | ||
)).To(Succeed()) | ||
|
||
errs := []error{} | ||
for _, pod := range pods.Items { | ||
Byf("Loading images to node %s via pod %s", &pod.Spec.NodeName, klog.KObj(&pod)) | ||
if err := i.loadImagesViaPod(ctx, clusterProxy, pod.Namespace, pod.Name, pod.Spec.Containers[0].Name); err != nil { | ||
errs = append(errs, err) | ||
} | ||
} | ||
|
||
Expect(kerrors.NewAggregate(errs)).ToNot(HaveOccurred()) | ||
} | ||
|
||
func (i imagePreloader) loadImagesViaPod(ctx context.Context, clusterProxy framework.ClusterProxy, namespace, podName, containerName string) error { | ||
// Open source tar file. | ||
reader, writer := io.Pipe() | ||
file, err := os.Open(i.sourceFile) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// Use go routine to pipe source file content into then stdin. | ||
go func(file *os.File, writer io.WriteCloser) { | ||
defer writer.Close() | ||
defer file.Close() | ||
_, _ = io.Copy(writer, file) | ||
}(file, writer) | ||
|
||
// Load the container images using ctr and delete the file. | ||
loadCommand := "ctr -n k8s.io images import -" | ||
return i.execPod(ctx, clusterProxy, namespace, podName, containerName, loadCommand, reader) | ||
} | ||
|
||
func (i imagePreloader) execPod(ctx context.Context, clusterProxy framework.ClusterProxy, namespace, podName, containerName, cmd string, stdin io.Reader) error { | ||
var hasStdin bool | ||
if stdin != nil { | ||
hasStdin = true | ||
} | ||
|
||
req := clusterProxy.GetClientSet().CoreV1().RESTClient().Post(). | ||
Namespace(namespace). | ||
Resource("pods"). | ||
Name(podName). | ||
SubResource("exec"). | ||
VersionedParams(&corev1.PodExecOptions{ | ||
Container: containerName, | ||
Command: []string{"/bin/sh", "-c", cmd}, | ||
Stdin: hasStdin, | ||
Stdout: true, | ||
Stderr: true, | ||
TTY: false, | ||
}, scheme.ParameterCodec) | ||
|
||
exec, err := remotecommand.NewSPDYExecutor(clusterProxy.GetRESTConfig(), "POST", req.URL()) | ||
if err != nil { | ||
return err | ||
} | ||
// WebSocketExecutor must be "GET" method as described in RFC 6455 Sec. 4.1 (page 17). | ||
websocketExec, err := remotecommand.NewWebSocketExecutor(clusterProxy.GetRESTConfig(), "GET", req.URL().String()) | ||
if err != nil { | ||
return err | ||
} | ||
exec, err = remotecommand.NewFallbackExecutor(websocketExec, exec, httpstream.IsUpgradeFailure) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
var stdout, stderr bytes.Buffer | ||
|
||
err = exec.StreamWithContext(ctx, remotecommand.StreamOptions{ | ||
Stdin: stdin, | ||
Stdout: &stdout, | ||
Stderr: &stderr, | ||
Tty: false, | ||
}) | ||
|
||
if err != nil { | ||
return errors.Wrapf(err, "running command %q stdout=%q, stderr=%q", cmd, stdout.String(), stderr.String()) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func getPreloadDaemonset() (*appsv1.DaemonSet, controllerutil.MutateFn, map[string]string) { | ||
labels := map[string]string{ | ||
"app": "image-preloader", | ||
} | ||
ds := &appsv1.DaemonSet{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Namespace: metav1.NamespaceSystem, | ||
Name: "image-preloader", | ||
Labels: labels, | ||
}, | ||
} | ||
muatetFn := func() error { | ||
ds.Labels = labels | ||
ds.Spec = appsv1.DaemonSetSpec{ | ||
Selector: &metav1.LabelSelector{ | ||
MatchLabels: labels, | ||
}, | ||
Template: corev1.PodTemplateSpec{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Labels: labels, | ||
}, | ||
Spec: corev1.PodSpec{ | ||
Containers: []corev1.Container{ | ||
{ | ||
Name: "pause", | ||
Image: "registry.k8s.io/pause:3.9", | ||
Command: []string{"/usr/bin/tail", "-f", "/dev/null"}, | ||
SecurityContext: &corev1.SecurityContext{ | ||
Privileged: ptr.To(true), | ||
}, | ||
VolumeMounts: []corev1.VolumeMount{ | ||
{ | ||
Name: "host", | ||
MountPath: "/", | ||
}, | ||
}, | ||
}, | ||
}, | ||
HostPID: true, | ||
HostIPC: true, | ||
Volumes: []corev1.Volume{ | ||
{ | ||
Name: "host", | ||
VolumeSource: corev1.VolumeSource{ | ||
HostPath: &corev1.HostPathVolumeSource{ | ||
Path: "/", | ||
Type: ptr.To(corev1.HostPathDirectory), | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
return nil | ||
} | ||
return ds, muatetFn, labels | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters