-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Remove metrics logic from cmd/helm-operator/main.go #3451
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
bharathi-tenneti
merged 7 commits into
operator-framework:master
from
bharathi-tenneti:helm-metrics
Jul 17, 2020
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
5144f4b
Remove metrics logic from cmd/helm-operator/main.go
bharathi-tenneti bcc99f2
Added changelog fragments, fixing sanity checks
bharathi-tenneti d5dc739
Remove checks for servicemonitor in test-e2e-helm.sh
bharathi-tenneti 8e9385c
Added changelog fragment
bharathi-tenneti e31105b
Removed clusterrolBinding for metric service, removed env section fro…
bharathi-tenneti 33c2b2f
reverted servcie creation
bharathi-tenneti f3d9da9
reverted clusterrolebinding metrics
bharathi-tenneti File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,11 @@ | ||
| # entries is a list of entries to include in | ||
| # release notes and/or the migration guide | ||
| entries: | ||
| - description: > | ||
| Remove legacy metrics generation code. | ||
| kind: "removal" | ||
| # Is this a breaking change? | ||
| breaking: true | ||
| migration: | ||
| header: Remove legacy metrics generation code from cmd/helm-operator/main.go, and test-e2e-helm.sh checks for servicemonitor. | ||
| body: TBD |
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -15,18 +15,13 @@ | |
| package main | ||
|
|
||
| import ( | ||
| "context" | ||
| "errors" | ||
| "fmt" | ||
| "os" | ||
| "runtime" | ||
| "strings" | ||
|
|
||
| "github.com/spf13/pflag" | ||
| v1 "k8s.io/api/core/v1" | ||
| metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
| "k8s.io/apimachinery/pkg/runtime/schema" | ||
| "k8s.io/apimachinery/pkg/util/intstr" | ||
| "k8s.io/client-go/rest" | ||
| "sigs.k8s.io/controller-runtime/pkg/cache" | ||
| crclient "sigs.k8s.io/controller-runtime/pkg/client" | ||
|
|
@@ -40,18 +35,11 @@ import ( | |
| "github.com/operator-framework/operator-sdk/pkg/helm/release" | ||
| "github.com/operator-framework/operator-sdk/pkg/helm/watches" | ||
| "github.com/operator-framework/operator-sdk/pkg/k8sutil" | ||
| kubemetrics "github.com/operator-framework/operator-sdk/pkg/kube-metrics" | ||
| "github.com/operator-framework/operator-sdk/pkg/log/zap" | ||
| "github.com/operator-framework/operator-sdk/pkg/metrics" | ||
| sdkVersion "github.com/operator-framework/operator-sdk/version" | ||
| ) | ||
|
|
||
| var ( | ||
| metricsHost = "0.0.0.0" | ||
| operatorMetricsPort int32 = 8686 | ||
|
|
||
| log = logf.Log.WithName("cmd") | ||
| ) | ||
| var log = logf.Log.WithName("cmd") | ||
|
|
||
| func printVersion() { | ||
| log.Info(fmt.Sprintf("Go Version: %s", runtime.Version())) | ||
|
|
@@ -135,7 +123,6 @@ func main() { | |
| log.Error(err, "Failed to create new manager factories.") | ||
| os.Exit(1) | ||
| } | ||
| var gvks []schema.GroupVersionKind | ||
| for _, w := range ws { | ||
| // Register the controller with the factory. | ||
| err := controller.Add(mgr, controller.WatchOptions{ | ||
|
|
@@ -151,76 +138,11 @@ func main() { | |
| log.Error(err, "Failed to add manager factory to controller.") | ||
| os.Exit(1) | ||
| } | ||
| gvks = append(gvks, w.GroupVersionKind) | ||
| } | ||
|
|
||
| addMetrics(context.TODO(), cfg, gvks) | ||
|
|
||
| // Start the Cmd | ||
| if err = mgr.Start(signals.SetupSignalHandler()); err != nil { | ||
| log.Error(err, "Manager exited non-zero.") | ||
| os.Exit(1) | ||
| } | ||
| } | ||
|
|
||
| // addMetrics will create the Services and Service Monitors to allow the operator export the metrics by using | ||
| // the Prometheus operator | ||
| func addMetrics(ctx context.Context, cfg *rest.Config, gvks []schema.GroupVersionKind) { | ||
| // Get the namespace the operator is currently deployed in. | ||
| operatorNs, err := k8sutil.GetOperatorNamespace() | ||
| if err != nil { | ||
| if errors.Is(err, k8sutil.ErrRunLocal) { | ||
| log.Info("Skipping CR metrics server creation; not running in a cluster.") | ||
| return | ||
| } | ||
| } | ||
|
|
||
| if err := serveCRMetrics(cfg, operatorNs, gvks); err != nil { | ||
| log.Info("Could not generate and serve custom resource metrics", "error", err.Error()) | ||
| } | ||
|
|
||
| // Add to the below struct any other metrics ports you want to expose. | ||
| servicePorts := []v1.ServicePort{ | ||
| {Port: operatorMetricsPort, Name: metrics.CRPortName, Protocol: v1.ProtocolTCP, | ||
| TargetPort: intstr.IntOrString{Type: intstr.Int, IntVal: operatorMetricsPort}}, | ||
| } | ||
|
|
||
| // Create Service object to expose the metrics port(s). | ||
| service, err := metrics.CreateMetricsService(ctx, cfg, servicePorts) | ||
| if err != nil { | ||
| log.Info("Could not create metrics Service", "error", err.Error()) | ||
| } | ||
|
|
||
| // CreateServiceMonitors will automatically create the prometheus-operator ServiceMonitor resources | ||
| // necessary to configure Prometheus to scrape metrics from this operator. | ||
| services := []*v1.Service{service} | ||
|
|
||
| // The ServiceMonitor is created in the same namespace where the operator is deployed | ||
| _, err = metrics.CreateServiceMonitors(cfg, operatorNs, services) | ||
| if err != nil { | ||
| log.Info("Could not create ServiceMonitor object", "error", err.Error()) | ||
| // If this operator is deployed to a cluster without the prometheus-operator running, it will return | ||
| // ErrServiceMonitorNotPresent, which can be used to safely skip ServiceMonitor creation. | ||
| if err == metrics.ErrServiceMonitorNotPresent { | ||
| log.Info("Install prometheus-operator in your cluster to create ServiceMonitor objects", "error", err.Error()) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // serveCRMetrics gets the Operator/CustomResource GVKs and generates metrics based on those types. | ||
| // It serves those metrics on "http://metricsHost:operatorMetricsPort". | ||
| func serveCRMetrics(cfg *rest.Config, operatorNs string, gvks []schema.GroupVersionKind) error { | ||
| // The metrics will be generated from the namespaces which are returned here. | ||
| // NOTE that passing nil or an empty list of namespaces in GenerateAndServeCRMetrics will result in an error. | ||
| ns, err := kubemetrics.GetNamespacesForMetrics(operatorNs) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| // Generate and serve custom resource specific metrics. | ||
| err = kubemetrics.GenerateAndServeCRMetrics(cfg, ns, gvks, metricsHost, operatorMetricsPort) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| return nil | ||
| } | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Need to also:
|
||
This file contains hidden or 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 hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is not missing remove from
cmd/ansible-operator/main.goas well?Also, should we not add the fragment here with TBD?
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
will be doing separate PR for Ansible, and regarding fragment I had a question if we are adding individual fragments for removal PRs. As in the meeting, some one was saying about doing one big Fragment with all PRs. I just need clarification on that.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can add many fragments in the same pr. IMO we can do all in the same PR as we are doing in the other cases to be easier to check that and we do not miss anything.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, we should add individual removal fragments. Those require migration sections, but we're doing this:
Then when we go to do the release, we'll run the generator and then rewrite the release notes, using what's there to remind us what to cover.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
okay, Ill follow above template, and add fragment for this PR.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
e.g https://github.com/operator-framework/operator-sdk/pull/3414/files