...

Source file src/sigs.k8s.io/cli-utils/pkg/apply/taskrunner/condition.go

Documentation: sigs.k8s.io/cli-utils/pkg/apply/taskrunner

     1  // Copyright 2020 The Kubernetes Authors.
     2  // SPDX-License-Identifier: Apache-2.0
     3  
     4  package taskrunner
     5  
     6  import (
     7  	"sigs.k8s.io/cli-utils/pkg/kstatus/status"
     8  	"sigs.k8s.io/cli-utils/pkg/object"
     9  )
    10  
    11  // Condition is a type that defines the types of conditions
    12  // which a WaitTask can use.
    13  type Condition string
    14  
    15  const (
    16  	// AllCurrent Condition means all the provided resources
    17  	// has reached (and remains in) the Current status.
    18  	AllCurrent Condition = "AllCurrent"
    19  
    20  	// AllNotFound Condition means all the provided resources
    21  	// has reached the NotFound status, i.e. they are all deleted
    22  	// from the cluster.
    23  	AllNotFound Condition = "AllNotFound"
    24  )
    25  
    26  // Meets returns true if the provided status meets the condition and
    27  // false if it does not.
    28  func (c Condition) Meets(s status.Status) bool {
    29  	switch c {
    30  	case AllCurrent:
    31  		return s == status.CurrentStatus
    32  	case AllNotFound:
    33  		return s == status.NotFoundStatus
    34  	default:
    35  		return false
    36  	}
    37  }
    38  
    39  // conditionMet tests whether the provided Condition holds true for
    40  // all resources in the list, according to the ResourceCache.
    41  // Resources in the cache older that the applied generation are non-matches.
    42  func conditionMet(taskContext *TaskContext, ids object.ObjMetadataSet, c Condition) bool {
    43  	switch c {
    44  	case AllCurrent:
    45  		return allMatchStatus(taskContext, ids, status.CurrentStatus)
    46  	case AllNotFound:
    47  		return allMatchStatus(taskContext, ids, status.NotFoundStatus)
    48  	default:
    49  		return noneMatchStatus(taskContext, ids, status.UnknownStatus)
    50  	}
    51  }
    52  
    53  // allMatchStatus checks whether all of the resources provided have the provided status.
    54  // Resources with older generations are considered non-matching.
    55  func allMatchStatus(taskContext *TaskContext, ids object.ObjMetadataSet, s status.Status) bool {
    56  	for _, id := range ids {
    57  		cached := taskContext.ResourceCache().Get(id)
    58  		if cached.Status != s {
    59  			return false
    60  		}
    61  
    62  		applyGen, _ := taskContext.InventoryManager().AppliedGeneration(id) // generation at apply time
    63  		cachedGen := int64(0)
    64  		if cached.Resource != nil {
    65  			cachedGen = cached.Resource.GetGeneration()
    66  		}
    67  		if cachedGen < applyGen {
    68  			// cache too old
    69  			return false
    70  		}
    71  	}
    72  	return true
    73  }
    74  
    75  // allMatchStatus checks whether none of the resources provided have the provided status.
    76  // Resources with older generations are considered matching.
    77  func noneMatchStatus(taskContext *TaskContext, ids object.ObjMetadataSet, s status.Status) bool {
    78  	for _, id := range ids {
    79  		cached := taskContext.ResourceCache().Get(id)
    80  		if cached.Status == s {
    81  			return false
    82  		}
    83  
    84  		applyGen, _ := taskContext.InventoryManager().AppliedGeneration(id) // generation at apply time
    85  		cachedGen := int64(0)
    86  		if cached.Resource != nil {
    87  			cachedGen = cached.Resource.GetGeneration()
    88  		}
    89  		if cachedGen < applyGen {
    90  			// cache too old
    91  			return false
    92  		}
    93  	}
    94  	return true
    95  }
    96  

View as plain text