-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathopenshift-linter.go
88 lines (77 loc) · 2.89 KB
/
openshift-linter.go
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
package main
import (
"flag"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"time"
)
//Check OpenShift/Kubernetes configuration files for probable errors and omissions
//Supports command line, server and GUI use
func main() {
flag.Usage = func() {
fmt.Fprintf(os.Stderr, "Usage: %s [<JSON/YAML file> [<JSON/YAML file>]]\nAlternatively, pipe input to STDIN: oc export dc --raw -o json | %s\n", filepath.Base(os.Args[0]), filepath.Base(os.Args[0]))
flag.PrintDefaults()
fmt.Fprintf(os.Stderr, "Commands:\n list\tPrint list of available checks\n")
os.Exit(0)
}
certificate := flag.String("c", "cert.pem", "TLS server certificate")
key := flag.String("k", "key.pem", "TLS server key")
host := flag.String("n", "localhost", "hostname")
port := flag.Int("p", 8443, "listen on port")
output := flag.String("o", "md", "output format (json, yaml or md)")
namespacePattern := flag.String("namespace", "^[a-z0-9_-]*$", "pattern for namespaces/projects")
namePattern := flag.String("name", "^[a-z0-9_-]+$", "pattern for names")
containerPattern := flag.String("container", "^[a-z0-9_-]+$", "pattern for containers")
envPattern := flag.String("env", "^[A-Z0-9_-]+$", "pattern for environment variables")
skipContainerPattern := flag.String("skip-containers", "", "pattern for skipped containers")
whitelistRegistriesPattern := flag.String("whitelist-registries", ".*", "pattern for whitelisted registries")
checkPattern := flag.String("checks", "^[a-z0-9 _-]+$", "pattern for selected checks")
flag.Parse()
args := flag.Args()
//use case [A]: STDIN handling
stdinFileInfo, _ := os.Stdin.Stat()
if stdinFileInfo.Mode()&os.ModeNamedPipe != 0 {
stdin, _ := ioutil.ReadAll(os.Stdin)
combinedResultMap, err := processBytes(stdin, LinterParams{*namespacePattern, *namePattern, *containerPattern, *envPattern, *skipContainerPattern, *whitelistRegistriesPattern, *checkPattern, *output})
if err != nil {
fmt.Fprintf(os.Stderr, "%v\n", err)
return
}
buffer, err := assembleOutput(combinedResultMap, *output)
if err != nil {
fmt.Fprintf(os.Stderr, "%v\n", err)
os.Exit(1)
}
fmt.Println(buffer)
os.Exit(len(buffer))
}
//use case [B]: server
//patterns are ignored in server mode - specify as follows:
//{ customNamespacePattern="...", customNamePattern="..."", ... }
if len(args) == 0 {
serve(*certificate, *key, *host, *port)
return
} else if len(args) == 1 {
switch args[0] {
case "list":
ListLinterItems()
os.Exit(0)
default:
break
}
}
// use case [C]: file input
for _, arg := range args {
start := time.Now()
buffer, err := processFile(arg, LinterParams{*namespacePattern, *namePattern, *containerPattern, *envPattern, *skipContainerPattern, *whitelistRegistriesPattern, *checkPattern, *output})
secs := time.Since(start).Seconds()
if err != nil {
fmt.Fprintf(os.Stderr, "%s: %v (%.2fs)\n", arg, err, secs)
os.Exit(1)
}
fmt.Println(buffer)
os.Exit(len(buffer))
}
}