forked from kubernetes/perf-tests
-
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.
Add GCP Compute API quotas usage measurement
- Loading branch information
Showing
23 changed files
with
267 additions
and
20 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
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,68 @@ | ||
/* | ||
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 prom | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"io/ioutil" | ||
"net/http" | ||
"net/url" | ||
"os" | ||
"time" | ||
|
||
"golang.org/x/oauth2/google" | ||
) | ||
|
||
// gcpManagedPrometheusClient talks to the Google Cloud Managed Service for Prometheus. | ||
// This only works if the cluster is hosted in GCP. | ||
// Details: https://cloud.google.com/stackdriver/docs/managed-prometheus. | ||
type gcpManagedPrometheusClient struct { | ||
client *http.Client | ||
uri string | ||
} | ||
|
||
func (mpc *gcpManagedPrometheusClient) Query(query string, queryTime time.Time) ([]byte, error) { | ||
params := url.Values{} | ||
params.Add("query", query) | ||
params.Add("time", queryTime.Format(time.RFC3339)) | ||
res, err := mpc.client.Get(mpc.uri + "?" + params.Encode()) | ||
if err != nil { | ||
return nil, err | ||
} | ||
defer res.Body.Close() | ||
resBody, err := ioutil.ReadAll(res.Body) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return resBody, nil | ||
} | ||
|
||
// NewGCPManagedPrometheusClient returns an HTTP client for talking to | ||
// the Google Cloud Managed Service for Prometheus. | ||
func NewGCPManagedPrometheusClient() (Client, error) { | ||
client, err := google.DefaultClient(context.TODO(), "https://www.googleapis.com/auth/monitoring.read") | ||
if err != nil { | ||
return nil, err | ||
} | ||
return &gcpManagedPrometheusClient{ | ||
client: client, | ||
uri: fmt.Sprintf("https://monitoring.googleapis.com/v1/projects/%s/location/global/prometheus/api/v1/query", os.Getenv("PROJECT")), | ||
}, nil | ||
} | ||
|
||
var _ Client = &gcpManagedPrometheusClient{} |
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,46 @@ | ||
/* | ||
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 prom | ||
|
||
import ( | ||
"context" | ||
"time" | ||
|
||
clientset "k8s.io/client-go/kubernetes" | ||
) | ||
|
||
// inClusterPrometheusClient talks to the Prometheus instance deployed in the test cluster. | ||
type inClusterPrometheusClient struct { | ||
client clientset.Interface | ||
} | ||
|
||
func (icpc *inClusterPrometheusClient) Query(query string, queryTime time.Time) ([]byte, error) { | ||
params := map[string]string{ | ||
"query": query, | ||
"time": queryTime.Format(time.RFC3339), | ||
} | ||
return icpc.client.CoreV1(). | ||
Services("monitoring"). | ||
ProxyGet("http", "prometheus-k8s", "9090", "api/v1/query", params). | ||
DoRaw(context.TODO()) | ||
} | ||
|
||
func NewInClusterPrometheusClient(c clientset.Interface) Client { | ||
return &inClusterPrometheusClient{client: c} | ||
} | ||
|
||
var _ Client = &inClusterPrometheusClient{} |
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,28 @@ | ||
/* | ||
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 prom | ||
|
||
import ( | ||
"time" | ||
) | ||
|
||
// Client provides interface for communicating with the Prometheus API. | ||
type Client interface { | ||
// Query sends a GET request to Prometheus with the "query" field | ||
// in the URL's query string set using the provided arguments. | ||
Query(query string, queryTime time.Time) ([]byte, error) | ||
} |
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
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
Oops, something went wrong.