forked from kubernetes-retired/contrib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mungers.go
133 lines (115 loc) · 3.69 KB
/
mungers.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
/*
Copyright 2015 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package mungers
import (
"fmt"
"k8s.io/contrib/mungegithub/features"
"k8s.io/contrib/mungegithub/github"
"k8s.io/kubernetes/pkg/util/sets"
"github.com/golang/glog"
"github.com/spf13/cobra"
"k8s.io/contrib/mungegithub/mungers/mungerutil"
)
// Munger is the interface which all mungers must implement to register
type Munger interface {
// Take action on a specific github issue:
Munge(obj *github.MungeObject)
AddFlags(cmd *cobra.Command, config *github.Config)
Name() string
RequiredFeatures() []string
Initialize(*github.Config, *features.Features) error
EachLoop() error
}
var mungerMap = map[string]Munger{}
var mungers = []Munger{}
// GetAllMungers returns a slice of all registered mungers. This list is
// completely independant of the mungers selected at runtime in --pr-mungers.
// This is all possible mungers.
func GetAllMungers() []Munger {
out := []Munger{}
for _, munger := range mungerMap {
out = append(out, munger)
}
return out
}
// GetActiveMungers returns a slice of all mungers which both registered and
// were requested by the user
func GetActiveMungers() []Munger {
return mungers
}
// RequestedFeatures returns a list of all feature which should be enabled
// for the running mungers
func RequestedFeatures() []string {
out := sets.NewString()
for _, m := range GetActiveMungers() {
f := m.RequiredFeatures()
out.Insert(f...)
}
return out.List()
}
// RegisterMungers will check if a requested munger exists and add it to
// the list.
func RegisterMungers(requestedMungers []string) error {
for _, name := range requestedMungers {
munger, found := mungerMap[name]
if !found {
return fmt.Errorf("couldn't find a munger named: %s", name)
}
mungers = append(mungers, munger)
}
return nil
}
// InitializeMungers will call munger.Initialize() for the requested mungers.
func InitializeMungers(config *github.Config, features *features.Features) error {
for _, munger := range mungers {
if err := munger.Initialize(config, features); err != nil {
return err
}
glog.Infof(mungerutil.PrettyString(munger))
glog.Infof("Initialized munger: %s", munger.Name())
}
return nil
}
// EachLoop will be called before we start a poll loop and will run the
// EachLoop function for all active mungers
func EachLoop() error {
for _, munger := range mungers {
if err := munger.EachLoop(); err != nil {
return err
}
}
return nil
}
// RegisterMunger should be called in `init()` by each munger to make itself
// available by name
func RegisterMunger(munger Munger) error {
if _, found := mungerMap[munger.Name()]; found {
return fmt.Errorf("a munger with that name (%s) already exists", munger.Name())
}
mungerMap[munger.Name()] = munger
glog.Infof("Registered %#v at %s", munger, munger.Name())
return nil
}
// RegisterMungerOrDie will call RegisterMunger but will be fatal on error
func RegisterMungerOrDie(munger Munger) {
if err := RegisterMunger(munger); err != nil {
glog.Fatalf("Failed to register munger: %s", err)
}
}
// MungeIssue will call each activated munger with the given object
func MungeIssue(obj *github.MungeObject) error {
for _, munger := range mungers {
munger.Munge(obj)
}
return nil
}