common

package
v1.151.0 Latest Latest
Warning

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

Go to latest
Published: May 12, 2026 License: Apache-2.0 Imports: 23 Imported by: 1

Documentation

Index

Constants

This section is empty.

Variables

View Source
var BasicDiff = func(fieldName protoreflect.Name, a, b proto.Message) (bool, error) {
	aField := a.ProtoReflect().Descriptor().Fields().ByName(fieldName)
	bField := b.ProtoReflect().Descriptor().Fields().ByName(fieldName)

	if IsFieldBehavior(aField, annotations.FieldBehavior_OUTPUT_ONLY) {
		return false, nil
	}

	if bField == nil {
		return true, nil
	}
	if !aField.Kind().IsValid() {
		return false, fmt.Errorf("unimplemented kind: %s", aField.Kind().String())
	}

	diff := false
	aVal := a.ProtoReflect().Get(aField)
	bVal := b.ProtoReflect().Get(bField)
	switch aField.Kind() {
	case protoreflect.MessageKind:
		if aField.IsList() || aField.IsMap() {
			if !aVal.Equal(bVal) {
				diff = true
			}
		} else {
			m := aVal.Message().Interface()

			switch m.(type) {
			case *timestamppb.Timestamp:
				if !aVal.Equal(bVal) {
					diff = true
				}
			case *durationpb.Duration:
				if !aVal.Equal(bVal) {
					diff = true
				}
			default:
				return false, fmt.Errorf("field %s not recursed", fieldName)
			}
		}
	default:
		if !aVal.Equal(bVal) {
			diff = true
		}
	}
	if diff && IsFieldBehavior(aField, annotations.FieldBehavior_IMMUTABLE) {
		return false, fmt.Errorf("change to immutable field %s", fieldName)
	}
	return diff, nil
}

Functions

func APIToProto added in v1.145.0

func APIToProto(in any, out proto.Message) error

APIToProto maps a typed-go struct to a proto Message, assuming they have the same json schema

func AddLabelManagedByCNRM added in v1.126.0

func AddLabelManagedByCNRM(labels map[string]string) map[string]string

func CompareProtoMessage added in v1.125.0

func CompareProtoMessage(a, b proto.Message, compareDiff CompareDiff) (sets.Set[string], error)

func CompareProtoMessageStructuredDiff added in v1.145.0

func CompareProtoMessageStructuredDiff(a, b proto.Message, compareDiff CompareDiff) (sets.Set[string], *structuredreporting.Diff, error)

CompareProtoMessageStructuredDiff computes the diff between two proto messages and returns both the set of changed field paths and a structured Diff object.

func ComputeGCPLabels

func ComputeGCPLabels(labels map[string]string) map[string]string

func ExtractProjectID added in v1.149.1

func ExtractProjectID(resourceName string) string

ExtractProjectID extracts the project ID (or number) from a GCP resource name or URL. Supported formats: - https://[hostname]/[version]/projects/[projectID]/... - //compute.googleapis.com/projects/[projectID]/... - projects/[projectID]/...

func IsFieldBehavior added in v1.125.0

func IsFieldBehavior(field protoreflect.FieldDescriptor, fieldBehavior annotations.FieldBehavior) bool

func NormalizeReferences added in v1.126.0

func NormalizeReferences(ctx context.Context, reader client.Reader, obj client.Object, projectRef *refs.ProjectIdentity) error

func NormalizeStringToTimestamp

func NormalizeStringToTimestamp(ts string) *timestamppb.Timestamp

func ProtoToAPI added in v1.145.0

func ProtoToAPI[T any](in proto.Message, out *T) error

ProtoToAPI maps a proto Message to a typed-go struct, assuming they have the same json schema

func RemoveByPrefixes

func RemoveByPrefixes(a map[string]string, prefixes ...string)

func VisitFields added in v1.126.0

func VisitFields(obj any, visitor Visitor) error

func WaitForDoneOrTimeout added in v1.129.2

func WaitForDoneOrTimeout(ctx context.Context, pollInterval time.Duration, doneFunc func() (bool, error)) error

Types

type CompareDiff added in v1.125.0

type CompareDiff func(fieldName protoreflect.Name, a, b proto.Message) (bool, error)

type LegacyCookie added in v1.145.0

type LegacyCookie struct {
	SpecHash string `json:"specHash"`
	GCPHash  string `json:"gcpHash"`
}

LegacyCookie is used for stateful reconciliation. It is stored in the status of the KCC resource.

func NewLegacyCookie added in v1.145.0

func NewLegacyCookie(spec, gcp proto.Message) (*LegacyCookie, error)

func (*LegacyCookie) Equal added in v1.145.0

func (c *LegacyCookie) Equal(lastModifiedCookie *string) bool

func (*LegacyCookie) String added in v1.145.0

func (c *LegacyCookie) String() string

ComposeCookie creates a cookie string from the spec and gcp hashes.

type Visitor added in v1.126.0

type Visitor interface {
	VisitField(path string, value any) error
}

Jump to

Keyboard shortcuts

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