Skip to content

Commit 5230f92

Browse files
author
Shawn Hurley
committed
Adding ansible operator controller and events package.
1 parent fa3bbbd commit 5230f92

File tree

5 files changed

+532
-0
lines changed

5 files changed

+532
-0
lines changed

Gopkg.lock

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

0 commit comments

Comments
 (0)