func applyRetentionPolicy(policy v1alpha1.RetentionPolicy, volumeSnapshots VolumeSnapshots, namespace string, vsClient vs_cs.Interface) error {

	// sorts the VolumeSnapshots according to CreationTimeStamp
	sort.Sort(VolumeSnapshots(volumeSnapshots))

	if !isPolicyEmpty(policy) {
		return nil
	}

	var buckets = [6]struct {
		Count     int64
		LastAdded func(d time.Time, nr int) int
		Last      int
	}{
		{policy.KeepLast, always, -1},
		{policy.KeepHourly, ymdh, -1},
		{policy.KeepDaily, ymd, -1},
		{policy.KeepWeekly, yw, -1},
		{policy.KeepMonthly, ym, -1},
		{policy.KeepYearly, y, -1},
	}

	var kept, removed VolumeSnapshots
	for nr, vs := range volumeSnapshots {
		var keepSnap bool
		// keep VolumeSnapshot that are matched with the policy
		for i, b := range buckets {
			if b.Count > 0 {
				val := b.LastAdded(vs.VolumeSnap.CreationTimestamp.Time, nr)
				if val != b.Last {
					keepSnap = true
					buckets[i].Last = val
					buckets[i].Count--
				}
			}
		}

		if keepSnap {
			kept = append(kept, vs)
		} else {
			removed = append(removed, vs)
		}
	}

	for _, vs := range removed {
		err := vsClient.SnapshotV1beta1().VolumeSnapshots(namespace).Delete(vs.VolumeSnap.Name, &v1.DeleteOptions{})
		if err != nil {
			if kerr.IsNotFound(err) {
				return nil
			}
			return err
		}
	}

	log.Infof("VolumeSnapshot kept: %d removed: %d", len(kept), len(removed))
	return nil
}
// ApplyRetentionPolicy do the following steps:
// 1. sorts all the VolumeSnapshot according to CreationTimeStamp.
// 2. then list that are to be kept and removed according to the policy.
// 3. remove VolumeSnapshot that are not necessary according to RetentionPolicy
ApplyRetentionPolicy do the following steps:
1. sorts all the VolumeSnapshot according to CreationTimeStamp.
2. then list that are to be kept and removed according to the policy.
3. remove VolumeSnapshot that are not necessary according to RetentionPolicy
// ApplyRetentionPolicy do the following steps : // 1 . sorts all the VolumeSnapshot according to CreationTimeStamp . // 2 . then list that are to be kept and removed according to the policy . // 3 . remove VolumeSnapshot that are not necessary according to RetentionPolicy
func applyRetentionPolicy(policy v1alpha1.RetentionPolicy, volumeSnapshots VolumeSnapshots, namespace string, vsClient vs_cs.Interface) error {
	sort.Sort(VolumeSnapshots(volumeSnapshots))
	if !isPolicyEmpty(policy) {
		return nil
	}
	var buckets = [6]struct {
		Count     int64
		LastAdded func(d time.Time, nr int) int
		Last      int
	}{
		{policy.KeepLast, always, -1},
		{policy.KeepHourly, ymdh, -1},
		{policy.KeepDaily, ymd, -1},
		{policy.KeepWeekly, yw, -1},
		{policy.KeepMonthly, ym, -1},
		{policy.KeepYearly, y, -1},
	}
	var kept, removed VolumeSnapshots
	for nr, vs := range volumeSnapshots {
		var keepSnap bool
		for i, b := range buckets {
			if b.Count > 0 {
				val := b.LastAdded(vs.VolumeSnap.CreationTimestamp.Time, nr)
				if val != b.Last {
					keepSnap = true
					buckets[i].Last = val
					buckets[i].Count--
				}
			}
		}
		if keepSnap {
			kept = append(kept, vs)
		} else {
			removed = append(removed, vs)
		}
	}
	for _, vs := range removed {
		err := vsClient.SnapshotV1beta1().VolumeSnapshots(namespace).Delete(vs.VolumeSnap.Name, &v1.DeleteOptions{})
		if err != nil {
			if kerr.IsNotFound(err) {
				return nil
			}
			return err
		}
	}
	log.Infof("VolumeSnapshot kept: %d removed: %d", len(kept), len(removed))
	return nil
}
func applyRetentionPolicy ( policy v1alpha1 . RetentionPolicy , volumeSnapshots VolumeSnapshots , namespace string , vsClient vs_cs . Interface ) error { sort . Sort ( VolumeSnapshots ( volumeSnapshots ) ) if ! isPolicyEmpty ( policy ) { return nil } var buckets = [ 6 ] struct { Count int64 LastAdded func ( d time . Time , nr int ) int Last int } { { policy . KeepLast , always , - 1 } , { policy . KeepHourly , ymdh , - 1 } , { policy . KeepDaily , ymd , - 1 } , { policy . KeepWeekly , yw , - 1 } , { policy . KeepMonthly , ym , - 1 } , { policy . KeepYearly , y , - 1 } , } var kept , removed VolumeSnapshots for nr , vs := range volumeSnapshots { var keepSnap bool for i , b := range buckets { if b . Count > 0 { val := b . LastAdded ( vs . VolumeSnap . CreationTimestamp . Time , nr ) if val != b . Last { keepSnap = true buckets [ i ] . Last = val buckets [ i ] . Count -- } } } if keepSnap { kept = append ( kept , vs ) } else { removed = append ( removed , vs ) } } for _ , vs := range removed { err := vsClient . SnapshotV1beta1 ( ) . VolumeSnapshots ( namespace ) . Delete ( vs . VolumeSnap . Name , & v1 . DeleteOptions { } ) if err != nil { if kerr . IsNotFound ( err ) { return nil } return err } } log . Infof ( " " , len ( kept ) , len ( removed ) ) return nil }
ApplyRetentionPolicy do the following steps:
1. sorts all the VolumeSnapshot according to CreationTimeStamp.
ApplyRetentionPolicy do the following steps : 1 . sorts all the VolumeSnapshot according to CreationTimeStamp .
// sorts the VolumeSnapshots according to CreationTimeStamp // keep VolumeSnapshot that are matched with the policy