sigs.k8s.io/kubebuilder/v3@v3.14.0/pkg/plugins/optional/grafana/v1alpha/scaffolds/internal/templates/resources.go (about)

     1  /*
     2  Copyright 2022 The Kubernetes Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package templates
    18  
    19  import (
    20  	"path/filepath"
    21  
    22  	"sigs.k8s.io/kubebuilder/v3/pkg/machinery"
    23  )
    24  
    25  var _ machinery.Template = &ResourcesManifest{}
    26  
    27  // Kustomization scaffolds a file that defines the kustomization scheme for the prometheus folder
    28  type ResourcesManifest struct {
    29  	machinery.TemplateMixin
    30  }
    31  
    32  // SetTemplateDefaults implements file.Template
    33  func (f *ResourcesManifest) SetTemplateDefaults() error {
    34  	if f.Path == "" {
    35  		f.Path = filepath.Join("grafana", "controller-resources-metrics.json")
    36  	}
    37  
    38  	// Grafana syntax use {{ }} quite often, which is collided with default delimiter for go template parsing.
    39  	// Provide an alternative delimiter here to avoid overlaps.
    40  	f.SetDelim("[[", "]]")
    41  	f.TemplateBody = controllerResourcesTemplate
    42  
    43  	f.IfExistsAction = machinery.OverwriteFile
    44  
    45  	return nil
    46  }
    47  
    48  // nolint: lll
    49  const controllerResourcesTemplate = `{
    50    "__inputs": [
    51      {
    52        "name": "DS_PROMETHEUS",
    53        "label": "Prometheus",
    54        "description": "",
    55        "type": "datasource",
    56        "pluginId": "prometheus",
    57        "pluginName": "Prometheus"
    58      }
    59    ],
    60    "__requires": [
    61      {
    62        "type": "datasource",
    63        "id": "prometheus",
    64        "name": "Prometheus",
    65        "version": "1.0.0"
    66      }
    67    ],
    68    "annotations": {
    69      "list": [
    70        {
    71          "builtIn": 1,
    72          "datasource": "-- Grafana --",
    73          "enable": true,
    74          "hide": true,
    75          "iconColor": "rgba(0, 211, 255, 1)",
    76          "name": "Annotations & Alerts",
    77          "target": {
    78            "limit": 100,
    79            "matchAny": false,
    80            "tags": [],
    81            "type": "dashboard"
    82          },
    83          "type": "dashboard"
    84        }
    85      ]
    86    },
    87    "editable": true,
    88    "fiscalYearStartMonth": 0,
    89    "graphTooltip": 0,
    90    "links": [],
    91    "liveNow": false,
    92    "panels": [
    93      {
    94        "datasource": "${DS_PROMETHEUS}",
    95        "fieldConfig": {
    96          "defaults": {
    97            "color": {
    98              "mode": "continuous-GrYlRd"
    99            },
   100            "custom": {
   101              "axisLabel": "",
   102              "axisPlacement": "auto",
   103              "barAlignment": 0,
   104              "drawStyle": "line",
   105              "fillOpacity": 20,
   106              "gradientMode": "scheme",
   107              "hideFrom": {
   108                "legend": false,
   109                "tooltip": false,
   110                "viz": false
   111              },
   112              "lineInterpolation": "smooth",
   113              "lineWidth": 3,
   114              "pointSize": 5,
   115              "scaleDistribution": {
   116                "type": "linear"
   117              },
   118              "showPoints": "auto",
   119              "spanNulls": false,
   120              "stacking": {
   121                "group": "A",
   122                "mode": "none"
   123              },
   124              "thresholdsStyle": {
   125                "mode": "off"
   126              }
   127            },
   128            "mappings": [],
   129            "thresholds": {
   130              "mode": "absolute",
   131              "steps": [
   132                {
   133                  "color": "green",
   134                  "value": null
   135                },
   136                {
   137                  "color": "red",
   138                  "value": 80
   139                }
   140              ]
   141            },
   142            "unit": "percent"
   143          },
   144          "overrides": []
   145        },
   146        "gridPos": {
   147          "h": 8,
   148          "w": 12,
   149          "x": 0,
   150          "y": 0
   151        },
   152        "id": 2,
   153        "interval": "1m",
   154        "links": [],
   155        "options": {
   156          "legend": {
   157            "calcs": [],
   158            "displayMode": "list",
   159            "placement": "bottom"
   160          },
   161          "tooltip": {
   162            "mode": "single",
   163            "sort": "none"
   164          }
   165        },
   166        "pluginVersion": "8.4.3",
   167        "targets": [
   168          {
   169            "datasource": "${DS_PROMETHEUS}",
   170            "exemplar": true,
   171            "expr": "rate(process_cpu_seconds_total{job=\"$job\", namespace=\"$namespace\", pod=\"$pod\"}[5m]) * 100",
   172            "format": "time_series",
   173            "interval": "",
   174            "intervalFactor": 2,
   175            "legendFormat": "Pod: {{pod}} | Container: {{container}}",
   176            "refId": "A",
   177            "step": 10
   178          }
   179        ],
   180        "title": "Controller CPU Usage",
   181        "type": "timeseries"
   182      },
   183      {
   184        "datasource": "${DS_PROMETHEUS}",
   185        "fieldConfig": {
   186          "defaults": {
   187            "color": {
   188              "mode": "continuous-GrYlRd"
   189            },
   190            "custom": {
   191              "axisLabel": "",
   192              "axisPlacement": "auto",
   193              "barAlignment": 0,
   194              "drawStyle": "line",
   195              "fillOpacity": 20,
   196              "gradientMode": "scheme",
   197              "hideFrom": {
   198                "legend": false,
   199                "tooltip": false,
   200                "viz": false
   201              },
   202              "lineInterpolation": "smooth",
   203              "lineWidth": 3,
   204              "pointSize": 5,
   205              "scaleDistribution": {
   206                "type": "linear"
   207              },
   208              "showPoints": "auto",
   209              "spanNulls": false,
   210              "stacking": {
   211                "group": "A",
   212                "mode": "none"
   213              },
   214              "thresholdsStyle": {
   215                "mode": "off"
   216              }
   217            },
   218            "mappings": [],
   219            "thresholds": {
   220              "mode": "absolute",
   221              "steps": [
   222                {
   223                  "color": "green",
   224                  "value": null
   225                },
   226                {
   227                  "color": "red",
   228                  "value": 80
   229                }
   230              ]
   231            },
   232            "unit": "bytes"
   233          },
   234          "overrides": []
   235        },
   236        "gridPos": {
   237          "h": 8,
   238          "w": 12,
   239          "x": 12,
   240          "y": 0
   241        },
   242        "id": 4,
   243        "interval": "1m",
   244        "links": [],
   245        "options": {
   246          "legend": {
   247            "calcs": [],
   248            "displayMode": "list",
   249            "placement": "bottom"
   250          },
   251          "tooltip": {
   252            "mode": "single",
   253            "sort": "none"
   254          }
   255        },
   256        "pluginVersion": "8.4.3",
   257        "targets": [
   258          {
   259            "datasource": "${DS_PROMETHEUS}",
   260            "exemplar": true,
   261            "expr": "process_resident_memory_bytes{job=\"$job\", namespace=\"$namespace\", pod=\"$pod\"}",
   262            "format": "time_series",
   263            "interval": "",
   264            "intervalFactor": 2,
   265            "legendFormat": "Pod: {{pod}} | Container: {{container}}",
   266            "refId": "A",
   267            "step": 10
   268          }
   269        ],
   270        "title": "Controller Memory Usage",
   271        "type": "timeseries"
   272      }
   273    ],
   274    "refresh": "",
   275    "style": "dark",
   276    "tags": [],
   277    "templating": {
   278      "list": [
   279        {
   280          "datasource": "${DS_PROMETHEUS}",
   281          "definition": "label_values(controller_runtime_reconcile_total{namespace=~\"$namespace\"}, job)",
   282          "hide": 0,
   283          "includeAll": false,
   284          "multi": false,
   285          "name": "job",
   286          "options": [],
   287          "query": {
   288            "query": "label_values(controller_runtime_reconcile_total{namespace=~\"$namespace\"}, job)",
   289            "refId": "StandardVariableQuery"
   290          },
   291          "refresh": 2,
   292          "regex": "",
   293          "skipUrlSync": false,
   294          "sort": 0,
   295          "type": "query"
   296        },
   297        {
   298          "current": {
   299            "selected": false,
   300            "text": "observability",
   301            "value": "observability"
   302          },
   303          "datasource": "${DS_PROMETHEUS}",
   304          "definition": "label_values(controller_runtime_reconcile_total, namespace)",
   305          "hide": 0,
   306          "includeAll": false,
   307          "multi": false,
   308          "name": "namespace",
   309          "options": [],
   310          "query": {
   311            "query": "label_values(controller_runtime_reconcile_total, namespace)",
   312            "refId": "StandardVariableQuery"
   313          },
   314          "refresh": 1,
   315          "regex": "",
   316          "skipUrlSync": false,
   317          "sort": 0,
   318          "type": "query"
   319        },
   320        {
   321          "current": {
   322            "selected": false,
   323            "text": "All",
   324            "value": "$__all"
   325          },
   326          "datasource": "${DS_PROMETHEUS}",
   327          "definition": "label_values(controller_runtime_reconcile_total{namespace=~\"$namespace\", job=~\"$job\"}, pod)",
   328          "hide": 2,
   329          "includeAll": true,
   330          "label": "pod",
   331          "multi": true,
   332          "name": "pod",
   333          "options": [],
   334          "query": {
   335            "query": "label_values(controller_runtime_reconcile_total{namespace=~\"$namespace\", job=~\"$job\"}, pod)",
   336            "refId": "StandardVariableQuery"
   337          },
   338          "refresh": 2,
   339          "regex": "",
   340          "skipUrlSync": false,
   341          "sort": 0,
   342          "type": "query"
   343        }
   344      ]
   345    },
   346    "time": {
   347      "from": "now-15m",
   348      "to": "now"
   349    },
   350    "timepicker": {},
   351    "timezone": "",
   352    "title": "Controller-Resources-Metrics",
   353    "weekStart": ""
   354  }
   355  `