From f8622ffa34bad94ed0a422c9312bfbce2690e9b3 Mon Sep 17 00:00:00 2001 From: Bob Steciuk Date: Wed, 11 Oct 2017 19:31:36 -0400 Subject: [PATCH] Added NormalizePath func, and consumed the SysSpec changes from PR #53730 --- cmd/kubeadm/app/apis/kubeadm/v1alpha1/BUILD | 1 + .../app/apis/kubeadm/v1alpha1/defaults.go | 13 ++----- cmd/kubeadm/app/preflight/checks.go | 21 +++++------ cmd/kubeadm/app/util/BUILD | 10 +++++- cmd/kubeadm/app/util/path.go | 36 +++++++++++++++++++ cmd/kubeadm/app/util/path_test.go | 29 +++++++++++++++ cmd/kubeadm/app/util/path_test_unix.go | 28 +++++++++++++++ cmd/kubeadm/app/util/path_test_windows.go | 28 +++++++++++++++ 8 files changed, 141 insertions(+), 25 deletions(-) create mode 100644 cmd/kubeadm/app/util/path.go create mode 100644 cmd/kubeadm/app/util/path_test.go create mode 100644 cmd/kubeadm/app/util/path_test_unix.go create mode 100644 cmd/kubeadm/app/util/path_test_windows.go diff --git a/cmd/kubeadm/app/apis/kubeadm/v1alpha1/BUILD b/cmd/kubeadm/app/apis/kubeadm/v1alpha1/BUILD index b75dec730b3f9..cf62fc4cb4eb0 100644 --- a/cmd/kubeadm/app/apis/kubeadm/v1alpha1/BUILD +++ b/cmd/kubeadm/app/apis/kubeadm/v1alpha1/BUILD @@ -19,6 +19,7 @@ go_library( deps = [ "//cmd/kubeadm/app/apis/kubeadm:go_default_library", "//cmd/kubeadm/app/constants:go_default_library", + "//cmd/kubeadm/app/util:go_default_library", "//vendor/k8s.io/apimachinery/pkg/apis/meta/v1:go_default_library", "//vendor/k8s.io/apimachinery/pkg/conversion:go_default_library", "//vendor/k8s.io/apimachinery/pkg/runtime:go_default_library", diff --git a/cmd/kubeadm/app/apis/kubeadm/v1alpha1/defaults.go b/cmd/kubeadm/app/apis/kubeadm/v1alpha1/defaults.go index fe2442ead6706..0d4e25493ce0c 100644 --- a/cmd/kubeadm/app/apis/kubeadm/v1alpha1/defaults.go +++ b/cmd/kubeadm/app/apis/kubeadm/v1alpha1/defaults.go @@ -18,13 +18,12 @@ package v1alpha1 import ( "net/url" - "path/filepath" - "runtime" "strings" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" kruntime "k8s.io/apimachinery/pkg/runtime" "k8s.io/kubernetes/cmd/kubeadm/app/constants" + "k8s.io/kubernetes/cmd/kubeadm/app/util" ) const ( @@ -96,7 +95,7 @@ func SetDefaults_MasterConfiguration(obj *MasterConfiguration) { // SetDefaults_NodeConfiguration assigns default values to a regular node func SetDefaults_NodeConfiguration(obj *NodeConfiguration) { if obj.CACertPath == "" { - obj.CACertPath = getDefaultCACertPath(runtime.GOOS) + obj.CACertPath = util.NormalizePath(DefaultCACertPath) } if len(obj.TLSBootstrapToken) == 0 { obj.TLSBootstrapToken = obj.Token @@ -112,11 +111,3 @@ func SetDefaults_NodeConfiguration(obj *NodeConfiguration) { } } } - -func getDefaultCACertPath(runtimeOS string) string { - if runtimeOS == "windows" { - return filepath.Join("C:", DefaultCACertPath) - } else { - return DefaultCACertPath - } -} diff --git a/cmd/kubeadm/app/preflight/checks.go b/cmd/kubeadm/app/preflight/checks.go index 9375c11bdc414..de5a1f42edf94 100644 --- a/cmd/kubeadm/app/preflight/checks.go +++ b/cmd/kubeadm/app/preflight/checks.go @@ -429,20 +429,15 @@ func (sysver SystemVerificationCheck) Check() (warnings, errors []error) { &system.CgroupsValidator{Reporter: reporter}) } - sysSpec, ok := system.SysSpecsByOS[runtimeOS] - if ok { - // Run all validators - for _, v := range validators { - warn, err := v.Validate(sysSpec) - if err != nil { - errs = append(errs, err) - } - if warn != nil { - warns = append(warns, warn) - } + // Run all validators + for _, v := range validators { + warn, err := v.Validate(system.DefaultSysSpec) + if err != nil { + errs = append(errs, err) + } + if warn != nil { + warns = append(warns, warn) } - } else { - errs = append(errs, fmt.Errorf("couldn't find system spec for os: %s", runtimeOS)) } if len(errs) != 0 { diff --git a/cmd/kubeadm/app/util/BUILD b/cmd/kubeadm/app/util/BUILD index 91b6f2a705526..8ad4885aa29fc 100644 --- a/cmd/kubeadm/app/util/BUILD +++ b/cmd/kubeadm/app/util/BUILD @@ -13,9 +13,16 @@ go_library( "endpoint.go", "error.go", "marshal.go", + "path.go", + "path_test_unix.go", "template.go", "version.go", - ], + ] + select({ + "@io_bazel_rules_go//go/platform:windows_amd64": [ + "path_test_windows.go", + ], + "//conditions:default": [], + }), deps = [ "//cmd/kubeadm/app/apis/kubeadm:go_default_library", "//cmd/kubeadm/app/preflight:go_default_library", @@ -32,6 +39,7 @@ go_test( "arguments_test.go", "endpoint_test.go", "error_test.go", + "path_test.go", "template_test.go", "version_test.go", ], diff --git a/cmd/kubeadm/app/util/path.go b/cmd/kubeadm/app/util/path.go new file mode 100644 index 0000000000000..cc288a841f0ad --- /dev/null +++ b/cmd/kubeadm/app/util/path.go @@ -0,0 +1,36 @@ +/* +Copyright 2017 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 ( + "path/filepath" + "runtime" +) + +// NormalizePath takes a slash-separated path (which would be a valid unix path) and ensures it is in the correct +// format for the os in which it is currently running (currently used to convert unix path to windows path) +// ex given "/etc/kubernetes/pki/ca.crt" on windows, it will return "C:\etc\kubernetes\pki\ca.crt" +// on linux, the call is a no-op +func NormalizePath(path string) string { + if runtime.GOOS == "windows" { + if filepath.VolumeName(path) == "" { + return filepath.FromSlash(filepath.Join("C:", path)) + } + return filepath.FromSlash(path) + } + return path +} diff --git a/cmd/kubeadm/app/util/path_test.go b/cmd/kubeadm/app/util/path_test.go new file mode 100644 index 0000000000000..c702c6bebd1f6 --- /dev/null +++ b/cmd/kubeadm/app/util/path_test.go @@ -0,0 +1,29 @@ +/* +Copyright 2017 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 "testing" + +// TestNormalizePath tests that the NormalizePath function transforms (or doesn't) the given path correctly. +func TestNormalizePath(t *testing.T) { + for _, test := range normalizePathTests { + got := NormalizePath(test.input) + if test.expected != got { + t.Errorf("NormalizePath(%s) = %s, want %s", test.input, got, test.expected) + } + } +} diff --git a/cmd/kubeadm/app/util/path_test_unix.go b/cmd/kubeadm/app/util/path_test_unix.go new file mode 100644 index 0000000000000..ee4010b1cd994 --- /dev/null +++ b/cmd/kubeadm/app/util/path_test_unix.go @@ -0,0 +1,28 @@ +// +build !windows + +/* +Copyright 2017 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 + +var normalizePathTests = []struct { + input string + expected string +}{ + {"/etc/kubernetes/pki/ca.crt", `/etc/kubernetes/pki/ca.crt`}, //unix - noop + {`C:\etc\kubernetes\pki\ca.crt`, `C:\etc\kubernetes\pki\ca.crt`}, //windows - also noop for linux + +} diff --git a/cmd/kubeadm/app/util/path_test_windows.go b/cmd/kubeadm/app/util/path_test_windows.go new file mode 100644 index 0000000000000..e1d8dd32d2570 --- /dev/null +++ b/cmd/kubeadm/app/util/path_test_windows.go @@ -0,0 +1,28 @@ +// +build windows + +/* +Copyright 2017 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 + +var normalizePathTests = []struct { + input string + expected string +}{ + {"/etc/kubernetes/pki/ca.crt", `C:\etc\kubernetes\pki\ca.crt`}, //unix to windows + {`C:\etc\kubernetes\pki\ca.crt`, `C:\etc\kubernetes\pki\ca.crt`}, //windows to windows + +}