-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
111 lines (93 loc) · 3.29 KB
/
Copy pathmain.go
File metadata and controls
111 lines (93 loc) · 3.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
// Command ocp-upgrade-precheck runs the OpenShift 4 cluster upgrade
// pre-checks documented in Red Hat KCS solution 7004992
// (https://access.redhat.com/solutions/7004992) against a live cluster.
package main
import (
"context"
"flag"
"fmt"
"os"
"sort"
"strings"
"time"
"ocp-upgrade-precheck/internal/checks"
"ocp-upgrade-precheck/internal/client"
"ocp-upgrade-precheck/internal/report"
)
const exitCodeSetupError = 2
func main() {
os.Exit(run(os.Args[1:]))
}
func run(args []string) int {
fs := flag.NewFlagSet("ocp-upgrade-precheck", flag.ContinueOnError)
kubeconfig := fs.String("kubeconfig", "", "path to kubeconfig (defaults to $KUBECONFIG, then ~/.kube/config, then in-cluster config)")
kubeContext := fs.String("context", "", "kubeconfig context to use (defaults to the current context)")
targetVersion := fs.String("target-version", "", "target OpenShift version being upgraded to, e.g. 4.16.10 (enables update-path, removed-API, and version-specific checks)")
jsonPath := fs.String("json", "", "also write a machine-readable JSON report to this path")
noColor := fs.Bool("no-color", false, "disable ANSI color in terminal output")
only := fs.String("only", "", "comma-separated list of check names to run exclusively (see --list-checks)")
skip := fs.String("skip", "", "comma-separated list of check names to skip")
timeout := fs.Duration("timeout", 30*time.Second, "per-check timeout")
listChecks := fs.Bool("list-checks", false, "print all check names and exit")
fs.Usage = func() {
fmt.Fprintln(fs.Output(), "ocp-upgrade-precheck runs the OpenShift 4 cluster upgrade pre-checks from")
fmt.Fprintln(fs.Output(), "Red Hat KCS solution 7004992: https://access.redhat.com/solutions/7004992")
fmt.Fprintln(fs.Output())
fmt.Fprintf(fs.Output(), "Usage: %s [flags]\n\n", fs.Name())
fs.PrintDefaults()
}
if err := fs.Parse(args); err != nil {
if err == flag.ErrHelp {
return 0
}
return exitCodeSetupError
}
if *listChecks {
for _, n := range checks.Names() {
fmt.Println(n)
}
return 0
}
clients, err := client.NewClients(*kubeconfig, *kubeContext)
if err != nil {
fmt.Fprintf(os.Stderr, "error: could not connect to cluster: %v\n", err)
return exitCodeSetupError
}
onlyList := splitCSV(*only)
skipList := splitCSV(*skip)
ctx := context.Background()
results := checks.Run(ctx, clients, checks.Options{TargetVersion: *targetVersion}, onlyList, skipList, *timeout)
sortResultsByName(results)
rep := &report.Report{
GeneratedAt: time.Now(),
ClusterServer: clients.Config.Host,
TargetVersion: *targetVersion,
Results: results,
}
report.PrintTerminal(os.Stdout, rep, !*noColor)
if *jsonPath != "" {
if err := report.WriteJSON(*jsonPath, rep); err != nil {
fmt.Fprintf(os.Stderr, "warning: could not write JSON report to %s: %v\n", *jsonPath, err)
} else {
fmt.Printf("\nJSON report written to %s\n", *jsonPath)
}
}
return rep.ExitCode()
}
func splitCSV(s string) []string {
if strings.TrimSpace(s) == "" {
return nil
}
parts := strings.Split(s, ",")
out := make([]string, 0, len(parts))
for _, p := range parts {
p = strings.TrimSpace(p)
if p != "" {
out = append(out, p)
}
}
return out
}
func sortResultsByName(results []report.Result) {
sort.SliceStable(results, func(i, j int) bool { return results[i].Name < results[j].Name })
}