client

package
v1.7.3 Latest Latest
Warning

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

Go to latest
Published: Nov 27, 2017 License: Apache-2.0 Imports: 27 Imported by: 0

Documentation

Overview

Package client implements the northbound client used to manage Calico configuration.

This client is the main entry point for applications that are managing or querying Calico configuration.

This client provides a typed interface for managing different resource types. The definitions for each resource type are defined in the following package:

github.com/projectcalico/libcalico-go/lib/api

The client has a number of methods that return interfaces for managing:

  • BGP Peer resources
  • Policy resources
  • IP Pool resources
  • Host endpoint resources
  • Workload endpoint resources
  • Profile resources
  • IP Address Management (IPAM)

See [resource definitions](http://docs.projectcalico.org/latest/reference/calicoctl/resources/) for details about the set of management commands for each resource type.

The resource management interfaces have a common set of commands to create, delete, update and retrieve resource instances. For example, an application using this client to manage host endpoint resources would create an instance of this client, create a new HostEndpoints interface and call the appropriate methods on that interface. For example:

// NewFromEnv() creates a new client and defaults to access an etcd backend datastore at
// http://127.0.0.1:2379.  For alternative backend access details, set the appropriate
// ENV variables specified in the CalicoAPIConfigSpec structure.
client, err := client.NewFromEnv()

// Obtain the interface for managing host endpoint resources.
hostendpoints := client.HostEndpoints()

// Create a new host endpoint.  All Create() methods return an error of type
// common.ErrorResourceAlreadyExists if the resource specified by its
// unique identifiers already exists.
hostEndpoint, err := hostEndpoints.Create(&api.HostEndpoint{
	Metadata: api.HostEndpointMetadata{
		Name: "endpoint1",
		Nodename: "hostname1",
	},
	Spec: api.HostEndpointSpec{
		InterfaceName: "eth0"
	},
}

// Update an existing host endpoint.  All Update() methods return an error of type
// common.ErrorResourceDoesNotExist if the resource specified by its
// unique identifiers does not exist.
hostEndpoint, err = hostEndpoints.Update(&api.HostEndpoint{
	Metadata: api.HostEndpointMetadata{
		Name: "endpoint1",
		Nodename: "hostname1",
	},
	Spec: api.HostEndpointSpec{
		InterfaceName: "eth0",
		Profiles: []string{"profile1"},
	},
}

// Apply (update or create) a hostEndpoint.  All Apply() methods will update a resource
// if it already exists, and will create a new resource if it does not.
hostEndpoint, err = hostEndpoints.Apply(&api.HostEndpoint{
	Metadata: api.HostEndpointMetadata{
		Name: "endpoint1",
		Nodename: "hostname1",
	},
	Spec: api.HostEndpointSpec{
		InterfaceName: "eth1",
		Profiles: []string{"profile1"},
	},
}

// Delete a hostEndpoint.  All Delete() methods return an error of type
// common.ErrorResourceDoesNotExist if the resource specified by its
// unique identifiers does not exist.
hostEndpoint, err = hostEndpoints.Delete(api.HostEndpointMetadata{
	Name: "endpoint1",
	Nodename: "hostname1",
})

// Get a hostEndpoint.  All Get() methods return an error of type
// common.ErrorResourceDoesNotExist if the resource specified by its
// unique identifiers does not exist.
hostEndpoint, err = hostEndpoints.Get(api.HostEndpointMetadata{
	Name: "endpoint1",
	Nodename: "hostname1",
})

// List all hostEndpoints.  All List() methods take a (sub-)set of the resource
// identifiers and return the corresponding list resource type that has an
// Items field containing a list of resources that match the supplied
// identifiers.
hostEndpointList, err := hostEndpoints.List(api.HostEndpointMetadata{})

Index

Constants

View Source
const (
	GlobalDefaultASNumber       = 64512
	GlobalDefaultLogLevel       = "info"
	GlobalDefaultIPIP           = false
	GlobalDefaultNodeToNodeMesh = true
)

Variables

This section is empty.

Functions

func LoadClientConfig

func LoadClientConfig(filename string) (*api.CalicoAPIConfig, error)

LoadClientConfig loads the ClientConfig from the specified file (if specified) or from environment variables (if the file is not specified).

func LoadClientConfigFromBytes

func LoadClientConfigFromBytes(b []byte) (*api.CalicoAPIConfig, error)

LoadClientConfig loads the ClientConfig from the supplied bytes containing YAML or JSON format data.

func LoadClientConfigFromEnvironment

func LoadClientConfigFromEnvironment() (*api.CalicoAPIConfig, error)

LoadClientConfig loads the ClientConfig from the specified file (if specified) or from environment variables (if the file is not specified).

Types

type AssignIPArgs

type AssignIPArgs struct {
	// The IP address to assign.
	IP net.IP

	// If specified, a handle which can be used to retrieve / release
	// the allocated IP addresses in the future.
	HandleID *string

	// A key/value mapping of metadata to store with the allocations.
	Attrs map[string]string

	// If specified, the hostname of the host on which IP addresses
	// will be allocated.  If not specified, this will default
	// to the value provided by os.Hostname.
	Hostname string
}

AssignIPArgs defines the set of arguments for assigning a specific IP address.

type AutoAssignArgs

type AutoAssignArgs struct {
	// The number of IPv4 addresses to automatically assign.
	Num4 int

	// The number of IPv6 addresses to automatically assign.
	Num6 int

	// If specified, a handle which can be used to retrieve / release
	// the allocated IP addresses in the future.
	HandleID *string

	// A key/value mapping of metadata to store with the allocations.
	Attrs map[string]string

	// If specified, the hostname of the host on which IP addresses
	// will be allocated.  If not specified, this will default
	// to the value provided by os.Hostname.
	Hostname string

	// If specified, the previously configured IPv4 pools from which
	// to assign IPv4 addresses.  If not specified, this defaults to all IPv4 pools.
	IPv4Pools []net.IPNet

	// If specified, the previously configured IPv6 pools from which
	// to assign IPv6 addresses.  If not specified, this defaults to all IPv6 pools.
	IPv6Pools []net.IPNet
}

AutoAssignArgs defines the set of arguments for assigning one or more IP addresses.

type BGPPeerInterface

type BGPPeerInterface interface {
	List(api.BGPPeerMetadata) (*api.BGPPeerList, error)
	Get(api.BGPPeerMetadata) (*api.BGPPeer, error)
	Create(*api.BGPPeer) (*api.BGPPeer, error)
	Update(*api.BGPPeer) (*api.BGPPeer, error)
	Apply(*api.BGPPeer) (*api.BGPPeer, error)
	Delete(api.BGPPeerMetadata) error
}

BGPPeerInterface has methods to work with BGPPeer resources.

type Client

type Client struct {
	// The backend client is currently public to allow access to datastore
	// specific functions that are used by calico/node.  This is a temporary
	// measure and users of the client API should not assume that the backend
	// will be available in the future.
	Backend bapi.Client
}

Client contains

func New

func New(config api.CalicoAPIConfig) (*Client, error)

New returns a connected Client. The ClientConfig can either be created explicitly, or can be loaded from a config file or environment variables using the LoadClientConfig() function.

func NewFromEnv added in v1.3.0

func NewFromEnv() (*Client, error)

NewFromEnv loads the config from ENV variables and returns a connected Client.

func (*Client) BGPPeers

func (c *Client) BGPPeers() BGPPeerInterface

BGPPeers returns an interface for managing BGP peer resources.

func (*Client) Config

func (c *Client) Config() ConfigInterface

Config returns an interface for managing system configuration..

func (*Client) EnsureInitialized added in v1.0.2

func (c *Client) EnsureInitialized() error

EnsureInitialized is used to ensure the backend datastore is correctly initialized for use by Calico. This method may be called multiple times, and will have no effect if the datastore is already correctly initialized.

Most Calico deployment scenarios will automatically implicitly invoke this method and so a general consumer of this API can assume that the datastore is already initialized.

func (*Client) HostEndpoints

func (c *Client) HostEndpoints() HostEndpointInterface

HostEndpoints returns an interface for managing host endpoint resources.

func (*Client) IPAM

func (c *Client) IPAM() IPAMInterface

IPAM returns an interface for managing IP address assignment and releasing.

func (*Client) IPPools

func (c *Client) IPPools() IPPoolInterface

IPPools returns an interface for managing IP pool resources.

func (*Client) Nodes

func (c *Client) Nodes() NodeInterface

Nodes returns an interface for managing node resources.

func (*Client) Policies

func (c *Client) Policies() PolicyInterface

Policies returns an interface for managing policy resources.

func (*Client) Profiles

func (c *Client) Profiles() ProfileInterface

Profiles returns an interface for managing profile resources.

func (*Client) WorkloadEndpoints

func (c *Client) WorkloadEndpoints() WorkloadEndpointInterface

WorkloadEndpoints returns an interface for managing workload endpoint resources.

type ConfigInterface

type ConfigInterface interface {
	SetNodeToNodeMesh(bool) error
	GetNodeToNodeMesh() (bool, error)
	SetGlobalASNumber(numorstring.ASNumber) error
	GetGlobalASNumber() (numorstring.ASNumber, error)
	SetGlobalIPIP(bool) error
	GetGlobalIPIP() (bool, error)
	SetNodeIPIPTunnelAddress(string, *net.IP) error
	GetNodeIPIPTunnelAddress(string) (*net.IP, error)
	SetGlobalLogLevel(string) error
	GetGlobalLogLevel() (string, error)
	SetNodeLogLevel(string, string) error
	SetNodeLogLevelUseGlobal(string) error
	GetNodeLogLevel(string) (string, ConfigLocation, error)
	GetFelixConfig(string, string) (string, bool, error)
	SetFelixConfig(string, string, string) error
	UnsetFelixConfig(string, string) error
	GetBGPConfig(string, string) (string, bool, error)
	SetBGPConfig(string, string, string) error
	UnsetBGPConfig(string, string) error
}

ConfigInterface provides methods for setting, unsetting and retrieving low level config options.

type ConfigLocation

type ConfigLocation int8
const (
	ConfigLocationNone ConfigLocation = iota
	ConfigLocationNode
	ConfigLocationGlobal
)

type HostEndpointInterface

type HostEndpointInterface interface {

	// List enumerates host endpoint resources matching the supplied metadata and
	// wildcarding missing identifiers.
	List(api.HostEndpointMetadata) (*api.HostEndpointList, error)

	// Get returns the host endpoint resource matching the supplied metadata.  The metadata
	// should contain all identifiers to uniquely identify a single resource.  If the
	// resource does not exist, a errors.ErrorResourceNotFound error is returned.
	Get(api.HostEndpointMetadata) (*api.HostEndpoint, error)

	// Create will create a new host endpoint resource.  If the resource already exists,
	// a errors.ErrorResourceAlreadyExists error is returned.
	Create(*api.HostEndpoint) (*api.HostEndpoint, error)

	// Update will update an existing host endpoint resource.  If the resource does not exist,
	// a errors.ErrorResourceDoesNotExist error is returned.
	Update(*api.HostEndpoint) (*api.HostEndpoint, error)

	// Apply with update an existing host endpoint resource, or create a new one if it does
	// not exist.
	Apply(*api.HostEndpoint) (*api.HostEndpoint, error)

	// Delete will delete a host endpoint resource.  The metadata should contain all identifiers
	// to uniquely identify a single resource.  If the resource does not exist, a
	// errors.ErrorResourceDoesNotExist error is returned.
	Delete(api.HostEndpointMetadata) error
}

HostEndpointInterface has methods to work with host endpoint resources.

type IPAMConfig

type IPAMConfig struct {
	// When StrictAffinity is true, addresses from a given block can only be
	// assigned by hosts with the blocks affinity.  If false, then AutoAllocateBlocks
	// must be true.  The default value is false.
	StrictAffinity bool

	// When AutoAllocateBlocks is true, Calico will automatically
	// allocate blocks of IP address to hosts as needed to assign addresses.
	// If false, then StrictAffinity must be true.  The default value is true.
	AutoAllocateBlocks bool
}

IPAMConfig contains global configuration options for Calico IPAM. This IPAM configuration is stored in the datastore and configures the behavior of Calico IPAM across an entire Calico cluster.

type IPAMInterface

type IPAMInterface interface {
	// AssignIP assigns the provided IP address to the provided host.  The IP address
	// must fall within a configured pool.  AssignIP will claim block affinity as needed
	// in order to satisfy the assignment.  An error will be returned if the IP address
	// is already assigned, or if StrictAffinity is enabled and the address is within
	// a block that does not have affinity for the given host.
	AssignIP(args AssignIPArgs) error

	// AutoAssign automatically assigns one or more IP addresses as specified by the
	// provided AutoAssignArgs.  AutoAssign returns the list of the assigned IPv4 addresses,
	// and the list of the assigned IPv6 addresses.
	AutoAssign(args AutoAssignArgs) ([]net.IP, []net.IP, error)

	// ReleaseIPs releases any of the given IP addresses that are currently assigned,
	// so that they are available to be used in another assignment.
	ReleaseIPs(ips []net.IP) ([]net.IP, error)

	// GetAssignmentAttributes returns the attributes stored with the given IP address
	// upon assignment.
	GetAssignmentAttributes(addr net.IP) (map[string]string, error)

	// IpsByHandle returns a list of all IP addresses that have been
	// assigned using the provided handle.
	IPsByHandle(handleID string) ([]net.IP, error)

	// ReleaseByHandle releases all IP addresses that have been assigned
	// using the provided handle.  Returns an error if no addresses
	// are assigned with the given handle.
	ReleaseByHandle(handleID string) error

	// ClaimAffinity claims affinity to the given host for all blocks
	// within the given CIDR.  The given CIDR must fall within a configured
	// pool. If an empty string is passed as the host, then the value returned by os.Hostname is used.
	ClaimAffinity(cidr net.IPNet, host string) ([]net.IPNet, []net.IPNet, error)

	// ReleaseAffinity releases affinity for all blocks within the given CIDR
	// on the given host.  If an empty string is passed as the host, then the
	// value returned by os.Hostname will be used.
	ReleaseAffinity(cidr net.IPNet, host string) error

	// ReleaseHostAffinities releases affinity for all blocks that are affine
	// to the given host.  If an empty string is passed as the host, the value returned by
	// os.Hostname will be used.
	ReleaseHostAffinities(host string) error

	// ReleasePoolAffinities releases affinity for all blocks within
	// the specified pool across all hosts.
	ReleasePoolAffinities(pool net.IPNet) error

	// GetIPAMConfig returns the global IPAM configuration.  If no IPAM configuration
	// has been set, returns a default configuration with StrictAffinity disabled
	// and AutoAllocateBlocks enabled.
	GetIPAMConfig() (*IPAMConfig, error)

	// SetIPAMConfig sets global IPAM configuration.  This can only
	// be done when there are no allocated blocks and IP addresses.
	SetIPAMConfig(cfg IPAMConfig) error

	// RemoveIPAMHost releases affinity for all blocks on the given host,
	// and removes all host-specific IPAM data from the datastore.
	// RemoveIPAMHost does not release any IP addresses claimed on the given host.
	// If an empty string is passed as the host then the value returned by os.Hostname is used.
	RemoveIPAMHost(host string) error
}

IPAMInterface has methods to perform IP address management.

type IPPoolInterface

type IPPoolInterface interface {
	List(api.IPPoolMetadata) (*api.IPPoolList, error)
	Get(api.IPPoolMetadata) (*api.IPPool, error)
	Create(*api.IPPool) (*api.IPPool, error)
	Update(*api.IPPool) (*api.IPPool, error)
	Apply(*api.IPPool) (*api.IPPool, error)
	Delete(api.IPPoolMetadata) error
}

PoolInterface has methods to work with Pool resources.

type NodeInterface

type NodeInterface interface {
	List(api.NodeMetadata) (*api.NodeList, error)
	Get(api.NodeMetadata) (*api.Node, error)
	Create(*api.Node) (*api.Node, error)
	Update(*api.Node) (*api.Node, error)
	Apply(*api.Node) (*api.Node, error)
	Delete(api.NodeMetadata) error
}

NodeInterface has methods to work with Node resources.

type PolicyInterface

type PolicyInterface interface {
	List(api.PolicyMetadata) (*api.PolicyList, error)
	Get(api.PolicyMetadata) (*api.Policy, error)
	Create(*api.Policy) (*api.Policy, error)
	Update(*api.Policy) (*api.Policy, error)
	Apply(*api.Policy) (*api.Policy, error)
	Delete(api.PolicyMetadata) error
}

PolicyInterface has methods to work with Policy resources.

type ProfileInterface

type ProfileInterface interface {
	List(api.ProfileMetadata) (*api.ProfileList, error)
	Get(api.ProfileMetadata) (*api.Profile, error)
	Create(*api.Profile) (*api.Profile, error)
	Update(*api.Profile) (*api.Profile, error)
	Apply(*api.Profile) (*api.Profile, error)
	Delete(api.ProfileMetadata) error
}

ProfileInterface has methods to work with Profile resources.

type WorkloadEndpointInterface

WorkloadEndpointInterface has methods to work with WorkloadEndpoint resources.

Jump to

Keyboard shortcuts

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