Skip to content

Commit 9ad296a

Browse files
authored
Merge pull request kubernetes#75056 from timothysc/configuser
Revert "Merge pull request kubernetes#74628 from hpandeycodeit/alpha_kubeconf"
2 parents 57da888 + ef31616 commit 9ad296a

File tree

4 files changed

+224
-1
lines changed

4 files changed

+224
-1
lines changed

cmd/kubeadm/app/cmd/alpha/BUILD

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ go_library(
55
srcs = [
66
"alpha.go",
77
"certs.go",
8+
"kubeconfig.go",
89
"kubelet.go",
910
"selfhosting.go",
1011
],
@@ -21,6 +22,7 @@ go_library(
2122
"//cmd/kubeadm/app/features:go_default_library",
2223
"//cmd/kubeadm/app/phases/certs:go_default_library",
2324
"//cmd/kubeadm/app/phases/certs/renewal:go_default_library",
25+
"//cmd/kubeadm/app/phases/kubeconfig:go_default_library",
2426
"//cmd/kubeadm/app/phases/kubelet:go_default_library",
2527
"//cmd/kubeadm/app/phases/selfhosting:go_default_library",
2628
"//cmd/kubeadm/app/preflight:go_default_library",
@@ -52,7 +54,10 @@ filegroup(
5254

5355
go_test(
5456
name = "go_default_test",
55-
srcs = ["certs_test.go"],
57+
srcs = [
58+
"certs_test.go",
59+
"kubeconfig_test.go",
60+
],
5661
embed = [":go_default_library"],
5762
deps = [
5863
"//cmd/kubeadm/app/constants:go_default_library",
@@ -61,6 +66,8 @@ go_test(
6166
"//cmd/kubeadm/app/util/pkiutil:go_default_library",
6267
"//cmd/kubeadm/test:go_default_library",
6368
"//cmd/kubeadm/test/cmd:go_default_library",
69+
"//cmd/kubeadm/test/kubeconfig:go_default_library",
70+
"//staging/src/k8s.io/client-go/tools/clientcmd:go_default_library",
6471
"//vendor/github.com/spf13/cobra:go_default_library",
6572
],
6673
)

cmd/kubeadm/app/cmd/alpha/alpha.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ func NewCmdAlpha(in io.Reader, out io.Writer) *cobra.Command {
3232

3333
cmd.AddCommand(newCmdCertsUtility())
3434
cmd.AddCommand(newCmdKubeletUtility())
35+
cmd.AddCommand(newCmdKubeConfigUtility(out))
3536
cmd.AddCommand(NewCmdSelfhosting(in))
3637

3738
// TODO: This command should be removed as soon as the kubeadm init phase refactoring is completed.
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
/*
2+
Copyright 2018 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package alpha
18+
19+
import (
20+
"io"
21+
22+
"github.com/pkg/errors"
23+
"github.com/spf13/cobra"
24+
kubeadmscheme "k8s.io/kubernetes/cmd/kubeadm/app/apis/kubeadm/scheme"
25+
kubeadmapiv1beta1 "k8s.io/kubernetes/cmd/kubeadm/app/apis/kubeadm/v1beta1"
26+
cmdutil "k8s.io/kubernetes/cmd/kubeadm/app/cmd/util"
27+
kubeconfigphase "k8s.io/kubernetes/cmd/kubeadm/app/phases/kubeconfig"
28+
kubeadmutil "k8s.io/kubernetes/cmd/kubeadm/app/util"
29+
configutil "k8s.io/kubernetes/cmd/kubeadm/app/util/config"
30+
"k8s.io/kubernetes/pkg/util/normalizer"
31+
)
32+
33+
var (
34+
kubeconfigLongDesc = normalizer.LongDesc(`
35+
Kubeconfig file utilities.
36+
` + cmdutil.AlphaDisclaimer)
37+
38+
userKubeconfigLongDesc = normalizer.LongDesc(`
39+
Outputs a kubeconfig file for an additional user.
40+
` + cmdutil.AlphaDisclaimer)
41+
42+
userKubeconfigExample = normalizer.Examples(`
43+
# Outputs a kubeconfig file for an additional user named foo
44+
kubeadm alpha kubeconfig user --client-name=foo
45+
`)
46+
)
47+
48+
// newCmdKubeConfigUtility returns main command for kubeconfig phase
49+
func newCmdKubeConfigUtility(out io.Writer) *cobra.Command {
50+
cmd := &cobra.Command{
51+
Use: "kubeconfig",
52+
Short: "Kubeconfig file utilities",
53+
Long: kubeconfigLongDesc,
54+
}
55+
56+
cmd.AddCommand(newCmdUserKubeConfig(out))
57+
return cmd
58+
}
59+
60+
// newCmdUserKubeConfig returns sub commands for kubeconfig phase
61+
func newCmdUserKubeConfig(out io.Writer) *cobra.Command {
62+
63+
cfg := &kubeadmapiv1beta1.InitConfiguration{}
64+
65+
// Default values for the cobra help text
66+
kubeadmscheme.Scheme.Default(cfg)
67+
68+
var token, clientName string
69+
var organizations []string
70+
71+
// Creates the UX Command
72+
cmd := &cobra.Command{
73+
Use: "user",
74+
Short: "Outputs a kubeconfig file for an additional user",
75+
Long: userKubeconfigLongDesc,
76+
Example: userKubeconfigExample,
77+
Run: func(cmd *cobra.Command, args []string) {
78+
if clientName == "" {
79+
kubeadmutil.CheckErr(errors.New("missing required argument --client-name"))
80+
}
81+
82+
// This call returns the ready-to-use configuration based on the default cfg populated by flags
83+
internalcfg, err := configutil.DefaultedInitConfiguration(cfg)
84+
kubeadmutil.CheckErr(err)
85+
86+
// if the kubeconfig file for an additional user has to use a token, use it
87+
if token != "" {
88+
kubeadmutil.CheckErr(kubeconfigphase.WriteKubeConfigWithToken(out, internalcfg, clientName, token))
89+
return
90+
}
91+
92+
// Otherwise, write a kubeconfig file with a generate client cert
93+
kubeadmutil.CheckErr(kubeconfigphase.WriteKubeConfigWithClientCert(out, internalcfg, clientName, organizations))
94+
},
95+
}
96+
97+
// Add flags to the command
98+
cmd.Flags().StringVar(&cfg.CertificatesDir, "cert-dir", cfg.CertificatesDir, "The path where certificates are stored")
99+
cmd.Flags().StringVar(&cfg.LocalAPIEndpoint.AdvertiseAddress, "apiserver-advertise-address", cfg.LocalAPIEndpoint.AdvertiseAddress, "The IP address the API server is accessible on")
100+
cmd.Flags().Int32Var(&cfg.LocalAPIEndpoint.BindPort, "apiserver-bind-port", cfg.LocalAPIEndpoint.BindPort, "The port the API server is accessible on")
101+
cmd.Flags().StringVar(&token, "token", token, "The token that should be used as the authentication mechanism for this kubeconfig, instead of client certificates")
102+
cmd.Flags().StringVar(&clientName, "client-name", clientName, "The name of user. It will be used as the CN if client certificates are created")
103+
cmd.Flags().StringSliceVar(&organizations, "org", organizations, "The orgnizations of the client certificate. It will be used as the O if client certificates are created")
104+
105+
return cmd
106+
}
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
/*
2+
Copyright 2018 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package alpha
18+
19+
import (
20+
"bytes"
21+
"fmt"
22+
"os"
23+
"testing"
24+
25+
"k8s.io/client-go/tools/clientcmd"
26+
kubeadmconstants "k8s.io/kubernetes/cmd/kubeadm/app/constants"
27+
"k8s.io/kubernetes/cmd/kubeadm/app/util/pkiutil"
28+
testutil "k8s.io/kubernetes/cmd/kubeadm/test"
29+
kubeconfigtestutil "k8s.io/kubernetes/cmd/kubeadm/test/kubeconfig"
30+
)
31+
32+
func TestKubeConfigSubCommandsThatWritesToOut(t *testing.T) {
33+
34+
// Temporary folders for the test case
35+
tmpdir := testutil.SetupTempDir(t)
36+
defer os.RemoveAll(tmpdir)
37+
38+
// Adds a pki folder with a ca cert to the temp folder
39+
pkidir := testutil.SetupPkiDirWithCertificateAuthorithy(t, tmpdir)
40+
41+
// Retrieves ca cert for assertions
42+
caCert, _, err := pkiutil.TryLoadCertAndKeyFromDisk(pkidir, kubeadmconstants.CACertAndKeyBaseName)
43+
if err != nil {
44+
t.Fatalf("couldn't retrieve ca cert: %v", err)
45+
}
46+
47+
commonFlags := []string{
48+
"--apiserver-advertise-address=1.2.3.4",
49+
"--apiserver-bind-port=1234",
50+
"--client-name=myUser",
51+
fmt.Sprintf("--cert-dir=%s", pkidir),
52+
}
53+
54+
var tests = []struct {
55+
name string
56+
command string
57+
withClientCert bool
58+
withToken bool
59+
additionalFlags []string
60+
}{
61+
{
62+
name: "user subCommand withClientCert",
63+
command: "user",
64+
withClientCert: true,
65+
},
66+
{
67+
name: "user subCommand withToken",
68+
withToken: true,
69+
command: "user",
70+
additionalFlags: []string{"--token=123456"},
71+
},
72+
}
73+
74+
for _, test := range tests {
75+
t.Run(test.name, func(t *testing.T) {
76+
buf := new(bytes.Buffer)
77+
78+
// Get subcommands working in the temporary directory
79+
cmd := newCmdUserKubeConfig(buf)
80+
81+
// Execute the subcommand
82+
allFlags := append(commonFlags, test.additionalFlags...)
83+
cmd.SetArgs(allFlags)
84+
if err := cmd.Execute(); err != nil {
85+
t.Fatal("Could not execute subcommand")
86+
}
87+
88+
// reads kubeconfig written to stdout
89+
config, err := clientcmd.Load(buf.Bytes())
90+
if err != nil {
91+
t.Errorf("couldn't read kubeconfig file from buffer: %v", err)
92+
return
93+
}
94+
95+
// checks that CLI flags are properly propagated
96+
kubeconfigtestutil.AssertKubeConfigCurrentCluster(t, config, "https://1.2.3.4:1234", caCert)
97+
98+
if test.withClientCert {
99+
// checks that kubeconfig files have expected client cert
100+
kubeconfigtestutil.AssertKubeConfigCurrentAuthInfoWithClientCert(t, config, caCert, "myUser")
101+
}
102+
103+
if test.withToken {
104+
// checks that kubeconfig files have expected token
105+
kubeconfigtestutil.AssertKubeConfigCurrentAuthInfoWithToken(t, config, "myUser", "123456")
106+
}
107+
})
108+
}
109+
}

0 commit comments

Comments
 (0)