Skip to content

Commit fae8f05

Browse files
committed
feat: Initialize the grafana plugin
1 parent 78ad658 commit fae8f05

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+2311
-9
lines changed

cmd/main.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import (
3333
deployimagev1alpha1 "sigs.k8s.io/kubebuilder/v3/pkg/plugins/golang/deploy-image/v1alpha1"
3434
golangv2 "sigs.k8s.io/kubebuilder/v3/pkg/plugins/golang/v2"
3535
golangv3 "sigs.k8s.io/kubebuilder/v3/pkg/plugins/golang/v3"
36+
grafanav1alpha1 "sigs.k8s.io/kubebuilder/v3/pkg/plugins/optional/grafana/v1alpha"
3637
)
3738

3839
func main() {
@@ -62,6 +63,7 @@ func main() {
6263
&kustomizecommonv2alpha.Plugin{},
6364
&declarativev1.Plugin{},
6465
&deployimagev1alpha1.Plugin{},
66+
&grafanav1alpha1.Plugin{},
6567
),
6668
cli.WithPlugins(externalPlugins...),
6769
cli.WithDefaultPlugins(cfgv2.Version, golangv2.Plugin{}),

docs/book/src/SUMMARY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@
112112
- [go/v2 plugin (Deprecated)](./plugins/go-v2-plugin.md)
113113
- [go/v3 plugin](./plugins/go-v3-plugin.md)
114114
- [Declarative V1](./plugins/declarative-v1.md)
115+
- [grafana/v1-alpha](./plugins/grafana-v1-alpha.md)
115116
- [Kustomize V1](./plugins/kustomize-v1.md)
116117
- [Plugins Versioning](./plugins/plugins-versioning.md)
117118

docs/book/src/plugins/available-plugins.md

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,21 @@
22

33
This section describes the plugins supported and shipped in with the Kubebuilder project.
44

5-
| Plugin | Key | Description |
6-
|---|---|---|
7-
| [go.kubebuilder.io/v2 - (Deprecated)](go-v2-plugin.md) | `go/v2` | Golang plugin responsible for scaffolding the legacy layout provided with Kubebuilder CLI >= `2.0.0` and < `3.0.0`. |
8-
| [go.kubebuilder.io/v3 - (Default scaffold with Kubebuilder init)](go-v3-plugin.md) | `go/v3` | Default scaffold used for creating a project when no plugin(s) are provided. Responsible for scaffolding Golang projects and its configurations. |
9-
| [declarative.go.kubebuilder.io/v1](declarative-v1.md) | `declarative/v1` | Optional plugin used to scaffold APIs/controllers using the [kubebuilder-declarative-pattern][kubebuilder-declarative-pattern] project. |
10-
| [kustomize.common.kubebuilder.io/v1](kustomize-v1.md) | `kustomize/v1` | Responsible for scaffold all manifests to configure the projects with [kustomize(v3)][kustomize]. (create and update the the `config/` directory). This plugin is used in the composition to create the plugin (`go/v3`). |
11-
| [kustomize.common.kubebuilder.io/v2-alpha](kustomize-v2-alpha.md) | `kustomize/v2-alpha` | It has the same purpose of `kustomize/v1`. However, it works with [kustomize][kustomize] version `v4` and addresses the required changes for future kustomize configurations. It will probably be used with the future `go/v4-alpha` plugin. |
12-
| `base.go.kubebuilder.io/v3` | `base/v3` | Responsible for scaffold all files which specific requires Golang. This plugin is used in the composition to create the plugin (`go/v3`) |
5+
| Plugin | Key | Description |
6+
| ---------------------------------------------------------------------------------- | -------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
7+
| [go.kubebuilder.io/v2 - (Deprecated)](go-v2-plugin.md) | `go/v2` | Golang plugin responsible for scaffolding the legacy layout provided with Kubebuilder CLI >= `2.0.0` and < `3.0.0`. |
8+
| [go.kubebuilder.io/v3 - (Default scaffold with Kubebuilder init)](go-v3-plugin.md) | `go/v3` | Default scaffold used for creating a project when no plugin(s) are provided. Responsible for scaffolding Golang projects and its configurations. |
9+
| [declarative.go.kubebuilder.io/v1](declarative-v1.md) | `declarative/v1` | Optional plugin used to scaffold APIs/controllers using the [kubebuilder-declarative-pattern][kubebuilder-declarative-pattern] project. |
10+
| [kustomize.common.kubebuilder.io/v1](kustomize-v1.md) | `kustomize/v1` | Responsible for scaffold all manifests to configure the projects with [kustomize(v3)][kustomize]. (create and update the the `config/` directory). This plugin is used in the composition to create the plugin (`go/v3`). |
11+
| [kustomize.common.kubebuilder.io/v2-alpha](kustomize-v2-alpha.md) | `kustomize/v2-alpha` | It has the same purpose of `kustomize/v1`. However, it works with [kustomize][kustomize] version `v4` and addresses the required changes for future kustomize configurations. It will probably be used with the future `go/v4-alpha` plugin. |
12+
| `base.go.kubebuilder.io/v3` | `base/v3` | Responsible for scaffold all files which specific requires Golang. This plugin is used in the composition to create the plugin (`go/v3`) |
13+
| [grafana.kubebuilder.io/v1-alpha](grafana-v1-alpha.md) | `grafana/v1-alpha` | Optional plugin which can be used to scaffold Grafana Manifests Dashboards for the default metrics which are exported by controller-runtime. |
1314

1415
<aside class="note">
1516

1617
<h1>You can also create your own plugins, see:</h1>
1718

18-
- [Creating your own plugins][create-plugins].
19+
- [Creating your own plugins][create-plugins].
1920

2021
</aside>
2122

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
# Grafana Plugin (`grafana/v1-alpha`)
2+
3+
The Grafana plugin is an optional plugin that can be used to scaffold Grafana Dashboards to allow you to check out the default metrics which are exported by projects using [controller-runtime][controller-runtime].
4+
5+
<aside class="note">
6+
<h1>Examples</h1>
7+
8+
You can check its default scaffold by looking at the `project-v3-with-grafana` projects under the [testdata][testdata] directory on the root directory of the Kubebuilder project.
9+
10+
</aside>
11+
12+
## When to use it ?
13+
14+
- If you are looking to observe the metrics exported by [controller metrics][controller-metrics] and collected by Prometheus via [Grafana][grafana].
15+
16+
## How to use it ?
17+
18+
### Prerequisites:
19+
20+
- Your project must be using [controller-runtime][controller-runtime] to expose the metrics via the [controller default metrics][controller-metrics] and they need to be collected by Prometheus.
21+
- Access to [Prometheus][prometheus].
22+
- Prometheus should have an endpoint exposed. (For `prometheus-operator`, this is similar as: http://prometheus-k8s.monitoring.svc:9090 )
23+
- The endpoint is ready to/already become the datasource of your Grafana. See [Add a data source](https://grafana.com/docs/grafana/latest/datasources/add-a-data-source/)
24+
- Access to [Grafana](https://grafana.com/docs/grafana/latest/setup-grafana/installation/). Make sure you have:
25+
- [Dashboard edit permission](https://grafana.com/docs/grafana/next/administration/roles-and-permissions/#dashboard-permissions)
26+
- Prometheus Data source
27+
![pre](https://user-images.githubusercontent.com/18136486/176119794-f6d69b0b-93f0-4f9e-a53c-daf9f77dadae.gif)
28+
29+
<aside class="note">
30+
31+
Check the [metrics][reference-metrics-doc] to know how to enable the metrics for your projects scaffold with Kubebuilder.
32+
33+
See that in the [config/prometheus][kustomize-plugin] you will find the ServiceMonitor to enable the metrics in the default endpoint `/metrics`.
34+
35+
</aside>
36+
37+
### Basic Usage
38+
39+
The Grafana plugin is attached to the `init` subcommand and the `edit` subcommand:
40+
41+
```sh
42+
# Initialize a new project with grafana plugin
43+
kubebuilder init --plugins grafana.kubebuilder.io/v1-alpha
44+
45+
# Enable grafana plugin to an existing project
46+
kubebuilder edit --plugins grafana.kubebuilder.io/v1-alpha
47+
```
48+
49+
The plugin will create a new directory and scaffold the JSON files under it (i.e. `grafana/controller-runtime-metrics.json`).
50+
51+
#### Show case:
52+
53+
See an example of how to use the plugin in your project:
54+
55+
![output](https://user-images.githubusercontent.com/18136486/175382307-9a6c3b8b-6cc7-4339-b221-2539d0fec042.gif)
56+
57+
#### Now, let's check how to use the Grafana dashboards
58+
59+
1. Copy the JSON file
60+
2. Visit `<your-grafana-url>/dashboard/import` to [import a new dashboard](https://grafana.com/docs/grafana/latest/dashboards/export-import/#import-dashboard).
61+
3. Paste the JSON content to `Import via panel json`, then press `Load` button
62+
<img width="644" alt="Screen Shot 2022-06-28 at 3 40 22 AM" src="https://user-images.githubusercontent.com/18136486/176121955-1c4aec9c-0ba4-4271-9767-e8d1726d9d9a.png">
63+
4. Select the data source for Prometheus metrics
64+
<img width="633" alt="Screen Shot 2022-06-28 at 3 41 26 AM" src="https://user-images.githubusercontent.com/18136486/176122261-e3eab5b0-9fc4-45fc-a68c-d9ce1cfe96ee.png">
65+
5. Once the json is imported in Grafana, the dashboard is ready.
66+
67+
### Grafana Dashboard
68+
69+
#### Controller Runtime Reconciliation total & errors
70+
71+
- Metrics:
72+
- controller_runtime_reconcile_total
73+
- controller_runtime_reconcile_errors_total
74+
- Query:
75+
- sum(rate(controller_runtime_reconcile_total{job="$job"}[5m])) by (instance, pod)
76+
- sum(rate(controller_runtime_reconcile_errors_total{job="$job"}[5m])) by (instance, pod)
77+
- Description:
78+
- Per-second rate of total reconciliation as measured over the last 5 minutes
79+
- Per-second rate of reconciliation errors as measured over the last 5 minutes
80+
81+
<img width="1430" alt="Screen Shot 2022-06-28 at 3 43 10 AM" src="https://user-images.githubusercontent.com/18136486/176122555-f3493658-6c99-4ad6-a9b7-63d85620d370.png">
82+
83+
## Subcommands
84+
85+
The Grafana plugin implements the following subcommands:
86+
87+
- edit (`$ kubebuilder edit [OPTIONS]`)
88+
89+
- init (`$ kubebuilder init [OPTIONS]`)
90+
91+
## Affected files
92+
93+
The following scaffolds will be created or updated by this plugin:
94+
95+
- `grafana/*.json`
96+
97+
## Further resources
98+
99+
- Refer to a sample of `servicemonitor` provided by [kustomize plugin][kustomize-plugin]
100+
- Check the [plugin implementation][plugin-implementation]
101+
- [Grafana Docs][grafana-docs] of importing JSON file
102+
- The usage of servicemonitor by [Prometheus Operator][servicemonitor]
103+
104+
[controller-metrics]: https://book.kubebuilder.io/reference/metrics-reference.html
105+
[controller-runtime]: https://github.com/kubernetes-sigs/controller-runtime
106+
[grafana]: https://grafana.com/docs/grafana/next/
107+
[grafana-docs]: https://grafana.com/docs/grafana/latest/dashboards/export-import/#import-dashboard
108+
[kustomize-plugin]: https://github.com/kubernetes-sigs/kubebuilder/blob/master/testdata/project-v3/config/prometheus/monitor.yaml
109+
[kube-prometheus]: https://github.com/prometheus-operator/kube-prometheus
110+
[plugin-implementation]: https://github.com/kubernetes-sigs/kubebuilder/tree/master/pkg/plugins/optional/grafana/alphav1
111+
[prometheus]: https://prometheus.io/docs/introduction/overview/
112+
[prom-operator]: https://prometheus-operator.dev/docs/prologue/introduction/
113+
[reference-metrics-doc]: https://book.kubebuilder.io/reference/metrics.html#exporting-metrics-for-prometheus
114+
[servicemonitor]: https://github.com/prometheus-operator/prometheus-operator/blob/main/Documentation/user-guides/getting-started.md#related-resources
115+
[testdata]: https://github.com/kubernetes-sigs/kubebuilder/tree/master/testdata
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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 v1alpha
18+
19+
import (
20+
"errors"
21+
22+
"sigs.k8s.io/kubebuilder/v3/pkg/config"
23+
)
24+
25+
func InsertPluginMetaToConfig(target config.Config, cfg pluginConfig) error {
26+
err := target.DecodePluginConfig(pluginKey, cfg)
27+
if !errors.As(err, &config.UnsupportedFieldError{}) {
28+
29+
if err != nil && !errors.As(err, &config.PluginKeyNotFoundError{}) {
30+
return err
31+
}
32+
33+
if err = target.EncodePluginConfig(pluginKey, cfg); err != nil {
34+
return err
35+
}
36+
37+
}
38+
39+
return nil
40+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
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 v1alpha
18+
19+
// nolint: lll
20+
const MetaDataDescription = `This command will add Grafana manifests to the project:
21+
- A JSON file includes dashboard manifest that can be directly copied to Grafana Web UI.
22+
('grafana/controller-runtime-metrics.json')
23+
24+
NOTE: This plugin requires:
25+
- Access to Prometheus
26+
- Your project must be using controller-runtime to expose the metrics via the controller metrics and they need to be collected by Prometheus.
27+
- Access to Grafana (https://grafana.com/docs/grafana/latest/setup-grafana/installation/)
28+
Check how to enable the metrics for your project by looking at the doc: https://book.kubebuilder.io/reference/metrics.html
29+
`
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
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 v1alpha
18+
19+
import (
20+
"fmt"
21+
22+
"sigs.k8s.io/kubebuilder/v3/pkg/config"
23+
"sigs.k8s.io/kubebuilder/v3/pkg/machinery"
24+
"sigs.k8s.io/kubebuilder/v3/pkg/plugin"
25+
"sigs.k8s.io/kubebuilder/v3/pkg/plugins/optional/grafana/v1alpha/scaffolds"
26+
)
27+
28+
var _ plugin.EditSubcommand = &editSubcommand{}
29+
30+
type editSubcommand struct {
31+
config config.Config
32+
}
33+
34+
func (p *editSubcommand) UpdateMetadata(cliMeta plugin.CLIMetadata, subcmdMeta *plugin.SubcommandMetadata) {
35+
subcmdMeta.Description = MetaDataDescription
36+
37+
subcmdMeta.Examples = fmt.Sprintf(` # Edit a common project with this plugin
38+
%[1]s edit --plugins=grafana.kubebuilder.io/v1-alpha
39+
`, cliMeta.CommandName)
40+
}
41+
42+
func (p *editSubcommand) InjectConfig(c config.Config) error {
43+
p.config = c
44+
return nil
45+
}
46+
47+
func (p *editSubcommand) Scaffold(fs machinery.Filesystem) error {
48+
if err := InsertPluginMetaToConfig(p.config, pluginConfig{}); err != nil {
49+
return err
50+
}
51+
52+
scaffolder := scaffolds.NewEditScaffolder()
53+
scaffolder.InjectFS(fs)
54+
return scaffolder.Scaffold()
55+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
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 v1alpha
18+
19+
import (
20+
"fmt"
21+
22+
"sigs.k8s.io/kubebuilder/v3/pkg/config"
23+
"sigs.k8s.io/kubebuilder/v3/pkg/machinery"
24+
"sigs.k8s.io/kubebuilder/v3/pkg/plugin"
25+
"sigs.k8s.io/kubebuilder/v3/pkg/plugins/optional/grafana/v1alpha/scaffolds"
26+
)
27+
28+
var _ plugin.InitSubcommand = &initSubcommand{}
29+
30+
type initSubcommand struct {
31+
config config.Config
32+
}
33+
34+
func (p *initSubcommand) UpdateMetadata(cliMeta plugin.CLIMetadata, subcmdMeta *plugin.SubcommandMetadata) {
35+
subcmdMeta.Description = MetaDataDescription
36+
37+
subcmdMeta.Examples = fmt.Sprintf(` # Initialize a common project with this plugin
38+
%[1]s init --plugins=grafana.kubebuilder.io/v1-alpha
39+
`, cliMeta.CommandName)
40+
}
41+
42+
func (p *initSubcommand) InjectConfig(c config.Config) error {
43+
p.config = c
44+
return nil
45+
}
46+
47+
func (p *initSubcommand) Scaffold(fs machinery.Filesystem) error {
48+
if err := InsertPluginMetaToConfig(p.config, pluginConfig{}); err != nil {
49+
return err
50+
}
51+
52+
scaffolder := scaffolds.NewInitScaffolder()
53+
scaffolder.InjectFS(fs)
54+
return scaffolder.Scaffold()
55+
}

0 commit comments

Comments
 (0)