feat(crd): add dual-format Args field supporting map and list
Add a custom Args type that accepts both a key-value map and a flat
CLI-style list, normalizing internally to []string so downstream code
requires no changes.
- Args: new []string alias with UnmarshalJSON (map + list) and
MarshalJSON (map) — keys are sorted for deterministic ordering
- CRD schema: switch args from typed array to
x-kubernetes-preserve-unknown-fields to accept either shape
- Bump go-flink-client to v0.2.2 (fix: programArgsList → programArg
query parameter name for Flink REST API)
Args can now be written as:
args:
kafka-host: broker:9092
kafka-group-id: my-group
Or the existing list format:
args:
- "--kafka-host"
- broker:9092
This commit is contained in:
@@ -1,7 +1,10 @@
|
||||
package v1alpha1
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"sort"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
metaV1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
@@ -9,6 +12,53 @@ import (
|
||||
|
||||
//go:generate go run sigs.k8s.io/controller-tools/cmd/controller-gen object paths=$GOFILE
|
||||
|
||||
// Args is a list of program arguments passed to the Flink job.
|
||||
// It accepts two formats in JSON/YAML:
|
||||
//
|
||||
// List: ["--kafka-host", "broker:9092", "--kafka-group-id", "my-group"]
|
||||
// Map: {kafka-host: "broker:9092", kafka-group-id: "my-group"}
|
||||
//
|
||||
// Map keys are automatically prefixed with "--" and expanded into alternating
|
||||
// key/value pairs. The internal representation is always a flat []string.
|
||||
type Args []string
|
||||
|
||||
// UnmarshalJSON implements dual-format deserialization.
|
||||
func (a *Args) UnmarshalJSON(data []byte) error {
|
||||
// Try map format first: {"key": "value", ...}
|
||||
var m map[string]string
|
||||
if err := json.Unmarshal(data, &m); err == nil {
|
||||
*a = make(Args, 0, len(m)*2)
|
||||
// Sort keys for deterministic ordering.
|
||||
keys := make([]string, 0, len(m))
|
||||
for k := range m {
|
||||
keys = append(keys, k)
|
||||
}
|
||||
sort.Strings(keys)
|
||||
for _, k := range keys {
|
||||
*a = append(*a, "--"+k, m[k])
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Fall back to list format: ["--key", "value", ...]
|
||||
var s []string
|
||||
if err := json.Unmarshal(data, &s); err != nil {
|
||||
return err
|
||||
}
|
||||
*a = Args(s)
|
||||
return nil
|
||||
}
|
||||
|
||||
// MarshalJSON serializes Args in the compact map format.
|
||||
func (a Args) MarshalJSON() ([]byte, error) {
|
||||
m := make(map[string]string, len(a)/2)
|
||||
for i := 0; i < len(a)-1; i += 2 {
|
||||
key := strings.TrimPrefix(a[i], "--")
|
||||
m[key] = a[i+1]
|
||||
}
|
||||
return json.Marshal(m)
|
||||
}
|
||||
|
||||
type FlinkJobSpec struct {
|
||||
Key string `json:"key"`
|
||||
Name string `json:"name"`
|
||||
@@ -19,7 +69,7 @@ type FlinkJobSpec struct {
|
||||
JarURIBasicAuthPassword *string `json:"jarURIBasicAuthPassword"`
|
||||
SavepointInterval metaV1.Duration `json:"savepointInterval"`
|
||||
EntryClass string `json:"entryClass"`
|
||||
Args []string `json:"args"`
|
||||
Args Args `json:"args,omitempty"`
|
||||
}
|
||||
|
||||
type FlinkJobStatus struct {
|
||||
|
||||
Reference in New Issue
Block a user