Skip to content

Commit

Permalink
Fix opts handling and fix filters (#640)
Browse files Browse the repository at this point in the history
* Fix opts handling and fix filters

* put back the if

* fix errant removal

* better ifs

* Fixie
  • Loading branch information
sudermanjr committed Aug 29, 2023
1 parent 8eb60d1 commit 328847a
Show file tree
Hide file tree
Showing 9 changed files with 35 additions and 32 deletions.
4 changes: 1 addition & 3 deletions pkg/dashboard/dashboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func Dashboard(opts Options) http.Handler {
return
}

tmpl, err := getTemplate("dashboard",
tmpl, err := getTemplate("dashboard", opts,
"container",
"dashboard",
"filter",
Expand All @@ -72,10 +72,8 @@ func Dashboard(opts Options) http.Handler {

data := struct {
VpaData summary.Summary
Opts Options
}{
VpaData: vpaData,
Opts: opts,
}

writeTemplate(tmpl, opts, &data, w)
Expand Down
7 changes: 2 additions & 5 deletions pkg/dashboard/namespace-list.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func NamespaceList(opts Options) http.Handler {
return
}

tmpl, err := getTemplate("namespace_list",
tmpl, err := getTemplate("namespace_list", opts,
"filter",
"namespace_list",
)
Expand All @@ -53,10 +53,7 @@ func NamespaceList(opts Options) http.Handler {
Namespaces []struct {
Name string
}
Opts Options
}{
Opts: opts,
}
}{}

for _, ns := range namespacesList.Items {
item := struct {
Expand Down
6 changes: 5 additions & 1 deletion pkg/dashboard/templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,18 @@ func getTemplateBox() *packr.Box {
}

// getTemplate puts together a template. Individual pieces can be overridden before rendering.
func getTemplate(name string, includedTemplates ...string) (*template.Template, error) {
func getTemplate(name string, opts Options, includedTemplates ...string) (*template.Template, error) {
tmpl := template.New(name).Funcs(template.FuncMap{
"printResource": helpers.PrintResource,
"getStatus": helpers.GetStatus,
"getStatusRange": helpers.GetStatusRange,
"resourceName": helpers.ResourceName,
"getUUID": helpers.GetUUID,
"hasField": helpers.HasField,

"opts": func() Options {
return opts
},
})

// join the default templates and included templates
Expand Down
24 changes: 14 additions & 10 deletions pkg/dashboard/templates/container.gohtml
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,13 @@
<div class="layoutCluster --start">
<h5>Guaranteed QoS</h5>

{{if lt $.GuaranteedCostInt 0}}
<p class="lower-number lower-number--negative">-${{$.GuaranteedCost}}/hour</p>
{{else}}
<p class="lower-number lower-number--positive">+${{$.GuaranteedCost}}/hour</p>
{{end}}
{{ if opts.EnableCost }}
{{ if lt $.GuaranteedCostInt 0 }}
<p class="lower-number lower-number--negative">-${{ $.GuaranteedCost }}/hour</p>
{{ else }}
<p class="lower-number lower-number--positive">+${{ $.GuaranteedCost }}/hour</p>
{{ end }}
{{ end }}
</div>

<table class="compTable callout">
Expand Down Expand Up @@ -114,11 +116,13 @@
<div class="layoutCluster --start">
<h5>Burstable QoS</h5>

{{if lt $.BurstableCostInt 0}}
<p class="lower-number lower-number--negative">-${{$.BurstableCost}}/hour</p>
{{else}}
<p class="lower-number lower-number--positive">+${{$.BurstableCost}}/hour</p>
{{end}}
{{ if opts.EnableCost }}
{{ if lt $.BurstableCostInt 0 }}
<p class="lower-number lower-number--negative">-${{ $.BurstableCost }}/hour</p>
{{ else }}
<p class="lower-number lower-number--positive">+${{ $.BurstableCost }}/hour</p>
{{ end }}
{{ end }}
</div>

<table class="compTable callout">
Expand Down
11 changes: 5 additions & 6 deletions pkg/dashboard/templates/dashboard.gohtml
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
<head>
{{ template "head" .Data }}

{{ if gt (len .Data.VpaData.Namespaces) 1 }}
{{ if gt (len .Data.VpaData.Namespaces) 1 }}
<script src="static/js/filter.js" type="module"></script>

{{ end }}

{{- if opts.EnableCost }}
<noscript>
<style>
#email-box, #api-token-box, #cost-settings-box {
Expand All @@ -16,11 +17,9 @@
</noscript>

<script>
window.INSIGHTS_HOST = "{{ .Data.Opts.InsightsHost }}"
window.INSIGHTS_HOST = "{{ opts.InsightsHost }}"
</script>
{{ end }}

{{- if .Data.Opts.EnableCost }}
<script defer src="static/js/email.js"></script>
<script defer src="static/js/api-token.js"></script>
<script defer src="static/js/cost_settings.js"></script>
Expand All @@ -35,7 +34,7 @@
<div class="layoutSidebar__main">
<main class="verticalRhythm --rhythm-3">

{{- if .Data.Opts.EnableCost }}
{{- if opts.EnableCost }}
{{ template "email" . }}
{{ template "api_token" . }}
{{ template "cost_settings" . }}
Expand Down
4 changes: 2 additions & 2 deletions pkg/dashboard/templates/filter.gohtml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{{ define "filter" }}
<noscript>
<p class="controls">{{ len .Namespaces }} namespaces found</p>
<p class="controls">{{ len . }} namespaces found</p>
</noscript>

<form
Expand Down Expand Up @@ -31,7 +31,7 @@
3. An output that's always shown visually. We have to remove content from the above two to get them to announce the same content back to back. This would make the words pop in and out visually.
Note that the elements used for dynamic screen reader announcements can't themselves be injected or they won't work. Must be pre-existing containers into which the announcment content is injected.
-->
{{ with $initialStatus := (len .Namespaces) | printf "%d total namespaces found" }}
{{ with $initialStatus := (len .) | printf "%d total namespaces found" }}
<div class="control-block">
<output aria-hidden="true">{{ $initialStatus }}</output>
<output class="visually-hidden" aria-live="polite">{{ $initialStatus }}</output>
Expand Down
6 changes: 3 additions & 3 deletions pkg/dashboard/templates/head.gohtml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{{ define "head" }}
<base href="{{ .Opts.BasePath }}">
<base href="{{ opts.BasePath }}">
<meta charset="utf-8" />
<title>Goldilocks by Fairwinds</title>
<meta
Expand All @@ -17,10 +17,10 @@
<link rel="stylesheet" href="static/css/utopia.css" />
<link rel="stylesheet" href="static/css/main.css" />
<link rel="stylesheet" href="static/css/fontawesome-5.7.2.css" />
<script src="static/js/main.js" type="module"></script>

{{- if .Opts.EnableCost }}
{{- if opts.EnableCost }}
<link rel="stylesheet" href="static/css/prism.css" />
<script src="static/js/main.js" type="module"></script>
<script defer src="static/js/prism.js"></script>
{{- end }}

Expand Down
2 changes: 2 additions & 0 deletions pkg/dashboard/templates/namespace.gohtml
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,11 @@
{{ $cName }}
</h4>

{{ if opts.EnableCost }}
{{ if gt $cSummary.ContainerCostInt 0 }}
<span class="top-number">${{ $cSummary.ContainerCost }}/hour</span>
{{ end }}
{{ end }}

<details open>
<summary>Details</summary>
Expand Down
3 changes: 1 addition & 2 deletions pkg/dashboard/templates/namespace_list.gohtml
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,9 @@

{{ if lt (len .Data.Namespaces) 1 }}
<p>No namespaces are labelled for use by Goldilocks. Try labelling one with <code class="language-shell">kubectl label ns NAMESPACE_NAME goldilocks.fairwinds.com/enabled=true</code></p>

{{ else }}
{{ if gt (len .Data.Namespaces) 1 }}
{{ template "filter" .Data }}
{{ template "filter" .Data.Namespaces }}
{{ end }}

<ul aria-live="off" class="namespaceList" id="js-filter-container" role="list">
Expand Down

0 comments on commit 328847a

Please sign in to comment.