Skip to content

Commit

Permalink
Added NormalizePath func, and consumed the SysSpec changes from PR ku…
Browse files Browse the repository at this point in the history
  • Loading branch information
bsteciuk committed Oct 11, 2017
1 parent 63b39fe commit f8622ff
Show file tree
Hide file tree
Showing 8 changed files with 141 additions and 25 deletions.
1 change: 1 addition & 0 deletions cmd/kubeadm/app/apis/kubeadm/v1alpha1/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
13 changes: 2 additions & 11 deletions cmd/kubeadm/app/apis/kubeadm/v1alpha1/defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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
Expand All @@ -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
}
}
21 changes: 8 additions & 13 deletions cmd/kubeadm/app/preflight/checks.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
10 changes: 9 additions & 1 deletion cmd/kubeadm/app/util/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -32,6 +39,7 @@ go_test(
"arguments_test.go",
"endpoint_test.go",
"error_test.go",
"path_test.go",
"template_test.go",
"version_test.go",
],
Expand Down
36 changes: 36 additions & 0 deletions cmd/kubeadm/app/util/path.go
Original file line number Diff line number Diff line change
@@ -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
}
29 changes: 29 additions & 0 deletions cmd/kubeadm/app/util/path_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
}
28 changes: 28 additions & 0 deletions cmd/kubeadm/app/util/path_test_unix.go
Original file line number Diff line number Diff line change
@@ -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

}
28 changes: 28 additions & 0 deletions cmd/kubeadm/app/util/path_test_windows.go
Original file line number Diff line number Diff line change
@@ -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

}

0 comments on commit f8622ff

Please sign in to comment.