k8s

package
v0.36.0 Latest Latest
Warning

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

Go to latest
Published: Aug 22, 2023 License: Apache-2.0 Imports: 32 Imported by: 7

Documentation

Overview

Package k8s gathers abstractions on top of Kubernetes Objects

Index

Constants

View Source
const FinalizerName = "steward.sap.com"

FinalizerName name to register finalizer

Variables

This section is empty.

Functions

func TryExtractTypeInfo added in v0.34.0

func TryExtractTypeInfo(obj interface{}) *metav1.TypeMeta

TryExtractTypeInfo tries to extract Kubernetes API object type information from the given object:

  • obj type T is "k8s.io/apimachinery/pkg/apis/meta/v1".TypeMeta or *"k8s.io/apimachinery/pkg/apis/meta/v1".TypeMeta.
  • The type T of obj or *T has the following methods: GetAPIVersion() string, GetKind() string
  • obj is a struct or a pointer to a struct. The struct has a field named "TypeMeta" of type "k8s.io/apimachinery/pkg/apis/meta/v1".TypeMeta

If type information is found, it is provided as a pointer to a metav1.TypeMeta struct. It may point to the original object or to a newly created instance. Therefore, modifying the returned instance may change the original object.

Note that the provided struct instance may be (partially) empty.

If obj is nil or no type information has been found, nil is returned.

Types

type ClientFactory

type ClientFactory interface {
	// CoreV1 returns the core/v1 Kubernetes client
	CoreV1() corev1client.CoreV1Interface

	// NetworkingV1 returns the networking/v1 Kubernetes client
	NetworkingV1() networkingv1client.NetworkingV1Interface

	// RbacV1 returns the rbac/v1 Kubernetes client
	RbacV1() rbacv1client.RbacV1Interface

	// Dynamic returns the dynamic Kubernetes client
	Dynamic() dynamic.Interface

	// StewardV1alpha1 returns the steward.sap.com/v1alpha1 Kubernetes client
	StewardV1alpha1() stewardv1alpha1client.StewardV1alpha1Interface

	// StewardInformerFactory returns the informer factory for Steward
	StewardInformerFactory() stewardinformers.SharedInformerFactory

	// TektonV1beta1 returns the tekton.dev/v1beta1 Kubernetes client
	TektonV1beta1() tektonv1beta1client.TektonV1beta1Interface

	// TektonInformerFactory returns the informer factory for Tekton
	TektonInformerFactory() tektoninformers.SharedInformerFactory
}

ClientFactory is the interface for Kubernetes client factories.

func NewClientFactory

func NewClientFactory(logger logr.Logger, config *rest.Config, resyncPeriod time.Duration) ClientFactory

NewClientFactory creates new client factory based on rest config

type PipelineRun

type PipelineRun interface {
	fmt.Stringer

	// GetReference returns a reference to the original PipelineRun API object
	// this instance has been created with.
	GetReference() *v1.ObjectReference

	// GetAPIObject returns the underlying PipelineRun API object which
	// may contain uncommitted changes.
	// The underlying PipelineRun API object MUST NOT be modified.
	GetAPIObject() *api.PipelineRun

	// GetStatus returns the status of the underlying PipelineRun API object.
	// The status of the underlying PipelineRun object MUST NOT be modified
	// directly. Instead the provided update functions must be used.
	GetStatus() *api.PipelineStatus

	// GetSpec returns the spec of the underlying PipelineRun API object.
	// The spec of the underlying PipelineRun object MUST NOT be modified.
	GetSpec() *api.PipelineSpec

	// GetName returns the name of the underlying PipelineRun API object.
	GetName() string

	// GetKey returns the key of the pipeline run to be used in the
	// controller workqueue or informer cache.
	GetKey() string

	// GetRunNamespace returns the namespace in which the build takes place
	// if available in the status of the underlying PipelineRun API object,
	// otherwise the empty string.
	GetRunNamespace() string

	// GetAuxNamespace returns the namespace hosting auxiliary services
	// if available in the status of the underlying PipelineRun API object,
	// otherwise the empty string.
	GetAuxNamespace() string

	// GetNamespace returns the namespace of the underlying pipelineRun object
	GetNamespace() string

	// GetValidatedJenkinsfileRepoServerURL validates the Jenkinsfile
	// Git repository URL and returns the URL without path.
	GetValidatedJenkinsfileRepoServerURL() (string, error)

	// HasDeletionTimestamp return whether the underlying PipelineRun API
	// object has a deletion timestamp set.
	HasDeletionTimestamp() bool

	// AddFinalizerAndCommitIfNotPresent adds the Steward finalizer to the list
	// of finalizers of the underlying PipelineRun API object if it is not
	// present already. The change is immediately committed.
	//
	// There must not be any other pending changes.
	AddFinalizerAndCommitIfNotPresent(ctx context.Context) error

	// CommitStatus writes the status of the underlying PipelineRun object to
	// storage.
	//
	// In case of a conflict (object in storage is different version than
	// ours), the update is retried with backoff:
	//   - wait
	//   - fetch object from storage
	//   - re-apply recorded status changes
	//   - update object status in storage
	//
	// After too many conflicts retrying is aborted, in which case an
	// error is returned.
	//
	// Non-conflict errors are returned without retrying.
	//
	// Pitfall: If the underlying PipelineRun API object was changed in memory
	// compared to the version in storage _before calling this function_,
	// that change _gets_ persisted in case there's _no_ update conflict, but
	// gets _lost_ in case there _is_ an update conflict! This is hard to find
	// by tests, as those typically do not encounter update conflicts.
	CommitStatus(ctx context.Context) ([]*api.StateItem, error)

	// DeleteFinalizerAndCommitIfExists deletes the Steward finalizer from the
	// list of finalizers of the underlying PipelineRun API object if it is
	// present. The change is immediately committed.
	//
	// There must not be any other pending changes.
	DeleteFinalizerAndCommitIfExists(ctx context.Context) error

	// InitState initializes the state as 'new' if it was undefined (empty)
	// before.
	// The state's start time will be set to the object's creation time.
	// Fails if a state is set already.
	InitState(ctx context.Context) error

	// UpdateState sets timestamp as end time of current (defined) state (A) and
	// stores it in the history. If no current state is defined a new state (A)
	// with creation time of the pipeline run as start time is created. It also
	// creates a new current state (B) with timestamp as start time.
	UpdateState(ctx context.Context, state api.State, timestamp metav1.Time) error

	// UpdateResult updates the result and finish timestamp.
	UpdateResult(ctx context.Context, result api.Result, finishedAt metav1.Time)

	// UpdateContainer updates the container info in the status.
	UpdateContainer(ctx context.Context, newContainerState *corev1.ContainerState)

	// StoreErrorAsMessage stores err with prefix as message in the status.
	// If err is nil, the message is NOT updated.
	StoreErrorAsMessage(ctx context.Context, err error, prefix string) error

	// UpdateRunNamespace sets namespace as the run namespace in the status.
	UpdateRunNamespace(namespace string)

	// UpdateAuxNamespace sets namespace as the auxiliary namespace in the
	// status.
	UpdateAuxNamespace(namespace string)

	// UpdateMessage sets msg as message in the status.
	UpdateMessage(msg string)
}

PipelineRun is a set of utility functions working on an underlying api.PipelineRun API object.

func NewPipelineRun

func NewPipelineRun(ctx context.Context, apiObj *api.PipelineRun, factory ClientFactory) (PipelineRun, error)

NewPipelineRun creates a new instance of PipelineRun based on the given apiObj.

If a factory is provided a new version of the pipelinerun is fetched. All changes are done on the fetched object. If no pipeline run can be found matching the apiObj, nil,nil is returned. An error is only returned if a Get for the pipelinerun returns an error other than a NotFound error. If you call with factory nil you can only use the Get* functions If you use functions changing the pipeline run without factroy set you will get an error. The provided PipelineRun object is never modified and copied as late as possible.

type PipelineRunByKeyFetcher

type PipelineRunByKeyFetcher interface {
	// ByKey fetches PipelineRun resource from Kubernetes
	// Return nil,nil if pipeline with key does not exist
	ByKey(ctx context.Context, key string) (*api.PipelineRun, error)
}

PipelineRunByKeyFetcher provides a function to fetch PipelineRuns by their key

type PipelineRunByNameFetcher

type PipelineRunByNameFetcher interface {
	// ByName fetches PipelineRun resource from Kubernetes by name and namespace
	// Return nil,nil if specified pipeline does not exist
	ByName(ctx context.Context, namespace, name string) (*api.PipelineRun, error)
}

PipelineRunByNameFetcher provides a function to fetch PipelineRuns by their name

type PipelineRunFetcher

type PipelineRunFetcher interface {
	PipelineRunByKeyFetcher
	PipelineRunByNameFetcher
}

PipelineRunFetcher combines PipelineRunByKeyFetcher and PipelineRunByNameFetcher

func NewClientBasedPipelineRunFetcher

func NewClientBasedPipelineRunFetcher(client stewardv1alpha1.StewardV1alpha1Interface) PipelineRunFetcher

NewClientBasedPipelineRunFetcher returns a PipelineRunFetcher that retrieves the objects from the given API client.

func NewListerBasedPipelineRunFetcher

func NewListerBasedPipelineRunFetcher(lister stewardLister.PipelineRunLister) PipelineRunFetcher

NewListerBasedPipelineRunFetcher returns a PipelineRunFetcher that retrieves the objects from the given `PipelineRunLister`. The returned fetcher provides the original pointers from the lister. Typically the lister is backed by a shared cache which must not be modified. Consumers should not mutate the original objects, but create deep copies when modification is required.

type RoleName

type RoleName string

RoleName to be attached

type ServiceAccountHelper added in v0.3.10

type ServiceAccountHelper interface {
	GetServiceAccountSecretNameRepeat(ctx context.Context) (string, error)
	GetServiceAccountSecretName(ctx context.Context) (string, error)
}

ServiceAccountHelper implements functions to get service account secret

type ServiceAccountManager

type ServiceAccountManager interface {
	CreateServiceAccount(ctx context.Context, name string, pipelineCloneSecretName string, imagePullSecretNames []string) (*ServiceAccountWrap, error)
	GetServiceAccount(ctx context.Context, name string) (*ServiceAccountWrap, error)
}

ServiceAccountManager manages serviceAccounts

func NewServiceAccountManager

func NewServiceAccountManager(factory ClientFactory, namespace string) ServiceAccountManager

NewServiceAccountManager creates ServiceAccountManager

type ServiceAccountWrap

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

ServiceAccountWrap wraps a Service Account and enriches it with futher things

func (*ServiceAccountWrap) AddRoleBinding

func (a *ServiceAccountWrap) AddRoleBinding(ctx context.Context, clusterRole RoleName, targetNamespace string) (*rbacv1.RoleBinding, error)

AddRoleBinding creates a role binding in the targetNamespace connecting the service account with the specified cluster role

func (*ServiceAccountWrap) AttachImagePullSecrets

func (a *ServiceAccountWrap) AttachImagePullSecrets(secretNames ...string)

AttachImagePullSecrets attaches a number of secrets to the service account. It does NOT create or update the resource via the underlying client.

func (*ServiceAccountWrap) AttachSecrets

func (a *ServiceAccountWrap) AttachSecrets(secretNames ...string)

AttachSecrets attaches a number of secrets to the service account. It does NOT create or update the resource via the underlying client.

func (*ServiceAccountWrap) GetServiceAccount

func (a *ServiceAccountWrap) GetServiceAccount() *corev1.ServiceAccount

GetServiceAccount returns *v1.ServiceAccount

func (ServiceAccountWrap) SetDoAutomountServiceAccountToken added in v0.3.10

func (a ServiceAccountWrap) SetDoAutomountServiceAccountToken(doAutomount bool)

SetDoAutomountServiceAccountToken sets the `automountServiceAccountToken` flag in the service account spec. It does NOT create or update the resource via the underlying client.

func (*ServiceAccountWrap) Update

func (a *ServiceAccountWrap) Update(ctx context.Context) error

Update performs an update of the service account resource object via the underlying client.

Directories

Path Synopsis
Package mocks is a generated GoMock package.
Package mocks is a generated GoMock package.
client-go/corev1
Package corev1 is a generated GoMock package.
Package corev1 is a generated GoMock package.
mocks
Package mocks is a generated GoMock package.
Package mocks is a generated GoMock package.

Jump to

Keyboard shortcuts

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