@@ -11,19 +11,30 @@ import (
1111 apierrors "k8s.io/apimachinery/pkg/api/errors"
1212 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1313 "k8s.io/apimachinery/pkg/types"
14- "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
1514)
1615
1716var credentialConfig struct {
1817 user string
1918 format string
2019}
2120
22- // credentialCmd represents the credential parent command
21+ // credentialCmd represents the credential parent command.
22+ // When called without a subcommand (e.g. "credential CLUSTER_NAME"),
23+ // it falls back to "show" for backward compatibility.
2324var credentialCmd = & cobra.Command {
24- Use : "credential" ,
25+ Use : "credential [CLUSTER_NAME] " ,
2526 Short : "Manage MySQL credentials" ,
26- Long : "Manage MySQL credentials for a MOCO cluster." ,
27+ Long : "Manage MySQL credentials for a MOCO cluster. When called with a cluster name and no subcommand, shows the credential (backward compatible)." ,
28+ Args : cobra .MaximumNArgs (1 ),
29+ RunE : func (cmd * cobra.Command , args []string ) error {
30+ if len (args ) == 0 {
31+ return cmd .Help ()
32+ }
33+ return credentialShow (cmd .Context (), args [0 ])
34+ },
35+ ValidArgsFunction : func (cmd * cobra.Command , args []string , toComplete string ) ([]string , cobra.ShellCompDirective ) {
36+ return mysqlClusterCandidates (cmd .Context (), cmd , args , toComplete )
37+ },
2738}
2839
2940// credentialShowCmd shows the credential of a specified user
@@ -119,9 +130,7 @@ func credentialRotate(ctx context.Context, clusterName string) error {
119130 DiscardOldPassword : false ,
120131 },
121132 }
122- if err := controllerutil .SetOwnerReference (cluster , cr , kubeClient .Scheme ()); err != nil {
123- return fmt .Errorf ("failed to set ownerReference: %w" , err )
124- }
133+ // ownerReference is set by the controller on first reconcile.
125134 if err := kubeClient .Create (ctx , cr ); err != nil {
126135 return fmt .Errorf ("failed to create CredentialRotation: %w" , err )
127136 }
@@ -169,18 +178,22 @@ func credentialDiscard(ctx context.Context, clusterName string) error {
169178}
170179
171180func init () {
172- // Show subcommand flags
173- fs := credentialShowCmd .Flags ()
181+ // Flags on the parent command for backward compatibility
182+ // ("kubectl moco credential -u moco-admin CLUSTER_NAME").
183+ fs := credentialCmd .Flags ()
174184 fs .StringVarP (& credentialConfig .user , "mysql-user" , "u" , constants .ReadOnlyUser , "User for login to mysql" )
175185 fs .StringVar (& credentialConfig .format , "format" , "plain" , "The format of output [`plain` or `mycnf`]" )
176186
177- _ = credentialShowCmd .RegisterFlagCompletionFunc ("mysql-user" , func (cmd * cobra.Command , args []string , toComplete string ) ([]string , cobra.ShellCompDirective ) {
187+ _ = credentialCmd .RegisterFlagCompletionFunc ("mysql-user" , func (cmd * cobra.Command , args []string , toComplete string ) ([]string , cobra.ShellCompDirective ) {
178188 return []string {constants .ReadOnlyUser , constants .WritableUser , constants .AdminUser }, cobra .ShellCompDirectiveDefault
179189 })
180- _ = credentialShowCmd .RegisterFlagCompletionFunc ("format" , func (cmd * cobra.Command , args []string , toComplete string ) ([]string , cobra.ShellCompDirective ) {
190+ _ = credentialCmd .RegisterFlagCompletionFunc ("format" , func (cmd * cobra.Command , args []string , toComplete string ) ([]string , cobra.ShellCompDirective ) {
181191 return []string {"plain" , "mycnf" }, cobra .ShellCompDirectiveDefault
182192 })
183193
194+ // "show" subcommand shares the same flags via the parent.
195+ credentialShowCmd .Flags ().AddFlagSet (credentialCmd .Flags ())
196+
184197 credentialCmd .AddCommand (credentialShowCmd )
185198 credentialCmd .AddCommand (credentialRotateCmd )
186199 credentialCmd .AddCommand (credentialDiscardCmd )
0 commit comments