Skip to content

Commit 50fc0fa

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

File tree

6 files changed

+665
-6
lines changed

6 files changed

+665
-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: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
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+
"github.com/sirupsen/logrus"
26+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
27+
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
28+
"k8s.io/apimachinery/pkg/runtime/schema"
29+
"sigs.k8s.io/controller-runtime/pkg/controller"
30+
crthandler "sigs.k8s.io/controller-runtime/pkg/handler"
31+
"sigs.k8s.io/controller-runtime/pkg/manager"
32+
"sigs.k8s.io/controller-runtime/pkg/source"
33+
)
34+
35+
// Options - options for your controller
36+
type Options struct {
37+
EventHandlers []events.EventHandler
38+
LoggingLevel events.LogLevel
39+
Runner runner.Runner
40+
Namespace string
41+
GVK schema.GroupVersionKind
42+
// StopChannel is used to deal with the bug:
43+
// https://github.com/kubernetes-sigs/controller-runtime/issues/103
44+
StopChannel <-chan struct{}
45+
}
46+
47+
// Add - Creates a new ansible operator controller and adds it to the manager
48+
func Add(mgr manager.Manager, options Options) {
49+
logrus.Infof("Watching %s/%v, %s, %s", options.GVK.Group, options.GVK.Version, options.GVK.Kind, options.Namespace)
50+
if options.EventHandlers == nil {
51+
options.EventHandlers = []events.EventHandler{}
52+
}
53+
eventHandlers := append(options.EventHandlers, events.NewLoggingEventHandler(options.LoggingLevel))
54+
55+
h := &AnsibleOperatorReconciler{
56+
Client: mgr.GetClient(),
57+
GVK: options.GVK,
58+
Runner: options.Runner,
59+
EventHandlers: eventHandlers,
60+
}
61+
62+
// Register the GVK with the schema
63+
mgr.GetScheme().AddKnownTypeWithName(options.GVK, &unstructured.Unstructured{})
64+
metav1.AddToGroupVersion(mgr.GetScheme(), schema.GroupVersion{
65+
Group: options.GVK.Group,
66+
Version: options.GVK.Version,
67+
})
68+
69+
//Create new controller runtime controller and set the controller to watch GVK.
70+
c, err := controller.New(fmt.Sprintf("%v-controller", strings.ToLower(options.GVK.Kind)), mgr, controller.Options{
71+
Reconciler: h,
72+
})
73+
if err != nil {
74+
log.Fatal(err)
75+
}
76+
u := &unstructured.Unstructured{}
77+
u.SetGroupVersionKind(options.GVK)
78+
if err := c.Watch(&source.Kind{Type: u}, &crthandler.EnqueueRequestForObject{}); err != nil {
79+
log.Fatal(err)
80+
}
81+
82+
r := NewReconcileLoop(time.Minute*1, options.GVK, mgr.GetClient())
83+
r.Stop = options.StopChannel
84+
cs := &source.Channel{Source: r.Source}
85+
cs.InjectStopChannel(options.StopChannel)
86+
if err := c.Watch(cs, &crthandler.EnqueueRequestForObject{}); err != nil {
87+
log.Fatal(err)
88+
}
89+
r.Start()
90+
}

0 commit comments

Comments
 (0)