Skip to content

Commit 2819c3c

Browse files
author
Alok G Singh
committed
Introduce the build concept
A new key Builds at the repo and branch levels contains the builds for a repo. Upgrade tests use the std build artefacts.
1 parent 9cd8a9f commit 2819c3c

File tree

17 files changed

+583
-321
lines changed

17 files changed

+583
-321
lines changed

config/config.yaml

+232-66
Large diffs are not rendered by default.

go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module github.com/TykTechnologies/gromit
22

3-
go 1.22.0
3+
go 1.23
44

55
require (
66
github.com/Masterminds/sprig/v3 v3.3.0

policy/bundle_test.go

+1
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ func TestBundleRender(t *testing.T) {
5555
if err != nil {
5656
t.Fatalf("Error creating temp dir: %v", err)
5757
}
58+
t.Logf("templates rendered to: %s", tmpDir)
5859
defer os.RemoveAll(tmpDir)
5960

6061
err = rp.SetBranch("master")

policy/policy.go

+44-48
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
11
package policy
22

33
import (
4-
"bytes"
54
"fmt"
65
"os"
76
"path/filepath"
7+
"slices"
88
"time"
99

10+
"maps"
11+
12+
"dario.cat/mergo"
1013
"github.com/jinzhu/copier"
1114
"github.com/rs/zerolog/log"
1215
"github.com/spf13/viper"
13-
"golang.org/x/exp/maps"
1416
)
1517

1618
// repoConfig contains all the attributes of a repo. Each element here
@@ -20,15 +22,11 @@ import (
2022
// levels
2123
type repoConfig struct {
2224
Owner string
23-
Description string
24-
PCRepo string
25-
DHRepo string
26-
CSRepo string
27-
PackageName string
28-
Reviewers []string
2925
ExposePorts string
26+
PackageName string
3027
Binary string
3128
Buildenv string
29+
Builds buildMap
3230
BaseImage string
3331
DistrolessBaseImage string
3432
Cgo bool
@@ -42,6 +40,26 @@ type repoConfig struct {
4240
Repos map[string]repoConfig `copier:"-"`
4341
}
4442

43+
// build models the variations in build and their corresponding packages
44+
type build struct {
45+
Flags []string
46+
BuildPackageName string
47+
Description string
48+
ImageTitle string
49+
PCRepo string
50+
DHRepo string
51+
CSRepo string
52+
CIRepo string
53+
Env []string
54+
Archs []struct {
55+
Docker string
56+
Deb string
57+
Go string
58+
}
59+
}
60+
61+
type buildMap map[string]*build
62+
4563
// Policies models the config file structure. There are three levels
4664
// at which a particular value can be set: group-level, repo, branch.
4765
// The group level is applicable for all the repos in that group.
@@ -66,6 +84,7 @@ type branchVals struct {
6684
UpgradeFromVer string
6785
Tests []string
6886
Features []string
87+
Builds buildMap
6988
DeletedFiles []string
7089
}
7190

@@ -77,13 +96,10 @@ type branchVals struct {
7796
type RepoPolicy struct {
7897
Owner string
7998
Name string
80-
Description string
8199
Default string
82-
PCRepo string
83-
DHRepo string
84-
CSRepo string
85-
Binary string
86100
PackageName string
101+
Binary string
102+
Builds buildMap
87103
Reviewers []string
88104
ExposePorts string
89105
Cgo bool
@@ -93,9 +109,7 @@ type RepoPolicy struct {
93109
Branch string
94110
Branchvals branchVals
95111
Branches map[string]branchVals
96-
prBranch string
97112
Timestamp string
98-
Visibility string
99113
}
100114

101115
// PushOptions collects the input required to update templates for a
@@ -115,7 +129,6 @@ func (rp *RepoPolicy) SetTimestamp(ts time.Time) {
115129
ts = time.Now().UTC()
116130
}
117131
rp.Timestamp = ts.Format(time.UnixDate)
118-
119132
}
120133

121134
// GetTimeStamp returns the timestamp currently set for the given repopolicy.
@@ -126,7 +139,7 @@ func (rp *RepoPolicy) GetTimeStamp() (time.Time, error) {
126139
return ts, err
127140
}
128141

129-
// SetBranch sets the Branch and Branchvals properties so that templates can simply access them instead of looking them up in the Branches map
142+
// SetBranch sets the Branch and Branchvals properties so that templates can simply access them instead of looking them up in the Branches map. This must be called before calling Render()
130143
func (rp *RepoPolicy) SetBranch(branch string) error {
131144
bv, found := rp.Branches[branch]
132145
if !found {
@@ -140,7 +153,7 @@ func (rp *RepoPolicy) SetBranch(branch string) error {
140153

141154
// GetAllBranches returns all the branches that are managed for this repo
142155
func (rp *RepoPolicy) GetAllBranches() []string {
143-
return maps.Keys(rp.Branches)
156+
return slices.Sorted(maps.Keys(rp.Branches))
144157
}
145158

146159
// GetRepoPolicy will fetch the RepoPolicy for the supplied repo with
@@ -193,6 +206,9 @@ func (p *Policies) GetRepoPolicy(repo string) (RepoPolicy, error) {
193206
if err != nil {
194207
return rp, err
195208
}
209+
// builds are merged
210+
log.Debug().Msgf("Merging builds for %s/%s", rp.Name, b)
211+
rbv.Builds = mergeBuilds(r.Builds, bbv.Builds)
196212
// attributes that are unions
197213
rbv.Features = newSetFromSlices(group.Features, r.Features, bbv.Features).Members()
198214
rbv.DeletedFiles = newSetFromSlices(p.DeletedFiles, group.DeletedFiles, r.DeletedFiles, bbv.DeletedFiles).Members()
@@ -204,6 +220,16 @@ func (p *Policies) GetRepoPolicy(repo string) (RepoPolicy, error) {
204220
return rp, nil
205221
}
206222

223+
// mergeBuilds returns a merged build map from _r_epo and _b_ranch level
224+
func mergeBuilds(r, b buildMap) buildMap {
225+
merged := make(buildMap)
226+
maps.Copy(merged, r)
227+
if err := mergo.Merge(&merged, b, mergo.WithOverride, mergo.WithAppendSlice); err != nil {
228+
log.Fatal().Interface("dst", merged).Interface("src", b).Msgf("could not merge branch-level build definitions for: %v", err)
229+
}
230+
return merged
231+
}
232+
207233
// ProcessBranch will render the templates into a git worktree for the supplied branch, commit and push the changes upstream
208234
// The upstream branch name is the supplied branch name prefixed with releng/ and is returned
209235
func (rp *RepoPolicy) ProcessBranch(pushOpts *PushOptions) error {
@@ -270,36 +296,6 @@ func (rp *RepoPolicy) ProcessBranch(pushOpts *PushOptions) error {
270296
return nil
271297
}
272298

273-
// Stringer implementation for Policies
274-
func (p Policies) String() string {
275-
w := new(bytes.Buffer)
276-
for _, grp := range p.Groups {
277-
for repo, crPol := range grp.Repos {
278-
fmt.Fprintf(w, "%s: package %s, image %s", repo, crPol.PackageName, crPol.DHRepo)
279-
rp, err := p.GetRepoPolicy(repo)
280-
if err != nil {
281-
log.Fatal().Str("repo", repo).Err(err).Msg("failed to get policy, this should not happen")
282-
}
283-
fmt.Fprintf(w, " %s\n", rp)
284-
}
285-
}
286-
return w.String()
287-
}
288-
289-
// Stringer implementation for RepoPolicy
290-
func (rp RepoPolicy) String() string {
291-
w := new(bytes.Buffer)
292-
for b, bv := range rp.Branches {
293-
fmt.Fprintf(w, " %s: package %s, image %s, features %v", b, rp.PackageName, rp.DHRepo, bv.Features)
294-
if len(bv.Buildenv) > 0 {
295-
fmt.Fprintf(w, " built on %s", bv.Buildenv)
296-
} else {
297-
fmt.Fprintf(w, " not built")
298-
}
299-
}
300-
return w.String()
301-
}
302-
303299
// LoadRepoPolicies populates the supplied policies with the policy key from a the config file
304300
// This will panic if the type assertions fail
305301
func LoadRepoPolicies(policies *Policies) error {

policy/policy_test.go

+20-3
Original file line numberDiff line numberDiff line change
@@ -29,21 +29,38 @@ func TestPolicyConfig(t *testing.T) {
2929
assert.EqualValues(t, "right", repo0.Branchvals.Buildenv, "testing branch-level override for main")
3030
assert.ElementsMatch(t, []string{"a", "b", "c", "d"}, repo0.Branchvals.Features, "testing merging of branchvals")
3131
assert.EqualValues(t, "repo0.conf", repo0.Branchvals.ConfigFile, "testing repo-level inheritance of branchvals")
32-
assert.EqualValues(t, "Repo Zero", repo0.Description, "testing repo-level value")
3332

3433
err = repo0.SetBranch("dev")
3534
if err != nil {
3635
t.Fatalf("Could not set dev branch: %v", err)
3736
}
3837
assert.EqualValues(t, "stillright", repo0.Branchvals.Buildenv, "testing overrides for dev")
3938
assert.ElementsMatch(t, []string{"a", "b", "e", "f"}, repo0.Branchvals.Features, "testing merging")
40-
assert.EqualValues(t, "Repo Zero", repo0.Description, "testing inheritance")
4139

4240
repo1, err := pol.GetRepoPolicy("repo1")
4341
if err != nil {
4442
t.Fatalf("Could not get repo1: %v", err)
4543
}
46-
assert.EqualValues(t, "Repo One", repo1.Description, "testing second repo")
4744
r1b := repo1.GetAllBranches()
4845
assert.EqualValues(t, []string{"main"}, r1b, "testing branches")
46+
err = repo1.SetBranch("main")
47+
if err != nil {
48+
t.Fatalf("Could not set main branch: %v", err)
49+
}
50+
assert.EqualValues(t, []string{"flagstd1", "flagstd2"}, repo1.Branchvals.Builds["std"].Flags, "testing explicit merge")
51+
assert.EqualValues(t, "repo1-std2", repo1.Branchvals.Builds["std2"].BuildPackageName, "testing implicit merges at branch")
52+
assert.EqualValues(t, []string{"flag2"}, repo1.Branchvals.Builds["std2"].Flags, "testing implicit merge from repo")
53+
assert.EqualValues(t, build{Flags: []string{"flagstd1", "flagstd2"},
54+
BuildPackageName: "repo1-pkg",
55+
DHRepo: "repo1-doc-right",
56+
Archs: []struct {
57+
Docker string
58+
Deb string
59+
Go string
60+
}{
61+
{"doc1", "deb1", "go1"},
62+
{"doc2", "deb2", "go2"}},
63+
}, *repo1.Branchvals.Builds["std"], "testing full merge")
64+
assert.EqualValues(t, []string{"repo1-doc-right"}, repo1.GetImages("DHRepo"), "testing getImages()")
65+
assert.EqualValues(t, []string{"doc1", "doc2"}, repo1.GetDockerPlatforms(), "testing getDockerPlatforms()")
4966
}

policy/templates/distroless/ci/Dockerfile.distroless

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ ARG EDITION
66

77
ENV DEBIAN_FRONTEND=noninteractive
88

9-
COPY *${TARGETARCH}.deb /
10-
RUN rm -f /*fips*.deb && dpkg -i /{{ .PackageName }}${EDITION}_*${TARGETARCH}.deb && rm /*.deb
9+
COPY ${BUILD_PACKAGE_NAME}*${TARGETARCH}.deb /
10+
RUN dpkg -i /${BUILD_PACKAGE_NAME}_*${TARGETARCH}.deb && rm /*.deb
1111

1212
FROM gcr.io/distroless/{{ .Branchvals.DistrolessBaseImage }}
1313

policy/templates/el7-pgo-build/ci/goreleaser/goreleaser-el7.yml

+1-12
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,7 @@
44
# - arm64
55
# - amd64
66

7-
{{- if has "el7-pgo-build" .Branchvals.Features }}
8-
{{- template "cgo_builds" . }}
9-
{{- else }}
10-
{{- template "builds" . }}
11-
{{- end }}
12-
{{- template "nfpm" . }}
13-
14-
publishers:
15-
- name: {{ .PCRepo }}-unstable
16-
env:
17-
- PACKAGECLOUD_TOKEN={{`{{ .Env.PACKAGECLOUD_TOKEN }}`}}
18-
cmd: {{`packagecloud publish --debvers "{{ .Env.DEBVERS }}" --rpmvers "{{ .Env.RPMVERS }}"`}} tyk/{{ .PCRepo }}-unstable {{`{{ .ArtifactPath }}`}}
7+
{{- template "builds" . }}
198

209
# This disables archives
2110
archives:

0 commit comments

Comments
 (0)