Skip to content

Commit

Permalink
change toleration config to be in yaml format
Browse files Browse the repository at this point in the history
  • Loading branch information
d-rk committed Aug 14, 2024
1 parent 6775b5e commit b8037a5
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 31 deletions.
21 changes: 18 additions & 3 deletions README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -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":"<key>", "operator":"<operator>", "values":["<value>"]}]}]}}'
affinity:
# note: other types of affinity also supported
nodeAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
nodeSelectorTerms:
- matchExpressions:
- key: "<key>"
operator: "<operator>"
values: [ "<value>" ]
# optional: tolerations to add to the pod
tolerations: '[{"effect":"<effect>","key":"<key>","operator":"<operator>","value":"<value>"}]'
tolerations:
- key: "<key>"
operator: "<operator>"
value: "<value>"
effect: "<effect>"
# optional: clientID config (defaults to kafkactl-{username})
clientID: my-client-id
Expand Down Expand Up @@ -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`):

Expand Down
35 changes: 13 additions & 22 deletions internal/common-operation.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package internal
import (
"crypto/tls"
"crypto/x509"
"encoding/json"
"fmt"
"net/http"
"os"
Expand Down Expand Up @@ -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
Expand All @@ -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 {
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion internal/k8s/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
12 changes: 7 additions & 5 deletions internal/k8s/pod_overrides.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package k8s

import "github.com/deviceinsight/kafkactl/v5/internal"

type imagePullSecretType struct {
Name string `json:"name"`
}
Expand All @@ -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 {
Expand Down

0 comments on commit b8037a5

Please sign in to comment.