generated from cybozu-go/neco-template
-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #93 from cybozu-go/kubectl-moco
kubectl-moco plugin
- Loading branch information
Showing
612 changed files
with
80,611 additions
and
566 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
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
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,37 @@ | ||
package cmd | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
|
||
"github.com/cybozu-go/moco" | ||
mocov1alpha1 "github.com/cybozu-go/moco/api/v1alpha1" | ||
corev1 "k8s.io/api/core/v1" | ||
"k8s.io/apimachinery/pkg/types" | ||
) | ||
|
||
func getPassword(ctx context.Context, cluster *mocov1alpha1.MySQLCluster, user string) (string, error) { | ||
secret := &corev1.Secret{} | ||
err := kubeClient.Get(ctx, types.NamespacedName{ | ||
Namespace: namespace, | ||
Name: "root-password-" + moco.UniqueName(cluster), | ||
}, secret) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
userPassKeys := map[string]string{ | ||
"root": moco.RootPasswordEnvName, | ||
moco.WritableUser: moco.WritablePasswordEnvName, | ||
moco.ReadOnlyUser: moco.ReadOnlyPasswordEnvName, | ||
} | ||
key, ok := userPassKeys[user] | ||
if !ok { | ||
return "", errors.New("unknown user: " + user) | ||
} | ||
password, ok := secret.Data[key] | ||
if !ok { | ||
return "", errors.New("unknown user: " + user) | ||
} | ||
return string(password), nil | ||
} |
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,62 @@ | ||
package cmd | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"fmt" | ||
|
||
mocov1alpha1 "github.com/cybozu-go/moco/api/v1alpha1" | ||
"github.com/spf13/cobra" | ||
"k8s.io/apimachinery/pkg/types" | ||
) | ||
|
||
var credentialConfig struct { | ||
user string | ||
format string | ||
} | ||
|
||
// credentialCmd represents the credential command | ||
var credentialCmd = &cobra.Command{ | ||
Use: "credential", | ||
Short: "Fetch the credential of a specified user", | ||
Long: "Fetch the credential of a specified user.", | ||
Args: cobra.ExactArgs(1), | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
return fetchCredential(cmd.Context(), args[0]) | ||
}, | ||
} | ||
|
||
func fetchCredential(ctx context.Context, clusterName string) error { | ||
cluster := &mocov1alpha1.MySQLCluster{} | ||
err := kubeClient.Get(ctx, types.NamespacedName{ | ||
Namespace: namespace, | ||
Name: clusterName, | ||
}, cluster) | ||
if err != nil { | ||
return err | ||
} | ||
password, err := getPassword(ctx, cluster, credentialConfig.user) | ||
if err != nil { | ||
return err | ||
} | ||
switch credentialConfig.format { | ||
case "plain": | ||
fmt.Println(password) | ||
case "myconf": | ||
fmt.Printf(`[client] | ||
user=%s | ||
password="%s" | ||
`, credentialConfig.user, password) | ||
default: | ||
return errors.New("unknown format: " + credentialConfig.format) | ||
} | ||
return nil | ||
} | ||
|
||
func init() { | ||
fs := credentialCmd.Flags() | ||
fs.StringVarP(&credentialConfig.user, "user", "u", "readonly", "User for login to mysql [`root`, `moco-writable` or `moco-readonly`]") | ||
fs.StringVar(&credentialConfig.format, "format", "plain", "The format of output [`plain` or `myconf`]") | ||
|
||
rootCmd.AddCommand(credentialCmd) | ||
} |
Oops, something went wrong.