From 310bafe658bce686e7286f9b5a0d7e4336581ecd Mon Sep 17 00:00:00 2001 From: ksubrmnn Date: Wed, 22 May 2019 10:34:28 -0700 Subject: [PATCH] Implement CRI detection for Windows --- .../apis/kubeadm/v1beta1/defaults_windows.go | 2 +- .../apis/kubeadm/v1beta2/defaults_windows.go | 2 +- .../app/constants/constants_windows.go | 2 +- cmd/kubeadm/app/util/runtime/BUILD | 13 ++++++- cmd/kubeadm/app/util/runtime/runtime.go | 20 ---------- cmd/kubeadm/app/util/runtime/runtime_unix.go | 38 +++++++++++++++++++ .../app/util/runtime/runtime_windows.go | 38 +++++++++++++++++++ 7 files changed, 90 insertions(+), 25 deletions(-) create mode 100644 cmd/kubeadm/app/util/runtime/runtime_unix.go create mode 100644 cmd/kubeadm/app/util/runtime/runtime_windows.go diff --git a/cmd/kubeadm/app/apis/kubeadm/v1beta1/defaults_windows.go b/cmd/kubeadm/app/apis/kubeadm/v1beta1/defaults_windows.go index 85231880198..3e2ab2bed5d 100644 --- a/cmd/kubeadm/app/apis/kubeadm/v1beta1/defaults_windows.go +++ b/cmd/kubeadm/app/apis/kubeadm/v1beta1/defaults_windows.go @@ -22,5 +22,5 @@ const ( // DefaultCACertPath defines default location of CA certificate on Windows DefaultCACertPath = "C:/etc/kubernetes/pki/ca.crt" // DefaultUrlScheme defines default socket url prefix - DefaultUrlScheme = "tcp" + DefaultUrlScheme = "npipe" ) diff --git a/cmd/kubeadm/app/apis/kubeadm/v1beta2/defaults_windows.go b/cmd/kubeadm/app/apis/kubeadm/v1beta2/defaults_windows.go index 7d4d3b82cb0..9f0986db46e 100644 --- a/cmd/kubeadm/app/apis/kubeadm/v1beta2/defaults_windows.go +++ b/cmd/kubeadm/app/apis/kubeadm/v1beta2/defaults_windows.go @@ -22,5 +22,5 @@ const ( // DefaultCACertPath defines default location of CA certificate on Windows DefaultCACertPath = "C:/etc/kubernetes/pki/ca.crt" // DefaultUrlScheme defines default socket url prefix - DefaultUrlScheme = "tcp" + DefaultUrlScheme = "npipe" ) diff --git a/cmd/kubeadm/app/constants/constants_windows.go b/cmd/kubeadm/app/constants/constants_windows.go index 4ee63a3ef4a..6daae0a1fff 100644 --- a/cmd/kubeadm/app/constants/constants_windows.go +++ b/cmd/kubeadm/app/constants/constants_windows.go @@ -20,5 +20,5 @@ package constants const ( // DefaultDockerCRISocket defines the default Docker CRI socket - DefaultDockerCRISocket = "tcp://localhost:2375" + DefaultDockerCRISocket = "npipe:////./pipe/docker_engine" ) diff --git a/cmd/kubeadm/app/util/runtime/BUILD b/cmd/kubeadm/app/util/runtime/BUILD index 27e839bb8d5..e320f461cf0 100644 --- a/cmd/kubeadm/app/util/runtime/BUILD +++ b/cmd/kubeadm/app/util/runtime/BUILD @@ -2,7 +2,11 @@ load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test") go_library( name = "go_default_library", - srcs = ["runtime.go"], + srcs = [ + "runtime.go", + "runtime_unix.go", + "runtime_windows.go", + ], importpath = "k8s.io/kubernetes/cmd/kubeadm/app/util/runtime", visibility = ["//visibility:public"], deps = [ @@ -10,7 +14,12 @@ go_library( "//staging/src/k8s.io/apimachinery/pkg/util/errors:go_default_library", "//vendor/github.com/pkg/errors:go_default_library", "//vendor/k8s.io/utils/exec:go_default_library", - ], + ] + select({ + "@io_bazel_rules_go//go/platform:windows": [ + "//vendor/github.com/Microsoft/go-winio:go_default_library", + ], + "//conditions:default": [], + }), ) go_test( diff --git a/cmd/kubeadm/app/util/runtime/runtime.go b/cmd/kubeadm/app/util/runtime/runtime.go index bd4a168db8f..71e2c04521a 100644 --- a/cmd/kubeadm/app/util/runtime/runtime.go +++ b/cmd/kubeadm/app/util/runtime/runtime.go @@ -17,7 +17,6 @@ limitations under the License. package util import ( - "os" "path/filepath" goruntime "runtime" "strings" @@ -180,23 +179,8 @@ func (runtime *DockerRuntime) ImageExists(image string) (bool, error) { return err == nil, nil } -// isExistingSocket checks if path exists and is domain socket -func isExistingSocket(path string) bool { - fileInfo, err := os.Stat(path) - if err != nil { - return false - } - - return fileInfo.Mode()&os.ModeSocket != 0 -} - // detectCRISocketImpl is separated out only for test purposes, DON'T call it directly, use DetectCRISocket instead func detectCRISocketImpl(isSocket func(string) bool) (string, error) { - const ( - dockerSocket = "/var/run/docker.sock" // The Docker socket is not CRI compatible - containerdSocket = "/run/containerd/containerd.sock" - ) - foundCRISockets := []string{} knownCRISockets := []string{ // Docker and containerd sockets are special cased below, hence not to be included here @@ -233,9 +217,5 @@ func detectCRISocketImpl(isSocket func(string) bool) (string, error) { // DetectCRISocket uses a list of known CRI sockets to detect one. If more than one or none is discovered, an error is returned. func DetectCRISocket() (string, error) { - if goruntime.GOOS != "linux" { - return constants.DefaultDockerCRISocket, nil - } - return detectCRISocketImpl(isExistingSocket) } diff --git a/cmd/kubeadm/app/util/runtime/runtime_unix.go b/cmd/kubeadm/app/util/runtime/runtime_unix.go new file mode 100644 index 00000000000..b15c3037313 --- /dev/null +++ b/cmd/kubeadm/app/util/runtime/runtime_unix.go @@ -0,0 +1,38 @@ +// +build !windows + +/* +Copyright 2019 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 util + +import ( + "os" +) + +const ( + dockerSocket = "/var/run/docker.sock" // The Docker socket is not CRI compatible + containerdSocket = "/run/containerd/containerd.sock" +) + +// isExistingSocket checks if path exists and is domain socket +func isExistingSocket(path string) bool { + fileInfo, err := os.Stat(path) + if err != nil { + return false + } + + return fileInfo.Mode()&os.ModeSocket != 0 +} diff --git a/cmd/kubeadm/app/util/runtime/runtime_windows.go b/cmd/kubeadm/app/util/runtime/runtime_windows.go new file mode 100644 index 00000000000..0c6a7b496dc --- /dev/null +++ b/cmd/kubeadm/app/util/runtime/runtime_windows.go @@ -0,0 +1,38 @@ +// +build windows + +/* +Copyright 2019 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 util + +import ( + winio "github.com/Microsoft/go-winio" +) + +const ( + dockerSocket = "//./pipe/docker_engine" // The Docker socket is not CRI compatible + containerdSocket = "//./pipe/containerd-containerd" // Proposed containerd named pipe for Windows +) + +// isExistingSocket checks if path exists and is domain socket +func isExistingSocket(path string) bool { + _, err := winio.DialPipe(path, nil) + if err != nil { + return false + } + + return true +}