util

package
v0.35.0 Latest Latest
Warning

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

Go to latest
Published: Jun 16, 2026 License: Apache-2.0 Imports: 15 Imported by: 4

Documentation

Index

Constants

This section is empty.

Variables

View Source
var EnsureCRD = func(ctx context.Context, config *rest.Config, manifest []byte, groupVersionKind schema.GroupVersionKind) error {
	exists, err := KindExists(config, groupVersionKind)
	if err != nil {
		return err
	} else if exists {
		return nil
	}

	err = wait.ExponentialBackoffWithContext(ctx, wait.Backoff{Duration: time.Second, Factor: 1.5, Cap: 5 * time.Minute, Steps: math.MaxInt32}, func(ctx context.Context) (bool, error) {
		err := applier.ApplyManifest(ctx, config, manifest)
		if err != nil {
			loghelper.Infof("Failed to apply CRD %s: %v", groupVersionKind.String(), err)
			return false, nil
		}
		return true, nil
	})
	if err != nil {
		return fmt.Errorf("failed to apply CRD %s: %w", groupVersionKind.String(), err)
	}

	var errKindExists error
	err = wait.ExponentialBackoffWithContext(ctx, wait.Backoff{Duration: time.Second, Factor: 1.5, Cap: time.Minute, Steps: math.MaxInt32}, func(_ context.Context) (bool, error) {
		var found bool
		found, errKindExists = KindExists(config, groupVersionKind)
		return found, nil
	})
	if err != nil {
		return fmt.Errorf("failed to find CRD %s: %w: %w", groupVersionKind.String(), err, errKindExists)
	}

	return nil
}

EnsureCRD should be replaceable by unit tests

View Source
var KindExists = func(config *rest.Config, groupVersionKind schema.GroupVersionKind) (bool, error) {
	discoveryClient, err := discovery.NewDiscoveryClientForConfig(config)
	if err != nil {
		return false, err
	}

	resources, err := discoveryClient.ServerResourcesForGroupVersion(groupVersionKind.GroupVersion().String())
	if err != nil {
		if kerrors.IsNotFound(err) {
			return false, nil
		}

		return false, err
	}

	for _, r := range resources.APIResources {
		if r.Kind == groupVersionKind.Kind {
			return true, nil
		}
	}

	return false, nil
}

KindExists checks if the given group/version kind is advertised by API discovery and should be replaceable by unit tests

Functions

func Contains

func Contains(needle string, haystack []string) bool

func ExtractClientObjectFromApplyConfiguration added in v0.32.0

func ExtractClientObjectFromApplyConfiguration(obj runtime.ApplyConfiguration) (client.Object, error)

ExtractClientObjectFromApplyConfiguration converts ApplyConfiguration to client.Object for efficient metadata access. It returns an unstructured.Unstructured which implements client.Object

func GetCause added in v0.20.0

func GetCause(err error) string

func MergeClientObjectIntoApplyConfiguration added in v0.32.0

func MergeClientObjectIntoApplyConfiguration(clientObj client.Object, applyConfig runtime.ApplyConfiguration) error

MergeClientObjectIntoApplyConfiguration writes the state of the mutated client object back into the given ApplyConfiguration so that an Apply call sends the mutated content. Use this after MutateObject so that plugin hook mutations (e.g. labels/annotations) are applied to the server instead of being discarded.

Types

This section is empty.

Jump to

Keyboard shortcuts

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