forked from GoogleCloudPlatform/k8s-stackdriver
/
monitored_resource_factory_config.go
54 lines (47 loc) · 1.48 KB
/
monitored_resource_factory_config.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package stackdriver
import (
"cloud.google.com/go/compute/metadata"
"fmt"
"github.com/golang/glog"
"strings"
)
type monitoredResourceFactoryConfig struct {
resourceModel resourceModelVersion
clusterName string
location string
projectID string
}
func newMonitoredResourceFactoryConfig(resourceModelVersion string) (*monitoredResourceFactoryConfig, error) {
clusterName, err := metadata.InstanceAttributeValue("cluster-name")
if err != nil {
glog.Warningf("'cluster-name' label is not specified on the VM, defaulting to the empty value")
clusterName = ""
}
clusterName = strings.TrimSpace(clusterName)
projectID, err := metadata.ProjectID()
if err != nil {
return nil, fmt.Errorf("failed to get project id: %v", err)
}
location, err := metadata.InstanceAttributeValue("cluster-location")
location = strings.TrimSpace(location)
if err != nil || location == "" {
glog.Warningf("Failed to retrieve cluster location, falling back to local zone: %s", err)
location, err = metadata.Zone()
if err != nil {
return nil, fmt.Errorf("error while getting cluster location: %v", err)
}
}
sdResourceModel := getResourceModelVersion(resourceModelVersion)
return &monitoredResourceFactoryConfig{
resourceModel: sdResourceModel,
clusterName: clusterName,
location: location,
projectID: projectID,
}, nil
}
func getResourceModelVersion(model string) resourceModelVersion {
if resourceModelVersion(model) == newTypes {
return newTypes
}
return oldTypes
}