diff --git a/README.adoc b/README.adoc index 273ba0e..531a30f 100644 --- a/README.adoc +++ b/README.adoc @@ -144,10 +144,25 @@ contexts: # optional: nodeSelector to add to the pod nodeSelector: key: value + # optional: affinity to add to the pod - affnity: '{"nodeAffinity": "requiredDuringSchedulingIgnoredDuringExecution": {nodeSelectorTerms: [{"matchExpressions":[{"key":"", "operator":"", "values":[""]}]}]}}' + affinity: + # note: other types of affinity also supported + nodeAffinity: + requiredDuringSchedulingIgnoredDuringExecution: + nodeSelectorTerms: + - matchExpressions: + - key: "" + operator: "" + values: [ "" ] + # optional: tolerations to add to the pod - tolerations: '[{"effect":"","key":"","operator":"","value":""}]' + tolerations: + - key: "" + operator: "" + value: "" + effect: "" + # optional: clientID config (defaults to kafkactl-{username}) clientID: my-client-id @@ -628,7 +643,7 @@ Producing protobuf message converted from JSON: kafkactl produce my-topic --key='{"keyField":123}' --key-proto-type MyKeyMessage --value='{"valueField":"value"}' --value-proto-type MyValueMessage --proto-file kafkamsg.proto ---- -A more complex protobuf message converted from a multi-line JSON string can be produced using a file input with custom separators. +A more complex protobuf message converted from a multi-line JSON string can be produced using a file input with custom separators. For example, if you have the following protobuf definition (`complex.proto`): diff --git a/internal/common-operation.go b/internal/common-operation.go index ad3233e..b63ceda 100644 --- a/internal/common-operation.go +++ b/internal/common-operation.go @@ -3,7 +3,6 @@ package internal import ( "crypto/tls" "crypto/x509" - "encoding/json" "fmt" "net/http" "os" @@ -62,6 +61,13 @@ type TLSConfig struct { Insecure bool } +type K8sToleration struct { + Key string `json:"key" yaml:"key"` + Operator string `json:"operator" yaml:"operator"` + Value string `json:"value" yaml:"value"` + Effect string `json:"effect" yaml:"effect"` +} + type K8sConfig struct { Enabled bool Binary string @@ -76,7 +82,7 @@ type K8sConfig struct { Annotations map[string]string NodeSelector map[string]string Affinity map[string]any - Tolerations []map[string]any + Tolerations []K8sToleration } type ConsumerConfig struct { @@ -178,28 +184,13 @@ func CreateClientContext() (ClientContext, error) { context.Kubernetes.Annotations = viper.GetStringMapString("contexts." + context.Name + ".kubernetes.annotations") context.Kubernetes.NodeSelector = viper.GetStringMapString("contexts." + context.Name + ".kubernetes.nodeSelector") context.Kubernetes.Affinity = viper.GetStringMap("contexts." + context.Name + ".kubernetes.affinity") - - t, err := convertJsonToListMap("tolerations", viper.GetString("contexts."+context.Name+".kubernetes.tolerations")) - context.Kubernetes.Tolerations = t - if err != nil { - return context, err - } - return context, nil -} -func convertJsonToListMap(fieldName string, jsonStr string) ([]map[string]any, error){ - var listMap []map[string]any - if (jsonStr == "") { - return listMap, nil - } - err := json.Unmarshal([]byte(jsonStr), &listMap) - if err != nil { - fmt.Errorf("Error parsing %s field", fieldName) - return listMap, err - } - return listMap, nil -} + if err := viper.UnmarshalKey("contexts."+context.Name+".kubernetes.tolerations", &context.Kubernetes.Tolerations); err != nil { + return context, err + } + return context, nil +} func CreateClient(context *ClientContext) (sarama.Client, error) { config, err := CreateClientConfig(context) diff --git a/internal/k8s/executor.go b/internal/k8s/executor.go index 5e49490..657bf15 100644 --- a/internal/k8s/executor.go +++ b/internal/k8s/executor.go @@ -38,7 +38,7 @@ type executor struct { annotations map[string]string nodeSelector map[string]string affinity map[string]any - tolerations []map[string]any + tolerations []internal.K8sToleration } const letterBytes = "abcdefghijklmnpqrstuvwxyz123456789" diff --git a/internal/k8s/pod_overrides.go b/internal/k8s/pod_overrides.go index 1ecce65..c56f92d 100644 --- a/internal/k8s/pod_overrides.go +++ b/internal/k8s/pod_overrides.go @@ -1,5 +1,7 @@ package k8s +import "github.com/deviceinsight/kafkactl/v5/internal" + type imagePullSecretType struct { Name string `json:"name"` } @@ -10,11 +12,11 @@ type metadataType struct { } type specType struct { - ImagePullSecrets []imagePullSecretType `json:"imagePullSecrets,omitempty"` - ServiceAccountName *string `json:"serviceAccountName,omitempty"` - NodeSelector *map[string]string `json:"nodeSelector,omitempty"` - Affinity *map[string]any `json:"affinity,omitempty"` - Tolerations *[]map[string]any `json:"tolerations,omitempty"` + ImagePullSecrets []imagePullSecretType `json:"imagePullSecrets,omitempty"` + ServiceAccountName *string `json:"serviceAccountName,omitempty"` + NodeSelector *map[string]string `json:"nodeSelector,omitempty"` + Affinity *map[string]any `json:"affinity,omitempty"` + Tolerations *[]internal.K8sToleration `json:"tolerations,omitempty"` } type PodOverrideType struct {