-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
157 lines (142 loc) · 3.41 KB
/
main.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package main
import (
"context"
"flag"
"fmt"
"os"
"os/exec"
"regexp"
"strings"
"time"
"github.com/go-stack/stack"
"k8s.io/client-go/dynamic"
"k8s.io/client-go/tools/clientcmd"
"k8s.io/klog/v2"
"github.com/rexagod/cpv/internal/client"
"github.com/rexagod/cpv/internal/options"
"github.com/rexagod/cpv/internal/profiles"
)
const (
contextTimeout = 5 * time.Minute
invalidProfileErr = "invalid profile: %s"
)
func main() {
// Get options.
o := options.NewOptions()
// Check if the endpoint at -address is up.
err := o.IsUp()
if err != nil {
klog.Fatal(err)
}
// Create a new client.
ctx, cancel := context.WithTimeout(context.Background(), contextTimeout)
defer cancel()
c := client.NewClient(ctx, o.Address, o.BearerToken)
if err := c.Init(); err != nil {
klog.Fatal(err)
}
// Create a new Kube client.
kubeconfig, err := clientcmd.BuildConfigFromFlags("", o.KubeconfigPath)
if err != nil {
klog.Fatal(err)
}
dc, err := dynamic.NewForConfig(kubeconfig)
if err != nil {
klog.Fatal(err)
}
// Track if any operation was performed based on the given inputs.
didOp := false
// Call profile-specific operator to validate the respective profile.
if o.Profile != "" && o.Validate {
didOp = true
p := profiles.CollectionProfile(o.Profile)
if !profiles.IsSupportedCollectionProfile(p) {
klog.Fatalf(invalidProfileErr, p)
}
err = profiles.ProfileOperators[p].Operator(
ctx,
dc,
c,
o.Noisy,
)
if err != nil {
klog.Error(err)
}
}
// Call profile-specific extractor to extract the metrics needed to implement the respective profile.
if o.Profile != "" && o.HasExtractor() {
didOp = true
p := profiles.CollectionProfile(o.Profile)
if !profiles.IsSupportedCollectionProfile(p) {
klog.Fatalf(invalidProfileErr, p)
}
err = profiles.ProfileExtractors[p].Extract(
ctx,
c,
o.AllowListFile,
o.RuleFile,
o.TargetSelectors,
o.OutputCardinality,
)
if err != nil {
klog.Error(err)
}
}
// Report implementation status for all supported profiles, or a particular one if specified.
if o.Status {
didOp = true
p := profiles.CollectionProfile(o.Profile)
if !profiles.IsSupportedCollectionProfile(p) && p != "" {
klog.Fatalf(invalidProfileErr, p)
}
err = profiles.ReportImplementationStatus(
ctx,
dc,
p,
o.Noisy,
)
if err != nil {
klog.Error(err)
}
}
// If no operation was performed, print usage.
if !didOp {
flag.Usage()
os.Exit(1)
}
// If quiet mode is enabled, open all generated manifests in $EDITOR.
if o.Quiet {
klog.Flush()
klogFileName := flag.Lookup("log_file").Value.String()
if klogFileName == "" {
panic(fmt.Errorf("failed to lookup log_file: %s", stack.Trace()))
}
raw, err := os.ReadFile(klogFileName)
if err != nil {
panic(err)
}
r := regexp.MustCompile(`refer: (.*)\b`)
matches := r.FindAllStringSubmatch(string(raw), -1)
if len(matches) > 0 {
var captureGroups []string
for _, match := range matches {
captureGroups = append(captureGroups, match[1])
}
editor := os.Getenv("EDITOR")
editorPath, err := exec.Command("which", editor).Output()
if err != nil {
panic(err)
}
// gosec complains if the args are not hardcoded.
// nolint:gosec
cmd := exec.Command(strings.Trim(string(editorPath), "\n"), captureGroups...)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
cmd.Stdin = os.Stdin
err = cmd.Run()
if err != nil {
panic(err)
}
}
}
}