github.com/oam-dev/kubevela@v1.9.11/pkg/resourcetracker/utils.go (about)

     1  /*
     2  Copyright 2021 The KubeVela Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8  	http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package resourcetracker
    18  
    19  import (
    20  	"sort"
    21  
    22  	"github.com/oam-dev/kubevela/apis/core.oam.dev/v1beta1"
    23  )
    24  
    25  type sortResourceTrackerByVersion struct {
    26  	rts     []*v1beta1.ResourceTracker
    27  	reverse bool
    28  }
    29  
    30  func (s sortResourceTrackerByVersion) Len() int { return len(s.rts) }
    31  
    32  func (s sortResourceTrackerByVersion) Swap(i, j int) { s.rts[i], s.rts[j] = s.rts[j], s.rts[i] }
    33  
    34  func (s sortResourceTrackerByVersion) Less(i, j int) bool {
    35  	if s.reverse {
    36  		return s.rts[i].Spec.ApplicationGeneration > s.rts[j].Spec.ApplicationGeneration
    37  	}
    38  	return s.rts[i].Spec.ApplicationGeneration < s.rts[j].Spec.ApplicationGeneration
    39  }
    40  
    41  // SortResourceTrackersByVersion sort resourceTrackers by version
    42  func SortResourceTrackersByVersion(rts []*v1beta1.ResourceTracker, descending bool) []*v1beta1.ResourceTracker {
    43  	s := sortResourceTrackerByVersion{rts: rts, reverse: descending}
    44  	sort.Sort(s)
    45  	return s.rts
    46  }