-
Notifications
You must be signed in to change notification settings - Fork 251
/
main.go
205 lines (170 loc) · 6.02 KB
/
main.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
package main
import (
"errors"
"flag"
"fmt"
"os"
"path/filepath"
"github.com/ghodss/yaml"
"github.com/sirupsen/logrus"
utilerrors "k8s.io/apimachinery/pkg/util/errors"
"k8s.io/apimachinery/pkg/util/sets"
"sigs.k8s.io/prow/pkg/config/org"
"sigs.k8s.io/prow/pkg/config/secret"
"sigs.k8s.io/prow/pkg/flagutil"
"sigs.k8s.io/prow/pkg/github"
"sigs.k8s.io/prow/pkg/interrupts"
"github.com/openshift/ci-tools/pkg/api"
"github.com/openshift/ci-tools/pkg/config"
"github.com/openshift/ci-tools/pkg/util/gzip"
)
type gitHubClient interface {
GetRepo(owner, name string) (github.FullRepo, error)
}
type options struct {
config.WhitelistOptions
peribolosConfig string
destOrg string
onlyOrg string
releaseRepoPath string
github flagutil.GitHubOptions
}
func gatherOptions() (options, error) {
o := options{}
fs := flag.NewFlagSet(os.Args[0], flag.ExitOnError)
fs.StringVar(&o.peribolosConfig, "peribolos-config", "", "Peribolos configuration file")
fs.StringVar(&o.releaseRepoPath, "release-repo-path", "", "Path to a openshift/release repository directory")
fs.StringVar(&o.destOrg, "destination-org", "", "Destination name of the peribolos configuration organzation")
fs.StringVar(&o.onlyOrg, "only-org", "", "Only dump config of the repos belonging to this org.")
o.github.AddFlags(fs)
o.WhitelistOptions.Bind(fs)
if err := fs.Parse(os.Args[1:]); err != nil {
return o, fmt.Errorf("faild to parse flags: %w", err)
}
return o, nil
}
func validateOptions(o *options) error {
var validationErrors []error
if len(o.releaseRepoPath) == 0 {
validationErrors = append(validationErrors, errors.New("--release-repo-path is not specified"))
}
if len(o.peribolosConfig) == 0 {
validationErrors = append(validationErrors, errors.New("--peribolos-config is not specified"))
}
if len(o.destOrg) == 0 {
validationErrors = append(validationErrors, errors.New("--destination-org is not specified"))
}
if err := o.github.Validate(false); err != nil {
validationErrors = append(validationErrors, err)
}
if err := o.Validate(); err != nil {
validationErrors = append(validationErrors, err)
}
return utilerrors.NewAggregate(validationErrors)
}
func main() {
o, err := gatherOptions()
if err != nil {
logrus.WithError(err).Fatal("failed to gather options")
}
if err := validateOptions(&o); err != nil {
logrus.WithError(err).Fatal("invalid options")
}
logger := logrus.WithField("destination-org", o.destOrg)
go func() {
interrupts.WaitForGracefulShutdown()
os.Exit(1)
}()
b, err := gzip.ReadFileMaybeGZIP(o.peribolosConfig)
if err != nil {
logger.WithError(err).Fatal("could not read peribolos configuration file")
}
var peribolosConfig org.FullConfig
if err := yaml.Unmarshal(b, &peribolosConfig); err != nil {
logger.WithError(err).Fatal("failed to unmarshal peribolos config")
}
if err := secret.Add(o.github.TokenPath); err != nil {
logrus.WithError(err).Fatal("Error starting secrets agent.")
}
gc, err := o.github.GitHubClient(false)
if err != nil {
logger.WithError(err).Fatal("Error getting GitHub client.")
}
orgRepos, err := getReposForPrivateOrg(o.releaseRepoPath, o.WhitelistOptions.WhitelistConfig.Whitelist, o.onlyOrg)
if err != nil {
logger.WithError(err).Fatal("couldn't get the list of org/repos that promote official images")
}
peribolosRepos := generateRepositories(gc, orgRepos, logger)
peribolosConfigByOrg := peribolosConfig.Orgs[o.destOrg]
peribolosConfigByOrg.Repos = peribolosRepos
peribolosConfig.Orgs[o.destOrg] = peribolosConfigByOrg
out, err := yaml.Marshal(peribolosConfig)
if err != nil {
logrus.WithError(err).Fatalf("%s failed to marshal output.", o.peribolosConfig)
}
if err := os.WriteFile(o.peribolosConfig, out, 0666); err != nil {
logrus.WithError(err).Fatal("Failed to write output.")
}
}
func generateRepositories(gc gitHubClient, orgRepos map[string]sets.Set[string], logger *logrus.Entry) map[string]org.Repo {
peribolosRepos := make(map[string]org.Repo)
yes := true
for orgName, repos := range orgRepos {
for repo := range repos {
logger.WithFields(logrus.Fields{"org": orgName, "repo": repo}).Info("Processing repository details...")
fullRepo, err := gc.GetRepo(orgName, repo)
if err != nil {
logger.WithError(err).Fatal("couldn't get repo details")
}
peribolosRepos[fullRepo.Name] = org.PruneRepoDefaults(org.Repo{
Description: &fullRepo.Description,
HomePage: &fullRepo.Homepage,
Private: &yes, // all repositories in private org should be private
HasIssues: &fullRepo.HasIssues,
HasProjects: &fullRepo.HasProjects,
HasWiki: &fullRepo.HasWiki,
AllowMergeCommit: &fullRepo.AllowMergeCommit,
AllowSquashMerge: &fullRepo.AllowSquashMerge,
AllowRebaseMerge: &fullRepo.AllowRebaseMerge,
Archived: &fullRepo.Archived,
DefaultBranch: &fullRepo.DefaultBranch,
})
}
}
return peribolosRepos
}
// getReposForPrivateOrg iterates through the release repository directory and creates a map of
// repository sets by organization that promote official images.
func getReposForPrivateOrg(releaseRepoPath string, whitelist map[string][]string, onlyOrg string) (map[string]sets.Set[string], error) {
ret := make(map[string]sets.Set[string])
for org, repos := range whitelist {
if onlyOrg != "" && onlyOrg != org {
continue
}
for _, repo := range repos {
if _, ok := ret[org]; !ok {
ret[org] = sets.New[string](repo)
} else {
ret[org].Insert(repo)
}
}
}
callback := func(c *api.ReleaseBuildConfiguration, i *config.Info) error {
if !api.BuildsAnyOfficialImages(c, api.WithoutOKD) {
return nil
}
if onlyOrg != "" && onlyOrg != i.Org {
return nil
}
repos, exist := ret[i.Org]
if !exist {
repos = sets.New[string]()
}
ret[i.Org] = repos.Insert(i.Repo)
return nil
}
if err := config.OperateOnCIOperatorConfigDir(filepath.Join(releaseRepoPath, config.CiopConfigInRepoPath), callback); err != nil {
return ret, fmt.Errorf("error while operating in ci-operator configuration files: %w", err)
}
return ret, nil
}