github.com/jmrodri/operator-sdk@v0.5.0/pkg/ansible/operator/operator.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 operator
    16  
    17  import (
    18  	"errors"
    19  	"math/rand"
    20  	"time"
    21  
    22  	"github.com/operator-framework/operator-sdk/pkg/ansible/controller"
    23  	"github.com/operator-framework/operator-sdk/pkg/ansible/flags"
    24  	"github.com/operator-framework/operator-sdk/pkg/ansible/proxy/controllermap"
    25  	"github.com/operator-framework/operator-sdk/pkg/ansible/runner"
    26  
    27  	"sigs.k8s.io/controller-runtime/pkg/manager"
    28  	logf "sigs.k8s.io/controller-runtime/pkg/runtime/log"
    29  	"sigs.k8s.io/controller-runtime/pkg/runtime/signals"
    30  )
    31  
    32  // Run - A blocking function which starts a controller-runtime manager
    33  // It starts an Operator by reading in the values in `./watches.yaml`, adds a controller
    34  // to the manager, and finally running the manager.
    35  func Run(done chan error, mgr manager.Manager, f *flags.AnsibleOperatorFlags, cMap *controllermap.ControllerMap) {
    36  	watches, err := runner.NewFromWatches(f.WatchesFile)
    37  	if err != nil {
    38  		logf.Log.WithName("manager").Error(err, "Failed to get watches")
    39  		done <- err
    40  		return
    41  	}
    42  	rand.Seed(time.Now().Unix())
    43  	c := signals.SetupSignalHandler()
    44  
    45  	for gvk, runner := range watches {
    46  		o := controller.Options{
    47  			GVK:          gvk,
    48  			Runner:       runner,
    49  			ManageStatus: runner.GetManageStatus(),
    50  		}
    51  		applyFlagsToControllerOptions(f, &o)
    52  		if d, ok := runner.GetReconcilePeriod(); ok {
    53  			o.ReconcilePeriod = d
    54  		}
    55  		ctr := controller.Add(mgr, o)
    56  		if ctr == nil {
    57  			done <- errors.New("failed to add controller")
    58  			return
    59  		}
    60  		cMap.Store(o.GVK, &controllermap.ControllerMapContents{Controller: *ctr,
    61  			WatchDependentResources:     runner.GetWatchDependentResources(),
    62  			WatchClusterScopedResources: runner.GetWatchClusterScopedResources(),
    63  			WatchMap:                    controllermap.NewWatchMap(),
    64  			UIDMap:                      controllermap.NewUIDMap(),
    65  		})
    66  	}
    67  	done <- mgr.Start(c)
    68  }
    69  
    70  func applyFlagsToControllerOptions(f *flags.AnsibleOperatorFlags, o *controller.Options) {
    71  	o.ReconcilePeriod = f.ReconcilePeriod
    72  }