in case /run/secrets/MARIADB_PASSWORD and secrets: MARIADB_PASSWORD are in uppercase, reformat the value of MARIADB_PASSWORD to mariadb-password. Also, reformat the secretconfig of the secrets. getSecretPathsLegacy returns secretSubPath. (#1826)

Signed-off-by: jose luis <2064537+sosan@users.noreply.github.com>
This commit is contained in:
Jose Luis 2024-02-21 05:35:18 +01:00 committed by GitHub
parent d872afb377
commit c0f7e910c8
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 30 additions and 8 deletions

View File

@ -31,6 +31,7 @@ import (
"text/template" "text/template"
"time" "time"
"github.com/compose-spec/compose-go/types"
"github.com/joho/godotenv" "github.com/joho/godotenv"
"github.com/kubernetes/kompose/pkg/kobject" "github.com/kubernetes/kompose/pkg/kobject"
"github.com/kubernetes/kompose/pkg/loader/compose" "github.com/kubernetes/kompose/pkg/loader/compose"
@ -971,3 +972,20 @@ func GetFileName(fileName string) string {
// Not format filename because can begin with .fileName // Not format filename because can begin with .fileName
return fileName return fileName
} }
// reformatSecretConfigUnderscoreWithDash takes a ServiceSecretConfig object as input and returns a new instance of ServiceSecretConfig
// where the values of Source and Target are formatted using the FormatResourceName function to replace underscores with dashes and lowercase,
// while the other fields remain unchanged. This is done to ensure consistency in the format of container names within the service's secret configuration.
// this function ensures that source, target names are in an acceptable format for Kubernetes and other systems that may require a specific naming format.
func reformatSecretConfigUnderscoreWithDash(secretConfig types.ServiceSecretConfig) types.ServiceSecretConfig {
newSecretConfig := types.ServiceSecretConfig{
Source: FormatResourceName(secretConfig.Source),
Target: FormatResourceName(secretConfig.Target),
UID: secretConfig.UID,
GID: secretConfig.GID,
Mode: secretConfig.Mode,
Extensions: secretConfig.Extensions,
}
return newSecretConfig
}

View File

@ -582,18 +582,18 @@ func (k *Kubernetes) CreateSecrets(komposeObject kobject.KomposeObject) ([]*api.
return nil, err return nil, err
} }
data := []byte(dataString) data := []byte(dataString)
fileName := GetFileName(config.File) resourceName := FormatResourceName(name)
secret := &api.Secret{ secret := &api.Secret{
TypeMeta: metav1.TypeMeta{ TypeMeta: metav1.TypeMeta{
Kind: "Secret", Kind: "Secret",
APIVersion: "v1", APIVersion: "v1",
}, },
ObjectMeta: metav1.ObjectMeta{ ObjectMeta: metav1.ObjectMeta{
Name: FormatResourceName(name), Name: resourceName,
Labels: transformer.ConfigLabels(name), Labels: transformer.ConfigLabels(resourceName),
}, },
Type: api.SecretTypeOpaque, Type: api.SecretTypeOpaque,
Data: map[string][]byte{fileName: data}, Data: map[string][]byte{resourceName: data},
} }
objects = append(objects, secret) objects = append(objects, secret)
} else { } else {
@ -800,6 +800,7 @@ func (k *Kubernetes) ConfigSecretVolumes(name string, service kobject.ServiceCon
var volumes []api.Volume var volumes []api.Volume
if len(service.Secrets) > 0 { if len(service.Secrets) > 0 {
for _, secretConfig := range service.Secrets { for _, secretConfig := range service.Secrets {
secretConfig := reformatSecretConfigUnderscoreWithDash(secretConfig)
if secretConfig.UID != "" { if secretConfig.UID != "" {
log.Warnf("Ignore pid in secrets for service: %s", name) log.Warnf("Ignore pid in secrets for service: %s", name)
} }
@ -911,8 +912,8 @@ func (k *Kubernetes) getSecretPathsLegacy(secretConfig types.ServiceSecretConfig
itemPath = lastPart itemPath = lastPart
} }
secretSubPath = "" // We didn't set a SubPath in legacy behavior secretSubPath = itemPath //"" // We didn't set a SubPath in legacy behavior
return itemPath, mountPath, "" return itemPath, mountPath, secretSubPath
} }
// ConfigVolumes configure the container volumes. // ConfigVolumes configure the container volumes.
@ -1165,6 +1166,9 @@ func ConfigEnvs(service kobject.ServiceConfig, opt kobject.ConvertOptions) ([]ap
// Load up the environment variables // Load up the environment variables
for _, v := range service.Environment { for _, v := range service.Environment {
if !keysFromEnvFile[v.Name] { if !keysFromEnvFile[v.Name] {
if strings.Contains(v.Value, "run/secrets") {
v.Value = FormatResourceName(v.Value)
}
envs = append(envs, api.EnvVar{ envs = append(envs, api.EnvVar{
Name: v.Name, Name: v.Name,
Value: v.Value, Value: v.Value,

View File

@ -1196,7 +1196,7 @@ func TestKubernetes_CreateSecrets(t *testing.T) {
Labels: transformer.ConfigLabels(dataSecrets[0].nameSecretConfig), Labels: transformer.ConfigLabels(dataSecrets[0].nameSecretConfig),
}, },
Type: api.SecretTypeOpaque, Type: api.SecretTypeOpaque,
Data: map[string][]byte{"CNAME": []byte("kompose.io")}, Data: map[string][]byte{dataSecrets[0].nameSecretConfig: []byte("kompose.io")},
}, },
}, },
}, },
@ -1216,7 +1216,7 @@ func TestKubernetes_CreateSecrets(t *testing.T) {
Labels: transformer.ConfigLabels(dataSecrets[1].nameSecretConfig), Labels: transformer.ConfigLabels(dataSecrets[1].nameSecretConfig),
}, },
Type: api.SecretTypeOpaque, Type: api.SecretTypeOpaque,
Data: map[string][]byte{"CNAME": []byte("kompose.io")}, Data: map[string][]byte{dataSecrets[1].nameSecretConfig: []byte("kompose.io")},
}, },
}, },
}, },