github.xiaoq7.com/operator-framework/operator-sdk@v0.8.2/pkg/ansible/runner/fake/runner.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 fake
    16  
    17  import (
    18  	"fmt"
    19  	"time"
    20  
    21  	"github.com/operator-framework/operator-sdk/pkg/ansible/runner"
    22  	"github.com/operator-framework/operator-sdk/pkg/ansible/runner/eventapi"
    23  	"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
    24  )
    25  
    26  // Runner - implements the Runner interface for a GVK that's being watched.
    27  type Runner struct {
    28  	Finalizer                   string
    29  	ReconcilePeriod             time.Duration
    30  	ManageStatus                bool
    31  	WatchDependentResources     bool
    32  	WatchClusterScopedResources bool
    33  	// Used to send error if Run should fail.
    34  	Error error
    35  	// Job Events that will be sent back from the runs channel
    36  	JobEvents []eventapi.JobEvent
    37  	//Stdout standard out to reply if failure occurs.
    38  	Stdout string
    39  }
    40  
    41  type runResult struct {
    42  	events <-chan eventapi.JobEvent
    43  	stdout string
    44  }
    45  
    46  func (r *runResult) Events() <-chan eventapi.JobEvent {
    47  	return r.events
    48  }
    49  
    50  func (r *runResult) Stdout() (string, error) {
    51  	if r.stdout != "" {
    52  		return r.stdout, nil
    53  	}
    54  	return r.stdout, fmt.Errorf("unable to find standard out")
    55  }
    56  
    57  // Run - runs the fake runner.
    58  func (r *Runner) Run(_ string, u *unstructured.Unstructured, _ string) (runner.RunResult, error) {
    59  	if r.Error != nil {
    60  		return nil, r.Error
    61  	}
    62  	c := make(chan eventapi.JobEvent)
    63  	go func() {
    64  		for _, je := range r.JobEvents {
    65  			c <- je
    66  		}
    67  		close(c)
    68  	}()
    69  	return &runResult{events: c, stdout: r.Stdout}, nil
    70  }
    71  
    72  // GetReconcilePeriod - new reconcile period.
    73  func (r *Runner) GetReconcilePeriod() (time.Duration, bool) {
    74  	return r.ReconcilePeriod, r.ReconcilePeriod != time.Duration(0)
    75  }
    76  
    77  // GetManageStatus - get managestatus.
    78  func (r *Runner) GetManageStatus() bool {
    79  	return r.ManageStatus
    80  }
    81  
    82  // GetWatchDependentResources - get watchDependentResources.
    83  func (r *Runner) GetWatchDependentResources() bool {
    84  	return r.WatchDependentResources
    85  }
    86  
    87  // GetWatchClusterScopedResources - get watchClusterScopedResources.
    88  func (r *Runner) GetWatchClusterScopedResources() bool {
    89  	return r.WatchClusterScopedResources
    90  }
    91  
    92  // GetFinalizer - gets the fake finalizer.
    93  func (r *Runner) GetFinalizer() (string, bool) {
    94  	return r.Finalizer, r.Finalizer != ""
    95  }