-
Notifications
You must be signed in to change notification settings - Fork 13
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add refresh-certs
Command
#623
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,21 @@ | ||
## k8s refresh-certs | ||
|
||
Refresh the certificates of the running node | ||
|
||
``` | ||
k8s refresh-certs [flags] | ||
``` | ||
|
||
### Options | ||
|
||
``` | ||
--expires-in string the time until the certificates expire, e.g., 1h, 2d, 4mo, 5y. Aditionally, any valid time unit for ParseDuration is accepted. | ||
--extra-sans stringArray extra SANs to add to the certificates. | ||
-h, --help help for refresh-certs | ||
--timeout duration the max time to wait for the command to execute (default 1m30s) | ||
``` | ||
|
||
### SEE ALSO | ||
|
||
* [k8s](k8s.md) - Canonical Kubernetes CLI | ||
|
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,77 @@ | ||
package k8s | ||
|
||
import ( | ||
"context" | ||
"time" | ||
|
||
apiv1 "github.com/canonical/k8s-snap-api/api/v1" | ||
cmdutil "github.com/canonical/k8s/cmd/util" | ||
"github.com/canonical/k8s/pkg/utils" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
func newRefreshCertsCmd(env cmdutil.ExecutionEnvironment) *cobra.Command { | ||
var opts struct { | ||
extraSANs []string | ||
expiresIn string | ||
timeout time.Duration | ||
} | ||
cmd := &cobra.Command{ | ||
Use: "refresh-certs", | ||
Short: "Refresh the certificates of the running node", | ||
PreRun: chainPreRunHooks(hookRequireRoot(env)), | ||
Run: func(cmd *cobra.Command, args []string) { | ||
ttl, err := utils.TTLToSeconds(opts.expiresIn) | ||
if err != nil { | ||
cmd.PrintErrf("Error: Failed to parse TTL. \n\nThe error was: %v\n", err) | ||
} | ||
|
||
client, err := env.Snap.K8sdClient("") | ||
if err != nil { | ||
cmd.PrintErrf("Error: Failed to create a k8sd client. Make sure that the k8sd service is running.\n\nThe error was: %v\n", err) | ||
env.Exit(1) | ||
return | ||
} | ||
|
||
ctx, cancel := context.WithTimeout(cmd.Context(), opts.timeout) | ||
cobra.OnFinalize(cancel) | ||
|
||
plan, err := client.RefreshCertificatesPlan(ctx, apiv1.RefreshCertificatesPlanRequest{}) | ||
if err != nil { | ||
cmd.PrintErrf("Error: Failed to get the certificates refresh plan.\n\nThe error was: %v\n", err) | ||
env.Exit(1) | ||
return | ||
} | ||
|
||
if len(plan.CertificateSigningRequests) > 0 { | ||
bschimke95 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
cmd.Println("The following CertificateSigningRequests should be approved. Run the following commands on any of the control plane nodes of the cluster:") | ||
for _, csr := range plan.CertificateSigningRequests { | ||
cmd.Printf("k8s kubectl certificate approve %s\n", csr) | ||
} | ||
} | ||
|
||
runRequest := apiv1.RefreshCertificatesRunRequest{ | ||
Seed: plan.Seed, | ||
ExpirationSeconds: ttl, | ||
ExtraSANs: opts.extraSANs, | ||
} | ||
|
||
cmd.Println("Waiting for certificates to be created...") | ||
runResponse, err := client.RefreshCertificatesRun(ctx, runRequest) | ||
if err != nil { | ||
cmd.PrintErrf("Error: Failed to refresh the certificates.\n\nThe error was: %v\n", err) | ||
env.Exit(1) | ||
return | ||
} | ||
addyess marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
expiryTimeUNIX := time.Unix(int64(runResponse.ExpirationSeconds), 0) | ||
cmd.Printf("Certificates have been successfully refreshed, and will expire at %v.\n", expiryTimeUNIX) | ||
}, | ||
} | ||
cmd.Flags().StringVar(&opts.expiresIn, "expires-in", "", "the time until the certificates expire, e.g., 1h, 2d, 4mo, 5y. Aditionally, any valid time unit for ParseDuration is accepted.") | ||
cmd.Flags().DurationVar(&opts.timeout, "timeout", 90*time.Second, "the max time to wait for the command to execute") | ||
cmd.Flags().StringArrayVar(&opts.extraSANs, "extra-sans", []string{}, "extra SANs to add to the certificates.") | ||
|
||
cmd.MarkFlagRequired("expires-in") | ||
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
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,15 @@ | ||
package k8sd | ||
|
||
import ( | ||
"context" | ||
|
||
apiv1 "github.com/canonical/k8s-snap-api/api/v1" | ||
) | ||
|
||
func (c *k8sd) RefreshCertificatesPlan(ctx context.Context, request apiv1.RefreshCertificatesPlanRequest) (apiv1.RefreshCertificatesPlanResponse, error) { | ||
return query(ctx, c, "POST", apiv1.RefreshCertificatesPlanRPC, request, &apiv1.RefreshCertificatesPlanResponse{}) | ||
} | ||
|
||
func (c *k8sd) RefreshCertificatesRun(ctx context.Context, request apiv1.RefreshCertificatesRunRequest) (apiv1.RefreshCertificatesRunResponse, error) { | ||
return query(ctx, c, "POST", apiv1.RefreshCertificatesRunRPC, request, &apiv1.RefreshCertificatesRunResponse{}) | ||
} |
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice, maybe the
k8sd
service isn't running. If that's the only reason, maybe indicate how to check that.