util

package
v1.8.0 Latest Latest
Warning

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

Go to latest
Published: Jan 25, 2023 License: Apache-2.0 Imports: 31 Imported by: 1

Documentation

Index

Constants

View Source
const (
	OperatorNamespaceEnv   = "OPERATOR_NAMESPACE"
	OperatorWebhookModeEnv = "WEBHOOK_MODE"
	ContainerAppName       = "APP"
	ContainerOperatorApp   = "OPERATOR"
	ContainerWebhookApp    = "WEBHOOK"
	HcoKvIoVersionName     = "HCO_KV_IO_VERSION"
	KubevirtVersionEnvV    = "KUBEVIRT_VERSION"
	CdiVersionEnvV         = "CDI_VERSION"
	CnaoVersionEnvV        = "NETWORK_ADDONS_VERSION"
	SspVersionEnvV         = "SSP_VERSION"
	TtoVersionEnvV         = "TTO_VERSION"
	HppoVersionEnvV        = "HPPO_VERSION"
	KvUiPluginImageEnvV    = "KV_CONSOLE_PLUGIN_IMAGE"
	HcoValidatingWebhook   = "validate-hco.kubevirt.io"
	HcoMutatingWebhookNS   = "mutate-ns-hco.kubevirt.io"
	AppLabel               = "app"
	UndefinedNamespace     = ""
	OpenshiftNamespace     = "openshift"
	OperatorTestNamespace  = "test-operators"
	OperatorHubNamespace   = "operators"
	APIVersionAlpha        = "v1alpha1"
	APIVersionBeta         = "v1beta1"
	CurrentAPIVersion      = APIVersionBeta
	APIVersionGroup        = "hco.kubevirt.io"
	APIVersion             = APIVersionGroup + "/" + CurrentAPIVersion
	HyperConvergedKind     = "HyperConverged"
	// Recommended labels by Kubernetes. See
	// https://kubernetes.io/docs/concepts/overview/working-with-objects/common-labels/
	AppLabelPrefix    = "app.kubernetes.io"
	AppLabelVersion   = AppLabelPrefix + "/version"
	AppLabelManagedBy = AppLabelPrefix + "/managed-by"
	AppLabelPartOf    = AppLabelPrefix + "/part-of"
	AppLabelComponent = AppLabelPrefix + "/component"
	// Operator name for managed-by label
	OperatorName = "hco-operator"
	// Value for "part-of" label
	HyperConvergedCluster    = "hyperconverged-cluster"
	OpenshiftNodeSelectorAnn = "openshift.io/node-selector"
	KubernetesMetadataName   = "kubernetes.io/metadata.name"

	// PrometheusNSLabel is the monitoring NS enable label, if the value is "true"
	PrometheusNSLabel = "openshift.io/cluster-monitoring"

	// HyperConvergedName is the name of the HyperConverged resource that will be reconciled
	HyperConvergedName          = "kubevirt-hyperconverged"
	MetricsHost                 = "0.0.0.0"
	MetricsPort           int32 = 8383
	HealthProbeHost             = "0.0.0.0"
	HealthProbePort       int32 = 6060
	ReadinessEndpointName       = "/readyz"
	LivenessEndpointName        = "/livez"
	HCOWebhookPath              = "/validate-hco-kubevirt-io-v1beta1-hyperconverged"
	HCONSWebhookPath            = "/mutate-ns-hco-kubevirt-io"
	DefaulterWebhookPath        = "/mutate-hco-kubevirt-io-v1beta1-hyperconverged"
	WebhookPort                 = 4343

	WebhookCertName       = "apiserver.crt"
	WebhookKeyName        = "apiserver.key"
	DefaultWebhookCertDir = "/apiserver.local.config/certificates"

	CliDownloadsServerPort       = 8080
	UiPluginServerPort     int32 = 9443

	ApiServerCRName = "cluster"

	DataImportCronEnabledAnnotation = "dataimportcrontemplate.kubevirt.io/enable"
)

HCO common constants

View Source
const (
	UpgradeableInitReason  = "Initializing"
	UpgradeableInitMessage = "The hyperConverged cluster operator is starting up"

	UpgradeableUpgradingReason  = "AlreadyPerformingUpgrade"
	UpgradeableUpgradingMessage = "upgrading the hyperConverged cluster operator to version "

	UpgradeableAllowReason  = "Upgradeable"
	UpgradeableAllowMessage = ""
)
View Source
const (
	LocalRunMode   RunModeType = "local"
	ClusterRunMode RunModeType = "cluster"

	// WatchNamespaceEnvVar is the constant for env variable WATCH_NAMESPACE
	// which is the namespace where the watch activity happens.
	// this value is empty if the operator is running with clusterScope.
	WatchNamespaceEnvVar = "WATCH_NAMESPACE"

	// PodNameEnvVar is the constant for env variable POD_NAME
	// which is the name of the current pod.
	PodNameEnvVar = "POD_NAME"
)
View Source
const OperatorConditionNameEnvVar = "OPERATOR_CONDITION_NAME"

OperatorConditionNameEnvVar - this Env var is set by OLM, so the Operator can discover it's OperatorCondition.

Variables

View Source
var ErrNoNamespace = fmt.Errorf("namespace not found for current environment")

ErrNoNamespace indicates that a namespace could not be found for the current environment

View Source
var ErrRunLocal = fmt.Errorf("operator run mode forced to local")

ErrRunLocal indicates that the operator is set to run in local mode (this error is returned by functions that only work on operators running in cluster mode)

View Source
var ForceRunModeEnv = "OSDK_FORCE_RUN_MODE"

ForceRunModeEnv indicates if the operator should be forced to run in either local or cluster mode (currently only used for local mode)

View Source
var GetClusterInfo = func() ClusterInfo {
	return clusterInfo
}
View Source
var GetFactory = func(cl client.Client) conditions.Factory {
	if operatorConditionFactory == nil {
		operatorConditionFactory = conditions.InClusterFactory{Client: cl}
	}
	return operatorConditionFactory
}
View Source
var GetOperatorNamespace = func(logger logr.Logger) (string, error) {
	if IsRunModeLocal() {
		return "", ErrRunLocal
	}
	nsBytes, err := os.ReadFile("/var/run/secrets/kubernetes.io/serviceaccount/namespace")
	if err != nil {
		if os.IsNotExist(err) {
			return "", ErrNoNamespace
		}
		return "", err
	}
	ns := strings.TrimSpace(string(nsBytes))
	logger.Info("Found namespace", "Namespace", ns)
	return ns, nil
}

GetOperatorNamespace returns the namespace the operator should be running in.

Functions

func AddCrToTheRelatedObjectList added in v1.7.0

func AddCrToTheRelatedObjectList(relatedObjects *[]corev1.ObjectReference, found client.Object, scheme *runtime.Scheme) (bool, error)

func ComponentResourceRemoval added in v1.2.0

func ComponentResourceRemoval(ctx context.Context, c client.Client, obj interface{}, hcoName string, logger logr.Logger, dryRun bool, wait bool, protectNonHCOObjects bool) (bool, error)

ComponentResourceRemoval removes the resource `obj` if it exists and belongs to the HCO with wait=true it will wait, (util ctx timeout, please set it!) for the resource to be effectively deleted

func ContainsString added in v1.3.0

func ContainsString(s []string, word string) bool

func DeepCopyLabels added in v1.4.0

func DeepCopyLabels(src, tgt *metav1.ObjectMeta)

func EnsureDeleted added in v1.2.0

func EnsureDeleted(ctx context.Context, c client.Client, obj client.Object, hcoName string, logger logr.Logger, dryRun bool, wait bool, protectNonHCOObjects bool) (bool, error)

EnsureDeleted calls ComponentResourceRemoval if the runtime object exists with wait=true it will wait, (util ctx timeout, please set it!) for the resource to be effectively deleted

func GetHcoKvIoVersion added in v1.4.0

func GetHcoKvIoVersion() string

func GetHcoPing added in v1.3.0

func GetHcoPing() healthz.Checker

func GetLabels added in v1.7.0

func GetLabels(hcName string, component AppComponent) map[string]string

func GetManifestDirPath added in v1.5.0

func GetManifestDirPath(envVarName string, defaultDir string) string

func GetOperatorNamespaceFromEnv

func GetOperatorNamespaceFromEnv() (string, error)

func GetRuntimeObject added in v1.2.0

func GetRuntimeObject(ctx context.Context, c client.Client, obj client.Object, logger logr.Logger) error

GetRuntimeObject will query the apiserver for the object

func GetWatchNamespace added in v1.3.0

func GetWatchNamespace() (string, error)

GetWatchNamespace returns the namespace the operator should be watching for changes

func IsActuallyNil added in v1.7.0

func IsActuallyNil(object interface{}) bool

IsActuallyNil checks if an interface object is actually nil. Just checking for == nil won't work, if the parameter is a pointer variable that holds nil.

func IsRunModeLocal added in v1.4.0

func IsRunModeLocal() bool

func NewProcessingError added in v1.5.0

func NewProcessingError(err error) error

NewProcessingError wraps an error if we want to actually stop processing by the error. unwrapping it will return the original error. unwrapping a regular error will return nil

func ToUnstructured added in v1.4.0

func ToUnstructured(obj interface{}) (*unstructured.Unstructured, error)

ToUnstructured converts an arbitrary object (which MUST obey the k8s object conventions) to an Unstructured

func UnmarshalYamlFileToObject added in v1.5.0

func UnmarshalYamlFileToObject(file io.Reader, o interface{}) error

func ValidateManifestDir added in v1.5.0

func ValidateManifestDir(dir string) error

ValidateManifestDir checks a directory contains manifests file in YAML format This function returns 3-state error:

err := ValidateManifestDir(...)
err == nil - OK: directory exists
err != nil && errors.Unwrap(err) == nil - directory does not exist, but that ok
err != nil && errors.Unwrap(err) != nil - actual error

Types

type AppComponent added in v1.4.0

type AppComponent string
const (
	AppComponentCompute    AppComponent = "compute"
	AppComponentStorage    AppComponent = "storage"
	AppComponentNetwork    AppComponent = "network"
	AppComponentMonitoring AppComponent = "monitoring"
	AppComponentSchedule   AppComponent = "schedule"
	AppComponentDeployment AppComponent = "deployment"
	AppComponentTekton     AppComponent = "tekton"
)

type ClusterInfo added in v1.1.0

type ClusterInfo interface {
	Init(ctx context.Context, cl client.Client, logger logr.Logger) error
	IsOpenshift() bool
	IsRunningLocally() bool
	GetDomain() string
	IsManagedByOLM() bool
	IsControlPlaneHighlyAvailable() bool
	IsInfrastructureHighlyAvailable() bool
	IsConsolePluginImageProvided() bool
	GetTLSSecurityProfile(hcoTLSSecurityProfile *openshiftconfigv1.TLSSecurityProfile) *openshiftconfigv1.TLSSecurityProfile
	RefreshAPIServerCR(ctx context.Context, c client.Client) error
	GetPod() *corev1.Pod
	GetDeployment() *appsv1.Deployment
	GetCSV() *csvv1alpha1.ClusterServiceVersion
}

type ClusterInfoImp added in v1.1.0

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

func (ClusterInfoImp) GetCSV added in v1.7.0

func (ClusterInfoImp) GetDeployment added in v1.7.0

func (c ClusterInfoImp) GetDeployment() *appsv1.Deployment

func (ClusterInfoImp) GetDomain added in v1.5.0

func (c ClusterInfoImp) GetDomain() string

func (ClusterInfoImp) GetPod added in v1.7.0

func (c ClusterInfoImp) GetPod() *corev1.Pod

func (*ClusterInfoImp) GetTLSSecurityProfile added in v1.7.0

func (c *ClusterInfoImp) GetTLSSecurityProfile(hcoTLSSecurityProfile *openshiftconfigv1.TLSSecurityProfile) *openshiftconfigv1.TLSSecurityProfile

func (*ClusterInfoImp) Init added in v1.6.0

func (c *ClusterInfoImp) Init(ctx context.Context, cl client.Client, logger logr.Logger) error

func (ClusterInfoImp) IsConsolePluginImageProvided added in v1.7.0

func (c ClusterInfoImp) IsConsolePluginImageProvided() bool

func (ClusterInfoImp) IsControlPlaneHighlyAvailable added in v1.6.0

func (c ClusterInfoImp) IsControlPlaneHighlyAvailable() bool

func (ClusterInfoImp) IsInfrastructureHighlyAvailable added in v1.6.0

func (c ClusterInfoImp) IsInfrastructureHighlyAvailable() bool

func (ClusterInfoImp) IsManagedByOLM added in v1.6.0

func (c ClusterInfoImp) IsManagedByOLM() bool

func (ClusterInfoImp) IsOpenshift added in v1.1.0

func (c ClusterInfoImp) IsOpenshift() bool

func (ClusterInfoImp) IsRunningLocally added in v1.2.0

func (c ClusterInfoImp) IsRunningLocally() bool

func (*ClusterInfoImp) RefreshAPIServerCR added in v1.7.0

func (c *ClusterInfoImp) RefreshAPIServerCR(ctx context.Context, cl client.Client) error

type Condition added in v1.6.0

type Condition interface {
	Set(ctx context.Context, status metav1.ConditionStatus, reason, message string) error
}

Condition - We just need the Set method in our code.

type EventEmitter added in v1.2.0

type EventEmitter interface {
	Init(pod *corev1.Pod, csv *csvv1alpha1.ClusterServiceVersion, recorder record.EventRecorder)
	EmitEvent(object runtime.Object, eventType, reason, msg string)
}

func GetEventEmitter added in v1.2.0

func GetEventEmitter() EventEmitter

type OperatorCondition added in v1.6.0

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

OperatorCondition wraps operator-lib's Condition to make it not crash, when running locally or in Kubernetes without OLM.

func NewOperatorCondition added in v1.6.0

func NewOperatorCondition(clusterInfo ClusterInfo, cl client.Client, condType string) (*OperatorCondition, error)

func (*OperatorCondition) Set added in v1.6.0

func (oc *OperatorCondition) Set(ctx context.Context, status metav1.ConditionStatus, reason, message string) error

type OwnResources added in v1.7.0

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

OwnResources holds the running POd, Deployment and CSV, if exist

func (*OwnResources) GetCSV added in v1.7.0

GetCSV returns the CSV that defines the application, or nil if not exists

func (*OwnResources) GetDeployment added in v1.7.0

func (or *OwnResources) GetDeployment() *appsv1.Deployment

GetDeployment returns the deployment that controls the running pod, or nil if not exists

func (*OwnResources) GetPod added in v1.7.0

func (or *OwnResources) GetPod() *corev1.Pod

GetPod returns the running pod, or nil if not exists

type RunModeType added in v1.3.0

type RunModeType string

Jump to

Keyboard shortcuts

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