lib

package
v1.3.5 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 28, 2025 License: Apache-2.0 Imports: 5 Imported by: 0

README

AWS Multi-ENI Controller Library

This package provides a Go library for managing AWS Elastic Network Interfaces (ENIs) programmatically. It's extracted from the AWS Multi-ENI Controller project and provides a clean API for creating, attaching, detaching, and deleting ENIs.

Installation

go get github.com/johnlam90/aws-multi-eni-controller

Usage

Basic Example
package main

import (
    "context"
    "log"
    "time"

    "github.com/go-logr/logr"
    "github.com/go-logr/zapr"
    "github.com/johnlam90/aws-multi-eni-controller/pkg/lib"
    "go.uber.org/zap"
)

func main() {
    // Create a logger
    zapLog, _ := zap.NewDevelopment()
    logger := zapr.NewLogger(zapLog)

    // Create a context with timeout
    ctx, cancel := context.WithTimeout(context.Background(), 5*time.Minute)
    defer cancel()

    // Create an ENI manager
    eniManager, err := lib.NewENIManager(ctx, "us-east-1", logger)
    if err != nil {
        log.Fatalf("Failed to create ENI manager: %v", err)
    }

    // Create an ENI
    options := lib.ENIOptions{
        SubnetID:           "subnet-12345678",
        SecurityGroupIDs:   []string{"sg-12345678"},
        Description:        "Example ENI",
        DeviceIndex:        1,
        DeleteOnTermination: true,
        Tags: map[string]string{
            "Name": "example-eni",
        },
    }

    eniID, err := eniManager.CreateENI(ctx, options)
    if err != nil {
        log.Fatalf("Failed to create ENI: %v", err)
    }

    // Attach the ENI to an instance
    err = eniManager.AttachENI(ctx, eniID, "i-12345678", options.DeviceIndex, options.DeleteOnTermination)
    if err != nil {
        log.Fatalf("Failed to attach ENI: %v", err)
    }

    // Later, detach and delete the ENI when done
    // ...
}
Advanced Configuration

You can create an ENI manager with custom configuration:

import (
    "github.com/johnlam90/aws-multi-eni-controller/pkg/config"
    "github.com/johnlam90/aws-multi-eni-controller/pkg/lib"
)

// Create a custom configuration
cfg := config.DefaultControllerConfig()
cfg.AWSRegion = "us-west-2"
cfg.DetachmentTimeout = 30 * time.Second
cfg.DefaultDeviceIndex = 2

// Create an ENI manager with custom configuration
eniManager, err := lib.NewENIManagerWithConfig(ctx, cfg, logger)

API Reference

Types
ENIManager

The main type for managing ENIs.

ENIOptions

Options for creating a new ENI:

  • SubnetID (string): The ID of the subnet to create the ENI in.
  • SecurityGroupIDs ([]string): A list of security group IDs to attach to the ENI.
  • Description (string): An optional description for the ENI.
  • DeviceIndex (int): The device index to use when attaching the ENI.
  • DeleteOnTermination (bool): Whether the ENI should be deleted when the instance is terminated.
  • Tags (map[string]string): A map of tags to apply to the ENI.
Methods
NewENIManager(ctx context.Context, region string, logger logr.Logger) (*ENIManager, error)

Creates a new ENI manager with default configuration for the specified region.

NewENIManagerWithConfig(ctx context.Context, cfg *config.ControllerConfig, logger logr.Logger) (*ENIManager, error)

Creates a new ENI manager with custom configuration.

CreateENI(ctx context.Context, options ENIOptions) (string, error)

Creates a new ENI with the specified options and returns its ID.

AttachENI(ctx context.Context, eniID, instanceID string, deviceIndex int, deleteOnTermination bool) error

Attaches an ENI to an instance.

DetachENI(ctx context.Context, attachmentID string) error

Detaches an ENI from an instance.

DeleteENI(ctx context.Context, eniID string) error

Deletes an ENI.

GetENIsByInstance(ctx context.Context, instanceID string) ([]NetworkInterfaceInfo, error)

Gets all ENIs attached to an instance. Note: This method is currently not fully implemented and will return an error.

GetSubnetsByVPC(ctx context.Context, vpcID string) ([]SubnetInfo, error)

Gets all subnets in a VPC. Note: This method is currently not fully implemented and will return an error.

GetSecurityGroupsByVPC(ctx context.Context, vpcID string) ([]SecurityGroupInfo, error)

Gets all security groups in a VPC. Note: This method is currently not fully implemented and will return an error.

Error Handling

All methods return meaningful error messages that can be used to diagnose issues. It's recommended to check errors and handle them appropriately in your application.

AWS Credentials

The library uses the default AWS SDK credential chain. Make sure your application has the necessary AWS credentials configured through environment variables, AWS configuration files, or IAM roles.

Examples

See the examples/library-usage directory for complete examples of how to use this library.

License

This library is licensed under the same license as the AWS Multi-ENI Controller project.

Documentation

Overview

Package lib provides a clean API for using AWS Multi-ENI Controller functionality as a library in other Go projects.

This package abstracts away the implementation details of the AWS Multi-ENI Controller and provides a simple interface for managing AWS Elastic Network Interfaces (ENIs).

Basic usage:

// Create a logger
zapLog, _ := zap.NewDevelopment()
logger := zapr.NewLogger(zapLog)

// Create a context with timeout
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Minute)
defer cancel()

// Create an ENI manager
eniManager, err := lib.NewENIManager(ctx, "us-east-1", logger)
if err != nil {
	log.Fatalf("Failed to create ENI manager: %v", err)
}

// Create an ENI
options := lib.ENIOptions{
	SubnetID:           "subnet-12345678",
	SecurityGroupIDs:   []string{"sg-12345678"},
	Description:        "Example ENI",
	DeviceIndex:        1,
	DeleteOnTermination: true,
}

eniID, err := eniManager.CreateENI(ctx, options)
if err != nil {
	log.Fatalf("Failed to create ENI: %v", err)
}

// Attach the ENI to an instance
err = eniManager.AttachENI(ctx, eniID, "i-12345678", 1, true)
if err != nil {
	log.Fatalf("Failed to attach ENI: %v", err)
}

// Detach the ENI
err = eniManager.DetachENI(ctx, "eni-attach-12345678")
if err != nil {
	log.Fatalf("Failed to detach ENI: %v", err)
}

// Delete the ENI
err = eniManager.DeleteENI(ctx, eniID)
if err != nil {
	log.Fatalf("Failed to delete ENI: %v", err)
}

For more examples, see the examples/library-usage directory.

Package lib provides a clean API for using AWS Multi-ENI Controller functionality as a library in other Go projects. It abstracts away the implementation details and provides a simple interface for managing ENIs.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ENIManager

type ENIManager struct {
	// contains filtered or unexported fields
}

ENIManager provides methods for managing AWS Elastic Network Interfaces.

func NewENIManager

func NewENIManager(ctx context.Context, region string, logger logr.Logger) (*ENIManager, error)

NewENIManager creates a new ENIManager with the given configuration.

func NewENIManagerWithConfig

func NewENIManagerWithConfig(ctx context.Context, cfg *config.ControllerConfig, logger logr.Logger) (*ENIManager, error)

NewENIManagerWithConfig creates a new ENIManager with the given configuration.

func (*ENIManager) AttachENI

func (m *ENIManager) AttachENI(ctx context.Context, eniID, instanceID string, deviceIndex int, deleteOnTermination bool) error

AttachENI attaches an ENI to an instance.

func (*ENIManager) CreateENI

func (m *ENIManager) CreateENI(ctx context.Context, options ENIOptions) (string, error)

CreateENI creates a new ENI with the given options and returns its ID.

func (*ENIManager) DeleteENI

func (m *ENIManager) DeleteENI(ctx context.Context, eniID string) error

DeleteENI deletes an ENI.

func (*ENIManager) DetachENI

func (m *ENIManager) DetachENI(ctx context.Context, attachmentID string) error

DetachENI detaches an ENI from an instance.

func (*ENIManager) GetENIsByInstance

func (m *ENIManager) GetENIsByInstance(ctx context.Context, instanceID string) ([]NetworkInterfaceInfo, error)

GetENIsByInstance gets all ENIs attached to an instance.

func (*ENIManager) GetSecurityGroupsByVPC

func (m *ENIManager) GetSecurityGroupsByVPC(ctx context.Context, vpcID string) ([]SecurityGroupInfo, error)

GetSecurityGroupsByVPC gets all security groups in a VPC.

func (*ENIManager) GetSubnetsByVPC

func (m *ENIManager) GetSubnetsByVPC(ctx context.Context, vpcID string) ([]SubnetInfo, error)

GetSubnetsByVPC gets all subnets in a VPC.

type ENIOptions

type ENIOptions struct {
	// SubnetID is the ID of the subnet to create the ENI in.
	SubnetID string
	// SecurityGroupIDs is a list of security group IDs to attach to the ENI.
	SecurityGroupIDs []string
	// Description is an optional description for the ENI.
	Description string
	// DeviceIndex is the device index to use when attaching the ENI.
	DeviceIndex int
	// DeleteOnTermination indicates whether the ENI should be deleted when the instance is terminated.
	DeleteOnTermination bool
	// Tags is a map of tags to apply to the ENI.
	Tags map[string]string
}

ENIOptions contains options for creating a new ENI.

type NetworkInterfaceInfo

type NetworkInterfaceInfo struct {
	ID          string
	SubnetID    string
	PrivateIP   string
	DeviceIndex int
	Status      string
}

NetworkInterfaceInfo represents information about a network interface

type SecurityGroupInfo

type SecurityGroupInfo struct {
	ID          string
	VpcID       string
	Name        string
	Description string
}

SecurityGroupInfo represents information about a security group

type SubnetInfo

type SubnetInfo struct {
	ID               string
	VpcID            string
	AvailabilityZone string
	CidrBlock        string
	Name             string
}

SubnetInfo represents information about a subnet

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL