github.com/cilium/cilium@v1.16.2/pkg/bgpv1/manager/instance/instance.go (about)

     1  // SPDX-License-Identifier: Apache-2.0
     2  // Copyright Authors of Cilium
     3  
     4  package instance
     5  
     6  import (
     7  	"context"
     8  
     9  	"github.com/sirupsen/logrus"
    10  
    11  	"github.com/cilium/cilium/pkg/bgpv1/gobgp"
    12  	"github.com/cilium/cilium/pkg/bgpv1/types"
    13  	v2alpha1api "github.com/cilium/cilium/pkg/k8s/apis/cilium.io/v2alpha1"
    14  )
    15  
    16  // ServerWithConfig is a container for providing interface with underlying router implementation
    17  // and Cilium's BGP control plane related configuration.
    18  //
    19  // It exports a method set for manipulating the BgpServer. However, this
    20  // struct is a dumb object. The calling code is required to keep the BgpServer's
    21  // configuration and associated configuration fields in sync.
    22  //
    23  // This is used in BGPv1 implementation.
    24  type ServerWithConfig struct {
    25  	// ASN is the local ASN number of the virtual router instance.
    26  	ASN uint32
    27  
    28  	// backed BgpServer configured in accordance to the accompanying
    29  	// CiliumBGPVirtualRouter configuration.
    30  	Server types.Router
    31  
    32  	// The CiliumBGPVirtualRouter configuration which drives the configuration
    33  	// of the above BgpServer.
    34  	//
    35  	// If this field is nil it means the above BgpServer has had no
    36  	// configuration applied to it.
    37  	Config *v2alpha1api.CiliumBGPVirtualRouter
    38  
    39  	// ReconcilerMetadata holds reconciler-specific metadata keyed by the reconciler name,
    40  	// opaque outside the respective reconciler.
    41  	ReconcilerMetadata map[string]any
    42  }
    43  
    44  // NewServerWithConfig will start an underlying BgpServer utilizing types.ServerParameters
    45  // for its initial configuration.
    46  //
    47  // The returned ServerWithConfig has a nil CiliumBGPVirtualRouter config, and is
    48  // ready to be provided to ReconcileBGPConfig.
    49  //
    50  // Canceling the provided context will kill the BgpServer along with calling the
    51  // underlying BgpServer's Stop() method.
    52  func NewServerWithConfig(ctx context.Context, log *logrus.Entry, params types.ServerParameters) (*ServerWithConfig, error) {
    53  	s, err := gobgp.NewGoBGPServer(ctx, log, params)
    54  	if err != nil {
    55  		return nil, err
    56  	}
    57  	return &ServerWithConfig{
    58  		ASN:                params.Global.ASN,
    59  		Server:             s,
    60  		Config:             nil,
    61  		ReconcilerMetadata: make(map[string]any),
    62  	}, nil
    63  }
    64  
    65  // BGPInstance is a container for providing interface with underlying router implementation.
    66  //
    67  // This is used in BGPv2 implementation.
    68  type BGPInstance struct {
    69  	ASN       uint32
    70  	Config    *v2alpha1api.CiliumBGPNodeInstance
    71  	Router    types.Router
    72  	Metadata  map[string]any
    73  	CancelCtx context.CancelFunc
    74  }
    75  
    76  // NewBGPInstance will start an underlying BGP instance utilizing types.ServerParameters
    77  // for its initial configuration.
    78  //
    79  // The returned BGPInstance has a nil CiliumBGPNodeInstance config, and is
    80  // ready to be provided to ReconcileBGPConfigV2.
    81  //
    82  // Canceling the provided context will kill the BGP instance along with calling the
    83  // underlying Router's Stop() method.
    84  func NewBGPInstance(ctx context.Context, log *logrus.Entry, params types.ServerParameters) (*BGPInstance, error) {
    85  	gobgpCtx, cancel := context.WithCancel(ctx)
    86  	s, err := gobgp.NewGoBGPServer(gobgpCtx, log, params)
    87  	if err != nil {
    88  		cancel()
    89  		return nil, err
    90  	}
    91  
    92  	return &BGPInstance{
    93  		ASN:       params.Global.ASN,
    94  		CancelCtx: cancel,
    95  		Config:    nil,
    96  		Router:    s,
    97  		Metadata:  make(map[string]any),
    98  	}, nil
    99  }