/
run_auth_client.go
101 lines (80 loc) · 2.34 KB
/
run_auth_client.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package run_auth_client
import (
"context"
"fmt"
"sync"
"time"
run "cloud.google.com/go/run/apiv2"
"github.com/APITeamLimited/globe-test/lib"
"github.com/APITeamLimited/globe-test/orchestrator/libOrch"
dnscache "go.mercari.io/go-dnscache"
"go.uber.org/zap"
"google.golang.org/api/option"
)
type RunAuthClient struct {
serviceClient *run.ServicesClient
liveServices []libOrch.LiveService
liveServicesMutex sync.Mutex
ctx context.Context
serviceAccount []byte
serviceUrlOverride []string
loadZones []string
resolver *dnscache.Resolver
}
var _ = libOrch.RunAuthClient(&RunAuthClient{})
func CreateServicesClient(ctx context.Context, standalone bool, loadZones []string) *RunAuthClient {
var serviceClient *run.ServicesClient
// Convert hex to bytes
serviceAccount := lib.GetHexEnvVariable("SERVICE_ACCOUNT_KEY_HEX", "NONE")
if string(serviceAccount) != "NONE" {
loadedServiceClient, err := run.NewServicesClient(ctx, option.WithCredentialsJSON(serviceAccount))
if err != nil {
panic(err)
}
serviceClient = loadedServiceClient
} else if !standalone {
fmt.Println("Service account key not found, assuming using overrides")
serviceClient = nil
} else {
panic("Service account key not found")
}
logger, err := zap.NewProduction()
if err != nil {
panic(err)
}
resolver, err := dnscache.New(10*time.Second, time.Minute, logger)
if err != nil {
panic(err)
}
runAuthClient := &RunAuthClient{
serviceClient: serviceClient,
ctx: ctx,
liveServices: []libOrch.LiveService{},
liveServicesMutex: sync.Mutex{},
serviceAccount: serviceAccount,
serviceUrlOverride: getServiceUrlOverrides(),
loadZones: loadZones,
resolver: resolver,
}
runAuthClient.startAutoRefreshLiveServices()
return runAuthClient
}
func getServiceUrlOverrides() []string {
// Loop through counter SERVICE_URL_OVERRIDE_1, SERVICE_URL_OVERRIDE_2, etc
// If the env variable is not set, return nil
// If the env variable is set, return the value
values := []string{}
index := 0
for {
value := lib.GetEnvVariableRaw(fmt.Sprintf("SERVICE_URL_OVERRIDE_%d", index), "NONE_LEFT", true)
if value == "NONE_LEFT" {
if index == 0 {
values = append(values, "NO")
}
break
}
values = append(values, value)
index++
}
return values
}