github.com/jmrodri/operator-sdk@v0.5.0/pkg/ready/ready.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 ready
    16  
    17  import (
    18  	"os"
    19  )
    20  
    21  const FileName = "/tmp/operator-sdk-ready"
    22  
    23  // Ready holds state about whether the operator is ready and communicates that
    24  // to a Kubernetes readiness probe.
    25  type Ready interface {
    26  	// Set ensures that future readiness probes will indicate that the operator
    27  	// is ready.
    28  	Set() error
    29  
    30  	// Unset ensures that future readiness probes will indicate that the
    31  	// operator is not ready.
    32  	Unset() error
    33  }
    34  
    35  // NewFileReady returns a Ready that uses the presence of a file on disk to
    36  // communicate whether the operator is ready. The operator's Pod definition
    37  // should include a readinessProbe of "exec" type that calls
    38  // "stat /tmp/operator-sdk-ready".
    39  func NewFileReady() Ready {
    40  	return fileReady{}
    41  }
    42  
    43  type fileReady struct{}
    44  
    45  // Set creates a file on disk whose presense can be used by a readiness probe
    46  // to determine that the operator is ready.
    47  func (r fileReady) Set() error {
    48  	f, err := os.Create(FileName)
    49  	if err != nil {
    50  		return err
    51  	}
    52  	return f.Close()
    53  }
    54  
    55  // Unset removes the file on disk that was created by Set().
    56  func (r fileReady) Unset() error {
    57  	return os.Remove(FileName)
    58  }