|
| 1 | +package check |
| 2 | + |
| 3 | +import ( |
| 4 | + "github.com/skupperproject/skupper/api/types" |
| 5 | + "github.com/skupperproject/skupper/internal/cmd/skupper/common/utils" |
| 6 | + "github.com/skupperproject/skupper/internal/cmd/skupper/debug/check/cli" |
| 7 | + "github.com/skupperproject/skupper/internal/cmd/skupper/debug/check/command" |
| 8 | + "github.com/skupperproject/skupper/internal/cmd/skupper/debug/check/kube" |
| 9 | + "github.com/skupperproject/skupper/internal/config" |
| 10 | + "github.com/spf13/cobra" |
| 11 | + "k8s.io/utils/ptr" |
| 12 | +) |
| 13 | + |
| 14 | +type cmdCheck struct { |
| 15 | + cmd *cobra.Command |
| 16 | + platformCommands map[types.Platform][]*command.Check |
| 17 | +} |
| 18 | + |
| 19 | +func NewCmdCheck() *cobra.Command { |
| 20 | + checkCmd := cmdCheck{ |
| 21 | + platformCommands: map[types.Platform][]*command.Check{}, |
| 22 | + } |
| 23 | + |
| 24 | + checkCmd.cmd = &cobra.Command{ |
| 25 | + Use: "check", |
| 26 | + Short: "Run diagnostics", |
| 27 | + Long: `Runs diagnostics to identify potential issues in the environment hosting Skupper`, |
| 28 | + Example: `skupper debug check -p kubernetes`, |
| 29 | + Run: func(cmd *cobra.Command, args []string) { |
| 30 | + utils.HandleError(utils.GenericError, checkCmd.Run(cmd, args)) |
| 31 | + }, |
| 32 | + } |
| 33 | + |
| 34 | + checkCmd.registerCommand(types.PlatformKubernetes, ptr.To(kube.NewCmdCheckK8sAccess())) |
| 35 | + checkCmd.registerCommand(types.PlatformKubernetes, ptr.To(kube.NewCmdCheckK8sVersion())) |
| 36 | + for _, cmds := range checkCmd.platformCommands { |
| 37 | + for i := range cmds { |
| 38 | + subCommand := *cmds[i] |
| 39 | + cmd := &cobra.Command{ |
| 40 | + Use: subCommand.Name(), |
| 41 | + Short: "check that " + subCommand.CheckDescription(), |
| 42 | + Run: func(cmd *cobra.Command, args []string) { |
| 43 | + status := cli.NewReporter() |
| 44 | + defer status.End() |
| 45 | + runCommandWithDeps(status, subCommand, map[string]bool{}, cmd) |
| 46 | + }, |
| 47 | + } |
| 48 | + // TODO Adjust "skupper" to args[0] |
| 49 | + cmd.Example = "skupper debug check " + subCommand.Name() |
| 50 | + checkCmd.cmd.AddCommand(cmd) |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + return checkCmd.cmd |
| 55 | +} |
| 56 | + |
| 57 | +func (c *cmdCheck) registerCommand(platform types.Platform, cmd *command.Check) { |
| 58 | + c.platformCommands[platform] = append(c.platformCommands[platform], cmd) |
| 59 | +} |
| 60 | + |
| 61 | +func (c cmdCheck) Run(cmd *cobra.Command, args []string) error { |
| 62 | + platform := config.GetPlatform() |
| 63 | + |
| 64 | + // Run all available sub-commands, in dependency order (falling back to declaration order) |
| 65 | + // In the map of processed dependencies, true indicates that the command previously ran successfully, |
| 66 | + // false that it failed previously |
| 67 | + processedDependencies := make(map[string]bool) |
| 68 | + |
| 69 | + status := cli.NewReporter() |
| 70 | + defer status.End() |
| 71 | + |
| 72 | + for i := range c.platformCommands[platform] { |
| 73 | + subCommand := *c.platformCommands[platform][i] |
| 74 | + if _, seen := processedDependencies[subCommand.Name()]; seen { |
| 75 | + // The command has already been run as a dependency, skip it |
| 76 | + continue |
| 77 | + } |
| 78 | + _ = runCommandWithDeps(status, subCommand, processedDependencies, cmd) |
| 79 | + } |
| 80 | + |
| 81 | + // For UX consistency, errors are handled internally |
| 82 | + return nil |
| 83 | +} |
| 84 | + |
| 85 | +func runCommandWithDeps(status cli.Reporter, dc command.Check, processed map[string]bool, cmd *cobra.Command) error { |
| 86 | + dependencies := dc.Dependencies() |
| 87 | + for i := range dependencies { |
| 88 | + dependency := *dependencies[i] |
| 89 | + if succeeded, seen := processed[dependency.Name()]; seen { |
| 90 | + if succeeded { |
| 91 | + // The command previously succeeded, skip it but continue |
| 92 | + continue |
| 93 | + } |
| 94 | + // The command previously failed, stop (assuming that the previous run reported the error) |
| 95 | + return nil |
| 96 | + } |
| 97 | + if err := runCommandWithDeps(status, dependency, processed, cmd); err != nil { |
| 98 | + return err |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + status.Start("Checking that " + dc.CheckDescription()) |
| 103 | + if err := dc.Run(status, cmd); err != nil { |
| 104 | + processed[dc.Name()] = false |
| 105 | + return err |
| 106 | + } |
| 107 | + |
| 108 | + processed[dc.Name()] = true |
| 109 | + status.Success("") |
| 110 | + return nil |
| 111 | +} |
0 commit comments