Pull "create sshpublickey" into its own subcommand

This commit is contained in:
John Gardiner Myers 2021-07-22 16:42:29 -07:00
parent 7876cc4454
commit de191e2366
9 changed files with 140 additions and 207 deletions

2
cmd/kops/BUILD.bazel generated
View File

@ -11,8 +11,8 @@ go_library(
"create_secret_cilium_encryptionconfig.go",
"create_secret_dockerconfig.go",
"create_secret_encryptionconfig.go",
"create_secret_sshpublickey.go",
"create_secret_weave_encryptionconfig.go",
"create_sshpublickey.go",
"delete.go",
"delete_cluster.go",
"delete_instance.go",

View File

@ -59,21 +59,8 @@ var (
# Create an instancegroup based on the YAML passed into stdin.
cat instancegroup.yaml | kops create -f -
# Create a cluster in AWS.
kops create cluster --name=k8s-cluster.example.com \
--state=s3://my-state-store --zones=us-east-1a \
--node-count=2 --node-size=t3.small --master-size=t3.small \
--dns-zone=example.com
# Create an instancegroup for the k8s-cluster.example.com cluster.
kops create ig --name=k8s-cluster.example.com node-example \
--role node --subnet my-subnet-name
# Create a new ssh public key called admin.
kops create secret sshpublickey admin -i ~/.ssh/id_rsa.pub \
--name k8s-cluster.example.com --state s3://my-state-store
`))
createShort = i18n.T("Create a resource by command line, filename or stdin.")
)
@ -102,6 +89,7 @@ func NewCmdCreate(f *util.Factory, out io.Writer) *cobra.Command {
cmd.AddCommand(NewCmdCreateInstanceGroup(f, out))
cmd.AddCommand(NewCmdCreateKeypair(f, out))
cmd.AddCommand(NewCmdCreateSecret(f, out))
cmd.AddCommand(NewCmdCreateSSHPublicKey(f, out))
return cmd
}

View File

@ -22,42 +22,36 @@ import (
"github.com/spf13/cobra"
"k8s.io/kops/cmd/kops/util"
"k8s.io/kubectl/pkg/util/i18n"
"k8s.io/kubectl/pkg/util/templates"
)
var (
createSecretLong = templates.LongDesc(i18n.T(`
Create a secret`))
createSecretExample = templates.Examples(i18n.T(`
# Create a new ssh public key called admin.
kops create secret sshpublickey admin -i ~/.ssh/id_rsa.pub \
--name k8s-cluster.example.com --state s3://my-state-store
kops create secret dockerconfig -f ~/.docker/config.json \
--name k8s-cluster.example.com --state s3://my-state-store
kops create secret encryptionconfig -f ~/.encryptionconfig.yaml \
--name k8s-cluster.example.com --state s3://my-state-store
`))
createSecretShort = i18n.T(`Create a secret.`)
)
func NewCmdCreateSecret(f *util.Factory, out io.Writer) *cobra.Command {
cmd := &cobra.Command{
Use: "secret",
Short: createSecretShort,
Long: createSecretLong,
Example: createSecretExample,
Use: "secret",
Short: createSecretShort,
}
// create subcommands
cmd.AddCommand(NewCmdCreateSecretPublicKey(f, out))
cmd.AddCommand(NewCmdCreateSecretDockerConfig(f, out))
cmd.AddCommand(NewCmdCreateSecretEncryptionConfig(f, out))
cmd.AddCommand(NewCmdCreateSecretWeaveEncryptionConfig(f, out))
cmd.AddCommand(NewCmdCreateSecretCiliumEncryptionConfig(f, out))
sshPublicKey := NewCmdCreateSSHPublicKey(f, out)
sshPublicKey.Hidden = true
innerArgs := sshPublicKey.Args
sshPublicKey.Args = func(cmd *cobra.Command, args []string) error {
if len(args) > 0 && args[0] == "admin" {
// Backwards compatibility
args = args[1:]
}
return innerArgs(cmd, args)
}
cmd.AddCommand(sshPublicKey)
return cmd
}

View File

@ -1,125 +0,0 @@
/*
Copyright 2019 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 main
import (
"context"
"fmt"
"io"
"io/ioutil"
"os"
"github.com/spf13/cobra"
"k8s.io/kops/cmd/kops/util"
"k8s.io/kubectl/pkg/util/i18n"
"k8s.io/kubectl/pkg/util/templates"
)
var (
createSecretSSHPublicKeyLong = templates.LongDesc(i18n.T(`
Create a new ssh public key, and store the key in the state store. The
key is not updated by this command.`))
createSecretSSHPublicKeyExample = templates.Examples(i18n.T(`
# Create a new ssh public key called admin.
kops create secret sshpublickey admin -i ~/.ssh/id_rsa.pub \
--name k8s-cluster.example.com --state s3://my-state-store
`))
createSecretSSHPublicKeyShort = i18n.T(`Create an ssh public key.`)
)
type CreateSecretPublickeyOptions struct {
ClusterName string
Name string
PublicKeyPath string
}
func NewCmdCreateSecretPublicKey(f *util.Factory, out io.Writer) *cobra.Command {
options := &CreateSecretPublickeyOptions{}
cmd := &cobra.Command{
Use: "sshpublickey",
Short: createSecretSSHPublicKeyShort,
Long: createSecretSSHPublicKeyLong,
Example: createSecretSSHPublicKeyExample,
Run: func(cmd *cobra.Command, args []string) {
ctx := context.TODO()
if len(args) == 0 {
exitWithError(fmt.Errorf("syntax: NAME -i <PublicKeyPath>"))
}
if len(args) != 1 {
exitWithError(fmt.Errorf("syntax: NAME -i <PublicKeyPath>"))
}
options.Name = args[0]
err := rootCommand.ProcessArgs(args[1:])
if err != nil {
exitWithError(err)
}
options.ClusterName = rootCommand.ClusterName(true)
err = RunCreateSecretPublicKey(ctx, f, os.Stdout, options)
if err != nil {
exitWithError(err)
}
},
}
cmd.Flags().StringVarP(&options.PublicKeyPath, "pubkey", "i", "", "Path to SSH public key")
return cmd
}
func RunCreateSecretPublicKey(ctx context.Context, f *util.Factory, out io.Writer, options *CreateSecretPublickeyOptions) error {
if options.PublicKeyPath == "" {
return fmt.Errorf("public key path is required (use -i)")
}
if options.Name == "" {
return fmt.Errorf("Name is required")
}
cluster, err := GetCluster(ctx, f, options.ClusterName)
if err != nil {
return err
}
clientset, err := f.Clientset()
if err != nil {
return err
}
sshCredentialStore, err := clientset.SSHCredentialStore(cluster)
if err != nil {
return err
}
data, err := ioutil.ReadFile(options.PublicKeyPath)
if err != nil {
return fmt.Errorf("error reading SSH public key %v: %v", options.PublicKeyPath, err)
}
err = sshCredentialStore.AddSSHPublicKey(options.Name, data)
if err != nil {
return fmt.Errorf("error adding SSH public key: %v", err)
}
return nil
}

View File

@ -0,0 +1,110 @@
/*
Copyright 2019 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 main
import (
"context"
"fmt"
"io"
"io/ioutil"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"k8s.io/kops/cmd/kops/util"
"k8s.io/kops/pkg/commands/commandutils"
"k8s.io/kubectl/pkg/util/i18n"
"k8s.io/kubectl/pkg/util/templates"
)
var (
createSSHPublicKeyLong = templates.LongDesc(i18n.T(`
Create a new SSH public key, and store the key in the state store. The
key is not updated by this command.`))
createSSHPublicKeyExample = templates.Examples(i18n.T(`
# Create a new SSH public key from the file ""~/.ssh/id_rsa.pub".
kops create sshpublickey k8s-cluster.example.com -i ~/.ssh/id_rsa.pub
`))
createSSHPublicKeyShort = i18n.T(`Create an SSH public key.`)
)
type CreateSSHPublicKeyOptions struct {
ClusterName string
PublicKeyPath string
}
func NewCmdCreateSSHPublicKey(f *util.Factory, out io.Writer) *cobra.Command {
options := &CreateSSHPublicKeyOptions{}
cmd := &cobra.Command{
Use: "sshpublickey [CLUSTER]",
Short: createSSHPublicKeyShort,
Long: createSSHPublicKeyLong,
Example: createSSHPublicKeyExample,
Args: rootCommand.clusterNameArgs(&options.ClusterName),
ValidArgsFunction: commandutils.CompleteClusterName(&rootCommand, true, false),
RunE: func(cmd *cobra.Command, args []string) error {
return RunCreateSSHPublicKey(context.TODO(), f, out, options)
},
}
cmd.Flags().StringVarP(&options.PublicKeyPath, "ssh-public-key", "i", "", "Path to SSH public key")
cmd.MarkFlagRequired("ssh-public-key")
cmd.RegisterFlagCompletionFunc("ssh-public-key", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
return []string{"pub"}, cobra.ShellCompDirectiveFilterFileExt
})
cmd.Flags().SetNormalizeFunc(func(f *pflag.FlagSet, name string) pflag.NormalizedName {
switch name {
case "pubkey":
name = "ssh-public-key"
}
return pflag.NormalizedName(name)
})
return cmd
}
func RunCreateSSHPublicKey(ctx context.Context, f *util.Factory, out io.Writer, options *CreateSSHPublicKeyOptions) error {
cluster, err := GetCluster(ctx, f, options.ClusterName)
if err != nil {
return err
}
clientset, err := f.Clientset()
if err != nil {
return err
}
sshCredentialStore, err := clientset.SSHCredentialStore(cluster)
if err != nil {
return err
}
data, err := ioutil.ReadFile(options.PublicKeyPath)
if err != nil {
return fmt.Errorf("error reading SSH public key %v: %v", options.PublicKeyPath, err)
}
err = sshCredentialStore.AddSSHPublicKey("admin", data)
if err != nil {
return fmt.Errorf("error adding SSH public key: %v", err)
}
return nil
}

View File

@ -698,12 +698,11 @@ func (i *integrationTest) setupCluster(t *testing.T, inputYAML string, ctx conte
}
if i.sshKey {
options := &CreateSecretPublickeyOptions{}
options := &CreateSSHPublicKeyOptions{}
options.ClusterName = i.clusterName
options.Name = "admin"
options.PublicKeyPath = path.Join(i.srcDir, "id_rsa.pub")
err := RunCreateSecretPublicKey(ctx, factory, &stdout, options)
err := RunCreateSSHPublicKey(ctx, factory, &stdout, options)
if err != nil {
t.Fatalf("error running %q create public key: %v", inputYAML, err)
}

View File

@ -30,20 +30,6 @@ kops create {-f FILENAME}... [flags]
# Create an instancegroup based on the YAML passed into stdin.
cat instancegroup.yaml | kops create -f -
# Create a cluster in AWS.
kops create cluster --name=k8s-cluster.example.com \
--state=s3://my-state-store --zones=us-east-1a \
--node-count=2 --node-size=t3.small --master-size=t3.small \
--dns-zone=example.com
# Create an instancegroup for the k8s-cluster.example.com cluster.
kops create ig --name=k8s-cluster.example.com node-example \
--role node --subnet my-subnet-name
# Create a new ssh public key called admin.
kops create secret sshpublickey admin -i ~/.ssh/id_rsa.pub \
--name k8s-cluster.example.com --state s3://my-state-store
```
### Options
@ -81,4 +67,5 @@ kops create {-f FILENAME}... [flags]
* [kops create instancegroup](kops_create_instancegroup.md) - Create an instancegroup.
* [kops create keypair](kops_create_keypair.md) - Add a CA certificate and private key to a keyset.
* [kops create secret](kops_create_secret.md) - Create a secret.
* [kops create sshpublickey](kops_create_sshpublickey.md) - Create an SSH public key.

View File

@ -5,24 +5,6 @@
Create a secret.
### Synopsis
Create a secret
### Examples
```
# Create a new ssh public key called admin.
kops create secret sshpublickey admin -i ~/.ssh/id_rsa.pub \
--name k8s-cluster.example.com --state s3://my-state-store
kops create secret dockerconfig -f ~/.docker/config.json \
--name k8s-cluster.example.com --state s3://my-state-store
kops create secret encryptionconfig -f ~/.encryptionconfig.yaml \
--name k8s-cluster.example.com --state s3://my-state-store
```
### Options
```
@ -56,6 +38,5 @@ Create a secret
* [kops create secret ciliumpassword](kops_create_secret_ciliumpassword.md) - Create a cilium encryption key.
* [kops create secret dockerconfig](kops_create_secret_dockerconfig.md) - Create a docker config.
* [kops create secret encryptionconfig](kops_create_secret_encryptionconfig.md) - Create an encryption config.
* [kops create secret sshpublickey](kops_create_secret_sshpublickey.md) - Create an ssh public key.
* [kops create secret weavepassword](kops_create_secret_weavepassword.md) - Create a weave encryption config.

View File

@ -1,31 +1,30 @@
<!--- This file is automatically generated by make gen-cli-docs; changes should be made in the go CLI command code (under cmd/kops) -->
## kops create secret sshpublickey
## kops create sshpublickey
Create an ssh public key.
Create an SSH public key.
### Synopsis
Create a new ssh public key, and store the key in the state store. The key is not updated by this command.
Create a new SSH public key, and store the key in the state store. The key is not updated by this command.
```
kops create secret sshpublickey [flags]
kops create sshpublickey [CLUSTER] [flags]
```
### Examples
```
# Create a new ssh public key called admin.
kops create secret sshpublickey admin -i ~/.ssh/id_rsa.pub \
--name k8s-cluster.example.com --state s3://my-state-store
# Create a new SSH public key from the file ""~/.ssh/id_rsa.pub".
kops create sshpublickey k8s-cluster.example.com -i ~/.ssh/id_rsa.pub
```
### Options
```
-h, --help help for sshpublickey
-i, --pubkey string Path to SSH public key
-h, --help help for sshpublickey
-i, --ssh-public-key string Path to SSH public key
```
### Options inherited from parent commands
@ -51,5 +50,5 @@ kops create secret sshpublickey [flags]
### SEE ALSO
* [kops create secret](kops_create_secret.md) - Create a secret.
* [kops create](kops_create.md) - Create a resource by command line, filename or stdin.