buehler / dotnet-operator-sdk

KubeOps is a kubernetes operator sdk in dotnet. Strongly inspired by kubebuilder.
https://buehler.github.io/dotnet-operator-sdk/
Apache License 2.0
236 stars 64 forks source link

Add [IgnorePropertyAttribute] that ignores a property when generating CRDs or support TimeSpan properties #452

Closed jefflill closed 2 years ago

jefflill commented 2 years ago

Is your feature request related to a problem? Please describe. I'd like to add TimeSpan properties my custom resource spec and status properties. This isn't supported out of the box:

[KubeOps.Operator.Errors.CrdConversionException]: During conversion of the property "Spec" with type "TaskSpec", an error occured. [inner:KubeOps.Operator.Errors.CrdConversionException: During conversion of the property "RetentionTime" with type "TimeSpan", an error occured.]

I was hoping to define TimeSpan properties with a type converter that converted to/from GOLANG duration format, but TimeSpan isn't supported at all.

As a workaround, I thought I could define the CRD properties as strings and then add hidden properties that did the conversion to/from the string properties, something like:

public class MySpec
{
    public string Timeout { get; set;}

    [IgnoreProperty]
    public TimeSpan TimeoutTimeSpan
    {
        get => GoDuration.Parse(Timeout);
        set => Timeout = GoDuration.FromTimeSpan(value).ToString();
    }
}

This wouldn't actually be too bad, but KubeOps has no [IgnoreProperty] attribute that I can see.

My next workaround is to add GetTimeout() and SetTimeout() methods (not horrible, but feels like Java).

Describe the solution you'd like

  1. Ideally, KubeOps could handle TimeSpan properties natively by parsing GOLANG style durations
  2. Add and implement an [IgnoreProperty] attribute
jefflill commented 2 years ago

FYI: I have a class that handles serialization for GOLANG durations (at least how I understand that they work):

https://github.com/nforgeio/neonKUBE/blob/master/Lib/Neon.Common/Time/GoDuration.cs

buehler commented 2 years ago

Hey @jefflill,

Yes, currently there is no [Ignore] attribute. It seems that this would be a good addition. Currently only scalar and string types are allowed in CRDs since it must be representable in yaml. However, an idea would be to have an ignore attribute and a "converter function" attribute, which must return a scalar type.

jefflill commented 2 years ago

I just finished coding the get/set method approach which is working OK. The [Ignore] attribute would probably be easy to implement, but it won't play well with ASPNET JsonPatchDocument class, so I'm not sure how much help.

The ideal solution would be a converter function like you suggest.

It might be worth thinking about baking in a solution for TimeSpan because I image that this will be quite common.

buehler commented 2 years ago

While that may be true, I'd like to stick to the standard. When we diverge from the standards that are implemented in Kubernetes, every change from K8S may result in a break.

I think the Ignore and Converter attributes are the way to go.

jefflill commented 2 years ago

I agree. I just re-read the API conventions and they explicitly say it's best to encode durations as seconds or something rather than requiring that programs in other languages be able to parse golang durations.