forked from kubernetes-sigs/kubebuilder
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
Showing
93 changed files
with
4,374 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. | ||
` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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{} |
Oops, something went wrong.