-
Notifications
You must be signed in to change notification settings - Fork 39.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
kubeadm-init: add --copy-credentials-for-user
Using this new parameter the administrator's credentials for the cluster will be copied to the specified user's home directory. WARNING: existing files will be overwritten. Usage: kubeadm init --copy-credentials-for-user=<someuser> Windows requires a different approach for copying the credentials than Unix, thus a new package is added - `platform` in `kubeadm/app/cmd`. It contains `init_unix.go` and `init_windows.go` that define the exported function - InitCopyCredentialsForUser(). The function works differently on Unix and Windows. The same package can be re-used for other multiplatform code.
- Loading branch information
Showing
6 changed files
with
322 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
load("@io_bazel_rules_go//go:def.bzl", "go_library") | ||
|
||
go_library( | ||
name = "go_default_library", | ||
srcs = [ | ||
"init_shared.go", | ||
] + select({ | ||
"@io_bazel_rules_go//go/platform:android": [ | ||
"init_unix.go", | ||
], | ||
"@io_bazel_rules_go//go/platform:darwin": [ | ||
"init_unix.go", | ||
], | ||
"@io_bazel_rules_go//go/platform:dragonfly": [ | ||
"init_unix.go", | ||
], | ||
"@io_bazel_rules_go//go/platform:freebsd": [ | ||
"init_unix.go", | ||
], | ||
"@io_bazel_rules_go//go/platform:linux": [ | ||
"init_unix.go", | ||
], | ||
"@io_bazel_rules_go//go/platform:nacl": [ | ||
"init_unix.go", | ||
], | ||
"@io_bazel_rules_go//go/platform:netbsd": [ | ||
"init_unix.go", | ||
], | ||
"@io_bazel_rules_go//go/platform:openbsd": [ | ||
"init_unix.go", | ||
], | ||
"@io_bazel_rules_go//go/platform:plan9": [ | ||
"init_unix.go", | ||
], | ||
"@io_bazel_rules_go//go/platform:solaris": [ | ||
"init_unix.go", | ||
], | ||
"@io_bazel_rules_go//go/platform:windows": [ | ||
"init_windows.go", | ||
], | ||
"//conditions:default": [], | ||
}), | ||
importpath = "k8s.io/kubernetes/cmd/kubeadm/app/cmd/platform", | ||
visibility = ["//visibility:public"], | ||
) | ||
|
||
filegroup( | ||
name = "package-srcs", | ||
srcs = glob(["**"]), | ||
tags = ["automanaged"], | ||
visibility = ["//visibility:private"], | ||
) | ||
|
||
filegroup( | ||
name = "all-srcs", | ||
srcs = [":package-srcs"], | ||
tags = ["automanaged"], | ||
visibility = ["//visibility:public"], | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/* | ||
Copyright 2018 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 platform | ||
|
||
// shared constants for init | ||
const ( | ||
InitErrorNoSuchUser = "no such user %q: %v" | ||
InitErrorUserIsRoot = "user is root %q" | ||
InitErrorNoHomeDir = "cannot obtain the home directory for user %q" | ||
InitErrorAtoiString = "cannot Atoi() UID/GID string %q: %v" | ||
InitErrorCannotCreateDir = "cannot create %q: %v" | ||
InitErrorCannotChown = "cannot chown %q: %v" | ||
InitErrorCannotOpenFileForReading = "cannot open file for reading %q: %v" | ||
InitErrorCannotOpenFileForWriting = "cannot open file for writing %q: %v" | ||
InitErrorCannotIoCopy = "cannot io.Copy() the configuration: %v" | ||
|
||
InitMessageCopyingCredentials = "[init] Copying administrator credentials to %q from %q\n" | ||
|
||
InitPathKube = "/.kube" | ||
InitPathConfig = "/config" | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
// +build !windows | ||
|
||
/* | ||
Copyright 2018 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 platform | ||
|
||
import ( | ||
"fmt" | ||
"io" | ||
"os" | ||
"os/user" | ||
"strconv" | ||
) | ||
|
||
// InitCopyCredentialsForUser will copy the cluster admin credentials to the home path of a non-root user | ||
func InitCopyCredentialsForUser(copyCredentialsForUser string, adminKubeConfigPath string) error { | ||
|
||
var err error | ||
var uid, gid int | ||
var usr *user.User | ||
var src, dest *os.File | ||
var kubeDir, configFilePath string | ||
|
||
usr, err = user.Lookup(copyCredentialsForUser) | ||
if err != nil { | ||
return fmt.Errorf(InitErrorNoSuchUser, copyCredentialsForUser, err) | ||
} | ||
|
||
if usr.Gid == "0" { | ||
return fmt.Errorf(InitErrorUserIsRoot, copyCredentialsForUser) | ||
} | ||
|
||
if usr.HomeDir == "" { | ||
return fmt.Errorf(InitErrorNoHomeDir, copyCredentialsForUser) | ||
} | ||
kubeDir = usr.HomeDir + InitPathKube | ||
configFilePath = kubeDir + InitPathConfig | ||
|
||
fmt.Printf(InitMessageCopyingCredentials, configFilePath, adminKubeConfigPath) | ||
|
||
uid, err = strconv.Atoi(usr.Uid) | ||
if err != nil { | ||
return fmt.Errorf(InitErrorAtoiString, usr.Uid, err) | ||
} | ||
|
||
gid, err = strconv.Atoi(usr.Gid) | ||
if err != nil { | ||
return fmt.Errorf(InitErrorAtoiString, usr.Gid, err) | ||
} | ||
|
||
if err := os.MkdirAll(kubeDir, 0700); err != nil { | ||
return fmt.Errorf(InitErrorCannotCreateDir, kubeDir, err) | ||
} | ||
|
||
// Chown doesn't work on Windows | ||
if err := os.Chown(kubeDir, uid, gid); err != nil { | ||
return fmt.Errorf(InitErrorCannotChown, kubeDir, err) | ||
} | ||
|
||
src, err = os.Open(adminKubeConfigPath) | ||
if err != nil { | ||
return fmt.Errorf(InitErrorCannotOpenFileForReading, adminKubeConfigPath, err) | ||
} | ||
defer src.Close() | ||
|
||
dest, err = os.OpenFile(configFilePath, os.O_RDWR|os.O_CREATE, 0700) | ||
if err != nil { | ||
return fmt.Errorf(InitErrorCannotOpenFileForWriting, configFilePath, err) | ||
} | ||
defer dest.Close() | ||
|
||
if _, err := io.Copy(dest, src); err != nil { | ||
return fmt.Errorf(InitErrorCannotIoCopy, err) | ||
} | ||
|
||
if err := os.Chown(configFilePath, uid, gid); err != nil { | ||
return fmt.Errorf(InitErrorCannotChown, configFilePath, err) | ||
} | ||
|
||
return nil | ||
} |
Oops, something went wrong.