Skip to content

Commit 8561e9f

Browse files
author
Shawn Hurley
committed
Adding ansible operator controller and events package.
* Adding new source for reconcilation
1 parent fe6d2a3 commit 8561e9f

File tree

6 files changed

+667
-6
lines changed

6 files changed

+667
-6
lines changed

Gopkg.lock

Lines changed: 127 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/ansible/controller/controller.go

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
// Copyright 2018 The Operator-SDK Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package controller
16+
17+
import (
18+
"fmt"
19+
"log"
20+
"strings"
21+
"time"
22+
23+
"github.com/operator-framework/operator-sdk/pkg/ansible/events"
24+
"github.com/operator-framework/operator-sdk/pkg/ansible/runner"
25+
26+
"github.com/sirupsen/logrus"
27+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
28+
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
29+
"k8s.io/apimachinery/pkg/runtime/schema"
30+
"sigs.k8s.io/controller-runtime/pkg/controller"
31+
crthandler "sigs.k8s.io/controller-runtime/pkg/handler"
32+
"sigs.k8s.io/controller-runtime/pkg/manager"
33+
"sigs.k8s.io/controller-runtime/pkg/source"
34+
)
35+
36+
// Options - options for your controller
37+
type Options struct {
38+
EventHandlers []events.EventHandler
39+
LoggingLevel events.LogLevel
40+
Runner runner.Runner
41+
Namespace string
42+
GVK schema.GroupVersionKind
43+
// StopChannel is used to deal with the bug:
44+
// https://github.com/kubernetes-sigs/controller-runtime/issues/103
45+
StopChannel <-chan struct{}
46+
}
47+
48+
// Add - Creates a new ansible operator controller and adds it to the manager
49+
func Add(mgr manager.Manager, options Options) {
50+
logrus.Infof("Watching %s/%v, %s, %s", options.GVK.Group, options.GVK.Version, options.GVK.Kind, options.Namespace)
51+
if options.EventHandlers == nil {
52+
options.EventHandlers = []events.EventHandler{}
53+
}
54+
eventHandlers := append(options.EventHandlers, events.NewLoggingEventHandler(options.LoggingLevel))
55+
56+
aor := &AnsibleOperatorReconciler{
57+
Client: mgr.GetClient(),
58+
GVK: options.GVK,
59+
Runner: options.Runner,
60+
EventHandlers: eventHandlers,
61+
}
62+
63+
// Register the GVK with the schema
64+
mgr.GetScheme().AddKnownTypeWithName(options.GVK, &unstructured.Unstructured{})
65+
metav1.AddToGroupVersion(mgr.GetScheme(), schema.GroupVersion{
66+
Group: options.GVK.Group,
67+
Version: options.GVK.Version,
68+
})
69+
70+
//Create new controller runtime controller and set the controller to watch GVK.
71+
c, err := controller.New(fmt.Sprintf("%v-controller", strings.ToLower(options.GVK.Kind)), mgr, controller.Options{
72+
Reconciler: aor,
73+
})
74+
if err != nil {
75+
log.Fatal(err)
76+
}
77+
u := &unstructured.Unstructured{}
78+
u.SetGroupVersionKind(options.GVK)
79+
if err := c.Watch(&source.Kind{Type: u}, &crthandler.EnqueueRequestForObject{}); err != nil {
80+
log.Fatal(err)
81+
}
82+
83+
r := NewReconcileLoop(time.Minute*1, options.GVK, mgr.GetClient())
84+
r.Stop = options.StopChannel
85+
cs := &source.Channel{Source: r.Source}
86+
cs.InjectStopChannel(options.StopChannel)
87+
if err := c.Watch(cs, &crthandler.EnqueueRequestForObject{}); err != nil {
88+
log.Fatal(err)
89+
}
90+
r.Start()
91+
}

0 commit comments

Comments
 (0)