-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
controller.go
149 lines (131 loc) · 4.92 KB
/
controller.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
// Copyright 2018 The Operator-SDK 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 controller
import (
"fmt"
"os"
"reflect"
"strings"
"time"
libpredicate "github.com/operator-framework/operator-lib/predicate"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"sigs.k8s.io/controller-runtime/pkg/controller"
logf "sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/manager"
ctrlpredicate "sigs.k8s.io/controller-runtime/pkg/predicate"
"sigs.k8s.io/controller-runtime/pkg/source"
"github.com/operator-framework/operator-sdk/internal/ansible/events"
"github.com/operator-framework/operator-sdk/internal/ansible/handler"
"github.com/operator-framework/operator-sdk/internal/ansible/runner"
)
var log = logf.Log.WithName("ansible-controller")
// Options - options for your controller
type Options struct {
EventHandlers []events.EventHandler
LoggingLevel events.LogLevel
Runner runner.Runner
GVK schema.GroupVersionKind
ReconcilePeriod time.Duration
ManageStatus bool
AnsibleDebugLogs bool
WatchDependentResources bool
WatchClusterScopedResources bool
WatchAnnotationsChanges bool
MaxConcurrentReconciles int
Selector metav1.LabelSelector
}
// Add - Creates a new ansible operator controller and adds it to the manager
func Add(mgr manager.Manager, options Options) *controller.Controller {
log.Info("Watching resource", "Options.Group", options.GVK.Group, "Options.Version",
options.GVK.Version, "Options.Kind", options.GVK.Kind)
if options.EventHandlers == nil {
options.EventHandlers = []events.EventHandler{}
}
eventHandlers := append(options.EventHandlers, events.NewLoggingEventHandler(options.LoggingLevel))
aor := &AnsibleOperatorReconciler{
Client: mgr.GetClient(),
GVK: options.GVK,
Runner: options.Runner,
EventHandlers: eventHandlers,
ReconcilePeriod: options.ReconcilePeriod,
ManageStatus: options.ManageStatus,
AnsibleDebugLogs: options.AnsibleDebugLogs,
APIReader: mgr.GetAPIReader(),
WatchAnnotationsChanges: options.WatchAnnotationsChanges,
}
scheme := mgr.GetScheme()
_, err := scheme.New(options.GVK)
if runtime.IsNotRegisteredError(err) {
// Register the GVK with the schema
scheme.AddKnownTypeWithName(options.GVK, &unstructured.Unstructured{})
metav1.AddToGroupVersion(mgr.GetScheme(), schema.GroupVersion{
Group: options.GVK.Group,
Version: options.GVK.Version,
})
} else if err != nil {
log.Error(err, "")
os.Exit(1)
}
//Create new controller runtime controller and set the controller to watch GVK.
c, err := controller.New(fmt.Sprintf("%v-controller", strings.ToLower(options.GVK.Kind)), mgr,
controller.Options{
Reconciler: aor,
MaxConcurrentReconciles: options.MaxConcurrentReconciles,
})
if err != nil {
log.Error(err, "")
os.Exit(1)
}
// Set up predicates.
predicates := []ctrlpredicate.Predicate{
ctrlpredicate.Or(ctrlpredicate.GenerationChangedPredicate{}, libpredicate.NoGenerationPredicate{}),
}
if options.WatchAnnotationsChanges {
predicates = []ctrlpredicate.Predicate{
ctrlpredicate.Or(ctrlpredicate.AnnotationChangedPredicate{}, predicates[0]),
}
}
p, err := parsePredicateSelector(options.Selector)
if err != nil {
log.Error(err, "")
os.Exit(1)
}
if p != nil {
predicates = append(predicates, p)
}
u := &unstructured.Unstructured{}
u.SetGroupVersionKind(options.GVK)
err = c.Watch(&source.Kind{Type: u}, &handler.LoggingEnqueueRequestForObject{}, predicates...)
if err != nil {
log.Error(err, "")
os.Exit(1)
}
return &c
}
// parsePredicateSelector parses the selector in the WatchOptions and creates a predicate
// that is used to filter resources based on the specified selector
func parsePredicateSelector(selector metav1.LabelSelector) (ctrlpredicate.Predicate, error) {
// If a selector has been specified in watches.yaml, add it to the watch's predicates.
if !reflect.ValueOf(selector).IsZero() {
p, err := ctrlpredicate.LabelSelectorPredicate(selector)
if err != nil {
return nil, fmt.Errorf("error constructing predicate from watches selector: %v", err)
}
return p, nil
}
return nil, nil
}