Skip to content

Commit fd8a834

Browse files
Vishal TakAxel von Bertoldi
andcommitted
Merge branch 'avonbertoldi/39143/pulp-supported-distros' into 'main'
Implement mage pulp:supportedOSVersions target See merge request https://gitlab.com/gitlab-org/gitlab-runner/-/merge_requests/6024 Merged-by: Vishal Tak <[email protected]> Approved-by: Vishal Tak <[email protected]> Reviewed-by: Vishal Tak <[email protected]> Reviewed-by: GitLab Duo <[email protected]> Co-authored-by: Axel von Bertoldi <[email protected]>
2 parents 590c4b5 + ac4cf21 commit fd8a834

File tree

4 files changed

+173
-7
lines changed

4 files changed

+173
-7
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ require (
9292
google.golang.org/grpc v1.75.1
9393
google.golang.org/protobuf v1.36.9
9494
gopkg.in/yaml.v2 v2.4.0
95+
gopkg.in/yaml.v3 v3.0.1
9596
k8s.io/api v0.32.10
9697
k8s.io/apimachinery v0.32.10
9798
k8s.io/client-go v0.32.10
@@ -272,7 +273,6 @@ require (
272273
gopkg.in/inf.v0 v0.9.1 // indirect
273274
gopkg.in/ini.v1 v1.67.0 // indirect
274275
gopkg.in/warnings.v0 v0.1.2 // indirect
275-
gopkg.in/yaml.v3 v3.0.1 // indirect
276276
k8s.io/klog/v2 v2.130.1 // indirect
277277
k8s.io/kube-openapi v0.0.0-20241105132330-32ad38e42d3f // indirect
278278
k8s.io/utils v0.0.0-20241104100929-3ea5e8cea738 // indirect

magefiles/packagecloud/releases.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -77,25 +77,25 @@ var (
7777
"debian": "bullseye",
7878
"ubuntu": "xenial",
7979
"raspbian": "bullseye",
80-
"linuxmint": "ulyana",
81-
"fedora": "40",
82-
"ol": "6",
80+
"linuxmint": "vanessa",
81+
"fedora": "42",
82+
"ol": "7",
8383
"el": "7",
8484
"amazon": "2",
8585
"sles": "12.5",
86-
"opensuse": "15.0",
86+
"opensuse": "15.6",
8787
}
8888

8989
skipReleases = map[string][]string{
9090
"debian": {},
91-
"ubuntu": {"hirsute", "groovy", "eoan", "disco", "cosmic", "artful", "zesty", "yakkety", "impish", "kinetic", "lunar", "mantic"},
91+
"ubuntu": {"hirsute", "groovy", "eoan", "disco", "cosmic", "artful", "zesty", "yakkety", "impish", "kinetic", "lunar", "mantic", "oracular"},
9292
"raspbian": {},
9393
"linuxmint": {},
9494
"fedora": {},
9595
"ol": {},
9696
"el": {},
9797
"amazon": {},
98-
"sles": {"15.0", "15.1"},
98+
"sles": {"15.0", "15.1", "15.2", "15.3"},
9999
"opensuse": {},
100100
}
101101
)

magefiles/pulp.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
//go:build mage
2+
3+
package main
4+
5+
import (
6+
"fmt"
7+
"strings"
8+
9+
"github.com/magefile/mage/mg"
10+
"gitlab.com/gitlab-org/gitlab-runner/magefiles/pulp"
11+
)
12+
13+
type Pulp mg.Namespace
14+
15+
// SupportedOSVersions prints the list of OS/versions for which runner packages will be released (for the given package type and release branch)
16+
func (p Pulp) SupportedOSVersions(dist, branch string) error {
17+
os, err := pulp.Releases(dist, branch)
18+
if err != nil {
19+
return err
20+
}
21+
fmt.Println(strings.Join(os, "\n"))
22+
return nil
23+
}

magefiles/pulp/releases.go

Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
package pulp
2+
3+
import (
4+
"errors"
5+
"fmt"
6+
"net/http"
7+
"os"
8+
"slices"
9+
"strings"
10+
"time"
11+
12+
"github.com/samber/lo"
13+
"gopkg.in/yaml.v3"
14+
)
15+
16+
const (
17+
pulpReposURL = "https://gitlab.com/api/v4/projects/75880111/repository/files/repos%2Frunner.yaml/raw"
18+
)
19+
20+
type (
21+
pulpRepository struct {
22+
Path string `yaml:"path"`
23+
}
24+
25+
pulpRepositories struct {
26+
Deb []pulpRepository `yaml:"deb"`
27+
Rpm []pulpRepository `yaml:"rpm"`
28+
}
29+
30+
pulpRelease struct {
31+
Repositories pulpRepositories `yaml:"repositories"`
32+
}
33+
34+
pulpRepos struct {
35+
Stable pulpRelease `yaml:"gitlab-runner"`
36+
Unstable pulpRelease `yaml:"unstable"`
37+
}
38+
39+
pulpConfig struct {
40+
Runner pulpRepos `yaml:"runner"`
41+
}
42+
)
43+
44+
var (
45+
dists = []string{"rpm", "deb"}
46+
branches = []string{"stable", "unstable"}
47+
)
48+
49+
func Releases(dist, branch string) ([]string, error) {
50+
if !slices.Contains(dists, dist) {
51+
return nil, fmt.Errorf("unsupported package type %q", dist)
52+
}
53+
54+
if !slices.Contains(branches, branch) {
55+
return nil, fmt.Errorf("unsupported branch %q", branch)
56+
}
57+
58+
tokenType, tokenValue, err := getToken()
59+
if err != nil {
60+
return nil, err
61+
}
62+
63+
config, err := getPulpRunnerConfig(tokenType, tokenValue)
64+
if err != nil {
65+
return nil, err
66+
}
67+
68+
return releasesForDistBranch(dist, branch, config), nil
69+
}
70+
71+
func firstEnv(envs ...string) (string, string, bool) {
72+
for _, env := range envs {
73+
if val, ok := os.LookupEnv(env); ok {
74+
return env, val, true
75+
}
76+
}
77+
return "", "", false
78+
}
79+
80+
func getToken() (string, string, error) {
81+
tokenType, tokenValue, ok := firstEnv("CI_JOB_TOKEN", "PRIVATE_TOKEN")
82+
if !ok {
83+
return "", "", errors.New("required 'CI_JOB_TOKEN' or 'PRIVATE_TOKEN' variable missing")
84+
}
85+
86+
if tokenValue == "" {
87+
return "", "", fmt.Errorf("%s cannot be empty", tokenType)
88+
}
89+
90+
tokenType = strings.ReplaceAll(tokenType, "_", "-")
91+
92+
return tokenType, tokenValue, nil
93+
}
94+
95+
func releasesForDistBranch(dist, branch string, config *pulpConfig) []string {
96+
var release pulpRelease
97+
switch branch {
98+
case "stable":
99+
release = config.Runner.Stable
100+
case "unstable":
101+
release = config.Runner.Unstable
102+
}
103+
104+
var repos []pulpRepository
105+
switch dist {
106+
case "deb":
107+
repos = release.Repositories.Deb
108+
case "rpm":
109+
repos = release.Repositories.Rpm
110+
}
111+
112+
return lo.Map(repos, func(repo pulpRepository, _ int) string {
113+
return repo.Path
114+
})
115+
}
116+
117+
// The full Pulp runner repo config file can be enjoyed at
118+
// https://gitlab.com/gitlab-org/build/pulp-repository-automation/-/blob/main/repos/runner.yaml?ref_type=heads
119+
func getPulpRunnerConfig(tokenType, tokenValue string) (*pulpConfig, error) {
120+
req, err := http.NewRequest("GET", pulpReposURL, nil)
121+
if err != nil {
122+
return nil, fmt.Errorf("failed to create request for url %q: %w", pulpReposURL, err)
123+
}
124+
req.Header.Add(tokenType, tokenValue)
125+
126+
client := http.Client{Timeout: 20 * time.Second}
127+
resp, err := client.Do(req)
128+
if err != nil {
129+
return nil, fmt.Errorf("failed to get url %q: %w", pulpReposURL, err)
130+
}
131+
defer resp.Body.Close()
132+
133+
if resp.StatusCode != http.StatusOK {
134+
return nil, fmt.Errorf("got unexpected response status code: %s", resp.Status)
135+
}
136+
137+
result := pulpConfig{}
138+
if err := yaml.NewDecoder(resp.Body).Decode(&result); err != nil {
139+
return nil, fmt.Errorf("failed to decode response body: %w", err)
140+
}
141+
142+
return &result, nil
143+
}

0 commit comments

Comments
 (0)