github.com/lablabs/operator-sdk@v0.8.2/pkg/ansible/controller/controller.go (about) 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 "os" 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/operator-framework/operator-sdk/pkg/predicate" 26 27 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 28 "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" 29 "k8s.io/apimachinery/pkg/runtime" 30 "k8s.io/apimachinery/pkg/runtime/schema" 31 "sigs.k8s.io/controller-runtime/pkg/controller" 32 crthandler "sigs.k8s.io/controller-runtime/pkg/handler" 33 "sigs.k8s.io/controller-runtime/pkg/manager" 34 logf "sigs.k8s.io/controller-runtime/pkg/runtime/log" 35 "sigs.k8s.io/controller-runtime/pkg/source" 36 ) 37 38 var log = logf.Log.WithName("ansible-controller") 39 40 // Options - options for your controller 41 type Options struct { 42 EventHandlers []events.EventHandler 43 LoggingLevel events.LogLevel 44 Runner runner.Runner 45 GVK schema.GroupVersionKind 46 ReconcilePeriod time.Duration 47 ManageStatus bool 48 WatchDependentResources bool 49 WatchClusterScopedResources bool 50 MaxWorkers int 51 } 52 53 // Add - Creates a new ansible operator controller and adds it to the manager 54 func Add(mgr manager.Manager, options Options) *controller.Controller { 55 log.Info("Watching resource", "Options.Group", options.GVK.Group, "Options.Version", options.GVK.Version, "Options.Kind", options.GVK.Kind) 56 if options.EventHandlers == nil { 57 options.EventHandlers = []events.EventHandler{} 58 } 59 eventHandlers := append(options.EventHandlers, events.NewLoggingEventHandler(options.LoggingLevel)) 60 61 aor := &AnsibleOperatorReconciler{ 62 Client: mgr.GetClient(), 63 GVK: options.GVK, 64 Runner: options.Runner, 65 EventHandlers: eventHandlers, 66 ReconcilePeriod: options.ReconcilePeriod, 67 ManageStatus: options.ManageStatus, 68 } 69 70 scheme := mgr.GetScheme() 71 _, err := scheme.New(options.GVK) 72 if runtime.IsNotRegisteredError(err) { 73 // Register the GVK with the schema 74 scheme.AddKnownTypeWithName(options.GVK, &unstructured.Unstructured{}) 75 metav1.AddToGroupVersion(mgr.GetScheme(), schema.GroupVersion{ 76 Group: options.GVK.Group, 77 Version: options.GVK.Version, 78 }) 79 } else if err != nil { 80 log.Error(err, "") 81 os.Exit(1) 82 } 83 84 //Create new controller runtime controller and set the controller to watch GVK. 85 c, err := controller.New(fmt.Sprintf("%v-controller", strings.ToLower(options.GVK.Kind)), mgr, controller.Options{ 86 Reconciler: aor, 87 MaxConcurrentReconciles: options.MaxWorkers, 88 }) 89 if err != nil { 90 log.Error(err, "") 91 os.Exit(1) 92 } 93 u := &unstructured.Unstructured{} 94 u.SetGroupVersionKind(options.GVK) 95 if err := c.Watch(&source.Kind{Type: u}, &crthandler.EnqueueRequestForObject{}, predicate.GenerationChangedPredicate{}); err != nil { 96 log.Error(err, "") 97 os.Exit(1) 98 } 99 return &c 100 }