|
| 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 | +} |
0 commit comments