github.com/kyma-project/kyma-environment-broker@v0.0.1/common/orchestration/ext.go (about)

     1  package orchestration
     2  
     3  import (
     4  	"time"
     5  )
     6  
     7  // Runtime is the data type which captures the needed runtime specific attributes to perform orchestrations on a given runtime.
     8  type Runtime struct {
     9  	InstanceID      string `json:"instanceId,omitempty"`
    10  	RuntimeID       string `json:"runtimeId"`
    11  	GlobalAccountID string `json:"globalAccountId"`
    12  	SubAccountID    string `json:"subaccountId"`
    13  	// The corresponding shoot cluster's .metadata.name value
    14  	ShootName string `json:"shootName"`
    15  	// The corresponding shoot cluster's .spec.maintenance.timeWindow.Begin value, which is in in "HHMMSS+[HHMM TZ]" format, e.g. "040000+0000"
    16  	MaintenanceWindowBegin time.Time `json:"maintenanceWindowBegin"`
    17  	// The corresponding shoot cluster's .spec.maintenance.timeWindow.End value, which is in "HHMMSS+[HHMM TZ]" format, e.g. "040000+0000"
    18  	MaintenanceWindowEnd time.Time `json:"maintenanceWindowEnd"`
    19  	MaintenanceDays      []string  `json:"maintenanceDays"`
    20  	Plan                 string    `json:"plan"`
    21  	Region               string    `json:"region"`
    22  }
    23  
    24  // RuntimeOperation holds information about operation performed on a runtime
    25  type RuntimeOperation struct {
    26  	Runtime `json:""`
    27  	ID      string `json:"-"`
    28  	DryRun  bool   `json:"dryRun"`
    29  	//customer notification
    30  	Notification      bool                  `json:"notification,omitempty"`
    31  	NotificationState NotificationStateType `json:"notificationstate,omitempty"`
    32  }
    33  
    34  // RuntimeResolver given an input slice of target specs to include and exclude, resolves and returns a list of unique Runtime objects.
    35  //
    36  //go:generate mockery --name=RuntimeResolver --output=automock --outpkg=automock --case=underscore
    37  type RuntimeResolver interface {
    38  	Resolve(targets TargetSpec) ([]Runtime, error)
    39  }
    40  
    41  // OperationExecutor implements methods to perform the operation corresponding to a Runtime.
    42  type OperationExecutor interface {
    43  	Execute(operationID string) (time.Duration, error)
    44  	Reschedule(operationID string, maintenanceWindowBegin, maintenanceWindowEnd time.Time) error
    45  }
    46  
    47  // Strategy interface encapsulates the strategy how the orchestration is performed.
    48  //
    49  //go:generate mockery --name=Strategy --output=automock --outpkg=automock --case=underscore
    50  type Strategy interface {
    51  	// Execute invokes OperationExecutor's Execute(operationID string) method for each operation according to the encapsulated strategy.
    52  	// The strategy is executed asynchronously. Successful call to the function returns a unique identifier, which can be used in a subsequent call to Wait().
    53  	Execute(operations []RuntimeOperation, strategySpec StrategySpec) (string, error)
    54  	// Wait blocks and waits until the execution with the given ID is finished.
    55  	Wait(executionID string)
    56  	// Cancel shutdowns a given execution.
    57  	Cancel(executionID string)
    58  	// Insert operations into the delaying queue of a given execution ID
    59  	Insert(execID string, operations []RuntimeOperation, strategySpec StrategySpec) error
    60  	// SpeedUp makes the retries speedFactor times faster, used for unit testing
    61  	SpeedUp(speedFactor int)
    62  }
    63  
    64  func ConvertSliceOfDaysToMap(days []string) map[time.Weekday]bool {
    65  	m := make(map[time.Weekday]bool)
    66  	for _, day := range days {
    67  		switch day {
    68  		case "Sun":
    69  			m[time.Sunday] = true
    70  		case "Mon":
    71  			m[time.Monday] = true
    72  		case "Tue":
    73  			m[time.Tuesday] = true
    74  		case "Wed":
    75  			m[time.Wednesday] = true
    76  		case "Thu":
    77  			m[time.Thursday] = true
    78  		case "Fri":
    79  			m[time.Friday] = true
    80  		case "Sat":
    81  			m[time.Saturday] = true
    82  		}
    83  	}
    84  	return m
    85  }
    86  
    87  func FirstAvailableDayDiff(currentDay time.Weekday, availableDays map[time.Weekday]bool) int {
    88  	availableDay := currentDay
    89  	for i := time.Weekday(0); i < 7; i++ {
    90  		nextDay := (currentDay + i) % 7
    91  		_, isAvailable := availableDays[nextDay]
    92  		if isAvailable {
    93  			availableDay = nextDay
    94  			break
    95  		}
    96  	}
    97  	diff := int(7-currentDay+availableDay) % 7
    98  
    99  	return diff
   100  }
   101  
   102  func NextAvailableDayDiff(currentDay time.Weekday, availableDays map[time.Weekday]bool) int {
   103  	availableDay := currentDay
   104  	for i := time.Weekday(0); i < 7; i++ {
   105  		nextDay := (currentDay + i + 1) % 7
   106  		_, isAvailable := availableDays[nextDay]
   107  		if isAvailable {
   108  			availableDay = nextDay
   109  			break
   110  		}
   111  	}
   112  	diff := int(7-currentDay+availableDay) % 7
   113  	if diff == 0 {
   114  		diff = 7
   115  	}
   116  
   117  	return diff
   118  }
   119  
   120  type NotificationStateType string
   121  
   122  const (
   123  	NotificationPending   NotificationStateType = "pending"
   124  	NotificationCreated   NotificationStateType = "created"
   125  	NotificationCancelled NotificationStateType = "cancelled"
   126  )