Skip to content

Commit d972653

Browse files
Merge pull request #17 from gabriel-samfira/remove-dependency-on-go-github
Remove dependency on go-github
2 parents 002b44d + f9b1221 commit d972653

File tree

7 files changed

+115
-76
lines changed

7 files changed

+115
-76
lines changed

cloudconfig/util.go

+12-17
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,7 @@ import (
2121
"strings"
2222

2323
"github.com/cloudbase/garm-provider-common/defaults"
24-
commonParams "github.com/cloudbase/garm-provider-common/params"
25-
"github.com/google/go-github/v55/github"
24+
"github.com/cloudbase/garm-provider-common/params"
2625
"github.com/pkg/errors"
2726
)
2827

@@ -61,7 +60,7 @@ func sortMapKeys(m map[string][]byte) []string {
6160
}
6261

6362
// GetSpecs returns the cloud config specific extra specs from the bootstrap params.
64-
func GetSpecs(bootstrapParams commonParams.BootstrapInstance) (CloudConfigSpec, error) {
63+
func GetSpecs(bootstrapParams params.BootstrapInstance) (CloudConfigSpec, error) {
6564
var extraSpecs CloudConfigSpec
6665
if len(bootstrapParams.ExtraSpecs) == 0 {
6766
return extraSpecs, nil
@@ -85,28 +84,24 @@ func GetSpecs(bootstrapParams commonParams.BootstrapInstance) (CloudConfigSpec,
8584
// GetRunnerInstallScript returns the runner install script for the given bootstrap params.
8685
// This function will return either the default script for the given OS type or will use the supplied template
8786
// if one is provided.
88-
func GetRunnerInstallScript(bootstrapParams commonParams.BootstrapInstance, tools github.RunnerApplicationDownload, runnerName string) ([]byte, error) {
89-
if tools.Filename == nil {
87+
func GetRunnerInstallScript(bootstrapParams params.BootstrapInstance, tools params.RunnerApplicationDownload, runnerName string) ([]byte, error) {
88+
if tools.GetFilename() == "" {
9089
return nil, fmt.Errorf("missing tools filename")
9190
}
9291

93-
if tools.DownloadURL == nil {
92+
if tools.GetDownloadURL() == "" {
9493
return nil, fmt.Errorf("missing tools download URL")
9594
}
9695

97-
var tempToken string
98-
if tools.TempDownloadToken != nil {
99-
tempToken = *tools.TempDownloadToken
100-
}
101-
96+
tempToken := tools.GetTempDownloadToken()
10297
extraSpecs, err := GetSpecs(bootstrapParams)
10398
if err != nil {
10499
return nil, errors.Wrap(err, "getting specs")
105100
}
106101

107102
installRunnerParams := InstallRunnerParams{
108-
FileName: *tools.Filename,
109-
DownloadURL: *tools.DownloadURL,
103+
FileName: tools.GetFilename(),
104+
DownloadURL: tools.GetDownloadURL(),
110105
TempDownloadToken: tempToken,
111106
MetadataURL: bootstrapParams.MetadataURL,
112107
RunnerUsername: defaults.DefaultUser,
@@ -137,7 +132,7 @@ func GetRunnerInstallScript(bootstrapParams commonParams.BootstrapInstance, tool
137132
// GetCloudInitConfig returns the cloud-init specific userdata config. This config can be used on most clouds
138133
// for most Linux machines. The install runner script must be generated separately either by GetRunnerInstallScript()
139134
// or some other means.
140-
func GetCloudInitConfig(bootstrapParams commonParams.BootstrapInstance, installScript []byte) (string, error) {
135+
func GetCloudInitConfig(bootstrapParams params.BootstrapInstance, installScript []byte) (string, error) {
141136
extraSpecs, err := GetSpecs(bootstrapParams)
142137
if err != nil {
143138
return "", errors.Wrap(err, "getting specs")
@@ -188,21 +183,21 @@ func GetCloudInitConfig(bootstrapParams commonParams.BootstrapInstance, installS
188183
// Windows initialization scripts are run by creating a separate CustomScriptExtension resource for each individual script.
189184
// On other clouds it may be different. This function aims to be generic, which is why it only supports the PreInstallScripts
190185
// via cloud-init.
191-
func GetCloudConfig(bootstrapParams commonParams.BootstrapInstance, tools github.RunnerApplicationDownload, runnerName string) (string, error) {
186+
func GetCloudConfig(bootstrapParams params.BootstrapInstance, tools params.RunnerApplicationDownload, runnerName string) (string, error) {
192187
installScript, err := GetRunnerInstallScript(bootstrapParams, tools, runnerName)
193188
if err != nil {
194189
return "", errors.Wrap(err, "generating script")
195190
}
196191

197192
var asStr string
198193
switch bootstrapParams.OSType {
199-
case commonParams.Linux:
194+
case params.Linux:
200195
cloudCfg, err := GetCloudInitConfig(bootstrapParams, installScript)
201196
if err != nil {
202197
return "", errors.Wrap(err, "getting cloud init config")
203198
}
204199
return cloudCfg, nil
205-
case commonParams.Windows:
200+
case params.Windows:
206201
asStr = string(installScript)
207202
default:
208203
return "", fmt.Errorf("unknown os type: %s", bootstrapParams.OSType)

go.mod

-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ module github.com/cloudbase/garm-provider-common
33
go 1.20
44

55
require (
6-
github.com/google/go-github/v55 v55.0.1-0.20230921135834-aa3fcbe7aabc
76
github.com/google/uuid v1.3.0
87
github.com/gorilla/handlers v1.5.1
98
github.com/mattn/go-isatty v0.0.19
@@ -20,6 +19,5 @@ require (
2019
require (
2120
github.com/davecgh/go-spew v1.1.1 // indirect
2221
github.com/felixge/httpsnoop v1.0.1 // indirect
23-
github.com/google/go-querystring v1.1.0 // indirect
2422
github.com/pmezard/go-difflib v1.0.0 // indirect
2523
)

go.sum

-7
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,6 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c
22
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
33
github.com/felixge/httpsnoop v1.0.1 h1:lvB5Jl89CsZtGIWuTcDM1E/vkVs49/Ml7JJe07l8SPQ=
44
github.com/felixge/httpsnoop v1.0.1/go.mod h1:m8KPJKqk1gH5J9DgRY2ASl2lWCfGKXixSwevea8zH2U=
5-
github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
6-
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
7-
github.com/google/go-github/v55 v55.0.1-0.20230921135834-aa3fcbe7aabc h1:wZybOt4gfOPJmwpe3CZFJYoREaqgngGeo1Y29zZePhg=
8-
github.com/google/go-github/v55 v55.0.1-0.20230921135834-aa3fcbe7aabc/go.mod h1:dx9O5B1Z9+WYDRfSIkPdJ/jszShiNtl++jbgL/3OM2c=
9-
github.com/google/go-querystring v1.1.0 h1:AnCroh3fv4ZBgVIf1Iwtovgjaw/GiKJo8M8yD/fhyJ8=
10-
github.com/google/go-querystring v1.1.0/go.mod h1:Kcdr2DB4koayq7X8pmAG4sNG59So17icRSOU623lUBU=
115
github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
126
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
137
github.com/gorilla/handlers v1.5.1 h1:9lRY6j8DEeeBT10CvO9hGW0gmky0BprnvDI5vfhUHH4=
@@ -29,7 +23,6 @@ golang.org/x/crypto v0.12.0/go.mod h1:NF0Gs7EO5K4qLn+Ylc+fih8BSTeIjAP05siRnAh98y
2923
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
3024
golang.org/x/sys v0.11.0 h1:eG7RXZHdqOJ1i+0lgLgCpSXAp6M3LYlAo6osgSi0xOM=
3125
golang.org/x/sys v0.11.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
32-
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
3326
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
3427
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
3528
gopkg.in/natefinch/lumberjack.v2 v2.2.1 h1:bBRl1b0OH9s/DuPhuXpNl+VtCaJXFZ5/uEFST95x9zc=

params/github.go

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package params
2+
3+
// RunnerApplicationDownload represents a binary for the self-hosted runner application that can be downloaded.
4+
// This is copied from the go-github package. It does not make sense to create a dependency on go-github just
5+
// for this struct.
6+
type RunnerApplicationDownload struct {
7+
OS *string `json:"os,omitempty"`
8+
Architecture *string `json:"architecture,omitempty"`
9+
DownloadURL *string `json:"download_url,omitempty"`
10+
Filename *string `json:"filename,omitempty"`
11+
TempDownloadToken *string `json:"temp_download_token,omitempty"`
12+
SHA256Checksum *string `json:"sha256_checksum,omitempty"`
13+
}
14+
15+
// GetArchitecture returns the Architecture field if it's non-nil, zero value otherwise.
16+
func (r *RunnerApplicationDownload) GetArchitecture() string {
17+
if r == nil || r.Architecture == nil {
18+
return ""
19+
}
20+
return *r.Architecture
21+
}
22+
23+
// GetDownloadURL returns the DownloadURL field if it's non-nil, zero value otherwise.
24+
func (r *RunnerApplicationDownload) GetDownloadURL() string {
25+
if r == nil || r.DownloadURL == nil {
26+
return ""
27+
}
28+
return *r.DownloadURL
29+
}
30+
31+
// GetFilename returns the Filename field if it's non-nil, zero value otherwise.
32+
func (r *RunnerApplicationDownload) GetFilename() string {
33+
if r == nil || r.Filename == nil {
34+
return ""
35+
}
36+
return *r.Filename
37+
}
38+
39+
// GetOS returns the OS field if it's non-nil, zero value otherwise.
40+
func (r *RunnerApplicationDownload) GetOS() string {
41+
if r == nil || r.OS == nil {
42+
return ""
43+
}
44+
return *r.OS
45+
}
46+
47+
// GetSHA256Checksum returns the SHA256Checksum field if it's non-nil, zero value otherwise.
48+
func (r *RunnerApplicationDownload) GetSHA256Checksum() string {
49+
if r == nil || r.SHA256Checksum == nil {
50+
return ""
51+
}
52+
return *r.SHA256Checksum
53+
}
54+
55+
// GetTempDownloadToken returns the TempDownloadToken field if it's non-nil, zero value otherwise.
56+
func (r *RunnerApplicationDownload) GetTempDownloadToken() string {
57+
if r == nil || r.TempDownloadToken == nil {
58+
return ""
59+
}
60+
return *r.TempDownloadToken
61+
}

params/params.go

+2-4
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@ package params
1616

1717
import (
1818
"encoding/json"
19-
20-
"github.com/google/go-github/v55/github"
2119
)
2220

2321
type (
@@ -63,8 +61,8 @@ type UserDataOptions struct {
6361
}
6462

6563
type BootstrapInstance struct {
66-
Name string `json:"name"`
67-
Tools []*github.RunnerApplicationDownload `json:"tools"`
64+
Name string `json:"name"`
65+
Tools []RunnerApplicationDownload `json:"tools"`
6866
// RepoURL is the URL the github runner agent needs to configure itself.
6967
RepoURL string `json:"repo_url"`
7068
// CallbackUrl is the URL where the instance can send a post, signaling

util/util.go

+37-42
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,8 @@ import (
3232
"unicode/utf16"
3333

3434
runnerErrors "github.com/cloudbase/garm-provider-common/errors"
35+
"github.com/cloudbase/garm-provider-common/params"
3536

36-
commonParams "github.com/cloudbase/garm-provider-common/params"
37-
38-
"github.com/google/go-github/v55/github"
3937
"github.com/google/uuid"
4038
gorillaHandlers "github.com/gorilla/handlers"
4139
"github.com/pkg/errors"
@@ -50,24 +48,24 @@ const alphanumeric = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuv
5048
var rxEmail = regexp.MustCompile("^[a-zA-Z0-9.!#$%&'*+\\/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$")
5149

5250
var (
53-
OSToOSTypeMap map[string]commonParams.OSType = map[string]commonParams.OSType{
54-
"almalinux": commonParams.Linux,
55-
"alma": commonParams.Linux,
56-
"alpine": commonParams.Linux,
57-
"archlinux": commonParams.Linux,
58-
"arch": commonParams.Linux,
59-
"centos": commonParams.Linux,
60-
"ubuntu": commonParams.Linux,
61-
"rhel": commonParams.Linux,
62-
"suse": commonParams.Linux,
63-
"opensuse": commonParams.Linux,
64-
"fedora": commonParams.Linux,
65-
"debian": commonParams.Linux,
66-
"flatcar": commonParams.Linux,
67-
"gentoo": commonParams.Linux,
68-
"rockylinux": commonParams.Linux,
69-
"rocky": commonParams.Linux,
70-
"windows": commonParams.Windows,
51+
OSToOSTypeMap map[string]params.OSType = map[string]params.OSType{
52+
"almalinux": params.Linux,
53+
"alma": params.Linux,
54+
"alpine": params.Linux,
55+
"archlinux": params.Linux,
56+
"arch": params.Linux,
57+
"centos": params.Linux,
58+
"ubuntu": params.Linux,
59+
"rhel": params.Linux,
60+
"suse": params.Linux,
61+
"opensuse": params.Linux,
62+
"fedora": params.Linux,
63+
"debian": params.Linux,
64+
"flatcar": params.Linux,
65+
"gentoo": params.Linux,
66+
"rockylinux": params.Linux,
67+
"rocky": params.Linux,
68+
"windows": params.Windows,
7169
}
7270

7371
githubArchMapping map[string]string = map[string]string{
@@ -86,9 +84,9 @@ var (
8684
}
8785

8886
//
89-
githubOSTag = map[commonParams.OSType]string{
90-
commonParams.Linux: "Linux",
91-
commonParams.Windows: "Windows",
87+
githubOSTag = map[params.OSType]string{
88+
params.Linux: "Linux",
89+
params.Windows: "Windows",
9290
}
9391
)
9492

@@ -119,7 +117,7 @@ func ResolveToGithubOSType(osType string) (string, error) {
119117
// ResolveToGithubTag returns the default OS tag that self hosted runners automatically
120118
// (and forcefully) adds to every runner that gets deployed. We need to keep track of those
121119
// tags internally as well.
122-
func ResolveToGithubTag(os commonParams.OSType) (string, error) {
120+
func ResolveToGithubTag(os params.OSType) (string, error) {
123121
ghOS, ok := githubOSTag[os]
124122
if !ok {
125123
return "", runnerErrors.NewNotFoundError("os %s is unknown", os)
@@ -178,37 +176,34 @@ func ConvertFileToBase64(file string) (string, error) {
178176
return base64.StdEncoding.EncodeToString(bytes), nil
179177
}
180178

181-
func OSToOSType(os string) (commonParams.OSType, error) {
179+
func OSToOSType(os string) (params.OSType, error) {
182180
osType, ok := OSToOSTypeMap[strings.ToLower(os)]
183181
if !ok {
184-
return commonParams.Unknown, fmt.Errorf("no OS to OS type mapping for %s", os)
182+
return params.Unknown, fmt.Errorf("no OS to OS type mapping for %s", os)
185183
}
186184
return osType, nil
187185
}
188186

189-
func GetTools(osType commonParams.OSType, osArch commonParams.OSArch, tools []*github.RunnerApplicationDownload) (github.RunnerApplicationDownload, error) {
187+
func GetTools(osType params.OSType, osArch params.OSArch, tools []params.RunnerApplicationDownload) (params.RunnerApplicationDownload, error) {
190188
// Validate image OS. Linux only for now.
191189
switch osType {
192-
case commonParams.Linux:
193-
case commonParams.Windows:
190+
case params.Linux:
191+
case params.Windows:
194192
default:
195-
return github.RunnerApplicationDownload{}, fmt.Errorf("unsupported OS type: %s", osType)
193+
return params.RunnerApplicationDownload{}, fmt.Errorf("unsupported OS type: %s", osType)
196194
}
197195

198196
switch osArch {
199-
case commonParams.Amd64:
200-
case commonParams.Arm:
201-
case commonParams.Arm64:
197+
case params.Amd64:
198+
case params.Arm:
199+
case params.Arm64:
202200
default:
203-
return github.RunnerApplicationDownload{}, fmt.Errorf("unsupported OS arch: %s", osArch)
201+
return params.RunnerApplicationDownload{}, fmt.Errorf("unsupported OS arch: %s", osArch)
204202
}
205203

206204
// Find tools for OS/Arch.
207205
for _, tool := range tools {
208-
if tool == nil {
209-
continue
210-
}
211-
if tool.OS == nil || tool.Architecture == nil {
206+
if tool.GetOS() == "" || tool.GetArchitecture() == "" {
212207
continue
213208
}
214209

@@ -221,11 +216,11 @@ func GetTools(osType commonParams.OSType, osArch commonParams.OSArch, tools []*g
221216
if err != nil {
222217
continue
223218
}
224-
if *tool.Architecture == ghArch && *tool.OS == ghOS {
225-
return *tool, nil
219+
if tool.GetArchitecture() == ghArch && tool.GetOS() == ghOS {
220+
return tool, nil
226221
}
227222
}
228-
return github.RunnerApplicationDownload{}, fmt.Errorf("failed to find tools for OS %s and arch %s", osType, osArch)
223+
return params.RunnerApplicationDownload{}, fmt.Errorf("failed to find tools for OS %s and arch %s", osType, osArch)
229224
}
230225

231226
// GetRandomString returns a secure random string

util/util_test.go

+3-4
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ import (
2525

2626
runnerErrors "github.com/cloudbase/garm-provider-common/errors"
2727
"github.com/cloudbase/garm-provider-common/params"
28-
"github.com/google/go-github/v55/github"
2928
"github.com/stretchr/testify/require"
3029
lumberjack "gopkg.in/natefinch/lumberjack.v2"
3130
)
@@ -211,10 +210,10 @@ func TestGetTools(t *testing.T) {
211210
t.Fatalf("failed to resolve to github os type: %s", err)
212211
}
213212

214-
tools := []*github.RunnerApplicationDownload{
213+
tools := []params.RunnerApplicationDownload{
215214
{
216-
OS: github.String(ghOS),
217-
Architecture: github.String(ghArch),
215+
OS: &ghOS,
216+
Architecture: &ghArch,
218217
},
219218
}
220219

0 commit comments

Comments
 (0)