...

Source file src/github.com/cert-manager/issuer-lib/controllers/predicates.go

Documentation: github.com/cert-manager/issuer-lib/controllers

     1  /*
     2  Copyright 2023 The cert-manager 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 controllers
    18  
    19  import (
    20  	"reflect"
    21  
    22  	cmutil "github.com/cert-manager/cert-manager/pkg/api/util"
    23  	cmapi "github.com/cert-manager/cert-manager/pkg/apis/certmanager/v1"
    24  	certificatesv1 "k8s.io/api/certificates/v1"
    25  	"sigs.k8s.io/controller-runtime/pkg/event"
    26  	"sigs.k8s.io/controller-runtime/pkg/predicate"
    27  
    28  	"github.com/cert-manager/issuer-lib/api/v1alpha1"
    29  	"github.com/cert-manager/issuer-lib/conditions"
    30  )
    31  
    32  // This predicate is used to indicate when a CertificateRequest event should
    33  // trigger a reconciliation of itself.
    34  //
    35  // In these cases we want to trigger:
    36  // - an annotation changed/ was added or removed
    37  // - a status condition was added or removed
    38  // - a status condition that does not have type == Ready was changed (aka. other Status value)
    39  type CertificateRequestPredicate struct {
    40  	predicate.Funcs
    41  }
    42  
    43  func (CertificateRequestPredicate) Update(e event.UpdateEvent) bool {
    44  	if e.ObjectOld == nil || e.ObjectNew == nil {
    45  		// a reference object is missing, just reconcile to be safe
    46  		return true
    47  	}
    48  
    49  	oldCr, oldOk := e.ObjectOld.(*cmapi.CertificateRequest)
    50  	newCr, newOk := e.ObjectNew.(*cmapi.CertificateRequest)
    51  	if !oldOk || !newOk {
    52  		// a reference object is invalid, just reconcile to be safe
    53  		return true
    54  	}
    55  
    56  	if len(oldCr.Status.Conditions) != len(newCr.Status.Conditions) {
    57  		// Fail fast in case we are certain a non-ready condition was added or removed.
    58  		return true
    59  	}
    60  
    61  	for _, oldCond := range oldCr.Status.Conditions {
    62  		if oldCond.Type == cmapi.CertificateRequestConditionReady {
    63  			// we can skip the Ready conditions
    64  			continue
    65  		}
    66  
    67  		newCond := cmutil.GetCertificateRequestCondition(newCr, oldCond.Type)
    68  		if (newCond == nil) || (oldCond.Status != newCond.Status) {
    69  			// we found a missing or changed condition
    70  			return true
    71  		}
    72  	}
    73  
    74  	// check if any of the annotations changed
    75  	return !reflect.DeepEqual(e.ObjectNew.GetAnnotations(), e.ObjectOld.GetAnnotations())
    76  }
    77  
    78  // This predicate is used to indicate when a CertificateSigningRequest event should
    79  // trigger a reconciliation of itself.
    80  //
    81  // In these cases we want to trigger:
    82  // - an annotation changed/ was added or removed
    83  // - a status condition was added or removed
    84  // - a status condition was changed
    85  type CertificateSigningRequestPredicate struct {
    86  	predicate.Funcs
    87  }
    88  
    89  func (CertificateSigningRequestPredicate) Update(e event.UpdateEvent) bool {
    90  	if e.ObjectOld == nil || e.ObjectNew == nil {
    91  		// a reference object is missing, just reconcile to be safe
    92  		return true
    93  	}
    94  
    95  	oldCr, oldOk := e.ObjectOld.(*certificatesv1.CertificateSigningRequest)
    96  	newCr, newOk := e.ObjectNew.(*certificatesv1.CertificateSigningRequest)
    97  	if !oldOk || !newOk {
    98  		// a reference object is invalid, just reconcile to be safe
    99  		return true
   100  	}
   101  
   102  	if len(oldCr.Status.Conditions) != len(newCr.Status.Conditions) {
   103  		// Fail fast in case we are certain a non-ready condition was added or removed.
   104  		return true
   105  	}
   106  
   107  	for _, oldCond := range oldCr.Status.Conditions {
   108  		newCond := conditions.GetCertificateSigningRequestStatusCondition(newCr.Status.Conditions, oldCond.Type)
   109  		if (newCond == nil) || (oldCond.Status != newCond.Status) {
   110  			// we found a missing or changed condition
   111  			return true
   112  		}
   113  	}
   114  
   115  	// check if any of the annotations changed
   116  	return !reflect.DeepEqual(e.ObjectNew.GetAnnotations(), e.ObjectOld.GetAnnotations())
   117  }
   118  
   119  // Predicate for Issuer events that should trigger the CertificateRequest reconciler
   120  //
   121  // In these cases we want to trigger:
   122  // - the Ready condition was added/ removed
   123  // - the Ready condition's Status property changed
   124  // - the Ready condition's observed generation changed
   125  type LinkedIssuerPredicate struct {
   126  	predicate.Funcs
   127  }
   128  
   129  // Update implements default UpdateEvent filter for validating resource version change.
   130  func (LinkedIssuerPredicate) Update(e event.UpdateEvent) bool {
   131  	if e.ObjectOld == nil || e.ObjectNew == nil {
   132  		// a reference object is missing, just reconcile to be safe
   133  		return true
   134  	}
   135  
   136  	issuerOld, okOld := e.ObjectOld.(v1alpha1.Issuer)
   137  	issuerNew, okNew := e.ObjectNew.(v1alpha1.Issuer)
   138  	if (!okOld || !okNew) ||
   139  		(issuerOld.GetStatus() == nil || issuerNew.GetStatus() == nil) {
   140  		// a reference object is invalid, just reconcile to be safe
   141  		return true
   142  	}
   143  
   144  	readyOld := conditions.GetIssuerStatusCondition(
   145  		issuerOld.GetStatus().Conditions,
   146  		cmapi.IssuerConditionReady,
   147  	)
   148  
   149  	readyNew := conditions.GetIssuerStatusCondition(
   150  		issuerNew.GetStatus().Conditions,
   151  		cmapi.IssuerConditionReady,
   152  	)
   153  
   154  	if readyOld == nil || readyNew == nil {
   155  		// the ready condition is not present in the old and/or new version
   156  		// we only want to reconcile if one of the two conditions is not nil
   157  		return readyOld != nil || readyNew != nil
   158  	}
   159  
   160  	return readyNew.Status != readyOld.Status || readyNew.ObservedGeneration != readyOld.ObservedGeneration
   161  }
   162  
   163  // Predicate for Issuer events that should trigger the Issuer reconciler
   164  //
   165  // In these cases we want to trigger:
   166  // - an annotation changed/ was added or removed
   167  // - the generation changed
   168  // - the Ready condition was added/ removed
   169  type IssuerPredicate struct {
   170  	predicate.Funcs
   171  }
   172  
   173  // Update implements default UpdateEvent filter for validating generation change.
   174  func (IssuerPredicate) Update(e event.UpdateEvent) bool {
   175  	if e.ObjectOld == nil || e.ObjectNew == nil {
   176  		// a reference object is missing, just reconcile to be safe
   177  		return true
   178  	}
   179  
   180  	if e.ObjectNew.GetGeneration() != e.ObjectOld.GetGeneration() {
   181  		// we noticed a generation change
   182  		return true
   183  	}
   184  
   185  	issuerOld, okOld := e.ObjectOld.(v1alpha1.Issuer)
   186  	issuerNew, okNew := e.ObjectNew.(v1alpha1.Issuer)
   187  	if (!okOld || !okNew) ||
   188  		(issuerOld.GetStatus() == nil || issuerNew.GetStatus() == nil) {
   189  		// a reference object is invalid, just reconcile to be safe
   190  		return true
   191  	}
   192  
   193  	readyOld := conditions.GetIssuerStatusCondition(
   194  		issuerOld.GetStatus().Conditions,
   195  		cmapi.IssuerConditionReady,
   196  	)
   197  
   198  	readyNew := conditions.GetIssuerStatusCondition(
   199  		issuerNew.GetStatus().Conditions,
   200  		cmapi.IssuerConditionReady,
   201  	)
   202  
   203  	if (readyOld == nil && readyNew != nil) ||
   204  		(readyOld != nil && readyNew == nil) {
   205  		// the ready condition is not present in the old or new version but
   206  		// is present in the new or old version
   207  		return true
   208  	}
   209  
   210  	// check if any of the annotations changed
   211  	return !reflect.DeepEqual(e.ObjectNew.GetAnnotations(), e.ObjectOld.GetAnnotations())
   212  }
   213  

View as plain text