github.com/fafucoder/cilium@v1.6.11/pkg/endpoint/events.go (about)

     1  // Copyright 2019 Authors of Cilium
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package endpoint
    16  
    17  import (
    18  	"github.com/cilium/cilium/pkg/eventqueue"
    19  	"github.com/cilium/cilium/pkg/logging/logfields"
    20  
    21  	"github.com/sirupsen/logrus"
    22  )
    23  
    24  // EndpointRegenerationEvent contains all fields necessary to regenerate an endpoint.
    25  type EndpointRegenerationEvent struct {
    26  	regenContext *regenerationContext
    27  	ep           *Endpoint
    28  }
    29  
    30  // Handle handles the regeneration event for the endpoint.
    31  func (ev *EndpointRegenerationEvent) Handle(res chan interface{}) {
    32  	e := ev.ep
    33  	regenContext := ev.regenContext
    34  
    35  	err := e.RLockAlive()
    36  	if err != nil {
    37  		e.LogDisconnectedMutexAction(err, "before regeneration")
    38  		res <- &EndpointRegenerationResult{
    39  			err: err,
    40  		}
    41  
    42  		return
    43  	}
    44  	e.RUnlock()
    45  
    46  	// We should only queue the request after we use all the endpoint's
    47  	// lock/unlock. Otherwise this can get a deadlock if the endpoint is
    48  	// being deleted at the same time. More info PR-1777.
    49  	doneFunc, err := e.owner.QueueEndpointBuild(regenContext.parentContext, uint64(e.ID))
    50  	if err != nil {
    51  		e.getLogger().WithError(err).Warning("unable to queue endpoint build")
    52  	} else if doneFunc != nil {
    53  		e.getLogger().Debug("Dequeued endpoint from build queue")
    54  
    55  		regenContext.DoneFunc = doneFunc
    56  
    57  		err = ev.ep.regenerate(ev.regenContext)
    58  
    59  		doneFunc()
    60  		e.notifyEndpointRegeneration(err)
    61  	} else {
    62  		// If another build has been queued for the endpoint, that means that
    63  		// that build will be able to take care of all of the work needed to
    64  		// regenerate the endpoint at this current point in time; queueing
    65  		// another build is a waste of resources.
    66  		e.getLogger().Debug("build not queued for endpoint because another build has already been queued")
    67  	}
    68  
    69  	res <- &EndpointRegenerationResult{
    70  		err: err,
    71  	}
    72  	return
    73  }
    74  
    75  // EndpointRegenerationResult contains the results of an endpoint regeneration.
    76  type EndpointRegenerationResult struct {
    77  	err error
    78  }
    79  
    80  // EndpointRevisionBumpEvent contains all fields necessary to bump the policy
    81  // revision of a given endpoint.
    82  type EndpointRevisionBumpEvent struct {
    83  	Rev uint64
    84  	ep  *Endpoint
    85  }
    86  
    87  // Handle handles the revision bump event for the Endpoint.
    88  func (ev *EndpointRevisionBumpEvent) Handle(res chan interface{}) {
    89  	// TODO: if the endpoint is not in a 'ready' state that means that
    90  	// we cannot set the policy revision, as something else has
    91  	// changed endpoint state which necessitates regeneration,
    92  	// *or* the endpoint is in a not-ready state (i.e., a prior
    93  	// regeneration failed, so there is no way that we can
    94  	// realize the policy revision yet. Should this be signaled
    95  	// to the routine waiting for the result of this event?
    96  	ev.ep.SetPolicyRevision(ev.Rev)
    97  	res <- struct{}{}
    98  }
    99  
   100  // PolicyRevisionBumpEvent queues an event for the given endpoint to set its
   101  // realized policy revision to rev. This may block depending on if events have
   102  // been queued up for the given endpoint. It blocks until the event has
   103  // succeeded, or if the event has been cancelled.
   104  func (e *Endpoint) PolicyRevisionBumpEvent(rev uint64) {
   105  	epBumpEvent := eventqueue.NewEvent(&EndpointRevisionBumpEvent{Rev: rev, ep: e})
   106  	// Don't check policy revision event results - it is best effort.
   107  	_, err := e.EventQueue.Enqueue(epBumpEvent)
   108  	if err != nil {
   109  		log.WithFields(logrus.Fields{
   110  			logfields.PolicyRevision: rev,
   111  			logfields.EndpointID:     e.ID,
   112  		}).Errorf("enqueue of EndpointRevisionBumpEvent failed: %s", err)
   113  	}
   114  }