github.com/kyma-project/kyma-environment-broker@v0.0.1/internal/broker/response_labels.go (about)

     1  package broker
     2  
     3  import (
     4  	"fmt"
     5  	"math"
     6  	"strings"
     7  	"time"
     8  
     9  	"github.com/kyma-project/kyma-environment-broker/internal"
    10  )
    11  
    12  const (
    13  	kubeconfigURLKey      = "KubeconfigURL"
    14  	trialExpiryDetailsKey = "Trial expiration details"
    15  	trialDocsKey          = "Trial documentation"
    16  	expireDuration        = time.Hour * 24 * 14
    17  	notExpiredInfoFormat  = "Your cluster expires %s."
    18  	expiredInfoFormat     = "Your cluster has expired. It is not operational and the link to the Dashboard is no longer valid." +
    19  		" To continue using Kyma, you must create a new cluster. To learn how, follow the link to the trial documentation."
    20  )
    21  
    22  func ResponseLabels(op internal.ProvisioningOperation, instance internal.Instance, brokerURL string, enableKubeconfigLabel bool) map[string]string {
    23  	brokerURL = strings.TrimLeft(brokerURL, "https://")
    24  	brokerURL = strings.TrimLeft(brokerURL, "http://")
    25  
    26  	responseLabels := make(map[string]string, 0)
    27  	responseLabels["Name"] = op.ProvisioningParameters.Parameters.Name
    28  	if enableKubeconfigLabel && !IsOwnClusterPlan(instance.ServicePlanID) {
    29  		responseLabels[kubeconfigURLKey] = fmt.Sprintf("https://%s/kubeconfig/%s", brokerURL, instance.InstanceID)
    30  	}
    31  
    32  	return responseLabels
    33  }
    34  
    35  func ResponseLabelsWithExpirationInfo(op internal.ProvisioningOperation, instance internal.Instance, brokerURL string, trialDocsURL string, enableKubeconfigLabel bool) map[string]string {
    36  	labels := ResponseLabels(op, instance, brokerURL, enableKubeconfigLabel)
    37  
    38  	expireTime := instance.CreatedAt.Add(expireDuration)
    39  	hoursLeft := calculateHoursLeft(expireTime)
    40  	if instance.IsExpired() {
    41  		delete(labels, kubeconfigURLKey)
    42  		labels[trialExpiryDetailsKey] = expiredInfoFormat
    43  		labels[trialDocsKey] = trialDocsURL
    44  	} else {
    45  		if hoursLeft < 0 {
    46  			hoursLeft = 0
    47  		}
    48  		daysLeft := math.Round(hoursLeft / 24)
    49  		switch {
    50  		case daysLeft == 0:
    51  			labels[trialExpiryDetailsKey] = fmt.Sprintf(notExpiredInfoFormat, "today")
    52  		case daysLeft == 1:
    53  			labels[trialExpiryDetailsKey] = fmt.Sprintf(notExpiredInfoFormat, "tomorrow")
    54  		default:
    55  			daysLeftNotice := fmt.Sprintf("in %2.f days", daysLeft)
    56  			labels[trialExpiryDetailsKey] = fmt.Sprintf(notExpiredInfoFormat, daysLeftNotice)
    57  		}
    58  	}
    59  
    60  	return labels
    61  }
    62  
    63  func calculateHoursLeft(expireTime time.Time) float64 {
    64  	timeLeftUntilExpire := time.Until(expireTime)
    65  	timeLeftUntilExpireRoundedToHours := timeLeftUntilExpire.Round(time.Hour)
    66  	return timeLeftUntilExpireRoundedToHours.Hours()
    67  }