0

I am following the below blog which explains how to create operator and import another CR into existing one. http://heidloff.net/article/accessing-third-party-custom-resources-go-operators/

here https://github.com/nheidloff/operator-sample-go/blob/aa9fd15605a54f712e1233423236bd152940f238/operator-application/controllers/application_controller.go#L276 , spec is created with hardcoded properties.

I want to import the spark operator types in my operator. https://github.com/GoogleCloudPlatform/spark-on-k8s-operator/blob/master/pkg/apis/sparkoperator.k8s.io/v1beta2/types.go

This spark operator is having say - 100+ types/properties. By following the above blog , i could create the Go object but it would be hardcoded. I want to create the dynamic object based on user provided values in CR YAML. e.g. - customer can provided 25 attributes , sometimes 50 for spark app. I need to have dynamic object created based on user YAML. Can anybody please help me out ?

user10437665
  • 95
  • 2
  • 9
  • You can import the Go types to your project and use them in your CR, the Go struct. – Jonas Nov 20 '22 at 19:45
  • I have done this already. I can access the types in controller but i need to create go struct in reconciler. All examples are having hardcoded values as in above example. I need to have dynamically generated go struct metaobject based on user manifest – user10437665 Nov 21 '22 at 04:18

2 Answers2

0

If you set the spec type to be a json object, you can have the Spec contain arbitrary json/yaml. You don't have to have a strongly typed Spec object, your operator can then decode it and do whatever you want with it during your reconcile operation as long as its you as you can serialize and deserialize it from json. Should be able to set it to json.RawMesage I think?

Scott Schulthess
  • 2,853
  • 2
  • 27
  • 35
0

What do you mean by hardcoded properties?

If I understood it correctly, you want to define an API for a resource which uses both types from an external operator and your customs. You can extend your API using the types from specific properties such as ScheduledSparkApplicationSpec from this. Here is an example API definition in Go:

type MyKindSpec struct {
    // using external third party api (you need to import it)
    SparkAppTemplate v1beta2.ScheduledSparkApplicationSpec `json:"sparkAppTemplate,omitempty"`
    // using kubernetes core api (you need to import it)
    Container v1.Container `json:"container,omitempty"`
    // using custom types
    MyCustomType MyCustomType `json:"myCustomType,omitempty"`
}

type MyCustomType struct {
    FirstField string `json:"firstField,omitempty"`
    SecondField []int `json:"secondField,omitempty"`
}
tuna
  • 136
  • 7