user10437665
user10437665

Reputation: 115

create dynamic object in GO operator controller

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 ?

Upvotes: 1

Views: 376

Answers (2)

tuna
tuna

Reputation: 308

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"`
}

Upvotes: 0

Scott Schulthess
Scott Schulthess

Reputation: 2923

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?

Upvotes: 0

Related Questions