client

package
v0.57.2 Latest Latest
Warning

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

Go to latest
Published: Aug 29, 2024 License: Apache-2.0 Imports: 48 Imported by: 7

Documentation

Overview

Package client provides primitives for interacting with Sonobuoy and the results archive.

Example

Example shows how to create a client and run Sonobuoy.

package main

import (
	"github.com/vmware-tanzu/sonobuoy/pkg/client"
	"github.com/vmware-tanzu/sonobuoy/pkg/config"
	"github.com/vmware-tanzu/sonobuoy/pkg/dynamic"
	"k8s.io/client-go/rest"
)

// Get a rest config from somewhere.
var cfg *rest.Config

// Example shows how to create a client and run Sonobuoy.
func main() {
	// Get an APIHelper with default implementations from client-go.
	apiHelper, err := dynamic.NewAPIHelperFromRESTConfig(cfg)
	if err != nil {
		panic(err)
	}

	// client.NewSonobuoyClient returns a struct that implements the client.Interface.
	sonobuoy, err := client.NewSonobuoyClient(cfg, apiHelper)
	if err != nil {
		panic(err)
	}

	// Each feature of Sonobuoy requires a config to customize the behavior.

	// Build up a RunConfig struct.
	// The command line client provides default values with override flags.
	runConfig := client.RunConfig{
		GenConfig: client.GenConfig{
			PluginEnvOverrides: map[string]map[string]string{
				"e2e": {"E2E_FOCUS": "[sig-networking]"},
			},
			Config:          config.New(),
			EnableRBAC:      true,
			ImagePullPolicy: "Always",
		},
	}

	// Runs sonobuoy on the cluster configured in $HOME/.kube/config.
	if err = sonobuoy.Run(&runConfig); err != nil {
		panic(err)
	}
}
Output:

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func E2EManifest added in v0.17.1

func E2EManifest(cfg *GenConfig) *manifest.Manifest

func SystemdLogsManifest added in v0.17.1

func SystemdLogsManifest(cfg *GenConfig) *manifest.Manifest

func UntarAll

func UntarAll(reader io.Reader, destDir, filename string) (filenames []string, returnErr error)

UntarAll expects a reader that contains tar'd data. It will untar the contents of the reader and write the output into destDir. If filename specified, then files will be named "filename, filename-01, filename-02". It returns a list of all the files it created.

func UntarFile added in v0.51.0

func UntarFile(filename string, destination string, delete bool) error

UntarFile untars the file, filename, into the given destination directory with the given prefix. If delete is true, it deletes the original file after extraction.

Types

type ConditionFuncWithProgress added in v0.53.1

type ConditionFuncWithProgress func() (string, bool, error)

ConditionFuncWithProgress is like wait.ConditionFunc but the extra string allows us to capture status information.

type DeleteConfig

type DeleteConfig struct {
	Namespace  string
	EnableRBAC bool
	DeleteAll  bool
	Wait       time.Duration
	WaitOutput string
	DryRun     bool
}

DeleteConfig are the input options for cleaning up a Sonobuoy run.

func NewDeleteConfig

func NewDeleteConfig() *DeleteConfig

NewDeleteConfig is a DeleteConfig using default images, RBAC enabled, and DeleteAll enabled.

func (*DeleteConfig) Validate added in v0.15.0

func (dc *DeleteConfig) Validate() error

Validate checks the config to determine if it is valid.

type DeletionError added in v0.51.0

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

func (*DeletionError) Error added in v0.51.0

func (d *DeletionError) Error() string

type GenConfig

type GenConfig struct {
	// Plugin transforms allows us to lazily apply generic transformations
	// to plugins after loading them.
	PluginTransforms map[string][]func(*manifest.Manifest) error

	Config          *config.Config
	EnableRBAC      bool
	ImagePullPolicy string
	SSHKeyPath      string

	// DynamicPlugins are plugins which we know by name and whose manifest
	// YAML are generated dynamically using the GenConfig settings.
	DynamicPlugins []string

	// StaticPlugins are plugins whose manifest YAML has been provided
	// explicitly and will be written without further consideration of other
	// GenConfig settings.
	StaticPlugins []*manifest.Manifest

	// PluginEnvOverrides are mappings between plugin name and k-v pairs to be
	// set as env vars on the given plugin. If a plugin has overrides set, it
	// will completely override all other env vars set on the plugin. Provided
	// out of band from the plugins because of how the dynamic plugins are not
	// yet able to be manipulated in this way.
	PluginEnvOverrides map[string]map[string]string

	// NodeSelectors, if set, will be applied to the aggregator pod allowing it
	// to be schedule on particular nodes.
	NodeSelectors map[string]string

	// ShowDefaultPodSpec determines whether or not the default pod spec for
	// the plugin should be included in the output.
	ShowDefaultPodSpec bool

	// The version of Kubernetes to assume. Used to surface for plugin images
	// and env vars.
	KubeVersion string
}

GenConfig are the input options for generating a Sonobuoy manifest.

func NewGenConfig

func NewGenConfig() *GenConfig

NewGenConfig is a GenConfig using the default config and NonDisruptiveConformance mode

func (*GenConfig) Validate added in v0.15.0

func (gc *GenConfig) Validate() error

Validate checks the config to determine if it is valid.

type Interface

type Interface interface {
	// Run generates the manifest, then tries to apply it to the cluster.
	// returns created resources or an error
	Run(cfg *RunConfig) error
	// GenerateManifest fills in a template with a Sonobuoy config
	GenerateManifest(cfg *GenConfig) ([]byte, error)
	// RetrieveResults copies results from a sonobuoy run into a Reader in tar format.
	RetrieveResults(cfg *RetrieveConfig) (io.Reader, <-chan error, error)
	// GetStatus determines the status of the sonobuoy run in order to assist the user.
	GetStatus(cfg *StatusConfig) (*aggregation.Status, error)
	// LogReader returns a reader that contains a merged stream of sonobuoy logs.
	LogReader(cfg *LogConfig) (*Reader, error)
	// Delete removes a sonobuoy run, namespace, and all associated resources.
	Delete(cfg *DeleteConfig) error
	// PreflightChecks runs a number of preflight checks to confirm the environment is good for Sonobuoy
	PreflightChecks(cfg *PreflightConfig) []error
}

Interface is the main contract that we will give to external consumers of this library This will provide a consistent look/feel to upstream and allow us to expose sonobuoy behavior to other automation systems.

type LogConfig

type LogConfig struct {
	// Follow determines if the logs should be followed or not (tail -f).
	Follow bool
	// Namespace is the namespace the sonobuoy aggregator is running in.
	Namespace string
	// Plugin is the name of the plugin to show the logs of.
	Plugin string
	// Out is the writer to write to.
	Out io.Writer
}

LogConfig are the input options for viewing a Sonobuoy run's logs.

func NewLogConfig

func NewLogConfig() *LogConfig

NewLogConfig is a LogConfig with follow disabled and default images.

func (*LogConfig) Validate added in v0.15.0

func (lc *LogConfig) Validate() error

Validate checks the config to determine if it is valid.

type PreflightConfig

type PreflightConfig struct {
	Namespace           string
	DNSNamespace        string
	DNSPodLabels        []string
	PreflightChecksSkip []string
}

PreflightConfig are the options passed to PreflightChecks.

func (*PreflightConfig) Validate added in v0.15.0

func (pfc *PreflightConfig) Validate() error

Validate checks the config to determine if it is valid.

type Reader

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

Reader provides an io.Reader interface to a channel of bytes. The first error received on the error channel will be returned by Read after the bytestream is drained and on all subsequent calls to Read. It is the responsibility of the program writing to bytestream to write an io.EOF to the error stream when it is done and close all channels.

func NewReader

func NewReader(bytestream chan []byte, errc chan error) *Reader

NewReader returns a configured Reader.

func (*Reader) Read

func (r *Reader) Read(p []byte) (int, error)

Read tries to fill up the passed in byte slice with messages from the channel. Read manages the message overflow ensuring no bytes are missed. If an error is set on the reader it will return the error immediately.

type RetrieveConfig

type RetrieveConfig struct {
	// Namespace is the namespace the sonobuoy aggregator is running in.
	Namespace string
	// Path is the location that the aggregator stores results in. Should
	// usually be the same value but can help with debugging some issues.
	Path string
}

RetrieveConfig are the input options for retrieving a Sonobuoy run's results.

func (*RetrieveConfig) Validate added in v0.15.0

func (rc *RetrieveConfig) Validate() error

Validate checks the config to determine if it is valid.

type RunConfig

type RunConfig struct {
	GenConfig
	GenFile    string
	Wait       time.Duration
	WaitOutput string
}

RunConfig are the input options for running Sonobuoy.

func NewRunConfig

func NewRunConfig() *RunConfig

NewRunConfig is a RunConfig with DefaultGenConfig and and preflight checks enabled.

func (*RunConfig) GetNamespace added in v0.55.0

func (rc *RunConfig) GetNamespace() string

GetNamespace will return the namespace from the genconfig or, if loading from a file, will dynamically parse the file and return the namespace from the first namespaced object.

func (*RunConfig) Validate added in v0.15.0

func (rc *RunConfig) Validate() error

Validate checks the config to determine if it is valid.

type SonobuoyClient

type SonobuoyClient struct {
	RestConfig *rest.Config
	// contains filtered or unexported fields
}

SonobuoyClient is a high-level interface to Sonobuoy operations.

func NewSonobuoyClient

func NewSonobuoyClient(restConfig *rest.Config, skc SonobuoyKubeAPIClient) (*SonobuoyClient, error)

NewSonobuoyClient creates a new SonobuoyClient

func (*SonobuoyClient) Client

func (s *SonobuoyClient) Client() (kubernetes.Interface, error)

Client creates or retrieves an existing kubernetes client from the SonobuoyClient's RESTConfig.

func (*SonobuoyClient) Delete

func (c *SonobuoyClient) Delete(cfg *DeleteConfig) error

Delete removes all the resources that Sonobuoy had created including its own namespace, cluster roles/bindings, and optionally e2e scoped namespaces.

func (*SonobuoyClient) GenerateManifest

func (c *SonobuoyClient) GenerateManifest(cfg *GenConfig) ([]byte, error)

GenerateManifest fills in a template with a Sonobuoy config

func (*SonobuoyClient) GenerateManifestAndPlugins added in v0.53.1

func (*SonobuoyClient) GenerateManifestAndPlugins(cfg *GenConfig) ([]byte, []*manifest.Manifest, error)

GenerateManifestAndPlugins fills in a template with a Sonobuoy config and also provides the objects representing the plugins. This is useful if you want to do any structured handling of the resulting plugins that would have been run.

func (*SonobuoyClient) GetStatus

func (c *SonobuoyClient) GetStatus(cfg *StatusConfig) (*aggregation.Status, error)

GetStatus returns the aggregation status that is set as an annotation on the aggregator pod. Returns an error if unable to find the namespace, pod, or annotation. Use GetStatusPod to also return the pod itself so that you can better understand the state of the system.

func (*SonobuoyClient) GetStatusPod added in v0.53.1

func (c *SonobuoyClient) GetStatusPod(cfg *StatusConfig) (*aggregation.Status, *corev1.Pod, error)

GetStatus returns the aggregation status that is set as an annotation on the aggregator pod. Returns an error if unable to find the namespace, pod, or annotation. Also returns the aggregator pod itself so that you can check its exact status or other annotations.

func (*SonobuoyClient) LogReader

func (s *SonobuoyClient) LogReader(cfg *LogConfig) (*Reader, error)

LogReader configures a Reader that provides an io.Reader interface to a merged stream of logs from various containers.

func (*SonobuoyClient) PreflightChecks

func (c *SonobuoyClient) PreflightChecks(cfg *PreflightConfig) []error

PreflightChecks runs all preflight checks in order, returning the first error encountered.

func (*SonobuoyClient) RetrieveResults

func (c *SonobuoyClient) RetrieveResults(cfg *RetrieveConfig) (io.Reader, <-chan error, error)

RetrieveResults copies results from a sonobuoy run into a Reader in tar format. It also returns a channel of errors, where any errors encountered when writing results will be sent, and an error in the case where the config validation fails.

func (*SonobuoyClient) Run

func (c *SonobuoyClient) Run(cfg *RunConfig) error

Run will use the given RunConfig to generate YAML for a series of resources and then create them in the cluster.

func (*SonobuoyClient) RunManifest added in v0.16.2

func (c *SonobuoyClient) RunManifest(cfg *RunConfig, manifest []byte) error

RunManifest is the same as Run(*RunConfig) execpt that the []byte given should represent the output from `sonobuoy gen`, a series of YAML resources separated by `---`. This method will disregard the RunConfig.GenConfig and instead use the given []byte as the manifest.

func (*SonobuoyClient) Version added in v0.14.0

func (c *SonobuoyClient) Version() (string, error)

Version gets the Kubernetes API version

func (*SonobuoyClient) WaitForRun added in v0.55.0

func (c *SonobuoyClient) WaitForRun(cfg *RunConfig) error

WaitForRun handles the 'wait' from sonobuoy run --wait. "Attaches" to the sonobuoy run in the configured namespace and then returns when completed. Returns errors encountered

type SonobuoyKubeAPIClient added in v0.11.4

type SonobuoyKubeAPIClient interface {
	CreateObject(*unstructured.Unstructured) (*unstructured.Unstructured, error)
	Name(*unstructured.Unstructured) (string, error)
	Namespace(*unstructured.Unstructured) (string, error)
	ResourceVersion(*unstructured.Unstructured) (string, error)
}

SonobuoyKubeAPIClient is the interface Sonobuoy uses to communicate with a kube-apiserver.

type StatusConfig added in v0.14.3

type StatusConfig struct {
	// Namespace is the namespace the sonobuoy aggregator is running in.
	Namespace string
}

StatusConfig is the input options for retrieving a Sonobuoy run's results.

func (*StatusConfig) Validate added in v0.15.0

func (sc *StatusConfig) Validate() error

Validate checks the config to determine if it is valid.

Directories

Path Synopsis
Package results provides a low level API to extract data from a Sonobuoy result archive.
Package results provides a low level API to extract data from a Sonobuoy result archive.
e2e
package e2e defines files and directories found in the e2e plugin results.
package e2e defines files and directories found in the e2e plugin results.

Jump to

Keyboard shortcuts

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