Skip to content

Commit

Permalink
add generator commit hash to node metadata (#53)
Browse files Browse the repository at this point in the history
  • Loading branch information
arvindbr8 committed Jan 18, 2024
1 parent 2270595 commit 596c3a3
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 7 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/testing.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@ jobs:
go-version: 1.21

- name: Run tests
run: go test -v ./...
run: go test -v ./... -buildvcs=true
23 changes: 23 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"os"
"path"
"regexp"
"runtime/debug"
"strconv"
"time"

Expand Down Expand Up @@ -163,6 +164,12 @@ func main() {
meshId = meshNamer.GenerateMeshId()
}

gitCommitHash, err := getGitCommitId()
if err != nil {
fmt.Fprintf(os.Stderr, "Error: unable to determine git commit ID: %s\n", err)
os.Exit(1)
}

input := configInput{
xdsServerUri: *xdsServerUri,
gcpProjectNumber: *gcpProjectNumber,
Expand All @@ -177,6 +184,7 @@ func main() {
includeDirectPathAuthority: *includeDirectPathAuthority,
ipv6Capable: isIPv6Capable(),
includeXDSTPNameInLDS: *includeXDSTPNameInLDS,
gitCommitHash: gitCommitHash,
}

if err := validate(input); err != nil {
Expand Down Expand Up @@ -230,6 +238,7 @@ type configInput struct {
includeDirectPathAuthority bool
ipv6Capable bool
includeXDSTPNameInLDS bool
gitCommitHash string
}

func validate(in configInput) error {
Expand Down Expand Up @@ -270,6 +279,7 @@ func generate(in configInput) ([]byte, error) {
},
Metadata: map[string]interface{}{
"INSTANCE_IP": in.ip,
"TRAFFICDIRECTOR_GRPC_BOOTSTRAP_GENERATOR_SHA": in.gitCommitHash,
},
},
}
Expand Down Expand Up @@ -333,6 +343,19 @@ func generate(in configInput) ([]byte, error) {
return json.MarshalIndent(c, "", " ")
}

func getGitCommitId() (string, error) {
info, ok := debug.ReadBuildInfo()
if !ok {
return "", fmt.Errorf("error calling debug.ReadBuildInfo")
}
for _, setting := range info.Settings {
if setting.Key == "vcs.revision" {
return setting.Value, nil
}
}
return "", fmt.Errorf("BuildInfo.Settings is missing vcs.revision")
}

func getHostIp() (string, error) {
hostname, err := os.Hostname()
if err != nil {
Expand Down
37 changes: 32 additions & 5 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,26 @@ import (
"net"
"net/http"
"net/http/httptest"
"regexp"
"strings"
"testing"

"github.com/google/go-cmp/cmp"
"github.com/google/uuid"
)

func TestGetGitCommitId(t *testing.T) {
commitId, err := getGitCommitId()
if err != nil {
t.Fatal(err)
}

re := regexp.MustCompile(`^[a-f0-9]{40}$`)
if !re.MatchString(commitId) {
t.Fatalf("getGitCommitId(): returned an invalid commit ID: %q. Want commit ID to be a valid SHA1 hash.", commitId)
}
}

func TestValidate(t *testing.T) {
tests := []struct {
desc string
Expand Down Expand Up @@ -99,6 +112,7 @@ func TestGenerate(t *testing.T) {
ip: "10.9.8.7",
zone: "uscentral-5",
metadataLabels: map[string]string{"k1": "v1", "k2": "v2"},
gitCommitHash: "7202b7c611ebd6d382b7b0240f50e9824200bffd",
},
wantOutput: `{
"xds_servers": [
Expand All @@ -119,6 +133,7 @@ func TestGenerate(t *testing.T) {
"cluster": "cluster",
"metadata": {
"INSTANCE_IP": "10.9.8.7",
"TRAFFICDIRECTOR_GRPC_BOOTSTRAP_GENERATOR_SHA": "7202b7c611ebd6d382b7b0240f50e9824200bffd",
"k1": "v1",
"k2": "v2"
},
Expand Down Expand Up @@ -149,6 +164,7 @@ func TestGenerate(t *testing.T) {
ip: "10.9.8.7",
zone: "uscentral-5",
secretsDir: "/secrets/dir/",
gitCommitHash: "7202b7c611ebd6d382b7b0240f50e9824200bffd",
},
wantOutput: `{
"xds_servers": [
Expand All @@ -168,7 +184,8 @@ func TestGenerate(t *testing.T) {
"id": "projects/123456789012345/networks/thedefault/nodes/52fdfc07-2182-454f-963f-5f0f9a621d72",
"cluster": "cluster",
"metadata": {
"INSTANCE_IP": "10.9.8.7"
"INSTANCE_IP": "10.9.8.7",
"TRAFFICDIRECTOR_GRPC_BOOTSTRAP_GENERATOR_SHA": "7202b7c611ebd6d382b7b0240f50e9824200bffd"
},
"locality": {
"zone": "uscentral-5"
Expand Down Expand Up @@ -204,6 +221,7 @@ func TestGenerate(t *testing.T) {
"INSTANCE-IP": "10.9.8.7",
"GCE-VM": "test-gce-vm",
},
gitCommitHash: "7202b7c611ebd6d382b7b0240f50e9824200bffd",
},
wantOutput: `{
"xds_servers": [
Expand All @@ -224,6 +242,7 @@ func TestGenerate(t *testing.T) {
"cluster": "cluster",
"metadata": {
"INSTANCE_IP": "10.9.8.7",
"TRAFFICDIRECTOR_GRPC_BOOTSTRAP_GENERATOR_SHA": "7202b7c611ebd6d382b7b0240f50e9824200bffd",
"TRAFFIC_DIRECTOR_CLIENT_ENVIRONMENT": {
"GCE-VM": "test-gce-vm",
"GCP-ZONE": "uscentral-5",
Expand Down Expand Up @@ -267,7 +286,8 @@ func TestGenerate(t *testing.T) {
"INSTANCE-IP": "10.9.8.7",
"GCE-VM": "test-gce-vm",
},
configMesh: "testmesh",
configMesh: "testmesh",
gitCommitHash: "7202b7c611ebd6d382b7b0240f50e9824200bffd",
},
wantOutput: `{
"xds_servers": [
Expand All @@ -288,6 +308,7 @@ func TestGenerate(t *testing.T) {
"cluster": "cluster",
"metadata": {
"INSTANCE_IP": "10.9.8.7",
"TRAFFICDIRECTOR_GRPC_BOOTSTRAP_GENERATOR_SHA": "7202b7c611ebd6d382b7b0240f50e9824200bffd",
"TRAFFIC_DIRECTOR_CLIENT_ENVIRONMENT": {
"GCE-VM": "test-gce-vm",
"GCP-ZONE": "uscentral-5",
Expand Down Expand Up @@ -324,6 +345,7 @@ func TestGenerate(t *testing.T) {
ip: "10.9.8.7",
zone: "uscentral-5",
ignoreResourceDeletion: true,
gitCommitHash: "7202b7c611ebd6d382b7b0240f50e9824200bffd",
},
wantOutput: `{
"xds_servers": [
Expand All @@ -344,7 +366,8 @@ func TestGenerate(t *testing.T) {
"id": "projects/123456789012345/networks/thedefault/nodes/52fdfc07-2182-454f-963f-5f0f9a621d72",
"cluster": "cluster",
"metadata": {
"INSTANCE_IP": "10.9.8.7"
"INSTANCE_IP": "10.9.8.7",
"TRAFFICDIRECTOR_GRPC_BOOTSTRAP_GENERATOR_SHA": "7202b7c611ebd6d382b7b0240f50e9824200bffd"
},
"locality": {
"zone": "uscentral-5"
Expand Down Expand Up @@ -374,6 +397,7 @@ func TestGenerate(t *testing.T) {
zone: "uscentral-5",
includeDirectPathAuthority: true,
ipv6Capable: true,
gitCommitHash: "7202b7c611ebd6d382b7b0240f50e9824200bffd",
},
wantOutput: `{
"xds_servers": [
Expand Down Expand Up @@ -413,7 +437,8 @@ func TestGenerate(t *testing.T) {
"cluster": "cluster",
"metadata": {
"INSTANCE_IP": "10.9.8.7",
"TRAFFICDIRECTOR_DIRECTPATH_C2P_IPV6_CAPABLE": true
"TRAFFICDIRECTOR_DIRECTPATH_C2P_IPV6_CAPABLE": true,
"TRAFFICDIRECTOR_GRPC_BOOTSTRAP_GENERATOR_SHA": "7202b7c611ebd6d382b7b0240f50e9824200bffd"
},
"locality": {
"zone": "uscentral-5"
Expand Down Expand Up @@ -444,6 +469,7 @@ func TestGenerate(t *testing.T) {
includeDirectPathAuthority: true,
ipv6Capable: true,
includeXDSTPNameInLDS: true,
gitCommitHash: "7202b7c611ebd6d382b7b0240f50e9824200bffd",
},
wantOutput: `{
"xds_servers": [
Expand Down Expand Up @@ -486,7 +512,8 @@ func TestGenerate(t *testing.T) {
"cluster": "cluster",
"metadata": {
"INSTANCE_IP": "10.9.8.7",
"TRAFFICDIRECTOR_DIRECTPATH_C2P_IPV6_CAPABLE": true
"TRAFFICDIRECTOR_DIRECTPATH_C2P_IPV6_CAPABLE": true,
"TRAFFICDIRECTOR_GRPC_BOOTSTRAP_GENERATOR_SHA": "7202b7c611ebd6d382b7b0240f50e9824200bffd"
},
"locality": {
"zone": "uscentral-5"
Expand Down
2 changes: 1 addition & 1 deletion tools/cloudbuild-artifacts.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ steps:
- name: golang:1.21
args: ['go', 'build', '.']
- name: golang:1.21
args: ['go', 'test', './...']
args: ['go', 'test', './...', "-buildvcs=true"]
- name: alpine
args: ['./tools/package.sh', '$COMMIT_SHA']
- name: gcr.io/cloud-builders/docker
Expand Down

0 comments on commit 596c3a3

Please sign in to comment.