mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-22 19:31:44 +00:00
kubeadm: add some output to the generate-csr command
Currently the "generate-csr" command does not have any output. Pass an io.Writer (bound to os.Stdout from /cmd) to the functions responsible for generating the kubeconfig / certs keys and CSRs. If nil is passed these functions don't output anything.
This commit is contained in:
parent
57a46531f5
commit
bae6b93d5c
@ -101,7 +101,7 @@ func NewCmdCertsUtility(out io.Writer) *cobra.Command {
|
|||||||
cmd.AddCommand(newCmdCertsRenewal(out))
|
cmd.AddCommand(newCmdCertsRenewal(out))
|
||||||
cmd.AddCommand(newCmdCertsExpiration(out, constants.KubernetesDir))
|
cmd.AddCommand(newCmdCertsExpiration(out, constants.KubernetesDir))
|
||||||
cmd.AddCommand(newCmdCertificateKey())
|
cmd.AddCommand(newCmdCertificateKey())
|
||||||
cmd.AddCommand(newCmdGenCSR())
|
cmd.AddCommand(newCmdGenCSR(out))
|
||||||
return cmd
|
return cmd
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -147,7 +147,7 @@ func (o *genCSRConfig) load() (err error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// newCmdGenCSR returns cobra.Command for generating keys and CSRs
|
// newCmdGenCSR returns cobra.Command for generating keys and CSRs
|
||||||
func newCmdGenCSR() *cobra.Command {
|
func newCmdGenCSR(out io.Writer) *cobra.Command {
|
||||||
config := newGenCSRConfig()
|
config := newGenCSRConfig()
|
||||||
|
|
||||||
cmd := &cobra.Command{
|
cmd := &cobra.Command{
|
||||||
@ -160,7 +160,7 @@ func newCmdGenCSR() *cobra.Command {
|
|||||||
if err := config.load(); err != nil {
|
if err := config.load(); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
return runGenCSR(config)
|
return runGenCSR(out, config)
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
config.addFlagSet(cmd.Flags())
|
config.addFlagSet(cmd.Flags())
|
||||||
@ -168,11 +168,11 @@ func newCmdGenCSR() *cobra.Command {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// runGenCSR contains the logic of the generate-csr sub-command.
|
// runGenCSR contains the logic of the generate-csr sub-command.
|
||||||
func runGenCSR(config *genCSRConfig) error {
|
func runGenCSR(out io.Writer, config *genCSRConfig) error {
|
||||||
if err := certsphase.CreateDefaultKeysAndCSRFiles(config.kubeadmConfig); err != nil {
|
if err := certsphase.CreateDefaultKeysAndCSRFiles(out, config.kubeadmConfig); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if err := kubeconfigphase.CreateDefaultKubeConfigsAndCSRFiles(config.kubeConfigDir, config.kubeadmConfig); err != nil {
|
if err := kubeconfigphase.CreateDefaultKubeConfigsAndCSRFiles(out, config.kubeConfigDir, config.kubeadmConfig); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
|
@ -334,7 +334,7 @@ func TestRunGenCSR(t *testing.T) {
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
err := runGenCSR(&config)
|
err := runGenCSR(nil, &config)
|
||||||
require.NoError(t, err, "expected runGenCSR to not fail")
|
require.NoError(t, err, "expected runGenCSR to not fail")
|
||||||
|
|
||||||
t.Log("The command generates key and CSR files in the configured --cert-dir")
|
t.Log("The command generates key and CSR files in the configured --cert-dir")
|
||||||
|
@ -19,6 +19,8 @@ package certs
|
|||||||
import (
|
import (
|
||||||
"crypto"
|
"crypto"
|
||||||
"crypto/x509"
|
"crypto/x509"
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
|
|
||||||
@ -477,15 +479,21 @@ func createKeyAndCSR(kubeadmConfig *kubeadmapi.InitConfiguration, cert *KubeadmC
|
|||||||
|
|
||||||
// CreateDefaultKeysAndCSRFiles is used in ExternalCA mode to create key files
|
// CreateDefaultKeysAndCSRFiles is used in ExternalCA mode to create key files
|
||||||
// and adjacent CSR files.
|
// and adjacent CSR files.
|
||||||
func CreateDefaultKeysAndCSRFiles(config *kubeadmapi.InitConfiguration) error {
|
func CreateDefaultKeysAndCSRFiles(out io.Writer, config *kubeadmapi.InitConfiguration) error {
|
||||||
certificates, err := leafCertificates(GetDefaultCertList())
|
certificates, err := leafCertificates(GetDefaultCertList())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
if out != nil {
|
||||||
|
fmt.Fprintf(out, "generating keys and CSRs in %s\n", config.CertificatesDir)
|
||||||
|
}
|
||||||
for _, cert := range certificates {
|
for _, cert := range certificates {
|
||||||
if err := createKeyAndCSR(config, cert); err != nil {
|
if err := createKeyAndCSR(config, cert); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
if out != nil {
|
||||||
|
fmt.Fprintf(out, " %s\n", cert.BaseName)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
@ -508,15 +508,21 @@ func createKubeConfigAndCSR(kubeConfigDir string, kubeadmConfig *kubeadmapi.Init
|
|||||||
|
|
||||||
// CreateDefaultKubeConfigsAndCSRFiles is used in ExternalCA mode to create
|
// CreateDefaultKubeConfigsAndCSRFiles is used in ExternalCA mode to create
|
||||||
// kubeconfig files and adjacent CSR files.
|
// kubeconfig files and adjacent CSR files.
|
||||||
func CreateDefaultKubeConfigsAndCSRFiles(kubeConfigDir string, kubeadmConfig *kubeadmapi.InitConfiguration) error {
|
func CreateDefaultKubeConfigsAndCSRFiles(out io.Writer, kubeConfigDir string, kubeadmConfig *kubeadmapi.InitConfiguration) error {
|
||||||
kubeConfigs, err := getKubeConfigSpecsBase(kubeadmConfig)
|
kubeConfigs, err := getKubeConfigSpecsBase(kubeadmConfig)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
if out != nil {
|
||||||
|
fmt.Fprintf(out, "generating keys and CSRs in %s\n", kubeConfigDir)
|
||||||
|
}
|
||||||
for name, spec := range kubeConfigs {
|
for name, spec := range kubeConfigs {
|
||||||
if err := createKubeConfigAndCSR(kubeConfigDir, kubeadmConfig, name, spec); err != nil {
|
if err := createKubeConfigAndCSR(kubeConfigDir, kubeadmConfig, name, spec); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
if out != nil {
|
||||||
|
fmt.Fprintf(out, " %s\n", name)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user