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

     1  // Copyright 2016-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  	"context"
    19  
    20  	"github.com/cilium/cilium/pkg/completion"
    21  	"github.com/cilium/cilium/pkg/endpoint/regeneration"
    22  	"github.com/cilium/cilium/pkg/revert"
    23  )
    24  
    25  // RegenerationContext provides context to regenerate() calls to determine
    26  // the caller, and which specific aspects to regeneration are necessary to
    27  // update the datapath to implement the new behavior.
    28  type regenerationContext struct {
    29  	// Reason provides context to source for the regeneration, which is
    30  	// used to generate useful log messages.
    31  	Reason string
    32  
    33  	// Stats are collected during the endpoint regeneration and provided
    34  	// back to the caller
    35  	Stats regenerationStatistics
    36  
    37  	// DoneFunc must be called when the most resource intensive portion of
    38  	// the regeneration is done
    39  	DoneFunc func()
    40  
    41  	datapathRegenerationContext *datapathRegenerationContext
    42  
    43  	parentContext context.Context
    44  
    45  	cancelFunc context.CancelFunc
    46  }
    47  
    48  func ParseExternalRegenerationMetadata(ctx context.Context, c context.CancelFunc, e *regeneration.ExternalRegenerationMetadata) *regenerationContext {
    49  	return &regenerationContext{
    50  		Reason: e.Reason,
    51  		datapathRegenerationContext: &datapathRegenerationContext{
    52  			regenerationLevel: e.RegenerationLevel,
    53  			ctCleaned:         make(chan struct{}),
    54  		},
    55  		parentContext: ctx,
    56  		cancelFunc:    c,
    57  	}
    58  }
    59  
    60  // datapathRegenerationContext contains information related to regenerating the
    61  // datapath (BPF, proxy, etc.).
    62  type datapathRegenerationContext struct {
    63  	bpfHeaderfilesHash string
    64  	epInfoCache        *epInfoCache
    65  	proxyWaitGroup     *completion.WaitGroup
    66  	ctCleaned          chan struct{}
    67  	completionCtx      context.Context
    68  	completionCancel   context.CancelFunc
    69  	currentDir         string
    70  	nextDir            string
    71  	regenerationLevel  regeneration.DatapathRegenerationLevel
    72  
    73  	finalizeList revert.FinalizeList
    74  	revertStack  revert.RevertStack
    75  }
    76  
    77  func (ctx *datapathRegenerationContext) prepareForProxyUpdates(parentCtx context.Context) {
    78  	completionCtx, completionCancel := context.WithTimeout(parentCtx, EndpointGenerationTimeout)
    79  	ctx.proxyWaitGroup = completion.NewWaitGroup(completionCtx)
    80  	ctx.completionCtx = completionCtx
    81  	ctx.completionCancel = completionCancel
    82  }