Skip to content

Commit f2ff3cb

Browse files
Merge pull request #5057 from danilo-gemoli/fix/ci-operator-config-mirror/no-tests-imgs-config
ci-operator-config-mirror: Do not mirror configs without tests and images
2 parents 6280ffc + 25ca8f1 commit f2ff3cb

2 files changed

Lines changed: 166 additions & 74 deletions

File tree

cmd/ci-operator-config-mirror/main.go

Lines changed: 81 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,81 @@ func main() {
107107
}()
108108

109109
configsByRepo := make(configsByRepo)
110-
callback := func(rbc *api.ReleaseBuildConfiguration, repoInfo *config.Info) error {
110+
callback := ciOperatorConfigsCallback(o, configsByRepo)
111+
112+
if err := o.OperateOnCIOperatorConfigDir(o.ConfigDir, callback); err != nil {
113+
logrus.WithError(err).Fatal("error while operating in the ci-operator configuration files")
114+
}
115+
116+
dest := filepath.Join(o.ConfigDir, o.toOrg)
117+
logger := logrus.WithField("destination", dest)
118+
if o.clean {
119+
logger.Info("Cleaning destination's sub directories")
120+
if err := configsByRepo.cleanDestinationSubdirs(dest); err != nil {
121+
logger.WithError(err).Fatal("couldn't cleanup destination's subdirectories")
122+
}
123+
}
124+
125+
logger.Info("Generating configurations...")
126+
if err := configsByRepo.generateConfigs(o.ConfigDir); err != nil {
127+
logger.WithError(err).Fatal("couldn't generate configurations")
128+
}
129+
}
130+
131+
func privateReleaseTagConfiguration(tagSpecification *api.ReleaseTagConfiguration) {
132+
if tagSpecification.Namespace == ocpNamespace {
133+
tagSpecification.Name = fmt.Sprintf("%s-priv", tagSpecification.Name)
134+
tagSpecification.Namespace = privatePromotionNamespace
135+
}
136+
}
137+
138+
func privateIntegrationRelease(release *api.Integration) {
139+
if release.Namespace == ocpNamespace {
140+
release.Name = fmt.Sprintf("%s-priv", release.Name)
141+
release.Namespace = privatePromotionNamespace
142+
}
143+
}
144+
145+
func privateBuildRoot(buildRoot *api.BuildRootImageConfiguration) {
146+
if buildRoot.ImageStreamTagReference.Namespace == ocpNamespace {
147+
buildRoot.ImageStreamTagReference.Name = fmt.Sprintf("%s-priv", buildRoot.ImageStreamTagReference.Name)
148+
buildRoot.ImageStreamTagReference.Namespace = privatePromotionNamespace
149+
}
150+
}
151+
152+
func privateBaseImages(baseImages map[string]api.ImageStreamTagReference) {
153+
for name, reference := range baseImages {
154+
if reference.Namespace == ocpNamespace && api.IsValidOCPVersion(reference.Name) {
155+
reference.Name = fmt.Sprintf("%s-priv", reference.Name)
156+
reference.Namespace = privatePromotionNamespace
157+
baseImages[name] = reference
158+
}
159+
}
160+
}
161+
162+
func privatePromotionConfiguration(promotion *api.PromotionConfiguration) {
163+
for i := range promotion.Targets {
164+
if promotion.Targets[i].Namespace == ocpNamespace {
165+
if promotion.Targets[i].Name != "" {
166+
promotion.Targets[i].Name = fmt.Sprintf("%s-priv", promotion.Targets[i].Name)
167+
} else { // promotion.Targets[i].Tag must be set
168+
promotion.Targets[i].Tag = fmt.Sprintf("%s-priv", promotion.Targets[i].Tag)
169+
}
170+
promotion.Targets[i].TagByCommit = false // Never use tag_by_commit for mirrored repos
171+
promotion.Targets[i].Namespace = privatePromotionNamespace
172+
} else {
173+
// Disable this target or it will conflict with its non `-priv` counterpart
174+
promotion.Targets[i].Disabled = true
175+
}
176+
}
177+
}
178+
179+
func strP(str string) *string {
180+
return &str
181+
}
182+
183+
func ciOperatorConfigsCallback(o options, configsByRepo configsByRepo) func(rbc *api.ReleaseBuildConfiguration, repoInfo *config.Info) error {
184+
return func(rbc *api.ReleaseBuildConfiguration, repoInfo *config.Info) error {
111185
logger := logrus.WithFields(logrus.Fields{"org": repoInfo.Org, "repo": repoInfo.Repo, "branch": repoInfo.Branch})
112186

113187
if repoInfo.Org == o.toOrg {
@@ -186,81 +260,14 @@ func main() {
186260

187261
repoInfo.Org = o.toOrg
188262
rbc.Metadata.Org = o.toOrg
189-
configsByRepo[repoInfo.Repo] = append(configsByRepo[repoInfo.Repo], config.DataWithInfo{
190-
Configuration: *rbc,
191-
Info: *repoInfo,
192-
})
193263

194-
return nil
195-
}
196-
197-
if err := o.OperateOnCIOperatorConfigDir(o.ConfigDir, callback); err != nil {
198-
logrus.WithError(err).Fatal("error while operating in the ci-operator configuration files")
199-
}
200-
201-
dest := filepath.Join(o.ConfigDir, o.toOrg)
202-
logger := logrus.WithField("destination", dest)
203-
if o.clean {
204-
logger.Info("Cleaning destination's sub directories")
205-
if err := configsByRepo.cleanDestinationSubdirs(dest); err != nil {
206-
logger.WithError(err).Fatal("couldn't cleanup destination's subdirectories")
264+
if len(rbc.Tests) > 0 || len(rbc.Images) > 0 {
265+
configsByRepo[repoInfo.Repo] = append(configsByRepo[repoInfo.Repo], config.DataWithInfo{
266+
Configuration: *rbc,
267+
Info: *repoInfo,
268+
})
207269
}
208-
}
209-
210-
logger.Info("Generating configurations...")
211-
if err := configsByRepo.generateConfigs(o.ConfigDir); err != nil {
212-
logger.WithError(err).Fatal("couldn't generate configurations")
213-
}
214-
}
215-
216-
func privateReleaseTagConfiguration(tagSpecification *api.ReleaseTagConfiguration) {
217-
if tagSpecification.Namespace == ocpNamespace {
218-
tagSpecification.Name = fmt.Sprintf("%s-priv", tagSpecification.Name)
219-
tagSpecification.Namespace = privatePromotionNamespace
220-
}
221-
}
222270

223-
func privateIntegrationRelease(release *api.Integration) {
224-
if release.Namespace == ocpNamespace {
225-
release.Name = fmt.Sprintf("%s-priv", release.Name)
226-
release.Namespace = privatePromotionNamespace
227-
}
228-
}
229-
230-
func privateBuildRoot(buildRoot *api.BuildRootImageConfiguration) {
231-
if buildRoot.ImageStreamTagReference.Namespace == ocpNamespace {
232-
buildRoot.ImageStreamTagReference.Name = fmt.Sprintf("%s-priv", buildRoot.ImageStreamTagReference.Name)
233-
buildRoot.ImageStreamTagReference.Namespace = privatePromotionNamespace
234-
}
235-
}
236-
237-
func privateBaseImages(baseImages map[string]api.ImageStreamTagReference) {
238-
for name, reference := range baseImages {
239-
if reference.Namespace == ocpNamespace && api.IsValidOCPVersion(reference.Name) {
240-
reference.Name = fmt.Sprintf("%s-priv", reference.Name)
241-
reference.Namespace = privatePromotionNamespace
242-
baseImages[name] = reference
243-
}
244-
}
245-
}
246-
247-
func privatePromotionConfiguration(promotion *api.PromotionConfiguration) {
248-
for i := range promotion.Targets {
249-
if promotion.Targets[i].Namespace == ocpNamespace {
250-
if promotion.Targets[i].Name != "" {
251-
promotion.Targets[i].Name = fmt.Sprintf("%s-priv", promotion.Targets[i].Name)
252-
} else { // promotion.Targets[i].Tag must be set
253-
promotion.Targets[i].Tag = fmt.Sprintf("%s-priv", promotion.Targets[i].Tag)
254-
}
255-
promotion.Targets[i].TagByCommit = false // Never use tag_by_commit for mirrored repos
256-
promotion.Targets[i].Namespace = privatePromotionNamespace
257-
} else {
258-
// Disable this target or it will conflict with its non `-priv` counterpart
259-
promotion.Targets[i].Disabled = true
260-
}
271+
return nil
261272
}
262273
}
263-
264-
func strP(str string) *string {
265-
return &str
266-
}

cmd/ci-operator-config-mirror/main_test.go

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,13 @@ import (
44
"reflect"
55
"testing"
66

7+
"github.com/google/go-cmp/cmp"
8+
79
"k8s.io/apimachinery/pkg/util/diff"
10+
"k8s.io/utils/ptr"
811

912
"github.com/openshift/ci-tools/pkg/api"
13+
"github.com/openshift/ci-tools/pkg/config"
1014
)
1115

1216
func TestPrivateReleaseTagConfiguration(t *testing.T) {
@@ -234,3 +238,84 @@ func TestPrivatePromotionConfiguration(t *testing.T) {
234238
})
235239
}
236240
}
241+
242+
func TestCIOperatorConfigsCallback(t *testing.T) {
243+
for _, tc := range []struct {
244+
name string
245+
opts options
246+
rbc *api.ReleaseBuildConfiguration
247+
repoInfo *config.Info
248+
wantConfigsByRepo configsByRepo
249+
}{
250+
{
251+
name: "Mirror a config",
252+
opts: options{
253+
toOrg: "openshift-priv",
254+
onlyOrg: "openshift",
255+
WhitelistOptions: config.WhitelistOptions{
256+
WhitelistConfig: config.WhitelistConfig{
257+
Whitelist: map[string][]string{
258+
"openshift": {"kubernetes"},
259+
},
260+
},
261+
},
262+
},
263+
rbc: &api.ReleaseBuildConfiguration{
264+
Tests: []api.TestStepConfiguration{{
265+
As: "e2e",
266+
}},
267+
},
268+
repoInfo: &config.Info{
269+
Metadata: api.Metadata{
270+
Org: "openshift",
271+
Repo: "kubernetes",
272+
},
273+
},
274+
wantConfigsByRepo: configsByRepo{
275+
"kubernetes": []config.DataWithInfo{{
276+
Configuration: api.ReleaseBuildConfiguration{
277+
Metadata: api.Metadata{Org: "openshift-priv"},
278+
CanonicalGoRepository: ptr.To("github.com/openshift/kubernetes"),
279+
Tests: []api.TestStepConfiguration{{As: "e2e"}},
280+
},
281+
Info: config.Info{Metadata: api.Metadata{Org: "openshift-priv", Repo: "kubernetes"}},
282+
}},
283+
},
284+
},
285+
{
286+
name: "Do not mirror config without tests and images",
287+
opts: options{
288+
toOrg: "openshift-priv",
289+
onlyOrg: "openshift",
290+
WhitelistOptions: config.WhitelistOptions{
291+
WhitelistConfig: config.WhitelistConfig{
292+
Whitelist: map[string][]string{
293+
"openshift": {"kubernetes"},
294+
},
295+
},
296+
},
297+
},
298+
rbc: &api.ReleaseBuildConfiguration{},
299+
repoInfo: &config.Info{
300+
Metadata: api.Metadata{
301+
Org: "openshift",
302+
Repo: "kubernetes",
303+
},
304+
},
305+
wantConfigsByRepo: configsByRepo{},
306+
},
307+
} {
308+
t.Run(tc.name, func(t *testing.T) {
309+
gotConfigsByRepo := make(configsByRepo)
310+
callback := ciOperatorConfigsCallback(tc.opts, gotConfigsByRepo)
311+
312+
if err := callback(tc.rbc, tc.repoInfo); err != nil {
313+
t.Fatalf("callback error: %s", err)
314+
}
315+
316+
if diff := cmp.Diff(tc.wantConfigsByRepo, gotConfigsByRepo); diff != "" {
317+
t.Errorf("unexpected configs: %s", diff)
318+
}
319+
})
320+
}
321+
}

0 commit comments

Comments
 (0)