k8sOVirtCredentialsMonitor

package module
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Jun 16, 2021 License: Apache-2.0 Imports: 11 Imported by: 0

README

K8S oVirt Credentials Monitor

GitHub Workflow Status

This library provides the ability to monitor a secret in Kubernetes and provide an updated oVirt SDK client to a hook function.

Basic usage

In order to use this library you will need to add it as a Go dependency:

go get github.com/oVirt/k8sOVirtCredentialsMonitor

Then you can set up the monitor:

// This variable will hold the SDK connection
var sdkConnection *ovirtsdk.Connection
// We use the connLock to avoid race conditions
var connLock := &sync.Mutex{}

// This callback will be called when the credentials change.
callback := k8sOVirtCredentialsMonitor.Callbacks{
    OnCredentialsChange: func (connection k8sOVirtCredentialsMonitor.OVirtConnection) {
        connLock.Lock()
        defer connLock.Unlock()

        // Fetch the SDK connection.
        sdkConnection = connection.GetSDK()
    },
    // Optional callbacks without extra parameters
    // - OnMonitorRunning - when the monitor has started
    // - OnMonitorShuttingDown - before the monitor is shutting down
    // - OnCredentialsValidate - allows you to validate credentials before use.
    //                           You can pass the
    //                           k8sOVirtCredentialsMonitor.ValidateCredentials
    //                           function here.
}

// Set up the credential monitor.
monitor, err := k8sOVirtCredentialsMonitor.New(
    k8sOVirtCredentialsMonitor.ConnectionConfig {
        // add Kubernetes connection parameters here
    },
    // Namespace to read the secret from. If empty this will be read 
    secretNamespace string,
    // The secret name must be provided.
    secretName string,
    // callbacks will be called when the connection updates.
    callbacks,
    // logger is an implementation of the k8sOVirtCredentialsMonitor.Logger
    // interface for pluggable logging. This parameter is optional.
    logger,
)
if err != nil {
    // Handle error
    panic(err)
}

// Set up the context for stopping the monitor.
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

// Run the monitor in the background. This goroutine runs until the context
// is canceled.
go monitor.Run(ctx)

// Set up initial connection. We do this in a lock to avoid race conditions.
connLock.Lock()
sdkConnection = monitor.GetConnection().GetSDK()
connLock.Unlock()

// Use sdkConnection here.

Configuration format

The oVirt configuration format in secrets is as follows:

apiVersion: v1
kind: Secret
metadata:
  name: ovirt-credentials
type: generic
data:
  ovirt_url: <oVirt engine URL here, base64 encoded>
  ovirt_username: <oVirt username here, base64 encoded>
  ovirt_password: <oVirt password here, base64 encoded>
  ovirt_insecure: <true or false, base64 encoded; optional; not recommended>
  ovirt_ca_bundle: <CA certificate in PEM format, base64-encoded; optional if ovirt_insecure is true>

Logging

This library supports logging via the following interface:

type Logger interface {
	Debugf(format string, args ...interface{})
	Infof(format string, args ...interface{})
	Warningf(format string, args ...interface{})
	Errorf(format string, args ...interface{})
}

If no logger is provided (nil) logging is disabled. You can also create a logger that logs via the Go testing facility:

logger := k8sOVirtCredentialsMonitor.NewTestLogger(t)

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ValidateCredentials added in v0.3.0

func ValidateCredentials(connection OVirtConnection) error

Types

type Callbacks

type Callbacks struct {
	// OnMonitorRunning is a callback that is called after the watch is set up for the secret.
	OnMonitorRunning func()

	// OnMonitorShuttingDown is a callback that is called before the watch is shut down.
	OnMonitorShuttingDown func()

	// OnCredentialsChange is called when the oVirt credentials change.
	OnCredentialsChange func(OVirtConnection)

	// OnValidateCredentials is called before the credentials are passed/returned. This
	// can be used to validate the credentials, e.g. by calling connection.GetSDK().Test()
	// You can pass the k8sOVirtCredentialsMonitor.ValidateCredentials function here.
	OnValidateCredentials func(OVirtConnection) error
}

Callbacks is a configuration structure that holds the possible callbacks for the monitor.

type ConnectionConfig

type ConnectionConfig struct {
	*rest.Config
}

ConnectionConfig contains the configuration for the Kubernetes API.

type Logger added in v0.2.0

type Logger interface {
	Debugf(format string, args ...interface{})
	Infof(format string, args ...interface{})
	Warningf(format string, args ...interface{})
	Errorf(format string, args ...interface{})
}

Logger provides pluggable logging for this library.

func NewTestLogger added in v0.2.0

func NewTestLogger(t *testing.T) Logger

type OVirtConnection

type OVirtConnection interface {
	// GetSDK returns a direct connection from the oVirt Go SDK.
	GetSDK() *ovirtsdk.Connection
}

OVirtConnection holds the created connection(s). This interface is added for extensibility.

type OVirtCredentialMonitor

type OVirtCredentialMonitor interface {
	// GetConnection returns the client from the latest credential update. It is recommended that this function should
	// only be called under a lock shared with the callback to avoid race conditions.
	// This function may return an error if the stored credentials are not valid.
	GetConnection() (OVirtConnection, error)
	// Run runs in foreground until the context expires.
	Run(ctx context.Context)
}

OVirtCredentialMonitor is a utility to monitor a Kubernetes secret and call a callback function with a new connection whenever the credentials change.

func New

func New(
	connectionConfig ConnectionConfig,
	secretConfig OVirtSecretConfig,
	callbacks Callbacks,
	logger Logger,
) (OVirtCredentialMonitor, error)

type OVirtSecretConfig

type OVirtSecretConfig struct {
	// Name is the name of the secret to be read.
	Name string
	// Namespace is the optional namespace specification.
	Namespace string
}

func (OVirtSecretConfig) String added in v0.2.0

func (o OVirtSecretConfig) String() string

func (OVirtSecretConfig) Validate

func (o OVirtSecretConfig) Validate() error

Jump to

Keyboard shortcuts

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