Merge pull request #4956 from chaosi-zju/token

cleanup: adjust log print content when delete token by karmadactl
This commit is contained in:
karmada-bot 2024-05-21 19:47:42 +08:00 committed by GitHub
commit 53af52e4a8
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 11 additions and 8 deletions

View File

@ -286,8 +286,11 @@ func (o *CommandTokenOptions) runListTokens(client kubeclient.Interface, out io.
// runDeleteTokens removes a bootstrap tokens from the server. // runDeleteTokens removes a bootstrap tokens from the server.
func (o *CommandTokenOptions) runDeleteTokens(out io.Writer, client kubeclient.Interface, tokenIDsOrTokens []string) error { func (o *CommandTokenOptions) runDeleteTokens(out io.Writer, client kubeclient.Interface, tokenIDsOrTokens []string) error {
for _, tokenIDOrToken := range tokenIDsOrTokens { for _, tokenIDOrToken := range tokenIDsOrTokens {
// Assume this is a token id and try to parse it // Assume this is a token id and try to parse it.
tokenID := tokenIDOrToken // Notes: Bootstrap Tokens take the form of abcdef.0123456789abcdef. The first part of the token is the public
// `Token ID` and is considered public information. It is used when referring to a token without leaking the secret
// part used for authentication. The second part is the `Token Secret` and should only be shared with trusted parties.
id := tokenIDOrToken
klog.V(1).Info("[token] parsing token") klog.V(1).Info("[token] parsing token")
if !bootstraputil.IsValidBootstrapTokenID(tokenIDOrToken) { if !bootstraputil.IsValidBootstrapTokenID(tokenIDOrToken) {
// Okay, the full token with both id and secret was probably passed. Parse it and extract the ID only // Okay, the full token with both id and secret was probably passed. Parse it and extract the ID only
@ -296,15 +299,15 @@ func (o *CommandTokenOptions) runDeleteTokens(out io.Writer, client kubeclient.I
return fmt.Errorf("given token didn't match pattern %q or %q", return fmt.Errorf("given token didn't match pattern %q or %q",
bootstrapapi.BootstrapTokenIDPattern, bootstrapapi.BootstrapTokenIDPattern) bootstrapapi.BootstrapTokenIDPattern, bootstrapapi.BootstrapTokenIDPattern)
} }
tokenID = bts.ID id = bts.ID
} }
tokenSecretName := bootstraputil.BootstrapTokenSecretName(tokenID) secretName := bootstraputil.BootstrapTokenSecretName(id)
klog.V(1).Infof("[token] deleting token %q", tokenID) klog.V(1).Infof("[token] deleting secret %s", secretName)
if err := client.CoreV1().Secrets(metav1.NamespaceSystem).Delete(context.TODO(), tokenSecretName, metav1.DeleteOptions{}); err != nil { if err := client.CoreV1().Secrets(metav1.NamespaceSystem).Delete(context.TODO(), secretName, metav1.DeleteOptions{}); err != nil {
return fmt.Errorf("failed to delete bootstrap token %q, err: %w", tokenID, err) return fmt.Errorf("failed to delete secret %q, err: %w", secretName, err)
} }
fmt.Fprintf(out, "bootstrap token %q deleted\n", tokenID) fmt.Fprintf(out, "bootstrap token %q deleted\n", id)
} }
return nil return nil
} }