Add support for bindata in cm (#1418)

Signed-off-by: Hang Yan <hang.yan@hotmail.com>
This commit is contained in:
Hang Yan 2021-08-29 08:33:16 +08:00 committed by GitHub
parent e82fe96c38
commit 0671410009
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 462 additions and 210 deletions

1
go.mod
View File

@ -43,6 +43,7 @@ require (
golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4 // indirect
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c // indirect
golang.org/x/sys v0.0.0-20210510120138-977fb7262007 // indirect
golang.org/x/tools v0.1.1 // indirect
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect
gopkg.in/yaml.v2 v2.3.0
gopkg.in/yaml.v3 v3.0.0-20200615113413-eeeca48fe776

View File

@ -17,6 +17,7 @@ limitations under the License.
package kubernetes
import (
"encoding/base64"
"fmt"
"io/ioutil"
"os"
@ -28,6 +29,8 @@ import (
"strconv"
"strings"
"golang.org/x/tools/godoc/util"
"github.com/fatih/structs"
"github.com/kubernetes/kompose/pkg/kobject"
"github.com/kubernetes/kompose/pkg/loader/compose"
@ -280,7 +283,7 @@ func (k *Kubernetes) IntiConfigMapFromFileOrDir(name, cmName, filePath string, s
dataMap[file.Name()] = data
}
}
configMap.Data = dataMap
initConfigMapData(configMap, dataMap)
case mode.IsRegular():
// do file stuff
@ -306,6 +309,23 @@ func useSubPathMount(cm *api.ConfigMap) bool {
return true
}
func initConfigMapData(configMap *api.ConfigMap, data map[string]string) {
stringData := map[string]string{}
binData := map[string][]byte{}
for k, v := range data {
isText := util.IsText([]byte(v))
if isText {
stringData[k] = v
} else {
binData[k] = []byte(base64.StdEncoding.EncodeToString([]byte(v)))
}
}
configMap.Data = stringData
configMap.BinaryData = binData
}
//InitConfigMapFromFile initializes a ConfigMap object
func (k *Kubernetes) InitConfigMapFromFile(name string, service kobject.ServiceConfig, fileName string) *api.ConfigMap {
content, err := GetContentFromFile(fileName)
@ -313,9 +333,6 @@ func (k *Kubernetes) InitConfigMapFromFile(name string, service kobject.ServiceC
log.Fatalf("Unable to retrieve file: %s", err)
}
dataMap := make(map[string]string)
dataMap[filepath.Base(fileName)] = content
configMapName := ""
for key, tmpConfig := range service.ConfigsMetaData {
if tmpConfig.File == fileName {
@ -331,8 +348,10 @@ func (k *Kubernetes) InitConfigMapFromFile(name string, service kobject.ServiceC
Name: FormatFileName(configMapName),
Labels: transformer.ConfigLabels(name),
},
Data: dataMap,
}
data := map[string]string{filepath.Base(fileName): content}
initConfigMapData(configMap, data)
return configMap
}
@ -925,6 +944,9 @@ func (k *Kubernetes) ConfigConfigMapVolumeSource(cmName string, targetPath strin
for k := range cm.Data {
keys = append(keys, k)
}
for k := range cm.BinaryData {
keys = append(keys, k)
}
key := keys[0]
_, p := path.Split(targetPath)
s.Items = []api.KeyToPath{

View File

@ -527,7 +527,7 @@ sed -e "s;%VERSION%;$version;g" -e "s;%CMD%;$cmd;g" $KOMPOSE_ROOT/script/test/f
convert::expect_success "$cmd" "/tmp/output-k8s.json"
cmd="kompose --provider openshift -f $KOMPOSE_ROOT/script/test/fixtures/service-label/docker-compose.yaml convert --stdout -j"
sed -e "s;%VERSION%;$version;g" -e "s;%CMD%;$cmd;g" $KOMPOSE_ROOT/script/test/fixtures/service-label/output-oc.json > /tmp/output-oc.json
sed -e "s;%VERSION%;$version;g" -e "s;%CMD%;$cmd;g" $KOMPOSE_ROOT/script/test/fixtures/service-label/output-os.json > /tmp/output-os.json
convert::expect_success "$cmd" "/tmp/output-oc.json"
######
@ -645,7 +645,7 @@ sed -e "s;%VERSION%;$version;g" -e "s;%CMD%;$cmd;g" "$KOMPOSE_ROOT/script/test/f
convert::expect_success "$cmd" "/tmp/output-k8s.json"
cmd="kompose convert --stdout --provider=openshift -j --volumes=configMap -f $KOMPOSE_ROOT/script/test/fixtures/configmap-volume/docker-compose.yml"
sed -e "s;%VERSION%;$version;g" -e "s;%CMD%;$cmd;g" "$KOMPOSE_ROOT/script/test/fixtures/configmap-volume/output-oc.json" > /tmp/output-oc.json
sed -e "s;%VERSION%;$version;g" -e "s;%CMD%;$cmd;g" "$KOMPOSE_ROOT/script/test/fixtures/configmap-volume/output-oc.json" > /tmp/output-os.json
convert::expect_success "$cmd" "/tmp/output-oc.json"

View File

@ -124,3 +124,12 @@ k8s_output="$KOMPOSE_ROOT/script/test/fixtures/deploy/placement/output-placement
os_output="$KOMPOSE_ROOT/script/test/fixtures/deploy/placement/output-placement-os.json"
convert::expect_success_and_warning "$k8s_cmd" "$k8s_output"
convert::expect_success_and_warning "$os_cmd" "$os_output"
# test configmap volume
k8s_cmd="kompose -f $KOMPOSE_ROOT/script/test/fixtures/configmap-volume/docker-compose.yml convert --stdout -j --with-kompose-annotation=false --volumes=configMap"
os_cmd="kompose --provider=openshift -f $KOMPOSE_ROOT/script/test/fixtures/configmap-volume/docker-compose.yml convert --stdout -j --with-kompose-annotation=false --volumes=configMap"
k8s_output="$KOMPOSE_ROOT/script/test/fixtures/configmap-volume/output-k8s.json"
os_output="$KOMPOSE_ROOT/script/test/fixtures/configmap-volume/output-os.json"
convert::expect_success_and_warning "$k8s_cmd" "$k8s_output"
convert::expect_success "$os_cmd" "$os_output"

Binary file not shown.

View File

@ -5,3 +5,7 @@ services:
volumes:
- ./tls:/etc/tls
- ./tls/a.key:/etc/test-a-key.key
db:
image: mysql
volumes:
- ./configs.tar:/data/configs.tar

View File

@ -4,18 +4,93 @@
"metadata": {},
"items": [
{
"apiVersion": "apps/v1",
"kind": "Deployment",
"apiVersion": "apps/v1",
"metadata": {
"annotations": {
"kompose.cmd": "%CMD%",
"kompose.version": "%VERSION%"
"name": "db",
"creationTimestamp": null,
"labels": {
"io.kompose.service": "db"
}
},
"spec": {
"replicas": 1,
"selector": {
"matchLabels": {
"io.kompose.service": "db"
}
},
"template": {
"metadata": {
"creationTimestamp": null,
"labels": {
"io.kompose.service": "db"
}
},
"spec": {
"volumes": [
{
"name": "db-cm0",
"configMap": {
"name": "db-cm0",
"items": [
{
"key": "configs.tar",
"path": "configs.tar"
}
]
}
}
],
"containers": [
{
"name": "db",
"image": "mysql",
"resources": {},
"volumeMounts": [
{
"name": "db-cm0",
"mountPath": "/data/configs.tar",
"subPath": "configs.tar"
}
]
}
],
"restartPolicy": "Always"
}
},
"strategy": {
"type": "Recreate"
}
},
"status": {}
},
{
"kind": "ConfigMap",
"apiVersion": "v1",
"metadata": {
"name": "db-cm0",
"creationTimestamp": null,
"labels": {
"io.kompose.service": "db"
},
"annotations": {
"use-subpath": "true"
}
},
"binaryData": {
"configs.tar": "SDRzSUFId3pMVjhBQSsxV3pXc1RRUlFmQ3lMdVJRL2lRVkNHVmFsSVBtWm12N0tXRkFvRksxSU1UU29GVjhLWVRHc2tYK3h1WkV1SjlPaS80TkdiWjIrQ2wrSk4vQ3M4OTZ4WFo3K3lTWGFEc1NZcEpmdExabWJmNC9mbXZielpOeThsNm13eFdtZG12dFpwN3pjT0xEQjdJSVEwUllGQTh4Q3RLQUJHQkdKSnd4S1JzS0xKRUJGRVpCVkFadzZ4eE5DemJHcnlVTXl1MVdVbU0ra0Uzdk1uTzVYZDhvYXgyV25SUmh2dVdzd2N5WlgvVStCZ3ZTREFCWGpRcUJleHJoTkZ3YXFHQks3cGVScFoxeVFkUzdJZ0lkaXlHeTFXeElxdWFycFdJQ2lueWhqcC9Lc0w1LzBMVXZ3UGdxclB6OU5IV1AvSC9ObFdQNzF6MTdmZlRuK2hDT1AxajVBQ29ETFBvRUlzZWYySDUxOGE5SUZjdFU1dG1udGpkZG96OHNIem9jcnk1UHNmY1dIMC9JbWt5T245dndoTWMvOFRMZkgreHdVbHZmd3ZPc0w2bjMzVlJ3anIveGdrMy84eWtjZnJIeEgrL3cvTlBKSUVMSG45ZzhzM3I0QVZBTFpwRFQ0cnd6MFl3TldCcTN3UVBuN3l3ZVZMTjZiYmNxTlMyZkdmUEl1dmZPeU5VVllDL1hVQWJ0VTZyUnp0ZHBzczE2U1czYk5ZbmIrSzdHNnBISEIvOExFRndMV0kxNksxSmxlZWttelZKYnkzbjk3M3FYY2Uvbjc5NE9Qai91ZnZINm9uWDA1dW56VXB5NE40LzUvOVBYQ0cvcy9GdFA4dkFtbi9YMjZFOVQrLzd2L1gvbzlsTkY3L1dNVlMydjhYZ1JjQ2hFZUMyL0ZGeHhFZlFZd3lvZVRKN2lwT3BmSWVNKzY4Rmd3SERxWTFMR2RITmQ0MG9nL1VlY2NRTTlEd3RqVDQzdjZUVTNWOFJYL1l0ZWZidDh1dXIzUHF2WndoK2diUXA3c2ZPT1ExTXhYWkR3NW1qRVEyRHpHaVJ6dTdVVTNlUFRtZUFOeldjR0syc1p3RjgzaWdtU1RIM0hpVEhnNmJRY2ZMYldETEpXaTBvLzJQM0xrZitmTHlEWXR3ZGVoMEJoNVh4ODdmZjROODJYMkpQTEgvajV3WUswNmpOYnZSYWROWFRiYk5xTlV6V2VXd3l4S3NTRklBNGxBdUJ0a1hPYVV2dkR6dk1reVJJa1dLaGVNUENNUUc3Z0FjQUFBPQ=="
}
},
{
"kind": "Deployment",
"apiVersion": "apps/v1",
"metadata": {
"name": "web",
"creationTimestamp": null,
"labels": {
"io.kompose.service": "web"
},
"name": "web"
}
},
"spec": {
"replicas": 1,
@ -24,63 +99,57 @@
"io.kompose.service": "web"
}
},
"strategy": {
"type": "Recreate"
},
"template": {
"metadata": {
"annotations": {
"kompose.cmd": "%CMD%",
"kompose.version": "%VERSION%"
},
"creationTimestamp": null,
"labels": {
"io.kompose.service": "web"
}
},
"spec": {
"containers": [
{
"image": "nginx",
"imagePullPolicy": "",
"name": "web",
"resources": {},
"volumeMounts": [
{
"mountPath": "/etc/tls",
"name": "web-cm0"
},
{
"mountPath": "/etc/test-a-key.key",
"name": "web-cm1",
"subPath": "test-a-key.key"
}
]
}
],
"restartPolicy": "Always",
"serviceAccountName": "",
"volumes": [
{
"name": "web-cm0",
"configMap": {
"name": "web-cm0"
},
"name": "web-cm0"
}
},
{
"name": "web-cm1",
"configMap": {
"name": "web-cm1",
"items": [
{
"key": "a.key",
"path": "test-a-key.key"
}
]
}
}
],
"name": "web-cm1"
"containers": [
{
"name": "web",
"image": "nginx",
"resources": {},
"volumeMounts": [
{
"name": "web-cm0",
"mountPath": "/etc/tls"
},
"name": "web-cm1"
{
"name": "web-cm1",
"mountPath": "/etc/test-a-key.key",
"subPath": "test-a-key.key"
}
]
}
],
"restartPolicy": "Always"
}
},
"strategy": {
"type": "Recreate"
}
},
"status": {}

View File

@ -1,161 +0,0 @@
{
"kind": "List",
"apiVersion": "v1",
"metadata": {},
"items": [
{
"kind": "DeploymentConfig",
"apiVersion": "v1",
"metadata": {
"name": "web",
"creationTimestamp": null,
"labels": {
"io.kompose.service": "web"
},
"annotations": {
"kompose.cmd": "%CMD%",
"kompose.version": "%VERSION%"
}
},
"spec": {
"strategy": {
"type": "Recreate",
"resources": {}
},
"triggers": [
{
"type": "ConfigChange"
},
{
"type": "ImageChange",
"imageChangeParams": {
"automatic": true,
"containerNames": [
"web"
],
"from": {
"kind": "ImageStreamTag",
"name": "web:latest"
}
}
}
],
"replicas": 1,
"test": false,
"selector": {
"io.kompose.service": "web"
},
"template": {
"metadata": {
"creationTimestamp": null,
"labels": {
"io.kompose.service": "web"
}
},
"spec": {
"volumes": [
{
"name": "web-cm0",
"configMap": {
"name": "web-cm0"
}
},
{
"name": "web-cm1",
"configMap": {
"name": "web-cm1",
"items": [
{
"key": "a.key",
"path": "test-a-key.key"
}
]
}
}
],
"containers": [
{
"name": "web",
"image": " ",
"resources": {},
"volumeMounts": [
{
"name": "web-cm0",
"mountPath": "/etc/tls"
},
{
"name": "web-cm1",
"mountPath": "/etc/test-a-key.key",
"subPath": "test-a-key.key"
}
]
}
],
"restartPolicy": "Always"
}
}
},
"status": {}
},
{
"kind": "ImageStream",
"apiVersion": "v1",
"metadata": {
"name": "web",
"creationTimestamp": null,
"labels": {
"io.kompose.service": "web"
}
},
"spec": {
"tags": [
{
"name": "latest",
"annotations": null,
"from": {
"kind": "DockerImage",
"name": "nginx"
},
"generation": null,
"importPolicy": {}
}
]
},
"status": {
"dockerImageRepository": ""
}
},
{
"kind": "ConfigMap",
"apiVersion": "v1",
"metadata": {
"name": "web-cm0",
"creationTimestamp": null,
"labels": {
"io.kompose.service": "web"
}
},
"data": {
"a.crt": "test-crt-data...",
"a.key": "test-key-data...."
}
},
{
"kind": "ConfigMap",
"apiVersion": "v1",
"metadata": {
"name": "web-cm1",
"creationTimestamp": null,
"labels": {
"io.kompose.service": "web"
},
"annotations": {
"use-subpath": "true"
}
},
"data": {
"a.key": "test-key-data...."
}
}
]
}

View File

@ -0,0 +1,308 @@
{
"kind": "List",
"apiVersion": "v1",
"metadata": {},
"items": [
{
"kind": "DeploymentConfig",
"apiVersion": "v1",
"metadata": {
"name": "db",
"creationTimestamp": null,
"labels": {
"io.kompose.service": "db"
}
},
"spec": {
"strategy": {
"type": "Recreate",
"resources": {}
},
"triggers": [
{
"type": "ConfigChange"
},
{
"type": "ImageChange",
"imageChangeParams": {
"automatic": true,
"containerNames": [
"db"
],
"from": {
"kind": "ImageStreamTag",
"name": "db:latest"
}
}
}
],
"replicas": 1,
"test": false,
"selector": {
"io.kompose.service": "db"
},
"template": {
"metadata": {
"creationTimestamp": null,
"labels": {
"io.kompose.service": "db"
}
},
"spec": {
"volumes": [
{
"name": "db-cm0",
"configMap": {
"name": "db-cm0",
"items": [
{
"key": "configs.tar",
"path": "configs.tar"
}
]
}
}
],
"containers": [
{
"name": "db",
"image": " ",
"resources": {},
"volumeMounts": [
{
"name": "db-cm0",
"mountPath": "/data/configs.tar",
"subPath": "configs.tar"
}
]
}
],
"restartPolicy": "Always"
}
}
},
"status": {
"latestVersion": 0,
"observedGeneration": 0,
"replicas": 0,
"updatedReplicas": 0,
"availableReplicas": 0,
"unavailableReplicas": 0
}
},
{
"kind": "ImageStream",
"apiVersion": "v1",
"metadata": {
"name": "db",
"creationTimestamp": null,
"labels": {
"io.kompose.service": "db"
}
},
"spec": {
"lookupPolicy": {
"local": false
},
"tags": [
{
"name": "",
"annotations": null,
"from": {
"kind": "DockerImage",
"name": "mysql"
},
"generation": null,
"importPolicy": {},
"referencePolicy": {
"type": ""
}
}
]
},
"status": {
"dockerImageRepository": ""
}
},
{
"kind": "ConfigMap",
"apiVersion": "v1",
"metadata": {
"name": "db-cm0",
"creationTimestamp": null,
"labels": {
"io.kompose.service": "db"
},
"annotations": {
"use-subpath": "true"
}
},
"binaryData": {
"configs.tar": "SDRzSUFId3pMVjhBQSsxV3pXc1RRUlFmQ3lMdVJRL2lRVkNHVmFsSVBtWm12N0tXRkFvRksxSU1UU29GVjhLWVRHc2tYK3h1WkV1SjlPaS80TkdiWjIrQ2wrSk4vQ3M4OTZ4WFo3K3lTWGFEc1NZcEpmdExabWJmNC9mbXZielpOeThsNm13eFdtZG12dFpwN3pjT0xEQjdJSVEwUllGQTh4Q3RLQUJHQkdKSnd4S1JzS0xKRUJGRVpCVkFadzZ4eE5DemJHcnlVTXl1MVdVbU0ra0Uzdk1uTzVYZDhvYXgyV25SUmh2dVdzd2N5WlgvVStCZ3ZTREFCWGpRcUJleHJoTkZ3YXFHQks3cGVScFoxeVFkUzdJZ0lkaXlHeTFXeElxdWFycFdJQ2lueWhqcC9Lc0w1LzBMVXZ3UGdxclB6OU5IV1AvSC9ObFdQNzF6MTdmZlRuK2hDT1AxajVBQ29ETFBvRUlzZWYySDUxOGE5SUZjdFU1dG1udGpkZG96OHNIem9jcnk1UHNmY1dIMC9JbWt5T245dndoTWMvOFRMZkgreHdVbHZmd3ZPc0w2bjMzVlJ3anIveGdrMy84eWtjZnJIeEgrL3cvTlBKSUVMSG45ZzhzM3I0QVZBTFpwRFQ0cnd6MFl3TldCcTN3UVBuN3l3ZVZMTjZiYmNxTlMyZkdmUEl1dmZPeU5VVllDL1hVQWJ0VTZyUnp0ZHBzczE2U1czYk5ZbmIrSzdHNnBISEIvOExFRndMV0kxNksxSmxlZWttelZKYnkzbjk3M3FYY2Uvbjc5NE9Qai91ZnZINm9uWDA1dW56VXB5NE40LzUvOVBYQ0cvcy9GdFA4dkFtbi9YMjZFOVQrLzd2L1gvbzlsTkY3L1dNVlMydjhYZ1JjQ2hFZUMyL0ZGeHhFZlFZd3lvZVRKN2lwT3BmSWVNKzY4Rmd3SERxWTFMR2RITmQ0MG9nL1VlY2NRTTlEd3RqVDQzdjZUVTNWOFJYL1l0ZWZidDh1dXIzUHF2WndoK2diUXA3c2ZPT1ExTXhYWkR3NW1qRVEyRHpHaVJ6dTdVVTNlUFRtZUFOeldjR0syc1p3RjgzaWdtU1RIM0hpVEhnNmJRY2ZMYldETEpXaTBvLzJQM0xrZitmTHlEWXR3ZGVoMEJoNVh4ODdmZjROODJYMkpQTEgvajV3WUswNmpOYnZSYWROWFRiYk5xTlV6V2VXd3l4S3NTRklBNGxBdUJ0a1hPYVV2dkR6dk1reVJJa1dLaGVNUENNUUc3Z0FjQUFBPQ=="
}
},
{
"kind": "DeploymentConfig",
"apiVersion": "v1",
"metadata": {
"name": "web",
"creationTimestamp": null,
"labels": {
"io.kompose.service": "web"
}
},
"spec": {
"strategy": {
"type": "Recreate",
"resources": {}
},
"triggers": [
{
"type": "ConfigChange"
},
{
"type": "ImageChange",
"imageChangeParams": {
"automatic": true,
"containerNames": [
"web"
],
"from": {
"kind": "ImageStreamTag",
"name": "web:latest"
}
}
}
],
"replicas": 1,
"test": false,
"selector": {
"io.kompose.service": "web"
},
"template": {
"metadata": {
"creationTimestamp": null,
"labels": {
"io.kompose.service": "web"
}
},
"spec": {
"volumes": [
{
"name": "web-cm0",
"configMap": {
"name": "web-cm0"
}
},
{
"name": "web-cm1",
"configMap": {
"name": "web-cm1",
"items": [
{
"key": "a.key",
"path": "test-a-key.key"
}
]
}
}
],
"containers": [
{
"name": "web",
"image": " ",
"resources": {},
"volumeMounts": [
{
"name": "web-cm0",
"mountPath": "/etc/tls"
},
{
"name": "web-cm1",
"mountPath": "/etc/test-a-key.key",
"subPath": "test-a-key.key"
}
]
}
],
"restartPolicy": "Always"
}
}
},
"status": {
"latestVersion": 0,
"observedGeneration": 0,
"replicas": 0,
"updatedReplicas": 0,
"availableReplicas": 0,
"unavailableReplicas": 0
}
},
{
"kind": "ImageStream",
"apiVersion": "v1",
"metadata": {
"name": "web",
"creationTimestamp": null,
"labels": {
"io.kompose.service": "web"
}
},
"spec": {
"lookupPolicy": {
"local": false
},
"tags": [
{
"name": "",
"annotations": null,
"from": {
"kind": "DockerImage",
"name": "nginx"
},
"generation": null,
"importPolicy": {},
"referencePolicy": {
"type": ""
}
}
]
},
"status": {
"dockerImageRepository": ""
}
},
{
"kind": "ConfigMap",
"apiVersion": "v1",
"metadata": {
"name": "web-cm0",
"creationTimestamp": null,
"labels": {
"io.kompose.service": "web"
}
},
"data": {
"a.crt": "test-crt-data...",
"a.key": "test-key-data...."
}
},
{
"kind": "ConfigMap",
"apiVersion": "v1",
"metadata": {
"name": "web-cm1",
"creationTimestamp": null,
"labels": {
"io.kompose.service": "web"
},
"annotations": {
"use-subpath": "true"
}
},
"data": {
"a.key": "test-key-data...."
}
}
]
}