-
Notifications
You must be signed in to change notification settings - Fork 24
/
dashboard.go
195 lines (163 loc) · 5.59 KB
/
dashboard.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
package cmd
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"os"
"path/filepath"
"strings"
"github.com/fluxninja/aperture/v2/cmd/aperturectl/cmd/utils"
"github.com/fluxninja/aperture/v2/pkg/log"
"github.com/google/go-jsonnet"
"github.com/spf13/cobra"
)
var (
policyFile string
datasourceName string
uri string
dashboardVersion string
outputDir string
overwrite bool
skipPull bool
)
func init() {
dashboardCmd.Flags().StringVar(&policyFile, "policy-file", "", "Path to the policy file to use")
dashboardCmd.Flags().StringVar(&datasourceName, "datasource-name", "controller-prometheus", "Name of the datasource to use")
dashboardCmd.Flags().StringVar(&uri, "uri", "", "URI of Custom Dashboards, could be a local path or a remote git repository, e.g. github.com/fluxninja/aperture/dashboards/grafana@latest. This field should not be provided when the Version is provided.")
dashboardCmd.Flags().StringVar(&dashboardVersion, "version", utils.LatestTag, "Version of official Aperture Dashboards, e.g. latest. This field should not be provided when the URI is provided")
dashboardCmd.Flags().StringVar(&outputDir, "output-dir", "dashboards", "Output directory for the generated dashboards")
dashboardCmd.Flags().BoolVar(&overwrite, "overwrite", false, "Overwrite the output directory if it already exists")
dashboardCmd.Flags().BoolVar(&skipPull, "skip-pull", false, "Skip pulling the dashboards from the remote repository")
}
var dashboardCmd = &cobra.Command{
Use: "dashboard",
Short: "Generate dashboards for Aperture",
Long: `Generate dashboards for Aperture`,
SilenceErrors: true,
RunE: func(*cobra.Command, []string) error {
if policyFile == "" {
return errors.New("policy-file is required")
}
if datasourceName == "" {
return errors.New("datasource-name is required")
}
if uri == "" && dashboardVersion == "" {
return errors.New("either uri or version is required")
}
// if uri is provided and version is default, set version to empty string
if uri != "" && dashboardVersion == utils.LatestTag {
dashboardVersion = ""
}
if outputDir == "" {
return errors.New("output-dir is required")
}
absOutputDir, err := setupOutputDir(outputDir)
if err != nil {
return err
}
err = Generate(policyFile, absOutputDir)
if err != nil {
return err
}
return nil
},
}
// Generate generates the dashboards for the given policy.
func Generate(policyFile, outputDir string) error {
_, err := os.Stat(policyFile)
if err != nil {
log.Info().Msgf("Error reading values file: %s", err.Error())
return err
}
_, dashboardsURIRoot, dashboardsDir, err := utils.Pull(uri, dashboardVersion, "dashboards", utils.DefaultDashboardsRepo, skipPull, true)
if err != nil {
return err
}
_, graph, policyName, policyBytes, err := utils.ProcessPolicy(policyFile)
if err != nil {
return err
}
err = generateDashboards(dashboardsURIRoot, dashboardsDir, string(policyBytes), graph, policyName, outputDir)
if err != nil {
return err
}
log.Info().Msgf("Generated dashboards for policy '%s' in '%s'", policyName, outputDir)
return nil
}
// generateDashboards generates the dashboards for the given policy.
func generateDashboards(dashboardsURIRoot, dashboardsDir, policyStr, graph, policyName, outputDir string) error {
// create the directory if it does not exist
err := os.MkdirAll(outputDir, os.ModePerm)
if err != nil {
return err
}
buf := bytes.Buffer{}
vm := jsonnet.MakeVM()
vm.SetTraceOut(&buf)
vm.Importer(&jsonnet.FileImporter{
JPaths: []string{dashboardsURIRoot},
})
vm.TLAReset()
vm.TLAVar("policyFile", policyStr)
vm.TLAVar("graph", graph)
vm.TLAVar("policyName", policyName)
vm.TLAVar("datasource", datasourceName)
dashboardGroupFile := filepath.Join(dashboardsDir, "grafana", "group.libsonnet")
dashboardsJSON, err := vm.EvaluateFile(dashboardGroupFile)
log.Debug().Msgf("Jsonnet generation trace: %s", buf.String())
if err != nil {
return err
}
type dashboards struct {
Dashboards map[string]interface{} `json:"dashboards"`
}
var dashboardsList dashboards
err = json.Unmarshal([]byte(dashboardsJSON), &dashboardsList)
if err != nil {
return err
}
for key, val := range dashboardsList.Dashboards {
outputFilePath := filepath.Join(outputDir, fmt.Sprintf("%s-%s.json", key, policyName))
err = saveJSONFile(outputFilePath, val.(map[string]interface{}))
if err != nil {
return err
}
}
return nil
}
// saveJSONFile saves the given content as a JSON file.
func saveJSONFile(outputFilePath string, content map[string]interface{}) error {
jsonBytes, err := json.MarshalIndent(content, "", " ")
if err != nil {
return err
}
err = os.WriteFile(outputFilePath, jsonBytes, 0o600)
if err != nil {
return err
}
return nil
}
// setupOutputDir creates the output directory if it does not exist and returns the absolute path to the output directory.
func setupOutputDir(outputDir string) (string, error) {
// ask for user confirmation if the output directory already exists
if !overwrite {
if _, err := os.Stat(outputDir); err == nil {
fmt.Printf("The output directory '%s' already exists. Do you want to merge the generated policy artifacts into the existing directory? [y/N]: ", outputDir)
var response string
fmt.Scanln(&response)
if strings.ToLower(response) != "y" {
return "", fmt.Errorf("output directory '%s' already exists", outputDir)
}
}
}
err := os.MkdirAll(outputDir, os.ModePerm)
if err != nil {
return "", err
}
absOutputDir, err := filepath.Abs(outputDir)
if err != nil {
return "", err
}
return filepath.Join(absOutputDir, "dashboards"), nil
}