-
Notifications
You must be signed in to change notification settings - Fork 249
/
main.go
158 lines (129 loc) · 4.8 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
package main
import (
"errors"
"flag"
"fmt"
"os"
"path"
"time"
"github.com/sirupsen/logrus"
utilerrors "k8s.io/apimachinery/pkg/util/errors"
"github.com/openshift/ci-tools/pkg/api/ocplifecycle"
"github.com/openshift/ci-tools/pkg/branchcuts/bumper"
mgr "github.com/openshift/ci-tools/pkg/branchcuts/config-manager"
cioperatorcfg "github.com/openshift/ci-tools/pkg/config"
)
const (
optsParsingErrorExitStatus = 1
reconcileErrorExitStatus = 2
releaseJobsPath = "ci-operator/config/openshift/release"
howManyDaysBefore = 14
lifecycleConfigFilePathDefaultPath = ""
)
type options struct {
curOCPVersion string
futureOCPVersion string
releaseJobsDir string
releaseRepoDir string
outDir string
lifecycleConfigFilePath string
logLevel int
newIntervalValue int
}
func (o *options) tryOverrideOCPVersions(curVersion, futureVersion string) {
if o.curOCPVersion == "" {
o.curOCPVersion = curVersion
}
if o.futureOCPVersion == "" {
o.futureOCPVersion = futureVersion
}
}
func (o *options) adjustPaths() error {
if o.releaseRepoDir != "" {
if !path.IsAbs(o.releaseRepoDir) {
return errors.New("release-repo-path path has to be absolute")
}
if path.IsAbs(o.releaseJobsDir) {
return errors.New("release-jobs path can't be absolute when release-jobs is specified")
}
o.releaseJobsDir = path.Join(o.releaseRepoDir, o.releaseJobsDir)
}
return nil
}
func gatherOptions() (*options, error) {
var errs []error
o := &options{}
flag.StringVar(&o.curOCPVersion, "current-release", "", "Current OCP version")
flag.StringVar(&o.futureOCPVersion, "future-release", "", "Next OCP version")
flag.StringVar(&o.releaseJobsDir, "release-jobs", releaseJobsPath, "Path to release-gating jobs .yaml files")
flag.StringVar(&o.releaseRepoDir, "release-repo-path", "", "Path to 'openshift/release/ folder")
flag.StringVar(&o.outDir, "out", "", "Output directory. Default to --release-jobs dir")
flag.StringVar(&o.lifecycleConfigFilePath, "lifecycle-config", lifecycleConfigFilePathDefaultPath, "Path to the lifecycle config file")
flag.IntVar(&o.newIntervalValue, "interval", 168, "New interval to set")
flag.IntVar(&o.logLevel, "log-level", int(logrus.DebugLevel), "Log level")
flag.Parse()
if _, err := ocplifecycle.ParseMajorMinor(o.curOCPVersion); o.curOCPVersion != "" && err != nil {
errs = append(errs, fmt.Errorf("error parsing current-release %s", o.curOCPVersion))
}
if _, err := ocplifecycle.ParseMajorMinor(o.futureOCPVersion); o.futureOCPVersion != "" && err != nil {
errs = append(errs, fmt.Errorf("error parsing future-release %s", o.futureOCPVersion))
}
if o.newIntervalValue < 0 {
errs = append(errs, errors.New("error parsing interval: value is not a positive integer"))
}
if o.lifecycleConfigFilePath == "" {
errs = append(errs, errors.New("lifecycle-config is mandatory"))
}
if !path.IsAbs(o.lifecycleConfigFilePath) {
errs = append(errs, errors.New("lifecycle-config path is not absolute"))
}
if o.outDir != "" && !path.IsAbs(o.outDir) {
errs = append(errs, errors.New("out path is not absolute"))
}
return o, utilerrors.NewAggregate(errs)
}
func main() {
o, err := gatherOptions()
if err != nil {
logrus.WithError(err).Error("failed to gather options")
os.Exit(optsParsingErrorExitStatus)
}
if err = o.adjustPaths(); err != nil {
logrus.WithError(err).Error("failed to gather options")
os.Exit(optsParsingErrorExitStatus)
}
logrus.SetLevel(logrus.Level(o.logLevel))
logrus.Debugf("using options %+v", o)
recStatus, err := reconcile(time.Now(), o)
switch recStatus {
case mgr.ReconcileOk:
logrus.Info("status reconciled")
default:
logrus.WithError(err).Error("failed to reconcile the status")
os.Exit(reconcileErrorExitStatus)
}
}
func reconcile(now time.Time, o *options) (mgr.ReconcileStatus, error) {
lifecycleConfig, err := ocplifecycle.LoadConfig(o.lifecycleConfigFilePath)
if err != nil {
return mgr.ReconcileErr, fmt.Errorf("OCP Lifecycle load: %w", err)
}
ocpLifecycle := ocplifecycle.NewOCPCodeFreezeLifecycle(&lifecycleConfig, now)
if !ocpLifecycle.IsBeforeCodeFreeze(howManyDaysBefore) {
return mgr.ReconcileSkipped, nil
}
currentVersion, futureVersion, err := ocpLifecycle.GetOCPVersions()
if err != nil {
return mgr.ReconcileErr, fmt.Errorf("OCP Lifecycle versions: %w", err)
}
o.tryOverrideOCPVersions(currentVersion.GetVersion(), futureVersion.GetVersion())
logrus.Debugf("using options %+v", o)
b := bumper.NewGeneratedReleaseGatingJobsBumper(o.curOCPVersion, o.releaseJobsDir, o.newIntervalValue)
bumped, err := bumper.Bump[*cioperatorcfg.DataWithInfo](b, &bumper.BumpingOptions{
OutDir: o.outDir,
})
if !bumped || err != nil {
return mgr.ReconcileErr, fmt.Errorf("bumper: %w", err)
}
return mgr.ReconcileOk, nil
}