Skip to content

Commit

Permalink
Merge pull request kubernetes#1799 from sosan/fix-issue-1778
Browse files Browse the repository at this point in the history
fix issue 1778
  • Loading branch information
k8s-ci-robot authored and sosan committed Feb 14, 2024
2 parents 4211798 + eda13cf commit 932c9b0
Show file tree
Hide file tree
Showing 3 changed files with 149 additions and 5 deletions.
30 changes: 30 additions & 0 deletions pkg/transformer/kubernetes/k8sutils.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import (
"text/template"
"time"

"github.com/compose-spec/compose-go/types"
"github.com/joho/godotenv"
"github.com/kubernetes/kompose/pkg/kobject"
"github.com/kubernetes/kompose/pkg/loader/compose"
Expand Down Expand Up @@ -959,3 +960,32 @@ func GetContainerArgs(service kobject.ServiceConfig) []string {
}
return args
}

// GetFileName extracts the file name from a given file path or file name.
// If the input fileName contains a "/", it retrieves the substring after the last "/".
// The function does not format the file name further, as it may contain periods or other valid characters.
// Returns the extracted file name.
func GetFileName(fileName string) string {
if strings.Contains(fileName, "/") {
fileName = fileName[strings.LastIndex(fileName, "/")+1:]
}
// Not format filename because can begin with .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
}
15 changes: 10 additions & 5 deletions pkg/transformer/kubernetes/kubernetes.go
Original file line number Diff line number Diff line change
Expand Up @@ -582,17 +582,18 @@ func (k *Kubernetes) CreateSecrets(komposeObject kobject.KomposeObject) ([]*api.
return nil, err
}
data := []byte(dataString)
resourceName := FormatResourceName(name)
secret := &api.Secret{
TypeMeta: metav1.TypeMeta{
Kind: "Secret",
APIVersion: "v1",
},
ObjectMeta: metav1.ObjectMeta{
Name: FormatResourceName(name),
Labels: transformer.ConfigLabels(name),
Name: resourceName,
Labels: transformer.ConfigLabels(resourceName),
},
Type: api.SecretTypeOpaque,
Data: map[string][]byte{name: data},
Data: map[string][]byte{resourceName: data},
}
objects = append(objects, secret)
} else {
Expand Down Expand Up @@ -799,6 +800,7 @@ func (k *Kubernetes) ConfigSecretVolumes(name string, service kobject.ServiceCon
var volumes []api.Volume
if len(service.Secrets) > 0 {
for _, secretConfig := range service.Secrets {
secretConfig := reformatSecretConfigUnderscoreWithDash(secretConfig)
if secretConfig.UID != "" {
log.Warnf("Ignore pid in secrets for service: %s", name)
}
Expand Down Expand Up @@ -910,8 +912,8 @@ func (k *Kubernetes) getSecretPathsLegacy(secretConfig types.ServiceSecretConfig
itemPath = lastPart
}

secretSubPath = "" // We didn't set a SubPath in legacy behavior
return itemPath, mountPath, ""
secretSubPath = itemPath //"" // We didn't set a SubPath in legacy behavior
return itemPath, mountPath, secretSubPath
}

// ConfigVolumes configure the container volumes.
Expand Down Expand Up @@ -1164,6 +1166,9 @@ func ConfigEnvs(service kobject.ServiceConfig, opt kobject.ConvertOptions) ([]ap
// Load up the environment variables
for _, v := range service.Environment {
if !keysFromEnvFile[v.Name] {
if strings.Contains(v.Value, "run/secrets") {
v.Value = FormatResourceName(v.Value)
}
envs = append(envs, api.EnvVar{
Name: v.Name,
Value: v.Value,
Expand Down
109 changes: 109 additions & 0 deletions pkg/transformer/kubernetes/kubernetes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1145,3 +1145,112 @@ func TestNamespaceGenerationBlank(t *testing.T) {
}
}
}

func TestKubernetes_CreateSecrets(t *testing.T) {
var komposeDefaultObject []kobject.KomposeObject
dataSecrets := []SecretsConfig{
{
nameSecretConfig: "config-ini",
nameSecret: "debug-config-ini",
pathFile: "../../../docs/CNAME",
},
{
nameSecretConfig: "new-config-init",
nameSecret: "new-debug-config-ini",
pathFile: "../../../docs/CNAME",
},
}

for i := 0; i < len(dataSecrets); i++ {
komposeDefaultObject = append(komposeDefaultObject, newKomposeObject())
komposeDefaultObject[i].Secrets = newSecrets(dataSecrets[i])
}

type fields struct {
Opt kobject.ConvertOptions
}
type args struct {
komposeObject kobject.KomposeObject
}

tests := []struct {
name string
fields fields
args args
want []*api.Secret
wantErr bool
}{
{
name: "CreateSecrets from default KomposeObject and secrets taken from CNAME file",
args: args{
komposeObject: komposeDefaultObject[0],
},
want: []*api.Secret{
{
TypeMeta: metav1.TypeMeta{
Kind: "Secret",
APIVersion: "v1",
},
ObjectMeta: metav1.ObjectMeta{
Name: FormatResourceName(dataSecrets[0].nameSecretConfig),
Labels: transformer.ConfigLabels(dataSecrets[0].nameSecretConfig),
},
Type: api.SecretTypeOpaque,
Data: map[string][]byte{dataSecrets[0].nameSecretConfig: []byte("kompose.io")},
},
},
},
{
name: "CreateSecrets from default KomposeObject and secrets taken from CNAME file",
args: args{
komposeObject: komposeDefaultObject[1],
},
want: []*api.Secret{
{
TypeMeta: metav1.TypeMeta{
Kind: "Secret",
APIVersion: "v1",
},
ObjectMeta: metav1.ObjectMeta{
Name: FormatResourceName(dataSecrets[1].nameSecretConfig),
Labels: transformer.ConfigLabels(dataSecrets[1].nameSecretConfig),
},
Type: api.SecretTypeOpaque,
Data: map[string][]byte{dataSecrets[1].nameSecretConfig: []byte("kompose.io")},
},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
k := &Kubernetes{
Opt: tt.fields.Opt,
}
got, err := k.CreateSecrets(tt.args.komposeObject)
if (err != nil) != tt.wantErr {
t.Errorf("Kubernetes.CreateSecrets() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("Kubernetes.CreateSecrets() = %v, want %v", got, tt.want)
}
})
}
}

// struct defines the configuration parameters required for creating a secret
type SecretsConfig struct {
nameSecretConfig string
nameSecret string
pathFile string
}

// creates a new instance of types.Secrets based on the provided SecretsConfig parameter
func newSecrets(stringsSecretConfig SecretsConfig) types.Secrets {
return types.Secrets{
stringsSecretConfig.nameSecretConfig: types.SecretConfig{
Name: stringsSecretConfig.nameSecret,
File: stringsSecretConfig.pathFile,
},
}
}

0 comments on commit 932c9b0

Please sign in to comment.