Skip to content

Commit

Permalink
Add experimental ready feature
Browse files Browse the repository at this point in the history
Signed-off-by: Alex Ellis (OpenFaaS Ltd) <[email protected]>
  • Loading branch information
alexellis committed Oct 3, 2022
1 parent ab5652e commit 0aaae97
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 0 deletions.
104 changes: 104 additions & 0 deletions cmd/ready.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
package cmd

import (
"fmt"
"os"
"strings"
"time"

execute "github.com/alexellis/go-execute/pkg/v1"
"github.com/alexellis/k3sup/pkg"
"github.com/spf13/cobra"
)

func MakeReady() *cobra.Command {
var command = &cobra.Command{
Use: "ready",
Short: "Check if a cluster is ready using kubectl.",
Long: `Check if the K3s cluster is ready using kubectl to query the nodes.
` + pkg.SupportMessageShort + `
`,
Example: ` # Check from a local file, with the context "default"
k3sup ready \
--context default \
--kubeconfig ./kubeconfig
# Check a merged kubeconfig with a custom context
k3sup ready \
--context e2e \
--kubeconfig $HOME/.kube/config
`,
SilenceUsage: true,
}

command.Flags().Int("attempts", 25, "Number of attempts to check for readiness")
command.Flags().Duration("pause", time.Second*2, "Pause between checking cluster for readiness")
command.Flags().String("kubeconfig", "$HOME/.kube/config", "Path to the kubeconfig file")
command.Flags().String("context", "default", "Name of the kubeconfig context to use")

command.RunE = func(cmd *cobra.Command, args []string) error {

attempts, _ := cmd.Flags().GetInt("attempts")
pause, _ := cmd.Flags().GetDuration("pause")
kubeconfig, _ := cmd.Flags().GetString("kubeconfig")
contextName, _ := cmd.Flags().GetString("context")

if len(kubeconfig) == 0 {
return fmt.Errorf("kubeconfig cannot be empty")
}

if len(contextName) == 0 {
return fmt.Errorf("context cannot be empty")
}

kubeconfig = os.ExpandEnv(kubeconfig)

// Inspired by Kind: https://github.com/kubernetes-sigs/kind/blob/master/pkg/cluster/internal/create/actions/waitforready/waitforready.go
for i := 0; i < attempts; i++ {
fmt.Printf("Checking cluster status: %d/%d \n", i+1, attempts)
task := execute.ExecTask{
Command: "kubectl",
Args: []string{
"get",
"nodes",
"--kubeconfig=" + kubeconfig,
"--context=" + contextName,
"-o=jsonpath='{.items..status.conditions[-1:].status}'",
},
StreamStdio: false,
}
res, err := task.Execute()
if err != nil {
return err
}
// fmt.Println(res.Stdout, res.Stderr, res.ExitCode)

if res.ExitCode == 0 {
fmt.Printf("Cluster is ready\n")
return nil
}

if strings.Contains(res.Stderr, "context was not found") {
return fmt.Errorf("context %s not found in %s", contextName, kubeconfig)
}

parts := strings.Split(res.Stdout, " ")
ready := true
for _, part := range parts {
trimmed := strings.TrimSpace(part)
if len(trimmed) > 0 && trimmed != "True" {
ready = false
}
}

if ready {
break
}
time.Sleep(pause)
}

return nil
}
return command
}
2 changes: 2 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ func main() {
cmdVersion := cmd.MakeVersion()
cmdJoin := cmd.MakeJoin()
cmdUpdate := cmd.MakeUpdate()
cmdReady := cmd.MakeReady()

printk3supASCIIArt := cmd.PrintK3supASCIIArt

Expand Down Expand Up @@ -52,6 +53,7 @@ func main() {
rootCmd.AddCommand(cmdVersion)
rootCmd.AddCommand(cmdJoin)
rootCmd.AddCommand(cmdUpdate)
rootCmd.AddCommand(cmdReady)

if err := rootCmd.Execute(); err != nil {
os.Exit(1)
Expand Down

0 comments on commit 0aaae97

Please sign in to comment.