func SetAccountCondition(
	conditions []awsv1alpha1.AccountCondition,
	conditionType awsv1alpha1.AccountConditionType,
	status corev1.ConditionStatus,
	reason string,
	message string,
	updateConditionCheck UpdateConditionCheck,
	ccs bool,
) []awsv1alpha1.AccountCondition {
	now := metav1.Now()
	existingCondition := FindAccountCondition(conditions, conditionType)
	if existingCondition == nil {
		if status == corev1.ConditionTrue {
			conditions = append(
				conditions,
				awsv1alpha1.AccountCondition{
					Type:               conditionType,
					Status:             status,
					Reason:             reason,
					Message:            message,
					LastTransitionTime: now,
					LastProbeTime:      now,
				},
			)
		}
	} else {
		if shouldUpdateCondition(
			existingCondition.Status, existingCondition.Reason, existingCondition.Message,
			status, reason, message,
			updateConditionCheck,
		) {
			if existingCondition.Status != status {
				existingCondition.LastTransitionTime = now
			}
			existingCondition.Status = status
			existingCondition.Reason = reason
			existingCondition.Message = message
		}
		existingCondition.LastProbeTime = now
	}
	if conditionType == awsv1alpha1.AccountReady {
		creatingCondition := FindAccountCondition(conditions, awsv1alpha1.AccountCreating)
		if creatingCondition != nil {
			readyDuration := now.Sub(creatingCondition.LastProbeTime.Time)
			localmetrics.Collector.SetAccountReadyDuration(ccs, readyDuration.Seconds())
		}
	}
	return conditions
}
SetAccountCondition sets a condition on a Account resource's status
TODO: This should probably be handled within the Account Struct
package utils
import (
	"time"
	awsv1alpha1 "github.com/openshift/aws-account-operator/pkg/apis/aws/v1alpha1"
	"github.com/openshift/aws-account-operator/pkg/localmetrics"
	corev1 "k8s.io/api/core/v1"
	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
type UpdateConditionCheck func(oldReason, oldMessage, newReason, newMessage string) bool
func UpdateConditionAlways(_, _, _, _ string) bool {
	return true
}
func UpdateConditionNever(_, _, _, _ string) bool {
	return false
}
func UpdateConditionIfReasonOrMessageChange(oldReason, oldMessage, newReason, newMessage string) bool {
	return oldReason != newReason ||
		oldMessage != newMessage
}
func shouldUpdateCondition(
	oldStatus corev1.ConditionStatus, oldReason, oldMessage string,
	newStatus corev1.ConditionStatus, newReason, newMessage string,
	updateConditionCheck UpdateConditionCheck,
) bool {
	if oldStatus != newStatus {
		return true
	}
	return updateConditionCheck(oldReason, oldMessage, newReason, newMessage)
}
func SetAccountClaimCondition(
	conditions []awsv1alpha1.AccountClaimCondition,
	conditionType awsv1alpha1.AccountClaimConditionType,
	status corev1.ConditionStatus,
	reason string,
	message string,
	updateConditionCheck UpdateConditionCheck,
	ccs bool,
) []awsv1alpha1.AccountClaimCondition {
	now := metav1.Now()
	existingCondition := FindAccountClaimCondition(conditions, conditionType)
	if existingCondition == nil {
		if status == corev1.ConditionTrue {
			conditions = append(
				conditions,
				awsv1alpha1.AccountClaimCondition{
					Type:               conditionType,
					Status:             status,
					Reason:             reason,
					Message:            message,
					LastTransitionTime: now,
					LastProbeTime:      now,
				},
			)
		}
	} else {
		if shouldUpdateCondition(
			existingCondition.Status, existingCondition.Reason, existingCondition.Message,
			status, reason, message,
			updateConditionCheck,
		) {
			if existingCondition.Status != status {
				existingCondition.LastTransitionTime = now
			}
			existingCondition.Status = status
			existingCondition.Reason = reason
			existingCondition.Message = message
			existingCondition.LastProbeTime = now
		}
	}
	if conditionType == awsv1alpha1.AccountClaimed {
		unclaimedCondition := FindAccountClaimCondition(conditions, awsv1alpha1.AccountUnclaimed)
		if unclaimedCondition != nil {
			readyDuration := now.Sub(unclaimedCondition.LastProbeTime.Time)
			localmetrics.Collector.SetAccountClaimReadyDuration(ccs, readyDuration.Seconds())
		}
	}
	return conditions
}
func FindAccountClaimCondition(conditions []awsv1alpha1.AccountClaimCondition, conditionType awsv1alpha1.AccountClaimConditionType) *awsv1alpha1.AccountClaimCondition {
	for i, condition := range conditions {
		if condition.Type == conditionType {
			return &conditions[i]
		}
	}
	return nil
}
func CreationConditionOlderThan(account awsv1alpha1.Account, duration time.Duration) bool {
	createCondition := FindAccountCondition(account.Status.Conditions, awsv1alpha1.AccountCreating)
	return time.Since(createCondition.LastProbeTime.Time) > duration
}