constructMetadataFromEnv creates local labels and labels to send to the peer using metadata exchange based off resource detection and environment variables. Returns local labels, and base 64 encoded protobuf.Struct containing metadata exchange labels.
(ctx context.Context)
| 195 | // Returns local labels, and base 64 encoded protobuf.Struct containing metadata |
| 196 | // exchange labels. |
| 197 | func constructMetadataFromEnv(ctx context.Context) (map[string]string, string) { |
| 198 | set := getAttrSetFromResourceDetector(ctx) |
| 199 | |
| 200 | labels := make(map[string]string) |
| 201 | labels["type"] = getFromResource("cloud.platform", set) |
| 202 | labels["canonical_service"] = getEnv("CSM_CANONICAL_SERVICE_NAME") |
| 203 | |
| 204 | // If type is not GCE or GKE only metadata exchange labels are "type" and |
| 205 | // "canonical_service". |
| 206 | cloudPlatformVal := labels["type"] |
| 207 | if cloudPlatformVal != "gcp_kubernetes_engine" && cloudPlatformVal != "gcp_compute_engine" { |
| 208 | return initializeLocalAndMetadataLabels(labels) |
| 209 | } |
| 210 | |
| 211 | // GCE and GKE labels: |
| 212 | labels["workload_name"] = getEnv("CSM_WORKLOAD_NAME") |
| 213 | |
| 214 | locationVal := "unknown" |
| 215 | if resourceVal, ok := set.Value("cloud.availability_zone"); ok && resourceVal.Type() == attribute.STRING { |
| 216 | locationVal = resourceVal.AsString() |
| 217 | } else if resourceVal, ok = set.Value("cloud.region"); ok && resourceVal.Type() == attribute.STRING { |
| 218 | locationVal = resourceVal.AsString() |
| 219 | } |
| 220 | labels["location"] = locationVal |
| 221 | |
| 222 | labels["project_id"] = getFromResource("cloud.account.id", set) |
| 223 | if cloudPlatformVal == "gcp_compute_engine" { |
| 224 | return initializeLocalAndMetadataLabels(labels) |
| 225 | } |
| 226 | |
| 227 | // GKE specific labels: |
| 228 | labels["namespace_name"] = getFromResource("k8s.namespace.name", set) |
| 229 | labels["cluster_name"] = getFromResource("k8s.cluster.name", set) |
| 230 | return initializeLocalAndMetadataLabels(labels) |
| 231 | } |
| 232 | |
| 233 | // initializeLocalAndMetadataLabels csm local labels for a CSM Plugin Option to |
| 234 | // record. It also builds out a base 64 encoded protobuf.Struct containing the |