From 0ea4eb563adb286582bc2180fce4b6bf900b7b4e Mon Sep 17 00:00:00 2001 From: Lyndon-Li Date: Tue, 26 Nov 2024 19:21:23 +0800 Subject: [PATCH] hybrid deploy Signed-off-by: Lyndon-Li --- pkg/install/daemonset.go | 32 ++++++++++++++++--- pkg/install/deployment.go | 7 ++++ pkg/install/resources.go | 7 ++++ pkg/plugin/clientmgmt/process/registry.go | 11 +++++++ .../clientmgmt/process/registry_test.go | 6 +++- 5 files changed, 58 insertions(+), 5 deletions(-) diff --git a/pkg/install/daemonset.go b/pkg/install/daemonset.go index 9cc3a814c..81b06b64c 100644 --- a/pkg/install/daemonset.go +++ b/pkg/install/daemonset.go @@ -57,8 +57,13 @@ func DaemonSet(namespace string, opts ...podTemplateOption) *appsv1.DaemonSet { userID := int64(0) mountPropagationMode := corev1.MountPropagationHostToContainer + dsName := "node-agent" + if c.forWindows { + dsName = "node-agent-windows" + } + daemonSet := &appsv1.DaemonSet{ - ObjectMeta: objectMeta(namespace, "node-agent"), + ObjectMeta: objectMeta(namespace, dsName), TypeMeta: metav1.TypeMeta{ Kind: "DaemonSet", APIVersion: appsv1.SchemeGroupVersion.String(), @@ -66,13 +71,14 @@ func DaemonSet(namespace string, opts ...podTemplateOption) *appsv1.DaemonSet { Spec: appsv1.DaemonSetSpec{ Selector: &metav1.LabelSelector{ MatchLabels: map[string]string{ - "name": "node-agent", + "name": dsName, }, }, Template: corev1.PodTemplateSpec{ ObjectMeta: metav1.ObjectMeta{ Labels: podLabels(c.labels, map[string]string{ - "name": "node-agent", + "name": dsName, + "role": "node-agent", }), Annotations: c.annotations, }, @@ -107,7 +113,7 @@ func DaemonSet(namespace string, opts ...podTemplateOption) *appsv1.DaemonSet { }, Containers: []corev1.Container{ { - Name: "node-agent", + Name: dsName, Image: c.image, Ports: containerPorts(), ImagePullPolicy: pullPolicy, @@ -205,6 +211,24 @@ func DaemonSet(namespace string, opts ...podTemplateOption) *appsv1.DaemonSet { }...) } + if c.forWindows { + daemonSet.Spec.Template.Spec.SecurityContext = nil + daemonSet.Spec.Template.Spec.Containers[0].SecurityContext = nil + daemonSet.Spec.Template.Spec.NodeSelector = map[string]string{ + "kubernetes.io/os": "windows", + } + daemonSet.Spec.Template.Spec.OS = &corev1.PodOS{ + Name: "windows", + } + } else { + daemonSet.Spec.Template.Spec.NodeSelector = map[string]string{ + "kubernetes.io/os": "linux", + } + daemonSet.Spec.Template.Spec.OS = &corev1.PodOS{ + Name: "linux", + } + } + daemonSet.Spec.Template.Spec.Containers[0].Env = append(daemonSet.Spec.Template.Spec.Containers[0].Env, c.envVars...) return daemonSet diff --git a/pkg/install/deployment.go b/pkg/install/deployment.go index 0c9a0adf4..d074b8a7e 100644 --- a/pkg/install/deployment.go +++ b/pkg/install/deployment.go @@ -58,6 +58,7 @@ type podTemplateConfig struct { repoMaintenanceJobConfigMap string nodeAgentConfigMap string itemBlockWorkerCount int + forWindows bool } func WithImage(image string) podTemplateOption { @@ -219,6 +220,12 @@ func WithItemBlockWorkerCount(itemBlockWorkerCount int) podTemplateOption { } } +func WithForWinows() podTemplateOption { + return func(c *podTemplateConfig) { + c.forWindows = true + } +} + func Deployment(namespace string, opts ...podTemplateOption) *appsv1.Deployment { // TODO: Add support for server args c := &podTemplateConfig{ diff --git a/pkg/install/resources.go b/pkg/install/resources.go index a2851c7af..9788d0419 100644 --- a/pkg/install/resources.go +++ b/pkg/install/resources.go @@ -418,6 +418,13 @@ func AllResources(o *VeleroOptions) *unstructured.UnstructuredList { if err := appendUnstructured(resources, ds); err != nil { fmt.Printf("error appending DaemonSet %s: %s\n", ds.GetName(), err.Error()) } + + dsOpts = append(dsOpts, WithForWinows()) + + dsWin := DaemonSet(o.Namespace, dsOpts...) + if err := appendUnstructured(resources, dsWin); err != nil { + fmt.Printf("error appending DaemonSet %s: %s\n", dsWin.GetName(), err.Error()) + } } return resources diff --git a/pkg/plugin/clientmgmt/process/registry.go b/pkg/plugin/clientmgmt/process/registry.go index 6238c45fb..744048690 100644 --- a/pkg/plugin/clientmgmt/process/registry.go +++ b/pkg/plugin/clientmgmt/process/registry.go @@ -20,6 +20,7 @@ import ( "fmt" "os" "path/filepath" + "strings" "github.com/pkg/errors" "github.com/sirupsen/logrus" @@ -153,6 +154,7 @@ func (r *registry) readPluginsDir(dir string) ([]string, error) { } if !executable(file) { + r.logger.Warnf("Searching plugin skip file %s, not executable, mode %v, ext %s", file.Name(), file.Mode(), strings.ToLower(filepath.Ext(file.Name()))) continue } @@ -163,6 +165,15 @@ func (r *registry) readPluginsDir(dir string) ([]string, error) { // executable determines if a file is executable. func executable(info os.FileInfo) bool { + return executableLinux(info) || executableWindows(info) +} + +func executableWindows(info os.FileInfo) bool { + ext := strings.ToLower(filepath.Ext(info.Name())) + return (ext == ".exe") +} + +func executableLinux(info os.FileInfo) bool { /* When we AND the mode with 0111: diff --git a/pkg/plugin/clientmgmt/process/registry_test.go b/pkg/plugin/clientmgmt/process/registry_test.go index 1f74ffca0..0bcef7c0b 100644 --- a/pkg/plugin/clientmgmt/process/registry_test.go +++ b/pkg/plugin/clientmgmt/process/registry_test.go @@ -114,7 +114,9 @@ func TestReadPluginsDir(t *testing.T) { WithFileAndMode("/plugins/nonexecutable2", []byte("plugin2"), 0644). WithFileAndMode("/plugins/executable3", []byte("plugin3"), 0755). WithFileAndMode("/plugins/nested/executable4", []byte("plugin4"), 0755). - WithFileAndMode("/plugins/nested/nonexecutable5", []byte("plugin4"), 0644) + WithFileAndMode("/plugins/nested/nonexecutable5", []byte("plugin4"), 0644). + WithFileAndMode("/plugins/nested/win-exe1.exe", []byte("plugin4"), 0600). + WithFileAndMode("/plugins/nested/WIN-EXE2.EXE", []byte("plugin4"), 0600) plugins, err := r.readPluginsDir(dir) require.NoError(t, err) @@ -123,6 +125,8 @@ func TestReadPluginsDir(t *testing.T) { "/plugins/executable1", "/plugins/executable3", "/plugins/nested/executable4", + "/plugins/nested/win-exe1.exe", + "/plugins/nested/WIN-EXE2.EXE", } sort.Strings(plugins)