9 Commits

Author SHA1 Message Date
8cf3ff9f73 Add local test directory 2022-05-24 11:17:46 +02:00
114fab4870 Fix the problem with environment as secret
We needed to filter the environment coming from a env file, but declared
as secet in `secret-vars` label

fix #17
2022-05-23 12:11:23 +02:00
5fc8c06d8b Layout again 2022-05-23 11:24:06 +02:00
51ca4e81d1 Layout... 2022-05-23 11:23:27 +02:00
a6f6b91ab9 Layout 2022-05-23 11:20:52 +02:00
63afc3066b Fix problems with local volumes to configmap
- make it possible to mount only one file as configmap
- remove "dots" from path to convert it to volume name
- see #11 that can be resolved
2022-05-23 11:13:42 +02:00
adrian-salas
1e8fd44857 ISSUE-12 - Trim space on port label (#14)
Co-authored-by: Adrian SALAS <adrian.salas@smile.fr>

fix #12
2022-05-22 22:53:05 +02:00
adrian-salas
1403f9b198 ISSUE-13 - Ignore comment prefix in envfiles (#15)
Co-authored-by: Adrian SALAS <adrian.salas@smile.fr>

fix #13
2022-05-22 22:52:07 +02:00
dependabot[bot]
22ef9211ec Bump github.com/compose-spec/compose-go from 1.2.4 to 1.2.5 (#10)
Bumps [github.com/compose-spec/compose-go](https://github.com/compose-spec/compose-go) from 1.2.4 to 1.2.5.
- [Release notes](https://github.com/compose-spec/compose-go/releases)
- [Commits](https://github.com/compose-spec/compose-go/compare/v1.2.4...v1.2.5)

---
updated-dependencies:
- dependency-name: github.com/compose-spec/compose-go
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <support@github.com>

Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2022-05-18 14:20:03 +02:00
9 changed files with 73 additions and 23 deletions

1
.gitignore vendored
View File

@@ -8,3 +8,4 @@ docker-compose*
!examples/**/docker-compose*
.credentials
release.id
configs/

View File

@@ -1,5 +1,5 @@
<div style="text-align:center">
<img src="./misc/logo.png" alt="Katenary Logo" />
<div style="text-align:center; margin: auto" align="center">
<img src="./misc/logo.png" alt="Katenary Logo" style="max-width: 90%" align="center"/>
</div>
Katenary is a tool to help transforming `docker-compose` files to a working Helm Chart for Kubernetes.
@@ -8,7 +8,7 @@ Katenary is a tool to help transforming `docker-compose` files to a working Helm
This project is partially made at [Smile](https://www.smile.eu)
<div style="text-align:center">
<div style="text-align:center" align="center">
<a href="https://www.smile.eu"><img src="./misc/Logo_Smile.png" alt="Smile Logo" width="250" /></a>
</div>

View File

@@ -264,6 +264,10 @@ func buildConfigMapFromPath(name, path string) *helm.ConfigMap {
c, _ := ioutil.ReadFile(f)
files[filename] = string(c)
}
} else {
c, _ := ioutil.ReadFile(path)
_, filename := filepath.Split(path)
files[filename] = string(c)
}
cm := helm.NewConfigMap(name, GetRelPath(path))
@@ -356,7 +360,6 @@ func prepareVolumes(deployment, name string, s *types.ServiceConfig, container *
pointToFile := ""
if !stat.IsDir() {
pointToFile = filepath.Base(volname)
volname = filepath.Dir(volname)
}
// the volume is a path and it's explicitally asked to be a configmap in labels
@@ -364,6 +367,7 @@ func prepareVolumes(deployment, name string, s *types.ServiceConfig, container *
cm.K8sBase.Metadata.Name = helm.ReleaseNameTpl + "-" + name + "-" + PathToName(volname)
// build a configmapRef for this volume
volname := PathToName(volname)
volumes = append(volumes, map[string]interface{}{
"name": volname,
"configMap": map[string]string{
@@ -386,7 +390,8 @@ func prepareVolumes(deployment, name string, s *types.ServiceConfig, container *
fileGeneratorChan <- cm
}
} else {
// rmove minus sign from volume name
// It's a Volume. Mount this from PVC to declare.
volname = strings.ReplaceAll(volname, "-", "")
isEmptyDir := false
@@ -568,6 +573,15 @@ func prepareEnvFromFiles(name string, s *types.ServiceConfig, container *helm.Co
secretsFiles = strings.Split(v, ",")
}
var secretVars []string
if v, ok := s.Labels[helm.LABEL_SECRETVARS]; ok {
secretVars = strings.Split(v, ",")
}
for i, s := range secretVars {
secretVars[i] = strings.TrimSpace(s)
}
// manage environment files (env_file in compose)
for _, envfile := range s.EnvFile {
f := PathToName(envfile)
@@ -589,7 +603,7 @@ func prepareEnvFromFiles(name string, s *types.ServiceConfig, container *helm.Co
}
envfile = filepath.Join(compose.GetCurrentDir(), envfile)
if err := store.AddEnvFile(envfile); err != nil {
if err := store.AddEnvFile(envfile, secretVars); err != nil {
logger.ActivateColors = true
logger.Red(err.Error())
logger.ActivateColors = false

View File

@@ -7,7 +7,7 @@ import (
)
// replaceChars replaces some chars in a string.
const replaceChars = `[^a-zA-Z0-9._]`
const replaceChars = `[^a-zA-Z0-9_]+`
// GetRelPath return the relative path from the root of the project.
func GetRelPath(path string) string {

14
generator/utils_test.go Normal file
View File

@@ -0,0 +1,14 @@
package generator
import (
"katenary/compose"
"testing"
)
func Test_PathToName(t *testing.T) {
path := compose.GetCurrentDir() + "/envéfoo.file"
name := PathToName(path)
if name != "env-foo-file" {
t.Error("Expected env-foo-file, got ", name)
}
}

View File

@@ -77,6 +77,7 @@ func Generate(p *compose.Parser, katernayVersion, appName, appVersion, chartVers
service.Ports = make([]types.ServicePortConfig, 0)
}
for _, port := range strings.Split(ports, ",") {
port = strings.TrimSpace(port)
target, err := strconv.Atoi(port)
if err != nil {
log.Fatal(err)

3
go.mod
View File

@@ -3,10 +3,9 @@ module katenary
go 1.16
require (
github.com/compose-spec/compose-go v1.2.4
github.com/compose-spec/compose-go v1.2.5
github.com/distribution/distribution/v3 v3.0.0-20220505155552-985711c1f414 // indirect
github.com/kr/pretty v0.2.0 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/spf13/cobra v1.4.0
github.com/xeipuuv/gojsonpointer v0.0.0-20190905194746-02993c407bfb // indirect
golang.org/x/mod v0.5.1

11
go.sum
View File

@@ -30,8 +30,8 @@ github.com/bugsnag/osext v0.0.0-20130617224835-0dd3f918b21b/go.mod h1:obH5gd0Bsq
github.com/bugsnag/panicwrap v0.0.0-20151223152923-e2c28503fcd0/go.mod h1:D/8v3kj0zr8ZAKg1AQ6crr+5VwKN5eIywRkfhyM/+dE=
github.com/cespare/xxhash v1.1.0/go.mod h1:XrSqR1VqqWfGrhpAt58auRo0WTKS1nRRg3ghfAqPWnc=
github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw=
github.com/compose-spec/compose-go v1.2.4 h1:nzTFqM8+2J7Veao5Pq5U451thinv3U1wChIvcjX59/A=
github.com/compose-spec/compose-go v1.2.4/go.mod h1:pAy7Mikpeft4pxkFU565/DRHEbDfR84G6AQuiL+Hdg8=
github.com/compose-spec/compose-go v1.2.5 h1:yGaACRJrWTQMFW89/ck2o1qd3gIxAji4EQkgewH3hqc=
github.com/compose-spec/compose-go v1.2.5/go.mod h1:a/H2RY5UejNRhzbNXHygSAxBARTpLmLlaCkh58xfStM=
github.com/coreos/bbolt v1.3.2/go.mod h1:iRUV2dpdMOn7Bo10OQBFzIJO9kkE559Wcmn+qkEiiKk=
github.com/coreos/etcd v3.3.10+incompatible/go.mod h1:uF7uidLiAD3TWHmW31ZFd/JWoc32PjwdhPthX9715RE=
github.com/coreos/go-semver v0.2.0/go.mod h1:nnelYz7RCh+5ahJtPPxZlU+153eP4D4r3EedlOD2RNk=
@@ -47,8 +47,6 @@ github.com/dgrijalva/jwt-go v0.0.0-20170104182250-a601269ab70c/go.mod h1:E3ru+11
github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ=
github.com/dgryski/go-sip13 v0.0.0-20181026042036-e10d5fee7954/go.mod h1:vAd38F8PWV+bWy6jNmig1y/TA+kYO4g3RSRF0IAv0no=
github.com/distribution/distribution/v3 v3.0.0-20210316161203-a01c71e2477e/go.mod h1:xpWTC2KnJMiDLkoawhsPQcXjvwATEBcbq0xevG2YR9M=
github.com/distribution/distribution/v3 v3.0.0-20220504180456-7a6b9e3042bd h1:KRoLSsR7wZ4H2dueR/O6BGBIXDxfOxUVmaMiu1QiQPw=
github.com/distribution/distribution/v3 v3.0.0-20220504180456-7a6b9e3042bd/go.mod h1:qLi7jGj1b5TUaYTB3ekkHiocxHJi8+3CFhXM54MGKBs=
github.com/distribution/distribution/v3 v3.0.0-20220505155552-985711c1f414 h1:KfVB1Z5fm10trO24Rn5Zzocd8sTm5k/gS24ijxQ1aJU=
github.com/distribution/distribution/v3 v3.0.0-20220505155552-985711c1f414/go.mod h1:2oyLKljQFnsI1tzJxjUg4GI+HEpDfzFP3LrGM04rKg0=
github.com/dnaeon/go-vcr v1.0.1/go.mod h1:aBB1+wY4s93YsC3HHjMBMrwTj2R9FHDzUr9KyGc8n1E=
@@ -121,7 +119,6 @@ github.com/mattn/go-shellwords v1.0.12/go.mod h1:EZzvwXDESEeg03EKmM+RmDnNOPKG4lL
github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0=
github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0=
github.com/mitchellh/mapstructure v1.1.2/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y=
github.com/mitchellh/mapstructure v1.4.3/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyuac5Z2hdY=
github.com/mitchellh/mapstructure v1.5.0/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
github.com/mitchellh/osext v0.0.0-20151018003038-5e2d6d41470f/go.mod h1:OkQIRizQZAeMln+1tSwduZz7+Af5oFlKirV/MSYes2A=
@@ -299,6 +296,6 @@ gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b h1:h8qDotaEPuJATrMmW04NCwg7v22aHH28wwpauUhK9Oo=
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gotest.tools/v3 v3.1.0 h1:rVV8Tcg/8jHUkPUorwjaMTtemIMVXfIPKiOqnhEhakk=
gotest.tools/v3 v3.1.0/go.mod h1:fHy7eyTmJFO5bQbUsEGQ1v4m2J3Jz9eWL54TP2/ZuYQ=
gotest.tools/v3 v3.2.0 h1:I0DwBVMGAx26dttAj1BtJLAkVGncrkkUXfJLC4Flt/I=
gotest.tools/v3 v3.2.0/go.mod h1:Mcr9QNxkg0uMvy/YElmo4SpXgJKWgQvYrT7Kw5RzJ1A=
honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=

View File

@@ -9,11 +9,14 @@ import (
// InlineConfig is made to represent a configMap or a secret
type InlineConfig interface {
AddEnvFile(filename string) error
AddEnvFile(filename string, filter []string) error
AddEnv(key, val string) error
Metadata() *Metadata
}
var _ InlineConfig = (*ConfigMap)(nil)
var _ InlineConfig = (*Secret)(nil)
// ConfigMap is made to represent a configMap with data.
type ConfigMap struct {
*K8sBase `yaml:",inline"`
@@ -42,7 +45,7 @@ func (c *ConfigMap) Metadata() *Metadata {
}
// AddEnvFile adds an environment file to the configMap.
func (c *ConfigMap) AddEnvFile(file string) error {
func (c *ConfigMap) AddEnvFile(file string, filter []string) error {
content, err := ioutil.ReadFile(file)
if err != nil {
return err
@@ -50,15 +53,26 @@ func (c *ConfigMap) AddEnvFile(file string) error {
lines := strings.Split(string(content), "\n")
for _, l := range lines {
//Check if the line is a comment
l = strings.TrimSpace(l)
if len(l) == 0 {
isComment := strings.HasPrefix(l, "#")
if len(l) == 0 || isComment {
continue
}
parts := strings.SplitN(l, "=", 2)
if len(parts) < 2 {
return errors.New("The environment file " + file + " is not valid")
}
c.Data[parts[0]] = parts[1]
var skip bool
for _, filterEnv := range filter {
if parts[0] == filterEnv {
skip = true
}
}
if !skip {
c.Data[parts[0]] = parts[1]
}
}
return nil
}
@@ -91,7 +105,7 @@ func NewSecret(name, path string) *Secret {
}
// AddEnvFile adds an environment file to the secret.
func (s *Secret) AddEnvFile(file string) error {
func (s *Secret) AddEnvFile(file string, filter []string) error {
content, err := ioutil.ReadFile(file)
if err != nil {
return err
@@ -100,14 +114,24 @@ func (s *Secret) AddEnvFile(file string) error {
lines := strings.Split(string(content), "\n")
for _, l := range lines {
l = strings.TrimSpace(l)
if len(l) == 0 {
isComment := strings.HasPrefix(l, "#")
if len(l) == 0 || isComment {
continue
}
parts := strings.SplitN(l, "=", 2)
if len(parts) < 2 {
return errors.New("The environment file " + file + " is not valid")
}
s.Data[parts[0]] = fmt.Sprintf(`{{ "%s" | b64enc }}`, parts[1])
var skip bool
for _, filterEnv := range filter {
if parts[0] == filterEnv {
skip = true
}
}
if !skip {
s.Data[parts[0]] = fmt.Sprintf(`{{ "%s" | b64enc }}`, parts[1])
}
}
return nil