Git Product home page Git Product logo

Comments (13)

alexzielenski avatar alexzielenski commented on September 28, 2024

/sig api-machinery

from kubernetes.

jpbetz avatar jpbetz commented on September 28, 2024

We have schemas at runtime don't we? So even though we don't compile, we should be able to run unstructured through UnstructuredToVal and get a correctly typed runtime object?

As a short term workaround, duration(object.duration) >= duration(\"2h\") should work.

from kubernetes.

alexzielenski avatar alexzielenski commented on September 28, 2024

Not only duration format but others as well:

str, ok := unstructured.(string)
if !ok {
return types.NewErr("invalid data, expected string, got %T", unstructured)
}
switch schema.Format() {
case "duration":
d, err := strfmt.ParseDuration(str)
if err != nil {
return types.NewErr("Invalid duration %s: %v", str, err)
}
return types.Duration{Duration: d}
case "date":
d, err := time.Parse(strfmt.RFC3339FullDate, str) // strfmt uses this format for OpenAPIv3 value validation
if err != nil {
return types.NewErr("Invalid date formatted string %s: %v", str, err)
}
return types.Timestamp{Time: d}
case "date-time":
d, err := strfmt.ParseDateTime(str)
if err != nil {
return types.NewErr("Invalid date-time formatted string %s: %v", str, err)
}
return types.Timestamp{Time: time.Time(d)}
case "byte":
base64 := strfmt.Base64{}
err := base64.UnmarshalText([]byte(str))
if err != nil {
return types.NewErr("Invalid byte formatted string %s: %v", str, err)
}
return types.Bytes(base64)
}

In addition to these, there seem to be different types for maplist and setlist, causing subtle differences CEL for how expressions operate on these values:

if schema.Type() == "array" {
l, ok := unstructured.([]interface{})
if !ok {
return types.NewErr("invalid data, expected an array for the provided schema with type=array")
}
if schema.Items() == nil {
return types.NewErr("invalid array type, expected Items with a non-empty Schema")
}
typedList := unstructuredList{elements: l, itemsSchema: schema.Items()}
listType := schema.XListType()
if listType != "" {
switch listType {
case "map":
mapKeys := schema.XListMapKeys()
return &unstructuredMapList{unstructuredList: typedList, escapedKeyProps: escapeKeyProps(mapKeys)}
case "set":
return &unstructuredSetList{unstructuredList: typedList}
case "atomic":
return &typedList
default:
return types.NewErr("invalid x-kubernetes-list-type, expected 'map', 'set' or 'atomic' but got %s", listType)
}
}
return &typedList
}

from kubernetes.

jpbetz avatar jpbetz commented on September 28, 2024

UnstructuredToVal is primarily for encoding maximum sizes etc for static cost checking, so the few differences seem to be limited to the formats which yield different types:

I meant common.UnstructuredToVal. I don't see anything in UnstructuredToVal that I wouldn't want applied to ValidationAdmissionPolicy. duration, date, date-time and byte all need to be coerced, but there is also numeric handling that seems appropriate. And yes, the handling of maps, lists and sets for equality is important.

from kubernetes.

jpbetz avatar jpbetz commented on September 28, 2024

Note that changing a field from type string to type duration, even for runtime, is a breaking change that is observable with expressions like type(object.duration)

EDIT: There are other ways to observe the breaking change too, this is just one example.

from kubernetes.

cici37 avatar cici37 commented on September 28, 2024

/cc
I agree that it would be great to transfer object to CEL value in VAP as what has been done for CRD validation rule. Since VAP is in GA and it would be a breaking change so a feature gate might be needed. Thanks for bringing it up!

from kubernetes.

jpbetz avatar jpbetz commented on September 28, 2024

+1 to the feature gate approach.

from kubernetes.

seans3 avatar seans3 commented on September 28, 2024

/triage accepted

from kubernetes.

T-Lakshmi avatar T-Lakshmi commented on September 28, 2024

/cc

from kubernetes.

jpbetz avatar jpbetz commented on September 28, 2024

@liggitt Is this the one you'd be okay with just fixing w/o a feature gate? It is possible to depend on the broken behavior but it's VERY unlikely (depending on a runtime type check, or expecting a duration to be a string, not a CEL duration type)

from kubernetes.

liggitt avatar liggitt commented on September 28, 2024

@liggitt Is this the one you'd be okay with just fixing w/o a feature gate?

what's the proposed fix?

from kubernetes.

alexzielenski avatar alexzielenski commented on September 28, 2024

@liggitt Is this the one you'd be okay with just fixing w/o a feature gate?

what's the proposed fix?

To change all CEL environments (VAP, MatchConditions) which expose a Kubernetes object to CEL to first call UnstructuredToVal so that the schema is considered in the CEL type.

from kubernetes.

liggitt avatar liggitt commented on September 28, 2024

We have schemas at runtime don't we?

I ... didn't think we did for admission of CRDs

from kubernetes.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.