github.com/giantswarm/apiextensions/v2@v2.6.2/pkg/apis/provider/v1alpha1/status_funcs.go (about)

     1  package v1alpha1
     2  
     3  import (
     4  	"sort"
     5  	"time"
     6  
     7  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
     8  )
     9  
    10  func NewStatusClusterNode(name, version string, labels map[string]string) StatusClusterNode {
    11  	return StatusClusterNode{
    12  		Labels:             labels,
    13  		LastTransitionTime: metav1.Now(),
    14  		Name:               name,
    15  		Version:            version,
    16  	}
    17  }
    18  
    19  func (s StatusCluster) GetCreatedCondition() StatusClusterCondition {
    20  	return getCondition(s.Conditions, StatusClusterStatusTrue, StatusClusterTypeCreated)
    21  }
    22  
    23  func (s StatusCluster) GetCreatingCondition() StatusClusterCondition {
    24  	return getCondition(s.Conditions, StatusClusterStatusTrue, StatusClusterTypeCreating)
    25  }
    26  
    27  func (s StatusCluster) GetDeletedCondition() StatusClusterCondition {
    28  	return getCondition(s.Conditions, StatusClusterStatusTrue, StatusClusterTypeDeleted)
    29  }
    30  
    31  func (s StatusCluster) GetDeletingCondition() StatusClusterCondition {
    32  	return getCondition(s.Conditions, StatusClusterStatusTrue, StatusClusterTypeDeleting)
    33  }
    34  
    35  func (s StatusCluster) GetUpdatedCondition() StatusClusterCondition {
    36  	return getCondition(s.Conditions, StatusClusterStatusTrue, StatusClusterTypeUpdated)
    37  }
    38  
    39  func (s StatusCluster) GetUpdatingCondition() StatusClusterCondition {
    40  	return getCondition(s.Conditions, StatusClusterStatusTrue, StatusClusterTypeUpdating)
    41  }
    42  
    43  func (s StatusCluster) HasCreatedCondition() bool {
    44  	return hasCondition(s.Conditions, StatusClusterStatusTrue, StatusClusterTypeCreated)
    45  }
    46  
    47  func (s StatusCluster) HasCreatingCondition() bool {
    48  	return hasCondition(s.Conditions, StatusClusterStatusTrue, StatusClusterTypeCreating)
    49  }
    50  
    51  func (s StatusCluster) HasDeletedCondition() bool {
    52  	return hasCondition(s.Conditions, StatusClusterStatusTrue, StatusClusterTypeDeleted)
    53  }
    54  
    55  func (s StatusCluster) HasDeletingCondition() bool {
    56  	return hasCondition(s.Conditions, StatusClusterStatusTrue, StatusClusterTypeDeleting)
    57  }
    58  
    59  func (s StatusCluster) HasUpdatedCondition() bool {
    60  	return hasCondition(s.Conditions, StatusClusterStatusTrue, StatusClusterTypeUpdated)
    61  }
    62  
    63  func (s StatusCluster) HasUpdatingCondition() bool {
    64  	return hasCondition(s.Conditions, StatusClusterStatusTrue, StatusClusterTypeUpdating)
    65  }
    66  
    67  func (s StatusCluster) HasVersion(semver string) bool {
    68  	return hasVersion(s.Versions, semver)
    69  }
    70  
    71  func (s StatusCluster) LatestVersion() string {
    72  	if len(s.Versions) == 0 {
    73  		return ""
    74  	}
    75  
    76  	latest := s.Versions[0]
    77  
    78  	for _, v := range s.Versions {
    79  		if latest.LastTransitionTime.Before(&v.LastTransitionTime) || latest.Date.Before(&v.Date) {
    80  			latest = v
    81  		}
    82  	}
    83  
    84  	return latest.Semver
    85  }
    86  
    87  func (s StatusCluster) WithCreatedCondition() []StatusClusterCondition {
    88  	return withCondition(s.Conditions, StatusClusterTypeCreating, StatusClusterTypeCreated, StatusClusterStatusTrue, time.Now())
    89  }
    90  
    91  func (s StatusCluster) WithCreatingCondition() []StatusClusterCondition {
    92  	return withCondition(s.Conditions, StatusClusterTypeCreated, StatusClusterTypeCreating, StatusClusterStatusTrue, time.Now())
    93  }
    94  
    95  func (s StatusCluster) WithDeletedCondition() []StatusClusterCondition {
    96  	return withCondition(s.Conditions, StatusClusterTypeDeleting, StatusClusterTypeDeleted, StatusClusterStatusTrue, time.Now())
    97  }
    98  
    99  func (s StatusCluster) WithDeletingCondition() []StatusClusterCondition {
   100  	return withCondition(s.Conditions, StatusClusterTypeDeleted, StatusClusterTypeDeleting, StatusClusterStatusTrue, time.Now())
   101  }
   102  
   103  func (s StatusCluster) WithNewVersion(version string) []StatusClusterVersion {
   104  	newVersion := StatusClusterVersion{
   105  		LastTransitionTime: metav1.Now(),
   106  		Semver:             version,
   107  	}
   108  
   109  	return withVersion(s.Versions, newVersion, ClusterVersionLimit)
   110  }
   111  
   112  func (s StatusCluster) WithUpdatedCondition() []StatusClusterCondition {
   113  	return withCondition(s.Conditions, StatusClusterTypeUpdating, StatusClusterTypeUpdated, StatusClusterStatusTrue, time.Now())
   114  }
   115  
   116  func (s StatusCluster) WithUpdatingCondition() []StatusClusterCondition {
   117  	return withCondition(s.Conditions, StatusClusterTypeUpdated, StatusClusterTypeUpdating, StatusClusterStatusTrue, time.Now())
   118  }
   119  
   120  func getCondition(conditions []StatusClusterCondition, s string, t string) StatusClusterCondition {
   121  	for _, c := range conditions {
   122  		if c.Status == s && c.Type == t {
   123  			return c
   124  		}
   125  	}
   126  
   127  	return StatusClusterCondition{}
   128  }
   129  
   130  func hasCondition(conditions []StatusClusterCondition, s string, t string) bool {
   131  	for _, c := range conditions {
   132  		if c.Status == s && c.Type == t {
   133  			return true
   134  		}
   135  	}
   136  
   137  	return false
   138  }
   139  
   140  func hasVersion(versions []StatusClusterVersion, search string) bool {
   141  	for _, v := range versions {
   142  		if v.Semver == search {
   143  			return true
   144  		}
   145  	}
   146  
   147  	return false
   148  }
   149  
   150  func withCondition(conditions []StatusClusterCondition, search string, replace string, status string, t time.Time) []StatusClusterCondition {
   151  	newConditions := []StatusClusterCondition{
   152  		{
   153  			LastTransitionTime: metav1.Time{Time: t},
   154  			Status:             status,
   155  			Type:               replace,
   156  		},
   157  	}
   158  
   159  	for _, c := range conditions {
   160  		if c.Type == search {
   161  			continue
   162  		}
   163  
   164  		newConditions = append(newConditions, c)
   165  	}
   166  
   167  	return newConditions
   168  }
   169  
   170  // withVersion computes a list of version history using the given list and new
   171  // version structure to append. withVersion also limits total amount of elements
   172  // in the list by cutting off the tail with respect to the limit parameter.
   173  func withVersion(versions []StatusClusterVersion, version StatusClusterVersion, limit int) []StatusClusterVersion {
   174  	if hasVersion(versions, version.Semver) {
   175  		return versions
   176  	}
   177  
   178  	var newVersions []StatusClusterVersion
   179  
   180  	start := 0
   181  	if len(versions) >= limit {
   182  		start = len(versions) - limit + 1
   183  	}
   184  
   185  	sort.Sort(sortClusterStatusVersionsByDate(versions))
   186  
   187  	for i := start; i < len(versions); i++ {
   188  		newVersions = append(newVersions, versions[i])
   189  	}
   190  
   191  	newVersions = append(newVersions, version)
   192  
   193  	return newVersions
   194  }