From 58acf4744af39338e67342a4307a7607acd847f4 Mon Sep 17 00:00:00 2001 From: ppbits Date: Fri, 7 Feb 2025 11:37:53 -0800 Subject: [PATCH 01/12] Add Windows support for kruise-daemon Signed-off-by: ppbits --- Dockerfile_windows | 11 +++ hack/daemon/win/daemon-ws2022.yaml | 84 ++++++++++++++++++ pkg/daemon/criruntime/factory.go | 82 +---------------- pkg/daemon/criruntime/factory_unix.go | 108 +++++++++++++++++++++++ pkg/daemon/criruntime/factory_windows.go | 32 +++++++ pkg/daemon/daemon.go | 6 +- 6 files changed, 238 insertions(+), 85 deletions(-) create mode 100644 Dockerfile_windows create mode 100644 hack/daemon/win/daemon-ws2022.yaml create mode 100644 pkg/daemon/criruntime/factory_unix.go create mode 100644 pkg/daemon/criruntime/factory_windows.go diff --git a/Dockerfile_windows b/Dockerfile_windows new file mode 100644 index 0000000000..a83085a066 --- /dev/null +++ b/Dockerfile_windows @@ -0,0 +1,11 @@ +# Build Windows image for kruise-daemon + +# Using Windows HostProcess container base image: https://github.com/microsoft/windows-host-process-containers-base-image +ARG BASE_IMAGE=mcr.microsoft.com/oss/kubernetes/windows-host-process-containers-base-image +ARG BASE_IMAGE_VERSION=v1.0.0 +FROM ${BASE_IMAGE}:${BASE_IMAGE_VERSION} + +WORKDIR / +COPY ./daemon.exe /kruise-daemon.exe + +ENTRYPOINT ["kruise-daemon.exe"] \ No newline at end of file diff --git a/hack/daemon/win/daemon-ws2022.yaml b/hack/daemon/win/daemon-ws2022.yaml new file mode 100644 index 0000000000..474aa9e82a --- /dev/null +++ b/hack/daemon/win/daemon-ws2022.yaml @@ -0,0 +1,84 @@ +apiVersion: apps/v1 +kind: DaemonSet +metadata: + labels: + control-plane: daemon + name: kruise-daemon-win + namespace: kruise-system +spec: + selector: + matchLabels: + control-plane: daemon + template: + metadata: + labels: + control-plane: daemon + spec: + affinity: + nodeAffinity: + requiredDuringSchedulingIgnoredDuringExecution: + nodeSelectorTerms: + - matchExpressions: + - key: type + operator: NotIn + values: + - virtual-kubelet + containers: + - args: + - --logtostderr=true + - --v=5 + - --addr=:10221 + - --feature-gates=ImagePullJobGate=true + - --enable-pprof=true + - --pprof-addr=localhost:10222 + workingDir: "$env:CONTAINER_SANDBOX_MOUNT_POINT/" + command: + - $env:CONTAINER_SANDBOX_MOUNT_POINT/kruise-daemon.exe + env: + - name: NODE_NAME + valueFrom: + fieldRef: + apiVersion: v1 + fieldPath: spec.nodeName + image: openkruise/kruise-daemon-win:test # Replace with the actual image + imagePullPolicy: Always + livenessProbe: + failureThreshold: 3 + httpGet: + path: /healthz + port: 10221 + scheme: HTTP + initialDelaySeconds: 60 + periodSeconds: 10 + successThreshold: 1 + timeoutSeconds: 1 + name: daemon + resources: + limits: + cpu: 50m + memory: 128Mi + requests: + cpu: "0" + memory: "0" + terminationMessagePath: /dev/termination-log + terminationMessagePolicy: File + dnsPolicy: ClusterFirstWithHostNet + hostNetwork: true + restartPolicy: Always + schedulerName: default-scheduler + securityContext: + windowsOptions: + hostProcess: true + runAsUserName: "NT AUTHORITY\\SYSTEM" + serviceAccount: kruise-daemon + serviceAccountName: kruise-daemon + terminationGracePeriodSeconds: 10 + tolerations: + - operator: Exists + nodeSelector: + kubernetes.io/os: windows + updateStrategy: + rollingUpdate: + maxSurge: 0 + maxUnavailable: 10% + type: RollingUpdate diff --git a/pkg/daemon/criruntime/factory.go b/pkg/daemon/criruntime/factory.go index ff6fffcc06..27ba1a8f7a 100644 --- a/pkg/daemon/criruntime/factory.go +++ b/pkg/daemon/criruntime/factory.go @@ -18,9 +18,7 @@ package criruntime import ( "context" - "flag" "fmt" - "os" "time" oteltrace "go.opentelemetry.io/otel/trace" @@ -38,10 +36,6 @@ const ( kubeRuntimeAPIVersion = "0.1.0" ) -var ( - CRISocketFileName = flag.String("socket-file", "", "The name of CRI socket file, and it should be in the mounted /hostvarrun directory.") -) - // Factory is the interface to get container and image runtime service type Factory interface { GetImageService() runtimeimage.ImageService @@ -73,8 +67,8 @@ type runtimeImpl struct { runtimeService criapi.RuntimeService } -func NewFactory(varRunPath string, accountManager daemonutil.ImagePullAccountManager) (Factory, error) { - cfgs := detectRuntime(varRunPath) +func NewFactory(accountManager daemonutil.ImagePullAccountManager) (Factory, error) { + cfgs := detectRuntime() if len(cfgs) == 0 { return nil, fmt.Errorf("not found container runtime sock") } @@ -147,75 +141,3 @@ func (f *factory) GetRuntimeServiceByName(runtimeName string) criapi.RuntimeServ } return nil } - -func detectRuntime(varRunPath string) (cfgs []runtimeConfig) { - var err error - - // firstly check if it is configured from flag - if CRISocketFileName != nil && len(*CRISocketFileName) > 0 { - filePath := fmt.Sprintf("%s/%s", varRunPath, *CRISocketFileName) - if _, err = os.Stat(filePath); err == nil { - cfgs = append(cfgs, runtimeConfig{ - runtimeType: ContainerRuntimeCommonCRI, - runtimeRemoteURI: fmt.Sprintf("unix://%s/%s", varRunPath, *CRISocketFileName), - }) - klog.InfoS("Find configured CRI socket with given flag", "filePath", filePath) - } else { - klog.ErrorS(err, "Failed to stat the CRI socket with given flag", "filePath", filePath) - } - return - } - - // if the flag is not set, then try to find runtime in the recognized types and paths. - - // containerd, with the same behavior of pullImage as commonCRI - { - if _, err = os.Stat(fmt.Sprintf("%s/containerd.sock", varRunPath)); err == nil { - cfgs = append(cfgs, runtimeConfig{ - runtimeType: ContainerRuntimeContainerd, - runtimeRemoteURI: fmt.Sprintf("unix://%s/containerd.sock", varRunPath), - }) - } - if _, err = os.Stat(fmt.Sprintf("%s/containerd/containerd.sock", varRunPath)); err == nil { - cfgs = append(cfgs, runtimeConfig{ - runtimeType: ContainerRuntimeContainerd, - runtimeRemoteURI: fmt.Sprintf("unix://%s/containerd/containerd.sock", varRunPath), - }) - } - } - - // cri-o - { - if _, err = os.Stat(fmt.Sprintf("%s/crio.sock", varRunPath)); err == nil { - cfgs = append(cfgs, runtimeConfig{ - runtimeType: ContainerRuntimeCommonCRI, - runtimeRemoteURI: fmt.Sprintf("unix://%s/crio.sock", varRunPath), - }) - } - if _, err = os.Stat(fmt.Sprintf("%s/crio/crio.sock", varRunPath)); err == nil { - cfgs = append(cfgs, runtimeConfig{ - runtimeType: ContainerRuntimeCommonCRI, - runtimeRemoteURI: fmt.Sprintf("unix://%s/crio/crio.sock", varRunPath), - }) - } - } - - // cri-docker dockerd as a compliant Container Runtime Interface, detail see https://github.com/Mirantis/cri-dockerd - { - if _, err = os.Stat(fmt.Sprintf("%s/cri-dockerd.sock", varRunPath)); err == nil { - cfgs = append(cfgs, runtimeConfig{ - runtimeType: ContainerRuntimeCommonCRI, - runtimeRemoteURI: fmt.Sprintf("unix://%s/cri-dockerd.sock", varRunPath), - }) - } - // Check if the cri-dockerd runtime socket exists in the expected k3s runtime directory. - // If found, append it to the runtime configuration list to ensure k3s can use cri-dockerd. - if _, err = os.Stat(fmt.Sprintf("%s/cri-dockerd/cri-dockerd.sock", varRunPath)); err == nil { - cfgs = append(cfgs, runtimeConfig{ - runtimeType: ContainerRuntimeCommonCRI, - runtimeRemoteURI: fmt.Sprintf("unix://%s/cri-dockerd/cri-dockerd.sock", varRunPath), - }) - } - } - return cfgs -} diff --git a/pkg/daemon/criruntime/factory_unix.go b/pkg/daemon/criruntime/factory_unix.go new file mode 100644 index 0000000000..0bbce85724 --- /dev/null +++ b/pkg/daemon/criruntime/factory_unix.go @@ -0,0 +1,108 @@ +//go:build !windows +// +build !windows + +/* +Copyright 2021 The Kruise 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 criruntime + +import ( + "flag" + "fmt" + "os" + + "k8s.io/klog/v2" +) + +const ( + varRunMountPath = "/hostvarrun" +) + +var ( + criSocketFileName = flag.String("socket-file", "", "The name of CRI socket file, and it should be in the mounted /hostvarrun directory.") +) + +func detectRuntime() (cfgs []runtimeConfig) { + var err error + + // firstly check if it is configured from flag + if criSocketFileName != nil && len(*criSocketFileName) > 0 { + filePath := fmt.Sprintf("%s/%s", varRunMountPath, *criSocketFileName) + if _, err = os.Stat(filePath); err == nil { + cfgs = append(cfgs, runtimeConfig{ + runtimeType: ContainerRuntimeCommonCRI, + runtimeRemoteURI: fmt.Sprintf("unix://%s/%s", varRunMountPath, *criSocketFileName), + }) + klog.InfoS("Find configured CRI socket with given flag", "filePath", filePath) + } else { + klog.ErrorS(err, "Failed to stat the CRI socket with given flag", "filePath", filePath) + } + return + } + + // if the flag is not set, then try to find runtime in the recognized types and paths. + + // containerd, with the same behavior of pullImage as commonCRI + { + if _, err = os.Stat(fmt.Sprintf("%s/containerd.sock", varRunMountPath)); err == nil { + cfgs = append(cfgs, runtimeConfig{ + runtimeType: ContainerRuntimeContainerd, + runtimeRemoteURI: fmt.Sprintf("unix://%s/containerd.sock", varRunMountPath), + }) + } + if _, err = os.Stat(fmt.Sprintf("%s/containerd/containerd.sock", varRunMountPath)); err == nil { + cfgs = append(cfgs, runtimeConfig{ + runtimeType: ContainerRuntimeContainerd, + runtimeRemoteURI: fmt.Sprintf("unix://%s/containerd/containerd.sock", varRunMountPath), + }) + } + } + + // cri-o + { + if _, err = os.Stat(fmt.Sprintf("%s/crio.sock", varRunMountPath)); err == nil { + cfgs = append(cfgs, runtimeConfig{ + runtimeType: ContainerRuntimeCommonCRI, + runtimeRemoteURI: fmt.Sprintf("unix://%s/crio.sock", varRunMountPath), + }) + } + if _, err = os.Stat(fmt.Sprintf("%s/crio/crio.sock", varRunMountPath)); err == nil { + cfgs = append(cfgs, runtimeConfig{ + runtimeType: ContainerRuntimeCommonCRI, + runtimeRemoteURI: fmt.Sprintf("unix://%s/crio/crio.sock", varRunMountPath), + }) + } + } + + // cri-docker dockerd as a compliant Container Runtime Interface, detail see https://github.com/Mirantis/cri-dockerd + { + if _, err = os.Stat(fmt.Sprintf("%s/cri-dockerd.sock", varRunMountPath)); err == nil { + cfgs = append(cfgs, runtimeConfig{ + runtimeType: ContainerRuntimeCommonCRI, + runtimeRemoteURI: fmt.Sprintf("unix://%s/cri-dockerd.sock", varRunMountPath), + }) + } + // Check if the cri-dockerd runtime socket exists in the expected k3s runtime directory. + // If found, append it to the runtime configuration list to ensure k3s can use cri-dockerd. + if _, err = os.Stat(fmt.Sprintf("%s/cri-dockerd/cri-dockerd.sock", varRunMountPath)); err == nil { + cfgs = append(cfgs, runtimeConfig{ + runtimeType: ContainerRuntimeCommonCRI, + runtimeRemoteURI: fmt.Sprintf("unix://%s/cri-dockerd/cri-dockerd.sock", varRunMountPath), + }) + } + } + return cfgs +} diff --git a/pkg/daemon/criruntime/factory_windows.go b/pkg/daemon/criruntime/factory_windows.go new file mode 100644 index 0000000000..d9e6c073e0 --- /dev/null +++ b/pkg/daemon/criruntime/factory_windows.go @@ -0,0 +1,32 @@ +//go:build windows +// +build windows + +/* +Copyright 2021 The Kruise 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 criruntime + +var ( + containerdRemoteURI = `npipe://./pipe/containerd-containerd` +) + +func detectRuntime() (cfgs []runtimeConfig) { + cfgs = append(cfgs, runtimeConfig{ + runtimeType: ContainerRuntimeContainerd, + runtimeRemoteURI: containerdRemoteURI, + }) + return cfgs +} diff --git a/pkg/daemon/daemon.go b/pkg/daemon/daemon.go index 8943063552..2490619073 100644 --- a/pkg/daemon/daemon.go +++ b/pkg/daemon/daemon.go @@ -50,10 +50,6 @@ import ( "sigs.k8s.io/controller-runtime/pkg/metrics" ) -const ( - varRunMountPath = "/hostvarrun" -) - var ( scheme = runtime.NewScheme() ) @@ -123,7 +119,7 @@ func NewDaemon(cfg *rest.Config, bindAddress string) (Daemon, error) { } accountManager := daemonutil.NewImagePullAccountManager(genericClient.KubeClient) - runtimeFactory, err := daemonruntime.NewFactory(varRunMountPath, accountManager) + runtimeFactory, err := daemonruntime.NewFactory(accountManager) if err != nil { return nil, fmt.Errorf("failed to new runtime factory: %v", err) } From e2f64ebb481dfeec2b6acd12bf6b42742e44e54c Mon Sep 17 00:00:00 2001 From: ppbits Date: Fri, 7 Feb 2025 14:48:24 -0800 Subject: [PATCH 02/12] Fix image service error Signed-off-by: ppbits --- pkg/daemon/criruntime/factory.go | 7 +------ pkg/daemon/criruntime/imageruntime/cri.go | 6 +++--- 2 files changed, 4 insertions(+), 9 deletions(-) diff --git a/pkg/daemon/criruntime/factory.go b/pkg/daemon/criruntime/factory.go index 27ba1a8f7a..5e9a53c8f2 100644 --- a/pkg/daemon/criruntime/factory.go +++ b/pkg/daemon/criruntime/factory.go @@ -83,12 +83,7 @@ func NewFactory(accountManager daemonutil.ImagePullAccountManager) (Factory, err var runtimeService criapi.RuntimeService var typedVersion *runtimeapi.VersionResponse - addr, _, err := kubeletutil.GetAddressAndDialer(cfg.runtimeRemoteURI) - if err != nil { - klog.ErrorS(err, "Failed to get address", "runtimeType", cfg.runtimeType, "runtimeURI", cfg.runtimeURI, "runtimeRemoteURI", cfg.runtimeRemoteURI) - continue - } - imageService, err = runtimeimage.NewCRIImageService(addr, accountManager) + imageService, err = runtimeimage.NewCRIImageService(cfg.runtimeRemoteURI, accountManager) if err != nil { klog.ErrorS(err, "Failed to new image service", "runtimeType", cfg.runtimeType, "runtimeURI", cfg.runtimeURI, "runtimeRemoteURI", cfg.runtimeRemoteURI) continue diff --git a/pkg/daemon/criruntime/imageruntime/cri.go b/pkg/daemon/criruntime/imageruntime/cri.go index 9de9abc591..b8a1916cc0 100644 --- a/pkg/daemon/criruntime/imageruntime/cri.go +++ b/pkg/daemon/criruntime/imageruntime/cri.go @@ -40,9 +40,9 @@ const ( ) // NewCRIImageService create a common CRI runtime -func NewCRIImageService(runtimeURI string, accountManager daemonutil.ImagePullAccountManager) (ImageService, error) { - klog.V(3).InfoS("Connecting to image service", "endpoint", runtimeURI) - addr, dialer, err := util.GetAddressAndDialer(runtimeURI) +func NewCRIImageService(remoteRuntimeURI string, accountManager daemonutil.ImagePullAccountManager) (ImageService, error) { + klog.V(3).InfoS("Connecting to image service", "endpoint", remoteRuntimeURI) + addr, dialer, err := util.GetAddressAndDialer(remoteRuntimeURI) if err != nil { return nil, err } From d2fb2f620ff5f9b351886f7409f418f8b2882f7d Mon Sep 17 00:00:00 2001 From: ppbits Date: Fri, 7 Feb 2025 14:49:52 -0800 Subject: [PATCH 03/12] minor fix Signed-off-by: ppbits --- pkg/daemon/criruntime/factory.go | 1 - 1 file changed, 1 deletion(-) diff --git a/pkg/daemon/criruntime/factory.go b/pkg/daemon/criruntime/factory.go index 5e9a53c8f2..8b7ecc7be7 100644 --- a/pkg/daemon/criruntime/factory.go +++ b/pkg/daemon/criruntime/factory.go @@ -26,7 +26,6 @@ import ( runtimeapi "k8s.io/cri-api/pkg/apis/runtime/v1" "k8s.io/klog/v2" criremote "k8s.io/kubernetes/pkg/kubelet/cri/remote" - kubeletutil "k8s.io/kubernetes/pkg/kubelet/util" runtimeimage "github.com/openkruise/kruise/pkg/daemon/criruntime/imageruntime" daemonutil "github.com/openkruise/kruise/pkg/daemon/util" From d8b54281d71f49ffbb034cb4e3ffb603107b3aa9 Mon Sep 17 00:00:00 2001 From: ppbits Date: Sun, 9 Feb 2025 16:25:17 -0800 Subject: [PATCH 04/12] rename daemon windows yaml Signed-off-by: ppbits --- hack/daemon/daemon-windows.yaml | 84 +++++++++++++++++++++++++++++++++ 1 file changed, 84 insertions(+) create mode 100644 hack/daemon/daemon-windows.yaml diff --git a/hack/daemon/daemon-windows.yaml b/hack/daemon/daemon-windows.yaml new file mode 100644 index 0000000000..474aa9e82a --- /dev/null +++ b/hack/daemon/daemon-windows.yaml @@ -0,0 +1,84 @@ +apiVersion: apps/v1 +kind: DaemonSet +metadata: + labels: + control-plane: daemon + name: kruise-daemon-win + namespace: kruise-system +spec: + selector: + matchLabels: + control-plane: daemon + template: + metadata: + labels: + control-plane: daemon + spec: + affinity: + nodeAffinity: + requiredDuringSchedulingIgnoredDuringExecution: + nodeSelectorTerms: + - matchExpressions: + - key: type + operator: NotIn + values: + - virtual-kubelet + containers: + - args: + - --logtostderr=true + - --v=5 + - --addr=:10221 + - --feature-gates=ImagePullJobGate=true + - --enable-pprof=true + - --pprof-addr=localhost:10222 + workingDir: "$env:CONTAINER_SANDBOX_MOUNT_POINT/" + command: + - $env:CONTAINER_SANDBOX_MOUNT_POINT/kruise-daemon.exe + env: + - name: NODE_NAME + valueFrom: + fieldRef: + apiVersion: v1 + fieldPath: spec.nodeName + image: openkruise/kruise-daemon-win:test # Replace with the actual image + imagePullPolicy: Always + livenessProbe: + failureThreshold: 3 + httpGet: + path: /healthz + port: 10221 + scheme: HTTP + initialDelaySeconds: 60 + periodSeconds: 10 + successThreshold: 1 + timeoutSeconds: 1 + name: daemon + resources: + limits: + cpu: 50m + memory: 128Mi + requests: + cpu: "0" + memory: "0" + terminationMessagePath: /dev/termination-log + terminationMessagePolicy: File + dnsPolicy: ClusterFirstWithHostNet + hostNetwork: true + restartPolicy: Always + schedulerName: default-scheduler + securityContext: + windowsOptions: + hostProcess: true + runAsUserName: "NT AUTHORITY\\SYSTEM" + serviceAccount: kruise-daemon + serviceAccountName: kruise-daemon + terminationGracePeriodSeconds: 10 + tolerations: + - operator: Exists + nodeSelector: + kubernetes.io/os: windows + updateStrategy: + rollingUpdate: + maxSurge: 0 + maxUnavailable: 10% + type: RollingUpdate From 5e45dd7c2a67443e6602ac211b0f52575613b45e Mon Sep 17 00:00:00 2001 From: ppbits Date: Sun, 9 Feb 2025 16:28:49 -0800 Subject: [PATCH 05/12] remove ws2022 yaml Signed-off-by: ppbits --- hack/daemon/win/daemon-ws2022.yaml | 84 ------------------------------ 1 file changed, 84 deletions(-) delete mode 100644 hack/daemon/win/daemon-ws2022.yaml diff --git a/hack/daemon/win/daemon-ws2022.yaml b/hack/daemon/win/daemon-ws2022.yaml deleted file mode 100644 index 474aa9e82a..0000000000 --- a/hack/daemon/win/daemon-ws2022.yaml +++ /dev/null @@ -1,84 +0,0 @@ -apiVersion: apps/v1 -kind: DaemonSet -metadata: - labels: - control-plane: daemon - name: kruise-daemon-win - namespace: kruise-system -spec: - selector: - matchLabels: - control-plane: daemon - template: - metadata: - labels: - control-plane: daemon - spec: - affinity: - nodeAffinity: - requiredDuringSchedulingIgnoredDuringExecution: - nodeSelectorTerms: - - matchExpressions: - - key: type - operator: NotIn - values: - - virtual-kubelet - containers: - - args: - - --logtostderr=true - - --v=5 - - --addr=:10221 - - --feature-gates=ImagePullJobGate=true - - --enable-pprof=true - - --pprof-addr=localhost:10222 - workingDir: "$env:CONTAINER_SANDBOX_MOUNT_POINT/" - command: - - $env:CONTAINER_SANDBOX_MOUNT_POINT/kruise-daemon.exe - env: - - name: NODE_NAME - valueFrom: - fieldRef: - apiVersion: v1 - fieldPath: spec.nodeName - image: openkruise/kruise-daemon-win:test # Replace with the actual image - imagePullPolicy: Always - livenessProbe: - failureThreshold: 3 - httpGet: - path: /healthz - port: 10221 - scheme: HTTP - initialDelaySeconds: 60 - periodSeconds: 10 - successThreshold: 1 - timeoutSeconds: 1 - name: daemon - resources: - limits: - cpu: 50m - memory: 128Mi - requests: - cpu: "0" - memory: "0" - terminationMessagePath: /dev/termination-log - terminationMessagePolicy: File - dnsPolicy: ClusterFirstWithHostNet - hostNetwork: true - restartPolicy: Always - schedulerName: default-scheduler - securityContext: - windowsOptions: - hostProcess: true - runAsUserName: "NT AUTHORITY\\SYSTEM" - serviceAccount: kruise-daemon - serviceAccountName: kruise-daemon - terminationGracePeriodSeconds: 10 - tolerations: - - operator: Exists - nodeSelector: - kubernetes.io/os: windows - updateStrategy: - rollingUpdate: - maxSurge: 0 - maxUnavailable: 10% - type: RollingUpdate From f207c4214efd907414fbf5620c9014b848dc34b8 Mon Sep 17 00:00:00 2001 From: Peng Peng Date: Wed, 26 Feb 2025 16:10:49 -0800 Subject: [PATCH 06/12] Add unit tests Signed-off-by: Peng Peng --- pkg/daemon/criruntime/factory_unix.go | 15 +- pkg/daemon/criruntime/factory_unix_test.go | 180 ++++++++++++++++++ pkg/daemon/criruntime/factory_windows.go | 5 + pkg/daemon/criruntime/factory_windows_test.go | 34 ++++ 4 files changed, 227 insertions(+), 7 deletions(-) create mode 100644 pkg/daemon/criruntime/factory_unix_test.go create mode 100644 pkg/daemon/criruntime/factory_windows_test.go diff --git a/pkg/daemon/criruntime/factory_unix.go b/pkg/daemon/criruntime/factory_unix.go index 0bbce85724..6006e58e82 100644 --- a/pkg/daemon/criruntime/factory_unix.go +++ b/pkg/daemon/criruntime/factory_unix.go @@ -32,6 +32,7 @@ const ( ) var ( + statFunc = os.Stat criSocketFileName = flag.String("socket-file", "", "The name of CRI socket file, and it should be in the mounted /hostvarrun directory.") ) @@ -41,7 +42,7 @@ func detectRuntime() (cfgs []runtimeConfig) { // firstly check if it is configured from flag if criSocketFileName != nil && len(*criSocketFileName) > 0 { filePath := fmt.Sprintf("%s/%s", varRunMountPath, *criSocketFileName) - if _, err = os.Stat(filePath); err == nil { + if _, err = statFunc(filePath); err == nil { cfgs = append(cfgs, runtimeConfig{ runtimeType: ContainerRuntimeCommonCRI, runtimeRemoteURI: fmt.Sprintf("unix://%s/%s", varRunMountPath, *criSocketFileName), @@ -57,13 +58,13 @@ func detectRuntime() (cfgs []runtimeConfig) { // containerd, with the same behavior of pullImage as commonCRI { - if _, err = os.Stat(fmt.Sprintf("%s/containerd.sock", varRunMountPath)); err == nil { + if _, err = statFunc(fmt.Sprintf("%s/containerd.sock", varRunMountPath)); err == nil { cfgs = append(cfgs, runtimeConfig{ runtimeType: ContainerRuntimeContainerd, runtimeRemoteURI: fmt.Sprintf("unix://%s/containerd.sock", varRunMountPath), }) } - if _, err = os.Stat(fmt.Sprintf("%s/containerd/containerd.sock", varRunMountPath)); err == nil { + if _, err = statFunc(fmt.Sprintf("%s/containerd/containerd.sock", varRunMountPath)); err == nil { cfgs = append(cfgs, runtimeConfig{ runtimeType: ContainerRuntimeContainerd, runtimeRemoteURI: fmt.Sprintf("unix://%s/containerd/containerd.sock", varRunMountPath), @@ -73,13 +74,13 @@ func detectRuntime() (cfgs []runtimeConfig) { // cri-o { - if _, err = os.Stat(fmt.Sprintf("%s/crio.sock", varRunMountPath)); err == nil { + if _, err = statFunc(fmt.Sprintf("%s/crio.sock", varRunMountPath)); err == nil { cfgs = append(cfgs, runtimeConfig{ runtimeType: ContainerRuntimeCommonCRI, runtimeRemoteURI: fmt.Sprintf("unix://%s/crio.sock", varRunMountPath), }) } - if _, err = os.Stat(fmt.Sprintf("%s/crio/crio.sock", varRunMountPath)); err == nil { + if _, err = statFunc(fmt.Sprintf("%s/crio/crio.sock", varRunMountPath)); err == nil { cfgs = append(cfgs, runtimeConfig{ runtimeType: ContainerRuntimeCommonCRI, runtimeRemoteURI: fmt.Sprintf("unix://%s/crio/crio.sock", varRunMountPath), @@ -89,7 +90,7 @@ func detectRuntime() (cfgs []runtimeConfig) { // cri-docker dockerd as a compliant Container Runtime Interface, detail see https://github.com/Mirantis/cri-dockerd { - if _, err = os.Stat(fmt.Sprintf("%s/cri-dockerd.sock", varRunMountPath)); err == nil { + if _, err = statFunc(fmt.Sprintf("%s/cri-dockerd.sock", varRunMountPath)); err == nil { cfgs = append(cfgs, runtimeConfig{ runtimeType: ContainerRuntimeCommonCRI, runtimeRemoteURI: fmt.Sprintf("unix://%s/cri-dockerd.sock", varRunMountPath), @@ -97,7 +98,7 @@ func detectRuntime() (cfgs []runtimeConfig) { } // Check if the cri-dockerd runtime socket exists in the expected k3s runtime directory. // If found, append it to the runtime configuration list to ensure k3s can use cri-dockerd. - if _, err = os.Stat(fmt.Sprintf("%s/cri-dockerd/cri-dockerd.sock", varRunMountPath)); err == nil { + if _, err = statFunc(fmt.Sprintf("%s/cri-dockerd/cri-dockerd.sock", varRunMountPath)); err == nil { cfgs = append(cfgs, runtimeConfig{ runtimeType: ContainerRuntimeCommonCRI, runtimeRemoteURI: fmt.Sprintf("unix://%s/cri-dockerd/cri-dockerd.sock", varRunMountPath), diff --git a/pkg/daemon/criruntime/factory_unix_test.go b/pkg/daemon/criruntime/factory_unix_test.go new file mode 100644 index 0000000000..13eeaee6a3 --- /dev/null +++ b/pkg/daemon/criruntime/factory_unix_test.go @@ -0,0 +1,180 @@ +//go:build !windows +// +build !windows + +/* +Copyright 2021 The Kruise 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 criruntime + +import ( + "flag" + "os" + "testing" + "time" +) + +type fileInfo struct { + name string +} + +func (f *fileInfo) Name() string { return f.name } +func (f *fileInfo) Size() int64 { return 0 } +func (f *fileInfo) Mode() os.FileMode { return 0644 } +func (f *fileInfo) ModTime() time.Time { return time.Time{} } +func (f *fileInfo) IsDir() bool { return false } +func (f *fileInfo) Sys() interface{} { return nil } + +func TestDetectRuntimeUnix(t *testing.T) { + testCases := []struct { + name string + flag string + runtimeType ContainerRuntimeType + runtimeRemoteURI string + statFunc func(name string) (os.FileInfo, error) + expectedCfgsCount int + }{ + { + name: "non-existent-socket-with-flag", + flag: "non-existent-socket", + runtimeType: ContainerRuntimeCommonCRI, + runtimeRemoteURI: "unix://hostvarrun/non-existent-socket", + statFunc: func(name string) (os.FileInfo, error) { + return nil, os.ErrNotExist + }, + expectedCfgsCount: 0, + }, + { + name: "crio.sock-with-flag", + flag: "crio.sock", + runtimeType: ContainerRuntimeCommonCRI, + runtimeRemoteURI: "unix://hostvarrun/crio.sock", + statFunc: func(name string) (os.FileInfo, error) { + if name == "hostvarrun/crio.sock" { + return &fileInfo{name: name}, nil + } + return nil, os.ErrNotExist + }, + expectedCfgsCount: 1, + }, + { + name: "containerd.sock", + flag: "", + runtimeType: ContainerRuntimeContainerd, + runtimeRemoteURI: "unix://hostvarrun/containerd.sock", + statFunc: func(name string) (os.FileInfo, error) { + if name == "hostvarrun/containerd.sock" { + return &fileInfo{name: name}, nil + } + return nil, os.ErrNotExist + }, + expectedCfgsCount: 1, + }, + { + name: "containerd/containerd.sock", + flag: "", + runtimeType: ContainerRuntimeContainerd, + runtimeRemoteURI: "unix://hostvarrun/containerd/containerd.sock", + statFunc: func(name string) (os.FileInfo, error) { + if name == "hostvarrun/containerd/containerd.sock" { + return &fileInfo{name: name}, nil + } + return nil, os.ErrNotExist + }, + expectedCfgsCount: 1, + }, + { + name: "crio.sock", + flag: "", + runtimeType: ContainerRuntimeCommonCRI, + runtimeRemoteURI: "unix://hostvarrun/crio.sock", + statFunc: func(name string) (os.FileInfo, error) { + if name == "hostvarrun/crio.sock" { + return &fileInfo{name: name}, nil + } + return nil, os.ErrNotExist + }, + expectedCfgsCount: 1, + }, + { + name: "crio/crio.sock", + flag: "", + runtimeType: ContainerRuntimeCommonCRI, + runtimeRemoteURI: "unix://hostvarrun/crio/crio.sock", + statFunc: func(name string) (os.FileInfo, error) { + if name == "hostvarrun/crio/crio.sock" { + return &fileInfo{name: name}, nil + } + return nil, os.ErrNotExist + }, + expectedCfgsCount: 1, + }, + { + name: "cri-dockerd", + flag: "", + runtimeType: ContainerRuntimeCommonCRI, + runtimeRemoteURI: "unix://hostvarrun/cri-dockerd.sock", + statFunc: func(name string) (os.FileInfo, error) { + if name == "hostvarrun/cri-dockerd.sock" { + return &fileInfo{name: name}, nil + } + return nil, os.ErrNotExist + }, + expectedCfgsCount: 1, + }, + { + name: "cri-dockerd/cri-dockerd.sock", + flag: "", + runtimeType: ContainerRuntimeCommonCRI, + runtimeRemoteURI: "unix://hostvarrun/cri-dockerd/cri-dockerd.sock", + statFunc: func(name string) (os.FileInfo, error) { + if name == "hostvarrun/cri-dockerd/cri-dockerd.sock" { + return &fileInfo{name: name}, nil + } + return nil, os.ErrNotExist + }, + expectedCfgsCount: 1, + }, + { + name: "non-existent-socket-without-flag", + flag: "", + runtimeType: ContainerRuntimeCommonCRI, + runtimeRemoteURI: "unix://hostvarrun/non-existent-socket", + statFunc: func(name string) (os.FileInfo, error) { + return nil, os.ErrNotExist + }, + expectedCfgsCount: 0, + }, + } + + for _, testCase := range testCases { + flag.Set("socket-file", testCase.flag) + statFunc = testCase.statFunc + defer func() { statFunc = os.Stat }() + + cfgs := detectRuntime() + if len(cfgs) != testCase.expectedCfgsCount { + t.Fatalf("expected %d runtime config, got %d", testCase.expectedCfgsCount, len(cfgs)) + } + if testCase.expectedCfgsCount > 0 { + if cfgs[0].runtimeRemoteURI != testCase.runtimeRemoteURI { + t.Fatalf("expected runtime remote URI to be %s, got %s", testCase.runtimeRemoteURI, cfgs[0].runtimeRemoteURI) + } + if cfgs[0].runtimeType != testCase.runtimeType { + t.Fatalf("expected runtime type to be %s, got %s", testCase.runtimeType, cfgs[0].runtimeType) + } + } + } +} diff --git a/pkg/daemon/criruntime/factory_windows.go b/pkg/daemon/criruntime/factory_windows.go index d9e6c073e0..1dc172d795 100644 --- a/pkg/daemon/criruntime/factory_windows.go +++ b/pkg/daemon/criruntime/factory_windows.go @@ -20,9 +20,14 @@ limitations under the License. package criruntime var ( + // containerdRemoteURI is the remote URI for containerd. + // On Windows the default CRI endpoint is npipe://./pipe/containerd-containerd . + // source: https://kubernetes.io/docs/setup/production-environment/container-runtimes/#containerd containerdRemoteURI = `npipe://./pipe/containerd-containerd` ) +// detectRuntime returns containerd runtime config +// Windows node pools support only the containerd runtime for most Kubernetes service providers. func detectRuntime() (cfgs []runtimeConfig) { cfgs = append(cfgs, runtimeConfig{ runtimeType: ContainerRuntimeContainerd, diff --git a/pkg/daemon/criruntime/factory_windows_test.go b/pkg/daemon/criruntime/factory_windows_test.go new file mode 100644 index 0000000000..2ab5ca8e6d --- /dev/null +++ b/pkg/daemon/criruntime/factory_windows_test.go @@ -0,0 +1,34 @@ +//go:build windows +// +build windows + +/* +Copyright 2021 The Kruise 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 criruntime + +import ( + "testing" +) + +func TestDetectRuntimeWindows(t *testing.T) { + cfgs := detectRuntime() + if len(cfgs) != 1 { + t.Fatalf("expected 1 runtime config, got %d", len(cfgs)) + } + if cfgs[0].runtimeRemoteURI != "npipe://./pipe/containerd-containerd" { + t.Fatalf("expected runtime remote URI to be npipe://./pipe/containerd-containerd, got %s", cfgs[0].runtimeRemoteURI) + } +} From 2860f051197d5782e98aaa8e4b4b8f4c2163bfe9 Mon Sep 17 00:00:00 2001 From: Peng Peng Date: Wed, 26 Feb 2025 18:55:09 -0800 Subject: [PATCH 07/12] fix unit tests Signed-off-by: Peng Peng --- pkg/daemon/criruntime/factory_unix_test.go | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/pkg/daemon/criruntime/factory_unix_test.go b/pkg/daemon/criruntime/factory_unix_test.go index 13eeaee6a3..0da475e42a 100644 --- a/pkg/daemon/criruntime/factory_unix_test.go +++ b/pkg/daemon/criruntime/factory_unix_test.go @@ -62,7 +62,7 @@ func TestDetectRuntimeUnix(t *testing.T) { runtimeType: ContainerRuntimeCommonCRI, runtimeRemoteURI: "unix://hostvarrun/crio.sock", statFunc: func(name string) (os.FileInfo, error) { - if name == "hostvarrun/crio.sock" { + if name == "/hostvarrun/crio.sock" { return &fileInfo{name: name}, nil } return nil, os.ErrNotExist @@ -75,7 +75,7 @@ func TestDetectRuntimeUnix(t *testing.T) { runtimeType: ContainerRuntimeContainerd, runtimeRemoteURI: "unix://hostvarrun/containerd.sock", statFunc: func(name string) (os.FileInfo, error) { - if name == "hostvarrun/containerd.sock" { + if name == "/hostvarrun/containerd.sock" { return &fileInfo{name: name}, nil } return nil, os.ErrNotExist @@ -88,7 +88,7 @@ func TestDetectRuntimeUnix(t *testing.T) { runtimeType: ContainerRuntimeContainerd, runtimeRemoteURI: "unix://hostvarrun/containerd/containerd.sock", statFunc: func(name string) (os.FileInfo, error) { - if name == "hostvarrun/containerd/containerd.sock" { + if name == "/hostvarrun/containerd/containerd.sock" { return &fileInfo{name: name}, nil } return nil, os.ErrNotExist @@ -101,7 +101,7 @@ func TestDetectRuntimeUnix(t *testing.T) { runtimeType: ContainerRuntimeCommonCRI, runtimeRemoteURI: "unix://hostvarrun/crio.sock", statFunc: func(name string) (os.FileInfo, error) { - if name == "hostvarrun/crio.sock" { + if name == "/hostvarrun/crio.sock" { return &fileInfo{name: name}, nil } return nil, os.ErrNotExist @@ -114,7 +114,7 @@ func TestDetectRuntimeUnix(t *testing.T) { runtimeType: ContainerRuntimeCommonCRI, runtimeRemoteURI: "unix://hostvarrun/crio/crio.sock", statFunc: func(name string) (os.FileInfo, error) { - if name == "hostvarrun/crio/crio.sock" { + if name == "/hostvarrun/crio/crio.sock" { return &fileInfo{name: name}, nil } return nil, os.ErrNotExist @@ -127,7 +127,7 @@ func TestDetectRuntimeUnix(t *testing.T) { runtimeType: ContainerRuntimeCommonCRI, runtimeRemoteURI: "unix://hostvarrun/cri-dockerd.sock", statFunc: func(name string) (os.FileInfo, error) { - if name == "hostvarrun/cri-dockerd.sock" { + if name == "/hostvarrun/cri-dockerd.sock" { return &fileInfo{name: name}, nil } return nil, os.ErrNotExist @@ -140,7 +140,7 @@ func TestDetectRuntimeUnix(t *testing.T) { runtimeType: ContainerRuntimeCommonCRI, runtimeRemoteURI: "unix://hostvarrun/cri-dockerd/cri-dockerd.sock", statFunc: func(name string) (os.FileInfo, error) { - if name == "hostvarrun/cri-dockerd/cri-dockerd.sock" { + if name == "/hostvarrun/cri-dockerd/cri-dockerd.sock" { return &fileInfo{name: name}, nil } return nil, os.ErrNotExist From b37ae58fd07b6c53f2e2ae11ca3f77941b9b09d6 Mon Sep 17 00:00:00 2001 From: Peng Peng Date: Wed, 26 Feb 2025 19:15:14 -0800 Subject: [PATCH 08/12] fix unit tests Signed-off-by: Peng Peng --- pkg/daemon/criruntime/factory_unix_test.go | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/pkg/daemon/criruntime/factory_unix_test.go b/pkg/daemon/criruntime/factory_unix_test.go index 0da475e42a..c00cc68552 100644 --- a/pkg/daemon/criruntime/factory_unix_test.go +++ b/pkg/daemon/criruntime/factory_unix_test.go @@ -50,7 +50,7 @@ func TestDetectRuntimeUnix(t *testing.T) { name: "non-existent-socket-with-flag", flag: "non-existent-socket", runtimeType: ContainerRuntimeCommonCRI, - runtimeRemoteURI: "unix://hostvarrun/non-existent-socket", + runtimeRemoteURI: "unix:///hostvarrun/non-existent-socket", statFunc: func(name string) (os.FileInfo, error) { return nil, os.ErrNotExist }, @@ -60,7 +60,7 @@ func TestDetectRuntimeUnix(t *testing.T) { name: "crio.sock-with-flag", flag: "crio.sock", runtimeType: ContainerRuntimeCommonCRI, - runtimeRemoteURI: "unix://hostvarrun/crio.sock", + runtimeRemoteURI: "unix:///hostvarrun/crio.sock", statFunc: func(name string) (os.FileInfo, error) { if name == "/hostvarrun/crio.sock" { return &fileInfo{name: name}, nil @@ -73,7 +73,7 @@ func TestDetectRuntimeUnix(t *testing.T) { name: "containerd.sock", flag: "", runtimeType: ContainerRuntimeContainerd, - runtimeRemoteURI: "unix://hostvarrun/containerd.sock", + runtimeRemoteURI: "unix:///hostvarrun/containerd.sock", statFunc: func(name string) (os.FileInfo, error) { if name == "/hostvarrun/containerd.sock" { return &fileInfo{name: name}, nil @@ -86,7 +86,7 @@ func TestDetectRuntimeUnix(t *testing.T) { name: "containerd/containerd.sock", flag: "", runtimeType: ContainerRuntimeContainerd, - runtimeRemoteURI: "unix://hostvarrun/containerd/containerd.sock", + runtimeRemoteURI: "unix:///hostvarrun/containerd/containerd.sock", statFunc: func(name string) (os.FileInfo, error) { if name == "/hostvarrun/containerd/containerd.sock" { return &fileInfo{name: name}, nil @@ -99,7 +99,7 @@ func TestDetectRuntimeUnix(t *testing.T) { name: "crio.sock", flag: "", runtimeType: ContainerRuntimeCommonCRI, - runtimeRemoteURI: "unix://hostvarrun/crio.sock", + runtimeRemoteURI: "unix:///hostvarrun/crio.sock", statFunc: func(name string) (os.FileInfo, error) { if name == "/hostvarrun/crio.sock" { return &fileInfo{name: name}, nil @@ -112,7 +112,7 @@ func TestDetectRuntimeUnix(t *testing.T) { name: "crio/crio.sock", flag: "", runtimeType: ContainerRuntimeCommonCRI, - runtimeRemoteURI: "unix://hostvarrun/crio/crio.sock", + runtimeRemoteURI: "unix:///hostvarrun/crio/crio.sock", statFunc: func(name string) (os.FileInfo, error) { if name == "/hostvarrun/crio/crio.sock" { return &fileInfo{name: name}, nil @@ -125,7 +125,7 @@ func TestDetectRuntimeUnix(t *testing.T) { name: "cri-dockerd", flag: "", runtimeType: ContainerRuntimeCommonCRI, - runtimeRemoteURI: "unix://hostvarrun/cri-dockerd.sock", + runtimeRemoteURI: "unix:///hostvarrun/cri-dockerd.sock", statFunc: func(name string) (os.FileInfo, error) { if name == "/hostvarrun/cri-dockerd.sock" { return &fileInfo{name: name}, nil @@ -138,7 +138,7 @@ func TestDetectRuntimeUnix(t *testing.T) { name: "cri-dockerd/cri-dockerd.sock", flag: "", runtimeType: ContainerRuntimeCommonCRI, - runtimeRemoteURI: "unix://hostvarrun/cri-dockerd/cri-dockerd.sock", + runtimeRemoteURI: "unix:///hostvarrun/cri-dockerd/cri-dockerd.sock", statFunc: func(name string) (os.FileInfo, error) { if name == "/hostvarrun/cri-dockerd/cri-dockerd.sock" { return &fileInfo{name: name}, nil @@ -151,7 +151,7 @@ func TestDetectRuntimeUnix(t *testing.T) { name: "non-existent-socket-without-flag", flag: "", runtimeType: ContainerRuntimeCommonCRI, - runtimeRemoteURI: "unix://hostvarrun/non-existent-socket", + runtimeRemoteURI: "unix:///hostvarrun/non-existent-socket", statFunc: func(name string) (os.FileInfo, error) { return nil, os.ErrNotExist }, From 230910d3bf923c4d0cb3ba44f30ad5510ee57715 Mon Sep 17 00:00:00 2001 From: Peng Peng Date: Wed, 26 Feb 2025 22:37:49 -0800 Subject: [PATCH 09/12] fix image service issue Signed-off-by: Peng Peng --- pkg/daemon/criruntime/factory.go | 2 +- pkg/daemon/criruntime/factory_unix.go | 12 ++++++++++++ pkg/daemon/criruntime/factory_windows.go | 9 +++++++++ pkg/daemon/criruntime/imageruntime/cri.go | 6 +++--- 4 files changed, 25 insertions(+), 4 deletions(-) diff --git a/pkg/daemon/criruntime/factory.go b/pkg/daemon/criruntime/factory.go index 8b7ecc7be7..3980b23413 100644 --- a/pkg/daemon/criruntime/factory.go +++ b/pkg/daemon/criruntime/factory.go @@ -82,7 +82,7 @@ func NewFactory(accountManager daemonutil.ImagePullAccountManager) (Factory, err var runtimeService criapi.RuntimeService var typedVersion *runtimeapi.VersionResponse - imageService, err = runtimeimage.NewCRIImageService(cfg.runtimeRemoteURI, accountManager) + imageService, err = newImageService(cfg, accountManager) if err != nil { klog.ErrorS(err, "Failed to new image service", "runtimeType", cfg.runtimeType, "runtimeURI", cfg.runtimeURI, "runtimeRemoteURI", cfg.runtimeRemoteURI) continue diff --git a/pkg/daemon/criruntime/factory_unix.go b/pkg/daemon/criruntime/factory_unix.go index 6006e58e82..daaf7c5075 100644 --- a/pkg/daemon/criruntime/factory_unix.go +++ b/pkg/daemon/criruntime/factory_unix.go @@ -25,6 +25,9 @@ import ( "os" "k8s.io/klog/v2" + kubeletutil "k8s.io/kubernetes/pkg/kubelet/util" + runtimeimage "github.com/openkruise/kruise/pkg/daemon/criruntime/imageruntime" + daemonutil "github.com/openkruise/kruise/pkg/daemon/util" ) const ( @@ -107,3 +110,12 @@ func detectRuntime() (cfgs []runtimeConfig) { } return cfgs } + +func newImageService(cfg runtimeConfig, accountManager daemonutil.ImagePullAccountManager) (runtimeimage.ImageService, error) { + addr, _, err := kubeletutil.GetAddressAndDialer(cfg.runtimeRemoteURI) + if err != nil { + klog.ErrorS(err, "Failed to get address", "runtimeType", cfg.runtimeType, "runtimeURI", cfg.runtimeURI, "runtimeRemoteURI", cfg.runtimeRemoteURI) + return nil, err + } + return runtimeimage.NewCRIImageService(addr, accountManager) +} \ No newline at end of file diff --git a/pkg/daemon/criruntime/factory_windows.go b/pkg/daemon/criruntime/factory_windows.go index 1dc172d795..3e4df7d60f 100644 --- a/pkg/daemon/criruntime/factory_windows.go +++ b/pkg/daemon/criruntime/factory_windows.go @@ -19,6 +19,11 @@ limitations under the License. package criruntime +import ( + daemonutil "github.com/openkruise/kruise/pkg/daemon/util" + runtimeimage "github.com/openkruise/kruise/pkg/daemon/criruntime/imageruntime" +) + var ( // containerdRemoteURI is the remote URI for containerd. // On Windows the default CRI endpoint is npipe://./pipe/containerd-containerd . @@ -35,3 +40,7 @@ func detectRuntime() (cfgs []runtimeConfig) { }) return cfgs } + +func newImageService(cfg runtimeConfig, accountManager daemonutil.ImagePullAccountManager) (runtimeimage.ImageService, error) { + return runtimeimage.NewCRIImageService(cfg.runtimeRemoteURI, accountManager) +} diff --git a/pkg/daemon/criruntime/imageruntime/cri.go b/pkg/daemon/criruntime/imageruntime/cri.go index b8a1916cc0..9de9abc591 100644 --- a/pkg/daemon/criruntime/imageruntime/cri.go +++ b/pkg/daemon/criruntime/imageruntime/cri.go @@ -40,9 +40,9 @@ const ( ) // NewCRIImageService create a common CRI runtime -func NewCRIImageService(remoteRuntimeURI string, accountManager daemonutil.ImagePullAccountManager) (ImageService, error) { - klog.V(3).InfoS("Connecting to image service", "endpoint", remoteRuntimeURI) - addr, dialer, err := util.GetAddressAndDialer(remoteRuntimeURI) +func NewCRIImageService(runtimeURI string, accountManager daemonutil.ImagePullAccountManager) (ImageService, error) { + klog.V(3).InfoS("Connecting to image service", "endpoint", runtimeURI) + addr, dialer, err := util.GetAddressAndDialer(runtimeURI) if err != nil { return nil, err } From 426ae950290ac366ffd4718d898ca2b164e82618 Mon Sep 17 00:00:00 2001 From: Peng Peng Date: Wed, 26 Feb 2025 22:56:08 -0800 Subject: [PATCH 10/12] fix golangci-lint issues Signed-off-by: Peng Peng --- pkg/daemon/criruntime/factory_unix.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/pkg/daemon/criruntime/factory_unix.go b/pkg/daemon/criruntime/factory_unix.go index daaf7c5075..8c0bc85942 100644 --- a/pkg/daemon/criruntime/factory_unix.go +++ b/pkg/daemon/criruntime/factory_unix.go @@ -24,10 +24,10 @@ import ( "fmt" "os" - "k8s.io/klog/v2" - kubeletutil "k8s.io/kubernetes/pkg/kubelet/util" runtimeimage "github.com/openkruise/kruise/pkg/daemon/criruntime/imageruntime" daemonutil "github.com/openkruise/kruise/pkg/daemon/util" + "k8s.io/klog/v2" + kubeletutil "k8s.io/kubernetes/pkg/kubelet/util" ) const ( @@ -118,4 +118,4 @@ func newImageService(cfg runtimeConfig, accountManager daemonutil.ImagePullAccou return nil, err } return runtimeimage.NewCRIImageService(addr, accountManager) -} \ No newline at end of file +} From 23080cd61c1fa4a84a4329e9bde9a9f96ce2ba8d Mon Sep 17 00:00:00 2001 From: Peng Peng Date: Wed, 26 Feb 2025 23:53:36 -0800 Subject: [PATCH 11/12] minor fix Signed-off-by: Peng Peng --- pkg/daemon/criruntime/factory_windows.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/daemon/criruntime/factory_windows.go b/pkg/daemon/criruntime/factory_windows.go index 3e4df7d60f..b3965a82a3 100644 --- a/pkg/daemon/criruntime/factory_windows.go +++ b/pkg/daemon/criruntime/factory_windows.go @@ -20,8 +20,8 @@ limitations under the License. package criruntime import ( - daemonutil "github.com/openkruise/kruise/pkg/daemon/util" runtimeimage "github.com/openkruise/kruise/pkg/daemon/criruntime/imageruntime" + daemonutil "github.com/openkruise/kruise/pkg/daemon/util" ) var ( From 07cfdfdd932ad254baff4374065948d7d387d592 Mon Sep 17 00:00:00 2001 From: Peng Peng Date: Tue, 4 Mar 2025 17:48:46 -0800 Subject: [PATCH 12/12] Add makefile entries for Windows daemon Signed-off-by: Peng Peng --- Dockerfile_windows | 2 +- Makefile | 8 ++++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/Dockerfile_windows b/Dockerfile_windows index a83085a066..aa4fbd6701 100644 --- a/Dockerfile_windows +++ b/Dockerfile_windows @@ -6,6 +6,6 @@ ARG BASE_IMAGE_VERSION=v1.0.0 FROM ${BASE_IMAGE}:${BASE_IMAGE_VERSION} WORKDIR / -COPY ./daemon.exe /kruise-daemon.exe +COPY ./bin/kruise-daemon.exe . ENTRYPOINT ["kruise-daemon.exe"] \ No newline at end of file diff --git a/Makefile b/Makefile index e252c2ccf2..fb52264a71 100644 --- a/Makefile +++ b/Makefile @@ -1,8 +1,10 @@ # Image URL to use all building/pushing image targets IMG ?= openkruise/kruise-manager:test HOOK_IMG ?= openkruise/kruise-helm-hook:test +WIN_DAEMON_IMG ?= openkruise/kruise-daemon-win:test # Platforms to build the image for PLATFORMS ?= linux/amd64,linux/arm64,linux/ppc64le +WIN_PLATFORMS ?= windows/amd64 CRD_OPTIONS ?= "crd:crdVersions=v1" # Get the currently used golang install path (in GOPATH/bin, unless GOBIN is set) @@ -72,6 +74,9 @@ endif build: generate fmt vet manifests ## Build manager binary. go build -o bin/manager main.go +build-win-daemon: ## Build Windows daemon binary. + GOOS=windows go build -o bin/kruise-daemon.exe ./cmd/daemon/main.go + run: manifests generate fmt vet ## Run a controller from your host. go run ./main.go @@ -81,6 +86,9 @@ docker-build: ## Build docker image with the manager. docker-push: ## Push docker image with the manager. docker push ${IMG} +docker-win-daemon: # Build Windows docker image with the daemon + docker buildx build -f ./Dockerfile_windows --pull --no-cache --platform=$(WIN_PLATFORMS) . -t $(WIN_DAEMON_IMG) + # Build and push the multiarchitecture docker images and manifest. docker-multiarch: docker buildx build -f ./Dockerfile_multiarch --pull --no-cache --platform=$(PLATFORMS) --push . -t $(IMG)