-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: CLI to update registry credentials
- Loading branch information
Showing
14 changed files
with
491 additions
and
6 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
// Copyright 2022 D2iQ, Inc. All rights reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package flags | ||
|
||
import ( | ||
"io" | ||
|
||
"github.com/mesosphere/dkp-cli-runtime/core/output" | ||
) | ||
|
||
// CLIConfig injects dependencies into CLI that are hard to mock, | ||
// enabling better unittesting. | ||
type CLIConfig struct { | ||
In io.Reader | ||
Output output.Output | ||
} |
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,45 @@ | ||
// Copyright 2022 D2iQ, Inc. All rights reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package cmd | ||
|
||
import ( | ||
"io" | ||
"os" | ||
|
||
"github.com/spf13/cobra" | ||
|
||
"github.com/mesosphere/dkp-cli-runtime/core/cmd/root" | ||
|
||
"github.com/mesosphere/dynamic-credential-provider/cmd/cli/cmd/flags" | ||
"github.com/mesosphere/dynamic-credential-provider/cmd/cli/cmd/update" | ||
) | ||
|
||
func NewCommand(in io.Reader, out, errOut io.Writer) (*cobra.Command, *flags.CLIConfig) { | ||
rootCmd, rootOptions := root.NewCommand(out, errOut) | ||
rootCmd.Use = "credential-manager" | ||
rootCmd.Short = "Create and dynamically manage registry credentials" | ||
rootCmd.SilenceUsage = true | ||
// disable cobra built-in error printing, we output the error with formatting. | ||
rootCmd.SilenceErrors = true | ||
rootCmd.DisableAutoGenTag = true | ||
|
||
config := &flags.CLIConfig{ | ||
In: in, | ||
Output: rootOptions.Output, | ||
} | ||
|
||
rootCmd.AddCommand(update.NewCommand(config)) | ||
|
||
return rootCmd, config | ||
} | ||
|
||
func Execute() { | ||
rootCmd, config := NewCommand(os.Stdin, os.Stdout, os.Stderr) | ||
|
||
if err := rootCmd.Execute(); err != nil { | ||
config.Output.Error(err, "") | ||
//nolint:revive // Common to do this in Cobra | ||
os.Exit(1) | ||
} | ||
} |
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,21 @@ | ||
// Copyright 2022 D2iQ, Inc. All rights reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package update | ||
|
||
import ( | ||
"github.com/spf13/cobra" | ||
|
||
"github.com/mesosphere/dynamic-credential-provider/cmd/cli/cmd/flags" | ||
"github.com/mesosphere/dynamic-credential-provider/cmd/cli/cmd/update/credentials" | ||
) | ||
|
||
func NewCommand(cmdCfg *flags.CLIConfig) *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "update", | ||
Short: "Update one of []", | ||
} | ||
|
||
cmd.AddCommand(credentials.NewCommand(cmdCfg)) | ||
return cmd | ||
} |
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 @@ | ||
// Copyright 2022 D2iQ, Inc. All rights reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package credentials | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/spf13/cobra" | ||
|
||
"github.com/mesosphere/dynamic-credential-provider/cmd/cli/cmd/flags" | ||
"github.com/mesosphere/dynamic-credential-provider/pkg/credentialmanager/secret" | ||
"github.com/mesosphere/dynamic-credential-provider/pkg/k8s/client" | ||
) | ||
|
||
func NewCommand(cmdCfg *flags.CLIConfig) *cobra.Command { | ||
var ( | ||
address string | ||
username string | ||
password string | ||
) | ||
|
||
cmd := &cobra.Command{ | ||
Use: "registry-credentials [address] [username] [password]", | ||
Short: "Update image registry credentials", | ||
Long: `Update image registry credentials in the running cluster: | ||
Examples: | ||
update registry-credentials --address=docker.io --username=myusername --password=mypassword | ||
update registry-credentials --address=myregistry:5000 --username=myusername --password=mypassword | ||
update registry-credentials --address=myregistry:5000/somepath --username=myusername --password=mypassword | ||
`, | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
k8sCLient, _, err := client.NewFromKubeconfig("") | ||
if err != nil { | ||
return err | ||
} | ||
|
||
manager := secret.NewSecretsCredentialManager(k8sCLient) | ||
|
||
err = manager.Update(context.Background(), address, username, password) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
cmdCfg.Output.Infof("Updated credentials") | ||
return nil | ||
}, | ||
} | ||
|
||
cmd.Flags().StringVar(&address, "address", "", "Address of the registry to update credentials") | ||
_ = cmd.MarkFlagRequired("address") | ||
cmd.Flags().StringVar(&username, "username", "", "New username for the registry") | ||
_ = cmd.MarkFlagRequired("username") | ||
cmd.Flags().StringVar(&password, "password", "", "New password for the registry") | ||
_ = cmd.MarkFlagRequired("password") | ||
|
||
return cmd | ||
} |
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,10 @@ | ||
// Copyright 2022 D2iQ, Inc. All rights reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package main | ||
|
||
import "github.com/mesosphere/dynamic-credential-provider/cmd/cli/cmd" | ||
|
||
func main() { | ||
cmd.Execute() | ||
} |
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,10 @@ | ||
// Copyright 2022 D2iQ, Inc. All rights reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package plugin | ||
|
||
import "context" | ||
|
||
type CredentialManager interface { | ||
Update(ctx context.Context, address, username, password string) error | ||
} |
Oops, something went wrong.