2020-04-24 14:50:21 +00:00
|
|
|
package gce
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
2022-08-21 21:13:44 +00:00
|
|
|
"io"
|
2020-04-24 14:50:21 +00:00
|
|
|
"net/http"
|
|
|
|
"net/url"
|
2020-04-26 11:33:36 +00:00
|
|
|
"strings"
|
2020-04-24 14:50:21 +00:00
|
|
|
|
2020-04-27 08:44:28 +00:00
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/logger"
|
2020-04-29 14:27:08 +00:00
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/promscrape/discoveryutils"
|
2020-04-24 14:50:21 +00:00
|
|
|
"golang.org/x/oauth2/google"
|
|
|
|
)
|
|
|
|
|
|
|
|
type apiConfig struct {
|
|
|
|
client *http.Client
|
2020-04-26 11:33:36 +00:00
|
|
|
zones []string
|
2020-04-24 14:50:21 +00:00
|
|
|
project string
|
2020-04-26 11:33:36 +00:00
|
|
|
filter string
|
2020-04-24 14:50:21 +00:00
|
|
|
tagSeparator string
|
|
|
|
port int
|
|
|
|
}
|
|
|
|
|
2020-05-04 13:21:24 +00:00
|
|
|
var configMap = discoveryutils.NewConfigMap()
|
2020-04-24 14:50:21 +00:00
|
|
|
|
2020-05-04 13:21:24 +00:00
|
|
|
func getAPIConfig(sdc *SDConfig) (*apiConfig, error) {
|
|
|
|
v, err := configMap.Get(sdc, func() (interface{}, error) { return newAPIConfig(sdc) })
|
2020-04-24 14:50:21 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-05-04 13:21:24 +00:00
|
|
|
return v.(*apiConfig), nil
|
2020-04-24 14:50:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func newAPIConfig(sdc *SDConfig) (*apiConfig, error) {
|
|
|
|
ctx := context.Background()
|
|
|
|
client, err := google.DefaultClient(ctx, "https://www.googleapis.com/auth/compute.readonly")
|
|
|
|
if err != nil {
|
2020-06-30 19:58:18 +00:00
|
|
|
return nil, fmt.Errorf("cannot create oauth2 client for gce: %w", err)
|
2020-04-24 14:50:21 +00:00
|
|
|
}
|
2020-04-27 08:44:28 +00:00
|
|
|
project := sdc.Project
|
|
|
|
if len(project) == 0 {
|
|
|
|
proj, err := getCurrentProject()
|
2020-04-26 11:33:36 +00:00
|
|
|
if err != nil {
|
2020-06-30 19:58:18 +00:00
|
|
|
return nil, fmt.Errorf("cannot determine the current project; make sure `vmagent` runs inside GCE; error: %w", err)
|
2020-04-27 08:44:28 +00:00
|
|
|
}
|
|
|
|
project = proj
|
|
|
|
logger.Infof("autodetected the current GCE project: %q", project)
|
|
|
|
}
|
2022-05-06 21:02:54 +00:00
|
|
|
zones := sdc.Zone.Zones
|
2020-04-27 08:44:28 +00:00
|
|
|
if len(zones) == 0 {
|
|
|
|
// Autodetect the current zone.
|
|
|
|
zone, err := getCurrentZone()
|
|
|
|
if err != nil {
|
2020-06-30 19:58:18 +00:00
|
|
|
return nil, fmt.Errorf("cannot determine the current zone; make sure `vmagent` runs inside GCE; error: %w", err)
|
2020-04-27 08:44:28 +00:00
|
|
|
}
|
|
|
|
zones = append(zones, zone)
|
|
|
|
logger.Infof("autodetected the current GCE zone: %q", zone)
|
|
|
|
} else if len(zones) == 1 && zones[0] == "*" {
|
|
|
|
// Autodetect zones for project.
|
2022-11-21 20:32:03 +00:00
|
|
|
// Do not pass sdc.Filter when discovering zones, since GCE doesn't support it.
|
|
|
|
// See https://github.com/VictoriaMetrics/VictoriaMetrics/issues/3202
|
|
|
|
zs, err := getZonesForProject(client, project)
|
2020-04-27 08:44:28 +00:00
|
|
|
if err != nil {
|
2020-06-30 19:58:18 +00:00
|
|
|
return nil, fmt.Errorf("cannot obtain zones for project %q: %w", project, err)
|
2020-04-26 11:33:36 +00:00
|
|
|
}
|
|
|
|
zones = zs
|
2020-04-27 08:44:28 +00:00
|
|
|
logger.Infof("autodetected all the zones for the GCE project %q: %q", project, zones)
|
2020-04-24 14:50:21 +00:00
|
|
|
}
|
|
|
|
tagSeparator := ","
|
|
|
|
if sdc.TagSeparator != nil {
|
|
|
|
tagSeparator = *sdc.TagSeparator
|
|
|
|
}
|
|
|
|
port := 80
|
|
|
|
if sdc.Port != nil {
|
|
|
|
port = *sdc.Port
|
|
|
|
}
|
|
|
|
return &apiConfig{
|
|
|
|
client: client,
|
2020-04-26 11:33:36 +00:00
|
|
|
zones: zones,
|
2020-04-27 08:44:28 +00:00
|
|
|
project: project,
|
2020-04-26 11:33:36 +00:00
|
|
|
filter: sdc.Filter,
|
2020-04-24 14:50:21 +00:00
|
|
|
tagSeparator: tagSeparator,
|
|
|
|
port: port,
|
|
|
|
}, nil
|
|
|
|
}
|
2020-04-26 11:33:36 +00:00
|
|
|
|
|
|
|
func getAPIResponse(client *http.Client, apiURL, filter, pageToken string) ([]byte, error) {
|
2020-04-27 13:00:49 +00:00
|
|
|
apiURL = appendNonEmptyQueryArg(apiURL, "filter", filter)
|
|
|
|
apiURL = appendNonEmptyQueryArg(apiURL, "pageToken", pageToken)
|
2020-04-26 11:33:36 +00:00
|
|
|
resp, err := client.Get(apiURL)
|
|
|
|
if err != nil {
|
2020-06-30 19:58:18 +00:00
|
|
|
return nil, fmt.Errorf("cannot query %q: %w", apiURL, err)
|
2020-04-26 11:33:36 +00:00
|
|
|
}
|
2020-04-27 08:44:28 +00:00
|
|
|
return readResponseBody(resp, apiURL)
|
|
|
|
}
|
|
|
|
|
|
|
|
func readResponseBody(resp *http.Response, apiURL string) ([]byte, error) {
|
2022-08-21 21:13:44 +00:00
|
|
|
data, err := io.ReadAll(resp.Body)
|
2020-04-26 11:33:36 +00:00
|
|
|
_ = resp.Body.Close()
|
|
|
|
if err != nil {
|
2020-06-30 19:58:18 +00:00
|
|
|
return nil, fmt.Errorf("cannot read response from %q: %w", apiURL, err)
|
2020-04-26 11:33:36 +00:00
|
|
|
}
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
|
|
|
return nil, fmt.Errorf("unexpected status code for %q; got %d; want %d; response body: %q",
|
|
|
|
apiURL, resp.StatusCode, http.StatusOK, data)
|
|
|
|
}
|
|
|
|
return data, nil
|
|
|
|
}
|
|
|
|
|
2020-04-27 13:00:49 +00:00
|
|
|
func appendNonEmptyQueryArg(apiURL, argName, argValue string) string {
|
|
|
|
if len(argValue) == 0 {
|
2020-04-26 11:33:36 +00:00
|
|
|
return apiURL
|
|
|
|
}
|
|
|
|
prefix := "?"
|
|
|
|
if strings.Contains(apiURL, "?") {
|
|
|
|
prefix = "&"
|
|
|
|
}
|
2020-04-27 13:00:49 +00:00
|
|
|
return apiURL + fmt.Sprintf("%s%s=%s", prefix, url.QueryEscape(argName), url.QueryEscape(argValue))
|
2020-04-26 11:33:36 +00:00
|
|
|
}
|
2020-04-27 08:44:28 +00:00
|
|
|
|
|
|
|
func getCurrentZone() (string, error) {
|
|
|
|
// See https://cloud.google.com/compute/docs/storing-retrieving-metadata#default
|
|
|
|
data, err := getGCEMetadata("instance/zone")
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
parts := strings.Split(string(data), "/")
|
|
|
|
if len(parts) != 4 {
|
|
|
|
return "", fmt.Errorf("unexpected data returned from GCE; it must contain something like `projects/projectnum/zones/zone`; data: %q", data)
|
|
|
|
}
|
|
|
|
return parts[3], nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func getCurrentProject() (string, error) {
|
|
|
|
// See https://cloud.google.com/compute/docs/storing-retrieving-metadata#default
|
|
|
|
data, err := getGCEMetadata("project/project-id")
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return string(data), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func getGCEMetadata(path string) ([]byte, error) {
|
|
|
|
// See https://cloud.google.com/compute/docs/storing-retrieving-metadata#default
|
|
|
|
metadataURL := "http://metadata.google.internal/computeMetadata/v1/" + path
|
|
|
|
req, err := http.NewRequest("GET", metadataURL, nil)
|
|
|
|
if err != nil {
|
2020-06-30 19:58:18 +00:00
|
|
|
return nil, fmt.Errorf("cannot create http request for %q: %w", metadataURL, err)
|
2020-04-27 08:44:28 +00:00
|
|
|
}
|
|
|
|
req.Header.Set("Metadata-Flavor", "Google")
|
2020-04-29 14:27:08 +00:00
|
|
|
resp, err := discoveryutils.GetHTTPClient().Do(req)
|
2020-04-27 08:44:28 +00:00
|
|
|
if err != nil {
|
2020-06-30 19:58:18 +00:00
|
|
|
return nil, fmt.Errorf("cannot obtain response to %q: %w", metadataURL, err)
|
2020-04-27 08:44:28 +00:00
|
|
|
}
|
|
|
|
return readResponseBody(resp, metadataURL)
|
|
|
|
}
|