-
Notifications
You must be signed in to change notification settings - Fork 805
feat(provider): add External Metrics provider #1863
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
Merged
Changes from all commits
Commits
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 |
|---|---|---|
|
|
@@ -1344,6 +1344,7 @@ spec: | |
| - prometheus | ||
| - influxdb | ||
| - datadog | ||
| - externalmetrics | ||
| - stackdriver | ||
| - cloudwatch | ||
| - newrelic | ||
|
|
||
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 |
|---|---|---|
|
|
@@ -1344,6 +1344,7 @@ spec: | |
| - prometheus | ||
| - influxdb | ||
| - datadog | ||
| - externalmetrics | ||
| - stackdriver | ||
| - cloudwatch | ||
| - newrelic | ||
|
|
||
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
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
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 |
|---|---|---|
|
|
@@ -1344,6 +1344,7 @@ spec: | |
| - prometheus | ||
| - influxdb | ||
| - datadog | ||
| - externalmetrics | ||
| - stackdriver | ||
| - cloudwatch | ||
| - newrelic | ||
|
|
||
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,156 @@ | ||
| /* | ||
| Copyright 2020 The Flux 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 providers | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "net/url" | ||
| "strings" | ||
| "time" | ||
|
|
||
| flaggerv1 "github.com/fluxcd/flagger/pkg/apis/flagger/v1beta1" | ||
| "k8s.io/apimachinery/pkg/labels" | ||
| "k8s.io/client-go/rest" | ||
| externalmetrics_client "k8s.io/metrics/pkg/client/external_metrics" | ||
| ) | ||
|
|
||
| // ExternalMetricsProvider fetches metrics from an ExternalMetricsProvider. | ||
| type ExternalMetricsProvider struct { | ||
| client externalmetrics_client.NamespacedMetricsGetter | ||
| } | ||
|
|
||
| // NewExternalMetricsProvider takes a provider spec, credentials, and a | ||
| // rest config, and returns a client ready to execute queries against the | ||
| // External Metrics API server. | ||
| func NewExternalMetricsProvider( | ||
| provider flaggerv1.MetricTemplateProvider, | ||
| credentials map[string][]byte, | ||
| config *rest.Config, | ||
| ) (*ExternalMetricsProvider, error) { | ||
| if config == nil { | ||
| return nil, fmt.Errorf( | ||
| "could not initialize ExternalMetricsProvider: rest config is nil", | ||
| ) | ||
| } | ||
|
|
||
| // clone to avoid mutating the shared config | ||
| restConfig := rest.CopyConfig(config) | ||
|
|
||
| // apply overrides from MetricTemplateProvider | ||
| if provider.Address != "" { | ||
| restConfig.Host = provider.Address | ||
| } | ||
| restConfig.TLSClientConfig.Insecure = provider.InsecureSkipVerify | ||
| if tokenBytes, ok := credentials["token"]; ok { | ||
| restConfig.BearerToken = string(tokenBytes) | ||
| } | ||
|
|
||
| restConfig.Timeout = 5 * time.Second | ||
|
|
||
| client, err := externalmetrics_client.NewForConfig(restConfig) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("error creating external metric client: %w", err) | ||
| } | ||
|
|
||
| return &ExternalMetricsProvider{ | ||
| client: client, | ||
| }, nil | ||
| } | ||
|
|
||
| // RunQuery retrieves the ExternalMetricValue from the External Metrics API | ||
| // at the ExternalMetricsProvider's address, using the provided query string, | ||
| // and returns the *first* result as a float64. | ||
| func (p *ExternalMetricsProvider) RunQuery(query string) (float64, error) { | ||
| namespace, metricName, selector, err := parseExternalMetricsQuery(query) | ||
| if err != nil { | ||
| return 0, fmt.Errorf("error parsing metric query: %w", err) | ||
| } | ||
|
|
||
| nm := p.client.NamespacedMetrics(namespace) | ||
| metricsList, err := nm.List(metricName, selector) | ||
| if err != nil { | ||
| return 0, fmt.Errorf("error querying external metrics API: %w", err) | ||
| } | ||
|
|
||
| if len(metricsList.Items) < 1 { | ||
| return 0, fmt.Errorf("no external metrics found: %w", ErrNoValuesFound) | ||
| } | ||
|
|
||
| vs := metricsList.Items[0].Value.AsApproximateFloat64() | ||
|
|
||
| return vs, nil | ||
| } | ||
|
|
||
| // IsOnline tests that the External Metrics API is reachable by looking for dummy metrics. | ||
| // If we don't get a network error, we assume the service is online. | ||
| func (p *ExternalMetricsProvider) IsOnline() (bool, error) { | ||
| nm := p.client.NamespacedMetrics("kube-system") | ||
| _, err := nm.List("dummy-metric", labels.Everything()) | ||
|
|
||
| if err != nil { | ||
| return false, fmt.Errorf("external metrics service unavailable: %w", err) | ||
| } | ||
| return true, nil | ||
| } | ||
|
|
||
| // parseExternalMetricsQuery parses a query string in the format: | ||
| // | ||
| // <namespace>/<metricName>?labelSelector=<urlencoded label selectors> | ||
| // | ||
| // where only the metricName is required. | ||
| // and returns the namespace, metricName, and labelSelector separately. | ||
| func parseExternalMetricsQuery(query string) (namespace string, metricName string, labelSelector labels.Selector, err error) { | ||
| // Adding a dummy protocol so we can leverage url.Parse for parsing the query string, easily extracting the path and query parameters. | ||
| u, err := url.Parse("dummy:///" + query) | ||
| if err != nil { | ||
| return "", "", labels.Everything(), fmt.Errorf("malformed query string, expected <namespace>/<metricName>?labelSelector=<urlencoded label selectors>, got %s", query) | ||
| } | ||
| path := strings.TrimPrefix(u.Path, "/") | ||
| parts := strings.Split(path, "/") | ||
| if len(parts) > 2 { | ||
| return "", "", labels.Everything(), fmt.Errorf("malformed query string, too many slashes, expected <namespace>/<metricName>?labelSelector=<urlencoded label selectors>, got %s", query) | ||
| } | ||
|
|
||
| namespace = "default" | ||
| switch len(parts) { | ||
| case 1: | ||
| // Format: "metric" | ||
| metricName = parts[0] | ||
| case 2: | ||
| // Format: "namespace/metric" or "/metric" | ||
| if parts[0] != "" { | ||
| namespace = parts[0] | ||
| } | ||
| metricName = parts[1] | ||
| } | ||
| if metricName == "" { | ||
| return "", "", labels.Everything(), fmt.Errorf("metric name cannot be empty") | ||
| } | ||
|
|
||
| qp := u.Query() | ||
| rawSelector := qp.Get("labelSelector") | ||
| if rawSelector == "" { | ||
| labelSelector = labels.Everything() | ||
| } else { | ||
| labelSelector, err = labels.Parse(rawSelector) | ||
| if err != nil { | ||
| return "", "", labels.Everything(), fmt.Errorf("error parsing label selector from string %s: %w", rawSelector, err) | ||
| } | ||
| } | ||
|
|
||
| return namespace, metricName, labelSelector, nil | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.