github.com/cilium/cilium@v1.16.2/pkg/endpoint/regenerationcontext.go (about)

     1  // SPDX-License-Identifier: Apache-2.0
     2  // Copyright Authors of Cilium
     3  
     4  package endpoint
     5  
     6  import (
     7  	"context"
     8  
     9  	"github.com/cilium/cilium/pkg/completion"
    10  	"github.com/cilium/cilium/pkg/endpoint/regeneration"
    11  	"github.com/cilium/cilium/pkg/logging/logfields"
    12  	"github.com/cilium/cilium/pkg/revert"
    13  )
    14  
    15  // RegenerationContext provides context to regenerate() calls to determine
    16  // the caller, and which specific aspects to regeneration are necessary to
    17  // update the datapath to implement the new behavior.
    18  type regenerationContext struct {
    19  	// Reason provides context to source for the regeneration, which is
    20  	// used to generate useful log messages.
    21  	Reason string
    22  
    23  	// Stats are collected during the endpoint regeneration and provided
    24  	// back to the caller
    25  	Stats regenerationStatistics
    26  
    27  	// DoneFunc must be called when the most resource intensive portion of
    28  	// the regeneration is done
    29  	DoneFunc func()
    30  
    31  	datapathRegenerationContext *datapathRegenerationContext
    32  
    33  	parentContext context.Context
    34  
    35  	cancelFunc context.CancelFunc
    36  }
    37  
    38  func ParseExternalRegenerationMetadata(ctx context.Context, c context.CancelFunc, e *regeneration.ExternalRegenerationMetadata) *regenerationContext {
    39  	if e.RegenerationLevel == regeneration.Invalid {
    40  		log.WithField(logfields.Reason, e.Reason).Errorf("Uninitialized regeneration level")
    41  	}
    42  
    43  	return &regenerationContext{
    44  		Reason: e.Reason,
    45  		datapathRegenerationContext: &datapathRegenerationContext{
    46  			regenerationLevel: e.RegenerationLevel,
    47  			ctCleaned:         make(chan struct{}),
    48  		},
    49  		parentContext: ctx,
    50  		cancelFunc:    c,
    51  	}
    52  }
    53  
    54  // datapathRegenerationContext contains information related to regenerating the
    55  // datapath (BPF, proxy, etc.).
    56  type datapathRegenerationContext struct {
    57  	bpfHeaderfilesHash string
    58  	epInfoCache        *epInfoCache
    59  	proxyWaitGroup     *completion.WaitGroup
    60  	ctCleaned          chan struct{}
    61  	completionCtx      context.Context
    62  	completionCancel   context.CancelFunc
    63  	currentDir         string
    64  	nextDir            string
    65  	regenerationLevel  regeneration.DatapathRegenerationLevel
    66  
    67  	finalizeList revert.FinalizeList
    68  	revertStack  revert.RevertStack
    69  }
    70  
    71  func (ctx *datapathRegenerationContext) prepareForProxyUpdates(parentCtx context.Context) {
    72  	completionCtx, completionCancel := context.WithTimeout(parentCtx, EndpointGenerationTimeout)
    73  	ctx.proxyWaitGroup = completion.NewWaitGroup(completionCtx)
    74  	ctx.completionCtx = completionCtx
    75  	ctx.completionCancel = completionCancel
    76  }