catalog

package
v0.26.0 Latest Latest
Warning

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

Go to latest
Published: Jun 22, 2021 License: MPL-2.0 Imports: 20 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// ConsulSourceKey is the key used in the meta to track the "k8s" source.
	// ConsulSourceValue is the value of the source.
	ConsulSourceKey   = "external-source"
	ConsulSourceValue = "kubernetes"

	// ConsulK8SNS is the key used in the meta to record the namespace
	// of the service/node registration.
	ConsulK8SNS = "external-k8s-ns"
)
View Source
const (
	// ConsulSyncPeriod is how often the syncer will attempt to
	// reconcile the expected service states with the remote Consul server.
	ConsulSyncPeriod = 30 * time.Second

	// ConsulServicePollPeriod is how often a service is checked for
	// whether it has instances to reap.
	ConsulServicePollPeriod = 60 * time.Second
)
View Source
const (
	TestConsulK8STag = "k8s"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type ConsulNodeServicesClient added in v0.12.0

type ConsulNodeServicesClient interface {
	// NodeServices returns consul services with the corresponding tag
	// registered to the Consul node with nodeName. opts is used as the
	// query options in the API call to consul. It returns the list of services
	// (not service instances) and the query meta from the API call.
	NodeServices(tag string, nodeName string, opts api.QueryOptions) ([]ConsulService, *api.QueryMeta, error)
}

ConsulNodeServicesClient is used to query for node services.

type ConsulService added in v0.12.0

type ConsulService struct {
	// Namespace is the Consul namespace the service is registered in.
	// If namespaces are disabled this will always be the empty string even
	// though the namespace is technically "default".
	Namespace string
	// Name is the name of the service in Consul.
	Name string
}

ConsulService is service registered in Consul.

type ConsulSyncer

type ConsulSyncer struct {
	Client *api.Client
	Log    hclog.Logger

	// EnableNamespaces indicates that a user is running Consul Enterprise
	// with version 1.7+ which is namespace aware. It enables Consul namespaces,
	// with syncing into either a single Consul namespace or mirrored from
	// k8s namespaces.
	EnableNamespaces bool

	// CrossNamespaceACLPolicy is the name of the ACL policy to attach to
	// any created Consul namespaces to allow cross namespace service discovery.
	// Only necessary if ACLs are enabled.
	CrossNamespaceACLPolicy string

	// SyncPeriod is the interval between full catalog syncs. These will
	// re-register all services to prevent overwrites of data. This should
	// happen relatively infrequently and default to 30 seconds.
	//
	// ServicePollPeriod is the interval to look for invalid services to
	// deregister. One request will be made for each synced service in
	// Kubernetes.
	//
	// For both syncs, smaller more frequent and focused syncs may be
	// triggered by known drift or changes.
	SyncPeriod        time.Duration
	ServicePollPeriod time.Duration

	// ConsulK8STag is the tag value for services registered.
	ConsulK8STag string

	// The Consul node name to register services with.
	ConsulNodeName string

	// ConsulNodeServicesClient is used to list services for a node. We use a
	// separate client for this API call that handles older version of Consul.
	ConsulNodeServicesClient ConsulNodeServicesClient
	// contains filtered or unexported fields
}

ConsulSyncer is a Syncer that takes the set of registrations and registers them with Consul. It also watches Consul for changes to the services and ensures the local set of registrations represents the source of truth, overwriting any external changes to the services.

func (*ConsulSyncer) Run

func (s *ConsulSyncer) Run(ctx context.Context)

Run is the long-running runloop for reconciling the local set of services to register with the remote state.

func (*ConsulSyncer) Sync

func (s *ConsulSyncer) Sync(rs []*api.CatalogRegistration)

Sync implements Syncer

type NamespacesNodeServicesClient added in v0.12.0

type NamespacesNodeServicesClient struct {
	Client *api.Client
}

NamespacesNodeServicesClient implements ConsulNodeServicesClient for Consul >= 1.7 which supports namespaces.

func (*NamespacesNodeServicesClient) NodeServices added in v0.12.0

func (s *NamespacesNodeServicesClient) NodeServices(
	tag string,
	nodeName string,
	opts api.QueryOptions) ([]ConsulService, *api.QueryMeta, error)

NodeServices returns Consul services tagged with tag registered on nodeName using a Consul API that is supported in Consul versions >= 1.7. If opts.Namespace is set to "*", services from all namespaces will be returned.

type NodePortSyncType

type NodePortSyncType string
const (
	// Only sync NodePort services with a node's ExternalIP address.
	// Doesn't sync if an ExternalIP doesn't exist
	ExternalOnly NodePortSyncType = "ExternalOnly"

	// Sync with an ExternalIP first, if it doesn't exist, use the
	// node's InternalIP address instead
	ExternalFirst NodePortSyncType = "ExternalFirst"

	// Sync NodePort services using
	InternalOnly NodePortSyncType = "InternalOnly"
)

type PreNamespacesNodeServicesClient added in v0.12.0

type PreNamespacesNodeServicesClient struct {
	Client *api.Client
}

PreNamespacesNodeServicesClient implements ConsulNodeServicesClient for Consul < 1.7 which does not support namespaces.

func (*PreNamespacesNodeServicesClient) NodeServices added in v0.12.0

func (s *PreNamespacesNodeServicesClient) NodeServices(
	tag string,
	nodeName string,
	opts api.QueryOptions) ([]ConsulService, *api.QueryMeta, error)

NodeServices returns Consul services tagged with tag registered on nodeName using a Consul API that is supported in Consul versions before 1.7. Consul versions after 1.7 still support this API but the API is not namespace-aware.

type ServiceResource

type ServiceResource struct {
	Log    hclog.Logger
	Client kubernetes.Interface
	Syncer Syncer

	// AllowK8sNamespacesSet is a set of k8s namespaces to explicitly allow for
	// syncing. It supports the special character `*` which indicates that
	// all k8s namespaces are eligible unless explicitly denied. This filter
	// is applied before checking pod annotations.
	AllowK8sNamespacesSet mapset.Set

	// DenyK8sNamespacesSet is a set of k8s namespaces to explicitly deny
	// syncing and thus service registration with Consul. An empty set
	// means that no namespaces are removed from consideration. This filter
	// takes precedence over AllowK8sNamespacesSet.
	DenyK8sNamespacesSet mapset.Set

	// ConsulK8STag is the tag value for services registered.
	ConsulK8STag string

	//ConsulServicePrefix prepends K8s services in Consul with a prefix
	ConsulServicePrefix string

	// ExplictEnable should be set to true to require explicit enabling
	// using annotations. If this is false, then services are implicitly
	// enabled (aka default enabled).
	ExplicitEnable bool

	// ClusterIPSync set to true (the default) syncs ClusterIP-type services.
	// Setting this to false will ignore ClusterIP services during the sync.
	ClusterIPSync bool

	// LoadBalancerEndpointsSync set to true (default false) will sync ServiceTypeLoadBalancer endpoints.
	LoadBalancerEndpointsSync bool

	// NodeExternalIPSync set to true (the default) syncs NodePort services
	// using the node's external ip address. When false, the node's internal
	// ip address will be used instead.
	NodePortSync NodePortSyncType

	// AddK8SNamespaceSuffix set to true appends Kubernetes namespace
	// to the service name being synced to Consul separated by a dash.
	// For example, service 'foo' in the 'default' namespace will be synced
	// as 'foo-default'.
	AddK8SNamespaceSuffix bool

	// EnableNamespaces indicates that a user is running Consul Enterprise
	// with version 1.7+ which is namespace aware. It enables Consul namespaces,
	// with syncing into either a single Consul namespace or mirrored from
	// k8s namespaces.
	EnableNamespaces bool

	// ConsulDestinationNamespace is the name of the Consul namespace to register all
	// synced services into if Consul namespaces are enabled and mirroring
	// is disabled. This will not be used if mirroring is enabled.
	ConsulDestinationNamespace string

	// EnableK8SNSMirroring causes Consul namespaces to be created to match the
	// organization within k8s. Services are registered into the Consul
	// namespace that mirrors their k8s namespace.
	EnableK8SNSMirroring bool

	// K8SNSMirroringPrefix is an optional prefix that can be added to the Consul
	// namespaces created while mirroring. For example, if it is set to "k8s-",
	// then the k8s `default` namespace will be mirrored in Consul's
	// `k8s-default` namespace.
	K8SNSMirroringPrefix string

	// The Consul node name to register service with.
	ConsulNodeName string
	// contains filtered or unexported fields
}

ServiceResource implements controller.Resource to sync Service resource types from K8S.

func (*ServiceResource) Delete

func (t *ServiceResource) Delete(key string, _ interface{}) error

Delete implements the controller.Resource interface.

func (*ServiceResource) Informer

Informer implements the controller.Resource interface.

func (*ServiceResource) Run

func (t *ServiceResource) Run(ch <-chan struct{})

Run implements the controller.Backgrounder interface.

func (*ServiceResource) Upsert

func (t *ServiceResource) Upsert(key string, raw interface{}) error

Upsert implements the controller.Resource interface.

type Syncer

type Syncer interface {
	// Sync is called to sync the full set of registrations.
	Sync([]*api.CatalogRegistration)
}

Syncer is responsible for syncing a set of Consul catalog registrations. An external system manages the set of registrations and periodically updates the Syncer. The Syncer should keep the remote system in sync with the given set of registrations.

Jump to

Keyboard shortcuts

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