Skip to content

Commit

Permalink
[plugins] Create monitoring plugin
Browse files Browse the repository at this point in the history
GitHub-issue: kubernetes-sigs#3078
Signed-off-by: Aviv Litman <[email protected]>

Uncomment monitoring go files

Signed-off-by: João Vilaça <[email protected]>

Generate working monitoring scaffolding

Signed-off-by: João Vilaça <[email protected]>

Allow kubebuilder init with monitoring bundle

Signed-off-by: João Vilaça <[email protected]>

Add metrics register to main file

Signed-off-by: João Vilaça <[email protected]>

Update testdata

Signed-off-by: João Vilaça <[email protected]>

Improve comments

Signed-off-by: Aviv Litman <[email protected]>
  • Loading branch information
avlitman committed Dec 1, 2022
1 parent 3044376 commit 85cdb6e
Show file tree
Hide file tree
Showing 98 changed files with 4,594 additions and 10 deletions.
2 changes: 2 additions & 0 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import (
golangv3 "sigs.k8s.io/kubebuilder/v3/pkg/plugins/golang/v3"
golangv4 "sigs.k8s.io/kubebuilder/v3/pkg/plugins/golang/v4"
grafanav1alpha1 "sigs.k8s.io/kubebuilder/v3/pkg/plugins/optional/grafana/v1alpha"
monitoringv1alpha1 "sigs.k8s.io/kubebuilder/v3/pkg/plugins/optional/monitoring/v1alpha"
)

func main() {
Expand Down Expand Up @@ -73,6 +74,7 @@ func main() {
&declarativev1.Plugin{},
&deployimagev1alpha1.Plugin{},
&grafanav1alpha1.Plugin{},
&monitoringv1alpha1.Plugin{},
),
cli.WithPlugins(externalPlugins...),
cli.WithDefaultPlugins(cfgv2.Version, golangv2.Plugin{}),
Expand Down
1 change: 1 addition & 0 deletions docs/book/src/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@
- [To add optional features](./plugins/to-add-optional-features.md)
- [declarative/v1](./plugins/declarative-v1.md)
- [grafana/v1-alpha](./plugins/grafana-v1-alpha.md)
- [monitoring/v1-alpha](./plugins/monitoring-v1-alpha.md)
- [deploy-image/v1-alpha](./plugins/deploy-image-plugin-v1-alpha.md)
- [To be extended for others tools](./plugins/to-be-extended.md)
- [kustomize/v1](./plugins/kustomize-v1.md)
Expand Down
11 changes: 11 additions & 0 deletions docs/book/src/plugins/creating-plugins.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,15 @@ kubebuilder create api --group example.com --version v1alpha1 --kind Memcached -

This plugin will perform a custom scaffold following the [Operator Pattern][operator-pattern].

The [`monitoring`][monitoring] plugin will help setting up Prometheus based monitoring, will provide best practices and tooling for monitoring requirements and help with standardizing the way monitoring is implemented in operators.

```sh
kubebuilder init --plugins="go/v4-alpha,monitoring/v1-alpha"
```
```sh
kubebuilder edit --plugins="monitoring/v1-alpha"
```

Another example is the [`grafana`][grafana] plugin that scaffolds a new folder container manifests to visualize operator status on Grafana Web UI:

```sh
Expand All @@ -106,6 +115,7 @@ Feel free to check the implementation under:

- deploy-image: <https://github.com/kubernetes-sigs/kubebuilder/tree/v3.7.0/pkg/plugins/golang/deploy-image/v1alpha1>
- grafana: <https://github.com/kubernetes-sigs/kubebuilder/tree/v3.7.0/pkg/plugins/optional/grafana/v1alpha>
- monitoring: <https://github.com/kubernetes-sigs/kubebuilder/tree/v3.7.0/pkg/plugins/optional/monitoring/v1alpha>

## Plugin Scaffolding

Expand Down Expand Up @@ -178,6 +188,7 @@ Alternatively, you can create a plugin bundle to include the target plugins. For
[controller-runtime]: https://github.com/kubernetes-sigs/controller-runtime
[deploy-image]: https://github.com/kubernetes-sigs/kubebuilder/tree/v3.7.0/pkg/plugins/golang/deploy-image/v1alpha1
[grafana]: https://github.com/kubernetes-sigs/kubebuilder/tree/v3.7.0/pkg/plugins/optional/grafana/v1alpha
[monitoring]: https://github.com/kubernetes-sigs/kubebuilder/tree/v3.7.0/pkg/plugins/optional/monitoring/v1alpha
[extending-cli]: ./extending-cli.md
[kb-util]: https://pkg.go.dev/sigs.k8s.io/kubebuilder/v3/pkg/plugin/util
[example-of-deploy-image-1]: https://github.com/kubernetes-sigs/kubebuilder/blob/df1ed6ccf19df40bd929157a91eaae6a9215bfc6/pkg/plugins/golang/deploy-image/v1alpha1/scaffolds/internal/templates/api/types.go#L58
Expand Down
45 changes: 45 additions & 0 deletions docs/book/src/plugins/monitoring-v1-alpha.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# Monitoring Plugin (`monitoring/v1-alpha`)

The Monitoring plugin is an optional plugin that can be used to scaffold Prometheus based monitoring, will provide best practices and tooling for monitoring requirements and help with standardizing the way monitoring is implemented in operators.

<aside class="note">
<h1>Examples</h1>

You can check its default scaffold by looking at the `project-v4-with-monitoring` or `project-v3-with-monitoring` projects under the [testdata][testdata] directory on the root directory of the Kubebuilder project.

</aside>

## When to use it ?

- If you are looking to implement Prometheus based monitoring to your operator and looking for a scaffold that will help you with the initial onboarding and will provide you with best practices and tooling.

## How to use it ?

### Prerequisites:

- Access to [Prometheus][prometheus].

### Basic Usage

The monitoring plugin is attached to the `init` subcommand and the `edit` subcommand:

```sh
# Initialize a new project with monitoring plugin
kubebuilder init --plugins="go/v4-alpha,monitoring/v1-alpha"
# or
kubebuilder init --plugins="go/v3,monitoring/v1-alpha"

# Enable monitoring plugin to an existing project
kubebuilder edit --plugins="monitoring/v1-alpha"
```

## Affected files

The following scaffolds will be created or updated by this plugin:

- `monitoring/`
- `main.go`
- `MakeFile`

[prometheus]: https://prometheus.io/docs/introduction/overview/
[testdata]: https://github.com/kubernetes-sigs/kubebuilder/tree/master/testdata
11 changes: 6 additions & 5 deletions docs/book/src/plugins/to-add-optional-features.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@

The following plugins are useful to generate code and take advantage of optional features

| Plugin | Key | Description |
| ---------------------------------------------------------------------------------- | -------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| [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. |
| [grafana.kubebuilder.io/v1-alpha](grafana-v1-alpha.md) | `grafana/v1-alpha` | Optional helper plugin which can be used to scaffold Grafana Manifests Dashboards for the default metrics which are exported by controller-runtime. |
| [deploy-image.go.kubebuilder.io/v1-alpha](deploy-image-plugin-v1-alpha) | `deploy-image/v1-alpha` | Optional helper plugin which can be used to scaffold APIs and controller with code implementation to Deploy and Manage an Operand(image). |
| Plugin | Key | Description |
| ---------------------------------------------------------------------------------- | -------------------- |--------------------------------------------------------------------------------------------------------------------------------------------|
| [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. |
| [grafana.kubebuilder.io/v1-alpha](grafana-v1-alpha.md) | `grafana/v1-alpha` | Optional helper plugin which can be used to scaffold Grafana Manifests Dashboards for the default metrics which are exported by controller-runtime. |
| [monitoring.kubebuilder.io/v1-alpha](monitoring-v1-alpha.md) | `monitoring/v1-alpha` | Optional helper plugin which can be used to scaffold Prometheus based monitoring. |
| [deploy-image.go.kubebuilder.io/v1-alpha](deploy-image-plugin-v1-alpha) | `deploy-image/v1-alpha` | Optional helper plugin which can be used to scaffold APIs and controller with code implementation to Deploy and Manage an Operand(image). |
40 changes: 40 additions & 0 deletions pkg/plugins/optional/monitoring/v1alpha/commons.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
Copyright 2022 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package v1alpha

import (
"errors"

"sigs.k8s.io/kubebuilder/v3/pkg/config"
)

func InsertPluginMetaToConfig(target config.Config, cfg pluginConfig) error {
err := target.DecodePluginConfig(pluginKey, cfg)
if !errors.As(err, &config.UnsupportedFieldError{}) {

if err != nil && !errors.As(err, &config.PluginKeyNotFoundError{}) {
return err
}

if err = target.EncodePluginConfig(pluginKey, cfg); err != nil {
return err
}

}

return nil
}
23 changes: 23 additions & 0 deletions pkg/plugins/optional/monitoring/v1alpha/constants.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
Copyright 2022 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package v1alpha

// nolint: lll
const MetaDataDescription = `This command will add Monitoring manifests to the project.
NOTE: This plugin requires access to Prometheus.
`
67 changes: 67 additions & 0 deletions pkg/plugins/optional/monitoring/v1alpha/edit.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
Copyright 2022 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package v1alpha

import (
"fmt"
"sigs.k8s.io/kubebuilder/v3/pkg/config"
"sigs.k8s.io/kubebuilder/v3/pkg/machinery"
"sigs.k8s.io/kubebuilder/v3/pkg/plugin"
"sigs.k8s.io/kubebuilder/v3/pkg/plugins/golang"
"sigs.k8s.io/kubebuilder/v3/pkg/plugins/optional/monitoring/v1alpha/scaffolds"
)

var _ plugin.EditSubcommand = &editSubcommand{}

type editSubcommand struct {
config config.Config
}

func (p *editSubcommand) UpdateMetadata(cliMeta plugin.CLIMetadata, subcmdMeta *plugin.SubcommandMetadata) {
subcmdMeta.Description = MetaDataDescription

subcmdMeta.Examples = fmt.Sprintf(` # Edit a common project with this plugin
%[1]s edit --plugins=monitoring.kubebuilder.io/v1-alpha
`, cliMeta.CommandName)
}

func (p *editSubcommand) InjectConfig(c config.Config) error {
if c.GetRepository() == "" {
repoPath, err := golang.FindCurrentRepo()
if err != nil {
return fmt.Errorf("error finding current repository: %v", err)
}
err = c.SetRepository(repoPath)
if err != nil {
return err
}
}

p.config = c

return nil
}

func (p *editSubcommand) Scaffold(fs machinery.Filesystem) error {
if err := InsertPluginMetaToConfig(p.config, pluginConfig{}); err != nil {
return err
}

scaffolder := scaffolds.NewEditScaffolder(p.config)
scaffolder.InjectFS(fs)
return scaffolder.Scaffold()
}
55 changes: 55 additions & 0 deletions pkg/plugins/optional/monitoring/v1alpha/init.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
Copyright 2022 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package v1alpha

import (
"fmt"

"sigs.k8s.io/kubebuilder/v3/pkg/config"
"sigs.k8s.io/kubebuilder/v3/pkg/machinery"
"sigs.k8s.io/kubebuilder/v3/pkg/plugin"
"sigs.k8s.io/kubebuilder/v3/pkg/plugins/optional/monitoring/v1alpha/scaffolds"
)

var _ plugin.InitSubcommand = &initSubcommand{}

type initSubcommand struct {
config config.Config
}

func (p *initSubcommand) UpdateMetadata(cliMeta plugin.CLIMetadata, subcmdMeta *plugin.SubcommandMetadata) {
subcmdMeta.Description = MetaDataDescription

subcmdMeta.Examples = fmt.Sprintf(` # Initialize a common project with this plugin
%[1]s init --plugins=monitoring.kubebuilder.io/v1-alpha
`, cliMeta.CommandName)
}

func (p *initSubcommand) InjectConfig(c config.Config) error {
p.config = c
return nil
}

func (p *initSubcommand) Scaffold(fs machinery.Filesystem) error {
if err := InsertPluginMetaToConfig(p.config, pluginConfig{}); err != nil {
return err
}

scaffolder := scaffolds.NewInitScaffolder(p.config)
scaffolder.InjectFS(fs)
return scaffolder.Scaffold()
}
60 changes: 60 additions & 0 deletions pkg/plugins/optional/monitoring/v1alpha/plugin.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
Copyright 2022 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package v1alpha

import (
"sigs.k8s.io/kubebuilder/v3/pkg/config"
cfgv3 "sigs.k8s.io/kubebuilder/v3/pkg/config/v3"
"sigs.k8s.io/kubebuilder/v3/pkg/model/stage"
"sigs.k8s.io/kubebuilder/v3/pkg/plugin"
"sigs.k8s.io/kubebuilder/v3/pkg/plugins"
)

const pluginName = "monitoring." + plugins.DefaultNameQualifier

var (
pluginVersion = plugin.Version{Number: 1, Stage: stage.Alpha}
supportedProjectVersions = []config.Version{cfgv3.Version}
pluginKey = plugin.KeyFor(Plugin{})
)

// Plugin implements the plugin.Full interface
type Plugin struct {
initSubcommand
editSubcommand
}

var (
_ plugin.Init = Plugin{}
)

// Name returns the name of the plugin
func (Plugin) Name() string { return pluginName }

// Version returns the version of the monitoring plugin
func (Plugin) Version() plugin.Version { return pluginVersion }

// SupportedProjectVersions returns an array with all project versions supported by the plugin
func (Plugin) SupportedProjectVersions() []config.Version { return supportedProjectVersions }

// GetInitSubcommand will return the subcommand which is responsible for initializing and scaffolding monitoring manifests
func (p Plugin) GetInitSubcommand() plugin.InitSubcommand { return &p.initSubcommand }

// GetEditSubcommand will return the subcommand which is responsible for adding monitoring manifests
func (p Plugin) GetEditSubcommand() plugin.EditSubcommand { return &p.editSubcommand }

type pluginConfig struct{}
Loading

0 comments on commit 85cdb6e

Please sign in to comment.