util

package
v1.0.0-rc2 Latest Latest
Warning

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

Go to latest
Published: Mar 26, 2021 License: Apache-2.0 Imports: 34 Imported by: 6

Documentation

Index

Constants

View Source
const (
	// TraitPrefixKey is prefix of trait name
	TraitPrefixKey = "trait"

	// Dummy used for dummy definition
	Dummy = "dummy"

	// DummyTraitMessage is a message for trait which don't have definition found
	DummyTraitMessage = "No TraitDefinition found, all framework capabilities will work as default"

	// DefinitionNamespaceEnv is env key for specifying a namespace to fetch definition
	DefinitionNamespaceEnv = "DEFINITION_NAMESPACE"
)
View Source
const (
	// ErrUpdateStatus is the error while applying status.
	ErrUpdateStatus = "cannot apply status"
	// ErrLocateAppConfig is the error while locating parent application.
	ErrLocateAppConfig = "cannot locate the parent application configuration to emit event to"
	// ErrLocateWorkload is the error while locate the workload
	ErrLocateWorkload = "cannot find the workload that the trait is referencing to"
	// ErrFetchChildResources is the error while fetching workload child resources
	ErrFetchChildResources = "failed to fetch workload child resources"

	// ErrStoreCapabilityInConfigMap is the error while storing capability in ConfigMap
	ErrStoreCapabilityInConfigMap = "cannot store capability %s in ConfigMap: %v"
	// ErrGenerateOpenAPIV2JSONSchemaForCapability is the error while generating OpenAPI v3 schema
	ErrGenerateOpenAPIV2JSONSchemaForCapability = "cannot generate OpenAPI v3 JSON schema for capability %s: %v"
	// ErrUpdateCapabilityInConfigMap is the error while creating or updating a capability
	ErrUpdateCapabilityInConfigMap = "cannot create or update capability %s in ConfigMap: %v"

	// ErrCreateConvertedWorklaodDefinition is the error while apply a WorkloadDefinition
	ErrCreateConvertedWorklaodDefinition = "cannot create converted WorkloadDefinition %s: %v"
)
View Source
const (
	// AppDefinitionNamespace is context key to define app namespace
	AppDefinitionNamespace namespaceContextKey = iota
)

Variables

View Source
var (
	// KindDeployment is the k8s Deployment kind.
	KindDeployment = reflect.TypeOf(appsv1.Deployment{}).Name()
	// KindService is the k8s Service kind.
	KindService = reflect.TypeOf(corev1.Service{}).Name()
	// ReconcileWaitResult is the time to wait between reconciliation.
	ReconcileWaitResult = reconcile.Result{RequeueAfter: 30 * time.Second}
)

Functions

func AddAnnotations

func AddAnnotations(o labelAnnotationObject, annos map[string]string)

AddAnnotations will merge annotations with existing ones. If any conflict keys, use new value to override existing value.

func AddLabels

func AddLabels(o labelAnnotationObject, labels map[string]string)

AddLabels will merge labels with existing labels. If any conflict keys, use new value to override existing value.

func BeEquivalentToError

func BeEquivalentToError(expected error) types.GomegaMatcher

BeEquivalentToError matches the error to take care of nil.

func ComputeHash

func ComputeHash(trait *v1alpha2.ComponentTrait) string

ComputeHash returns a hash value calculated from pod template and a collisionCount to avoid hash collision. The hash will be safe encoded to avoid bad words.

func ConvertComponentDef2WorkloadDef added in v1.0.0

func ConvertComponentDef2WorkloadDef(dm discoverymapper.DiscoveryMapper, componentDef *v1alpha2.ComponentDefinition,
	workloadDef *v1alpha2.WorkloadDefinition) error

ConvertComponentDef2WorkloadDef help convert a ComponentDefinition to WorkloadDefinition

func ConvertTemplateJSON2Object added in v0.3.3

func ConvertTemplateJSON2Object(capabilityName string, in *runtime.RawExtension, schematic *common.Schematic) (types.Capability, error)

ConvertTemplateJSON2Object convert spec.extension to object

func ConvertWorkloadGVK2Definition added in v1.0.0

func ConvertWorkloadGVK2Definition(dm discoverymapper.DiscoveryMapper, def common.WorkloadGVK) (common.DefinitionReference, error)

ConvertWorkloadGVK2Definition help convert a GVK to DefinitionReference

func DeepHashObject

func DeepHashObject(hasher hash.Hash, objectToWrite interface{})

DeepHashObject writes specified object to hash using the spew library which follows pointers and prints actual values of the nested objects ensuring the hash does not change when a pointer changes.

func FetchScopeDefinition

FetchScopeDefinition fetch corresponding scopeDefinition given a scope

func FetchTraitDefinition

FetchTraitDefinition fetch corresponding traitDefinition given a trait

func FetchWorkload

func FetchWorkload(ctx context.Context, c client.Client, mLog logr.Logger, oamTrait oam.Trait) (
	*unstructured.Unstructured, error)

FetchWorkload fetch the workload that a trait refers to

func FetchWorkloadChildResources

func FetchWorkloadChildResources(ctx context.Context, mLog logr.Logger, r client.Reader,
	dm discoverymapper.DiscoveryMapper, workload *unstructured.Unstructured) ([]*unstructured.Unstructured, error)

FetchWorkloadChildResources fetch corresponding child resources given a workload

func FetchWorkloadDefinition

FetchWorkloadDefinition fetch corresponding workloadDefinition given a workload

func GenTraitName

func GenTraitName(componentName string, ct *v1alpha2.ComponentTrait, traitType string) string

GenTraitName generate trait name

func GetComponent

GetComponent will get Component and RevisionName by AppConfigComponent

func GetDefinition added in v0.3.5

func GetDefinition(ctx context.Context, cli client.Reader, definition runtime.Object, definitionName string) error

GetDefinition get definition from two level namespace

func GetDefinitionName

func GetDefinitionName(dm discoverymapper.DiscoveryMapper, u *unstructured.Unstructured, typeLabel string) (string, error)

GetDefinitionName return the Definition name of any resources the format of the definition of a resource is <kind plurals>.<group> Now the definition name of a resource could also be defined as `definition.oam.dev/name` in `metadata.annotations` typeLabel specified which Definition it is, if specified, will directly get definition from label.

func GetDefinitionNamespaceWithCtx added in v0.3.5

func GetDefinitionNamespaceWithCtx(ctx context.Context) string

GetDefinitionNamespaceWithCtx will get namespace from context, it will try get `AppDefinitionNamespace` key, if not found, will use default system level namespace defined in `systemvar.SystemDefinitonNamespace`

func GetDummyTraitDefinition

func GetDummyTraitDefinition(u *unstructured.Unstructured) *v1alpha2.TraitDefinition

GetDummyTraitDefinition will generate a dummy TraitDefinition for CustomResource that won't block app from running. OAM runtime will report warning if they got this dummy definition.

func GetDummyWorkloadDefinition

func GetDummyWorkloadDefinition(u *unstructured.Unstructured) *v1alpha2.WorkloadDefinition

GetDummyWorkloadDefinition will generate a dummy WorkloadDefinition for CustomResource that won't block app from running. OAM runtime will report warning if they got this dummy definition.

func GetGVKFromDefinition

func GetGVKFromDefinition(dm discoverymapper.DiscoveryMapper, definitionRef common.DefinitionReference) (schema.GroupVersionKind, error)

GetGVKFromDefinition help get Group Version Kind from DefinitionReference

func GetObjectGivenGVKAndName added in v0.3.1

func GetObjectGivenGVKAndName(ctx context.Context, client client.Reader,
	gvk schema.GroupVersionKind, namespace, name string) (*unstructured.Unstructured, error)

GetObjectGivenGVKAndName fetches the kubernetes object given its gvk and name

func GetObjectsGivenGVKAndLabels added in v0.3.3

func GetObjectsGivenGVKAndLabels(ctx context.Context, cli client.Reader,
	gvk schema.GroupVersionKind, namespace string, labels map[string]string) (*unstructured.UnstructuredList, error)

GetObjectsGivenGVKAndLabels fetches the kubernetes object given its gvk and labels by list API

func GetScopeGVK added in v0.3.1

GetScopeGVK Get ScopeDefinition

func JSONMarshal

func JSONMarshal(o interface{}) []byte

JSONMarshal returns the JSON encoding

func LocateParentAppConfig

func LocateParentAppConfig(ctx context.Context, client client.Client, oamObject oam.Object) (oam.Object, error)

LocateParentAppConfig locate the parent application configuration object

func MergeMapOverrideWithDst

func MergeMapOverrideWithDst(src, dst map[string]string) map[string]string

MergeMapOverrideWithDst merges two could be nil maps. Keep the dst for any conflicts,

func Object2Map

func Object2Map(obj interface{}) (map[string]interface{}, error)

Object2Map turn the Object to a map

func Object2RawExtension added in v1.0.0

func Object2RawExtension(obj interface{}) runtime.RawExtension

Object2RawExtension converts an object to a rawExtension

func Object2Unstructured

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

Object2Unstructured converts an object to an unstructured struct

func PassLabel

func PassLabel(parentObj, childObj labelAnnotationObject)

PassLabel passes through labels from the parent to the child object

func PassLabelAndAnnotation

func PassLabelAndAnnotation(parentObj, childObj labelAnnotationObject)

PassLabelAndAnnotation passes through labels and annotation objectMeta from the parent to the child object when annotation or labels has conflicts, the parentObj will override the childObj.

func PatchCondition

func PatchCondition(ctx context.Context, r client.StatusClient, workload ConditionedObject,
	condition ...cpv1alpha1.Condition) error

PatchCondition condition for a conditioned object

func RawExtension2AppConfig added in v1.0.0

func RawExtension2AppConfig(raw runtime.RawExtension) (*v1alpha2.ApplicationConfiguration, error)

RawExtension2AppConfig converts runtime.RawExtention to ApplicationConfiguration

func RawExtension2Map added in v0.3.0

func RawExtension2Map(raw *runtime.RawExtension) (map[string]interface{}, error)

RawExtension2Map will convert rawExtension to map

func RawExtension2Unstructured added in v1.0.0

func RawExtension2Unstructured(raw *runtime.RawExtension) (*unstructured.Unstructured, error)

RawExtension2Unstructured converts a rawExtension to an unstructured struct

func RemoveAnnotations added in v1.0.0

func RemoveAnnotations(o labelAnnotationObject, removeKeys []string)

RemoveAnnotations removes keys that contains in the removekeys slice from the annotation

func RemoveLabels added in v1.0.0

func RemoveLabels(o labelAnnotationObject, removeKeys []string)

RemoveLabels removes keys that contains in the removekeys slice from the label

func SetNamespaceInCtx added in v1.0.0

func SetNamespaceInCtx(ctx context.Context, namespace string) context.Context

SetNamespaceInCtx set app namespace in context, Sometimes webhook handler may receive request that appNs is empty string, and will cause error when search definition So if namespace is empty, it will use `default` namespace by default.

func UnMarshalStringToComponentDefinition added in v1.0.0

func UnMarshalStringToComponentDefinition(s string) (*v1beta1.ComponentDefinition, error)

UnMarshalStringToComponentDefinition parse a string to a componentDefinition object

func UnMarshalStringToTraitDefinition added in v0.3.1

func UnMarshalStringToTraitDefinition(s string) (*v1beta1.TraitDefinition, error)

UnMarshalStringToTraitDefinition parse a string to a traitDefinition object

func UnMarshalStringToWorkloadDefinition added in v0.3.1

func UnMarshalStringToWorkloadDefinition(s string) (*v1alpha2.WorkloadDefinition, error)

UnMarshalStringToWorkloadDefinition parse a string to a workloadDefinition object

func UnpackRevisionData

func UnpackRevisionData(rev *appsv1.ControllerRevision) (*v1alpha2.Component, error)

UnpackRevisionData will unpack revision.Data to Component

Types

type AlreadyExistMatcher

type AlreadyExistMatcher struct {
}

AlreadyExistMatcher matches the error to be already exist

func (AlreadyExistMatcher) FailureMessage

func (matcher AlreadyExistMatcher) FailureMessage(actual interface{}) (message string)

FailureMessage builds an error message.

func (AlreadyExistMatcher) Match

func (matcher AlreadyExistMatcher) Match(actual interface{}) (success bool, err error)

Match matches error.

func (AlreadyExistMatcher) NegatedFailureMessage

func (matcher AlreadyExistMatcher) NegatedFailureMessage(actual interface{}) (message string)

NegatedFailureMessage builds an error message.

type ConditionedObject

type ConditionedObject interface {
	oam.Object

	oam.Conditioned
}

A ConditionedObject is an Object type with condition field

type ErrorMatcher

type ErrorMatcher struct {
	ExpectedError error
}

ErrorMatcher matches errors.

func (ErrorMatcher) FailureMessage

func (matcher ErrorMatcher) FailureMessage(actual interface{}) (message string)

FailureMessage builds an error message.

func (ErrorMatcher) Match

func (matcher ErrorMatcher) Match(actual interface{}) (success bool, err error)

Match matches an error.

func (ErrorMatcher) NegatedFailureMessage

func (matcher ErrorMatcher) NegatedFailureMessage(actual interface{}) (message string)

NegatedFailureMessage builds an error message.

type NotFoundMatcher

type NotFoundMatcher struct {
}

NotFoundMatcher matches the error to be not found.

func (NotFoundMatcher) FailureMessage

func (matcher NotFoundMatcher) FailureMessage(actual interface{}) (message string)

FailureMessage builds an error message.

func (NotFoundMatcher) Match

func (matcher NotFoundMatcher) Match(actual interface{}) (success bool, err error)

Match matches the api error.

func (NotFoundMatcher) NegatedFailureMessage

func (matcher NotFoundMatcher) NegatedFailureMessage(actual interface{}) (message string)

NegatedFailureMessage builds an error message.

type Template added in v0.3.1

type Template struct {
	TemplateStr        string
	Health             string
	CustomStatus       string
	CapabilityCategory types.CapabilityCategory
	Reference          common.WorkloadGVK
	Helm               *common.Helm
	// TODO: Add scope definition too
	ComponentDefinition *v1beta1.ComponentDefinition
	WorkloadDefinition  *v1beta1.WorkloadDefinition
	TraitDefinition     *v1beta1.TraitDefinition
}

Template includes its string, health and its category

func LoadTemplate added in v0.3.1

LoadTemplate Get template according to key

func NewTemplate added in v0.3.3

func NewTemplate(schematic *common.Schematic, status *common.Status, raw *runtime.RawExtension) (*Template, error)

NewTemplate will create template for inner AbstractEngine using.

type WorkloadType added in v1.0.0

type WorkloadType string

WorkloadType describe the workload type of ComponentDefinition

const (
	// ComponentDef describe a workload of Defined by ComponentDefinition
	ComponentDef WorkloadType = "ComponentDef"

	// HELMDef describe a workload refer to HELM
	HELMDef WorkloadType = "HelmDef"

	// ReferWorkload describe an existing workload
	ReferWorkload WorkloadType = "ReferWorkload"
)

Jump to

Keyboard shortcuts

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