github.com/lablabs/operator-sdk@v0.8.2/pkg/ansible/controller/status/utils.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 status
    16  
    17  import (
    18  	"k8s.io/api/core/v1"
    19  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    20  )
    21  
    22  const (
    23  	// RunningReason - Condition is running
    24  	RunningReason = "Running"
    25  	// SuccessfulReason - Condition is running due to reconcile being successful
    26  	SuccessfulReason = "Successful"
    27  	// FailedReason - Condition is failed due to ansible failure
    28  	FailedReason = "Failed"
    29  	// UnknownFailedReason - Condition is unknown
    30  	UnknownFailedReason = "Unknown"
    31  )
    32  
    33  const (
    34  	// RunningMessage - message for running reason.
    35  	RunningMessage = "Running reconciliation"
    36  	// SuccessfulMessage - message for successful reason.
    37  	SuccessfulMessage = "Awaiting next reconciliation"
    38  )
    39  
    40  // NewCondition -  condition
    41  func NewCondition(condType ConditionType, status v1.ConditionStatus, ansibleResult *AnsibleResult, reason, message string) *Condition {
    42  	return &Condition{
    43  		Type:               condType,
    44  		Status:             status,
    45  		LastTransitionTime: metav1.Now(),
    46  		Reason:             reason,
    47  		Message:            message,
    48  		AnsibleResult:      ansibleResult,
    49  	}
    50  }
    51  
    52  // GetCondition returns the condition with the provided type.
    53  func GetCondition(status Status, condType ConditionType) *Condition {
    54  	for i := range status.Conditions {
    55  		c := status.Conditions[i]
    56  		if c.Type == condType {
    57  			return &c
    58  		}
    59  	}
    60  	return nil
    61  }
    62  
    63  // SetCondition updates the scheduledReport to include the provided condition. If the condition that
    64  // we are about to add already exists and has the same status and reason then we are not going to update.
    65  func SetCondition(status *Status, condition Condition) {
    66  	currentCond := GetCondition(*status, condition.Type)
    67  	if currentCond != nil && currentCond.Status == condition.Status && currentCond.Reason == condition.Reason {
    68  		return
    69  	}
    70  	// Do not update lastTransitionTime if the status of the condition doesn't change.
    71  	if currentCond != nil && currentCond.Status == condition.Status {
    72  		condition.LastTransitionTime = currentCond.LastTransitionTime
    73  	}
    74  	newConditions := filterOutCondition(status.Conditions, condition.Type)
    75  	status.Conditions = append(newConditions, condition)
    76  }
    77  
    78  // RemoveCondition removes the scheduledReport condition with the provided type.
    79  func RemoveCondition(status *Status, condType ConditionType) {
    80  	status.Conditions = filterOutCondition(status.Conditions, condType)
    81  }
    82  
    83  // filterOutCondition returns a new slice of scheduledReport conditions without conditions with the provided type.
    84  func filterOutCondition(conditions []Condition, condType ConditionType) []Condition {
    85  	var newConditions []Condition
    86  	for _, c := range conditions {
    87  		if c.Type == condType {
    88  			continue
    89  		}
    90  		newConditions = append(newConditions, c)
    91  	}
    92  	return newConditions
    93  }