Skip to content

Commit

Permalink
feat: forenklet koden til kcat-generator
Browse files Browse the repository at this point in the history
  • Loading branch information
Kyrremann committed Mar 1, 2022
1 parent 76d5eb3 commit 2cad9b4
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 104 deletions.
87 changes: 18 additions & 69 deletions pkg/config/kcat.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,86 +19,35 @@ const (
KafkaCatConfigName = "kcat.conf"
)

func NewKCatConfig(secret *v1.Secret, dest string) Config {
return &KCat{
Config: fmt.Sprintf("# nais %s\n# kcat -F %s -t %s.your.topic\n", time.Now().Truncate(time.Minute), KafkaCatConfigName, secret.Namespace),
Secret: secret,
PrefixPath: dest,
RequiredFiles: map[string]string{
consts.KafkaCertificateKey: consts.KafkaCertificateCrtFile,
consts.KafkaPrivateKeyKey: consts.KafkaPrivateKeyPemFile,
consts.KafkaCAKey: consts.KafkaCACrtFile,
},
RequiredLocation: map[string]string{
consts.KafkaCertificateCrtFile: KafkaCatSslCertificateLocation,
consts.KafkaPrivateKeyPemFile: KafkaCatSslKeyLocation,
consts.KafkaCACrtFile: KafkaCatSslCaLocation,
},
func WriteKCatConfigToFile(secret *v1.Secret, destinationPath string) error {
configFile := fmt.Sprintf("# nais %s\n# kcat -F %s -t %s.your.topic\n", time.Now().Truncate(time.Minute), KafkaCatConfigName, secret.Namespace)
envsToFile := map[string]string{
KafkaCatBootstrapServers: string(secret.Data[consts.KafkaBrokersKey]),
"ssl": KafkaSecurityProtocolLocation,
KafkaCatSslCertificateLocation: filepath.Join(destinationPath, consts.KafkaCertificateCrtFile),
KafkaCatSslKeyLocation: filepath.Join(destinationPath, consts.KafkaPrivateKeyPemFile),
KafkaCatSslCaLocation: filepath.Join(destinationPath, consts.KafkaCACrtFile),
}
}

type KCat struct {
Config string
Secret *v1.Secret
PrefixPath string
RequiredFiles map[string]string
RequiredLocation map[string]string
}
for key, value := range envsToFile {
configFile += fmt.Sprintf("%s=%s\n", key, value)

func (k *KCat) WriteConfigToFile() error {
if err := k.write(); err != nil {
return fmt.Errorf("write %s to file: %s", KafkaCatConfigName, err)
}
return nil
}

func (k *KCat) write() error {
if err := common.WriteToFile(k.PrefixPath, KafkaCatConfigName, []byte(k.Config)); err != nil {
if err := common.WriteToFile(destinationPath, KafkaCatConfigName, []byte(configFile)); err != nil {
return fmt.Errorf("write to file: %s", err)
}
return nil
}

func (k *KCat) Set(key string, value []byte) {
k.Config += fmt.Sprintf("%s=%s\n", key, string(value))
}

func (k *KCat) SetPath(key, path string) {
k.Config += fmt.Sprintf("%s=%s\n", key, path)
}

func (k *KCat) Generate() (string, error) {
err := common.RequiredSecretDataExists(k.RequiredFiles, k.Secret.Data, KafkaCatConfigName)
if err != nil {
return "", err
}

for key, value := range k.Secret.Data {
if err := k.toFile(key, value); err != nil {
return "", fmt.Errorf("write to file for key: %s\n %s", key, err)
}
k.toEnv(key, value)
secretsToFile := map[string]string{
consts.KafkaCertificateKey: consts.KafkaCertificateCrtFile,
consts.KafkaPrivateKeyKey: consts.KafkaPrivateKeyPemFile,
consts.KafkaCAKey: consts.KafkaCACrtFile,
}
k.Config += fmt.Sprintf("%s=ssl\n", KafkaSecurityProtocolLocation)
return k.Config, nil
}

func (k *KCat) toFile(key string, value []byte) error {
path := k.PrefixPath
requiredFile := k.RequiredFiles[key]
if requiredFile != "" {
if err := common.WriteToFile(path, requiredFile, value); err != nil {
for fileName, valueKey := range secretsToFile {
if err := common.WriteToFile(destinationPath, fileName, secret.Data[valueKey]); err != nil {
return err
}
if k.RequiredLocation[requiredFile] != "" {
k.SetPath(k.RequiredLocation[requiredFile], filepath.Join(path, requiredFile))
}
}
return nil
}

func (k *KCat) toEnv(key string, value []byte) {
if key == consts.KafkaBrokersKey {
k.Set(KafkaCatBootstrapServers, value)
}
return nil
}
29 changes: 9 additions & 20 deletions pkg/config/kcat_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import (
"github.com/nais/cli/pkg/consts"
"github.com/nais/cli/pkg/test"
"github.com/stretchr/testify/assert"
"io/ioutil"
"os"
"path/filepath"
"strings"
"testing"
)
Expand All @@ -22,29 +24,16 @@ func TestKcatConfigGenerated(t *testing.T) {
}

tmpDest := test.SetupDest(t)
kcatConfig := NewKCatConfig(test.SetupSecret(envKeys), tmpDest)
result, err := kcatConfig.Generate()
err := WriteKCatConfigToFile(test.SetupSecret(envKeys), tmpDest)
assert.NoError(t, err)

assert.True(t, strings.Contains(result, KafkaCatSslCaLocation))
assert.True(t, strings.Contains(result, KafkaCatSslKeyLocation))
assert.True(t, strings.Contains(result, KafkaCatSslCertificateLocation))
assert.True(t, strings.Contains(result, KafkaSecurityProtocolLocation))

defer os.Remove(tmpDest)
}

func TestKcatSecretMissingRequiredData(t *testing.T) {

var envKeys = []string{
consts.KafkaCAKey,
consts.KafkaCertificateKey,
}
result, err := ioutil.ReadFile(filepath.Join(tmpDest, KafkaCatConfigName))
assert.NoError(t, err)

tmpDest := test.SetupDest(t)
kcatConfig := NewKCatConfig(test.SetupSecret(envKeys), tmpDest)
_, err := kcatConfig.Generate()
assert.EqualError(t, err, "can not generate kcat.conf config, secret missing required key: KAFKA_PRIVATE_KEY")
assert.True(t, strings.Contains(string(result), KafkaCatSslCaLocation))
assert.True(t, strings.Contains(string(result), KafkaCatSslKeyLocation))
assert.True(t, strings.Contains(string(result), KafkaCatSslCertificateLocation))
assert.True(t, strings.Contains(string(result), KafkaSecurityProtocolLocation))

defer os.Remove(tmpDest)
}
18 changes: 3 additions & 15 deletions pkg/secret/secret.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,10 +90,11 @@ func (s *Secret) CreateKafkaConfigs() error {
if err := s.CreateJavaConfig(); err != nil {
return err
}
if err := s.CreateKCatConfig(); err != nil {
err := config.WriteKCatConfigToFile(s.Secret, s.DestinationPath)
if err != nil {
return err
}
err := config.WriteKafkaEnvConfigToFile(s.Secret, s.DestinationPath)
err = config.WriteKafkaEnvConfigToFile(s.Secret, s.DestinationPath)
if err != nil {
return err
}
Expand Down Expand Up @@ -126,16 +127,3 @@ func (s *Secret) CreateJavaConfig() error {
}
return nil
}

func (s *Secret) CreateKCatConfig() error {
kCatConfig := config.NewKCatConfig(s.Secret, s.DestinationPath)
_, err := kCatConfig.Generate()
if err != nil {
return err
}

if err := kCatConfig.WriteConfigToFile(); err != nil {
return err
}
return nil
}

0 comments on commit 2cad9b4

Please sign in to comment.