-
Notifications
You must be signed in to change notification settings - Fork 249
/
main.go
177 lines (160 loc) · 5.1 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
package main
import (
"context"
"flag"
"fmt"
"io/fs"
"os"
"path/filepath"
"github.com/sirupsen/logrus"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
kerrors "k8s.io/apimachinery/pkg/util/errors"
"k8s.io/test-infra/prow/flagutil"
"k8s.io/test-infra/prow/logrusutil"
ctrlruntimeclient "sigs.k8s.io/controller-runtime/pkg/client"
crcontrollerutil "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
"sigs.k8s.io/yaml"
"github.com/openshift/ci-tools/pkg/api/ocplifecycle"
)
type options struct {
logLevel string
scheduleDir string
dryRun bool
validateOnly bool
kubernetesOptions flagutil.KubernetesOptions
}
func gatherOptions() (*options, error) {
o := &options{kubernetesOptions: flagutil.KubernetesOptions{NOInClusterConfigDefault: true}}
fs := flag.NewFlagSet(os.Args[0], flag.ExitOnError)
fs.StringVar(&o.logLevel, "log-level", logrus.InfoLevel.String(), "Level at which to log output.")
fs.StringVar(&o.scheduleDir, "schedule-dir", "", "Directory holding schedules.")
o.kubernetesOptions.AddFlags(fs)
fs.BoolVar(&o.dryRun, "dry-run", true, "Do not mutate cluster state.")
fs.BoolVar(&o.validateOnly, "validate-only", false, "Whether to only validate the schedule files.")
if err := fs.Parse(os.Args[1:]); err != nil {
return o, fmt.Errorf("failed to parse flags: %w", err)
}
return o, nil
}
func validateOptions(o *options) error {
_, err := logrus.ParseLevel(o.logLevel)
if err != nil {
return fmt.Errorf("invalid --log-level: %w", err)
}
if o.scheduleDir == "" {
return fmt.Errorf("--schedule-dir must be specified")
}
return o.kubernetesOptions.Validate(o.dryRun)
}
func validateSchedules(schedules ocplifecycle.Config) error {
var errs []error
for _, lifecyclePhaseByVersions := range schedules {
for version, lifecyclePhases := range lifecyclePhaseByVersions {
if _, err := ocplifecycle.ParseMajorMinor(version); err != nil {
errs = append(errs, fmt.Errorf("invalid version: %s", version))
}
for i, lifecyclePhase := range lifecyclePhases {
if err := lifecyclePhase.Event.Validate(); err != nil {
errs = append(errs, fmt.Errorf("unknown event: %s", lifecyclePhase.Event))
}
if i == 0 {
continue
}
if lifecyclePhase.When.After(lifecyclePhases[i-1].When.Time) {
errs = append(errs, fmt.Errorf("version %s: event `%s` date is after event `%s`", version, lifecyclePhase.Event, lifecyclePhases[i-1].Event))
}
}
}
}
return kerrors.NewAggregate(errs)
}
func main() {
logrusutil.ComponentInit()
o, err := gatherOptions()
if err != nil {
logrus.WithError(err).Fatal("Failed to gather options")
}
if err := validateOptions(o); err != nil {
logrus.WithError(err).Fatalf("Invalid options")
}
level, _ := logrus.ParseLevel(o.logLevel)
logrus.SetLevel(level)
schedules, err := readSchedules(o.scheduleDir)
if err != nil {
logrus.WithError(err).Fatal("Could not read schedules.")
}
if o.validateOnly {
if err := validateSchedules(*schedules); err != nil {
logrus.WithError(err).Fatal("error while validating the schedules.")
}
} else {
raw, err := yaml.Marshal(schedules)
if err != nil {
logrus.WithError(err).Fatal("Could not find marshal schedules")
}
kubeConfigs, err := o.kubernetesOptions.LoadClusterConfigs(nil)
if err != nil {
logrus.WithError(err).Fatal("Could not load kube config")
}
var errors []error
for ctx, config := range kubeConfigs {
client, err := ctrlruntimeclient.New(&config, ctrlruntimeclient.Options{})
if err != nil {
errors = append(errors, fmt.Errorf("could not get client for cluster %q: %w", ctx, err))
}
if err := upsertConfigMap(string(raw), client); err != nil {
errors = append(errors, fmt.Errorf("could not upsert configmap for cluster %q: %w", ctx, err))
}
}
if len(errors) > 0 {
logrus.WithError(kerrors.NewAggregate(errors)).Fatal("Failed to update cluster state.")
}
}
}
func readSchedules(scheduleDir string) (*ocplifecycle.Config, error) {
var config *ocplifecycle.Config
if err := filepath.Walk(scheduleDir, func(path string, info fs.FileInfo, err error) error {
if err != nil {
return err
}
if filepath.Ext(info.Name()) != ".yaml" {
return nil
}
raw, err := os.ReadFile(path)
if err != nil {
return fmt.Errorf("could not read file: %w", err)
}
var schedule Schedule
if err := yaml.Unmarshal(raw, &schedule); err != nil {
return fmt.Errorf("could not unmarshal file: %w", err)
}
config = addToConfig(schedule, config)
return nil
}); err != nil {
return nil, err
}
return config, nil
}
const (
scheduleNamespace = "ci"
scheduleConfigmap = "release-schedules"
scheduleKey = "schedules.yaml"
)
func upsertConfigMap(schedules string, client ctrlruntimeclient.Client) error {
configmap := &v1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{
Name: scheduleConfigmap,
Namespace: scheduleNamespace,
},
}
mutate := func() error {
if configmap.Data == nil {
configmap.Data = map[string]string{}
}
configmap.Data[scheduleKey] = schedules
return nil
}
_, err := crcontrollerutil.CreateOrUpdate(context.Background(), client, configmap, mutate)
return err
}