1
0
mirror of https://github.com/openshift/source-to-image.git synced 2026-02-05 12:44:54 +01:00

Revert "support .s2i/image_metadata.json with --as-dockerfile"

This reverts commit 38d2b729fd.
This commit is contained in:
gabemontero
2019-12-10 11:11:13 -05:00
parent 13e74a7a2b
commit 594f5bf313
5 changed files with 32 additions and 122 deletions

5
.gitignore vendored
View File

@@ -5,8 +5,3 @@
.vscode/
cmd/s2i/debug
*~
.idea
upload
*.classpath
*.project
*.settings

View File

@@ -30,11 +30,8 @@ const (
// DefaultScripts is the location of scripts downloaded from default location (io.openshift.s2i.scripts-url label).
DefaultScripts = "downloads" + string(os.PathSeparator) + "defaultScripts"
// SourceConfig is the location of .s2i directory downloaded with application sources
SourceConfig = "upload" + string(os.PathSeparator) + "src" + string(os.PathSeparator) + ".s2i"
// SourceScripts is the location of scripts downloaded with application sources.
SourceScripts = SourceConfig + string(os.PathSeparator) + "bin"
SourceScripts = "upload" + string(os.PathSeparator) + "src" + string(os.PathSeparator) + ".s2i" + string(os.PathSeparator) + "bin"
// UploadScripts is the location of scripts that will be uploaded to the image during STI build.
UploadScripts = "upload" + string(os.PathSeparator) + "scripts"

View File

@@ -2,6 +2,7 @@ package sti
import (
"archive/tar"
"encoding/json"
"fmt"
"io"
"io/ioutil"
@@ -522,30 +523,50 @@ func checkAndGetNewLabels(builder *STI, docker dockerpkg.Docker, tar s2itar.Tar,
log.V(3).Infof("Checking for new Labels to apply... ")
// metadata filename and its path inside the container
sourceFilepath := filepath.Join("/tmp/.s2i", util.MetadataFilename)
metadataFilename := "image_metadata.json"
sourceFilepath := filepath.Join("/tmp/.s2i", metadataFilename)
// create the 'downloadPath' folder if it doesn't exist
downloadPath := filepath.Join(builder.config.WorkingDir, "metadata")
log.V(3).Infof("Creating the download path '%s'", downloadPath)
if err := os.MkdirAll(downloadPath, 0700); err != nil {
log.Errorf("Error creating dir %q for '%s': %v", downloadPath, util.MetadataFilename, err)
log.Errorf("Error creating dir %q for '%s': %v", downloadPath, metadataFilename, err)
return err
}
// download & extract the file from container
if _, err := downloadAndExtractFileFromContainer(docker, tar, sourceFilepath, downloadPath, containerID); err != nil {
log.V(3).Infof("unable to download and extract '%s' ... continuing", util.MetadataFilename)
log.V(3).Infof("unable to download and extract '%s' ... continuing", metadataFilename)
return nil
}
// open the file
if data, err := util.ProcessImageMetadataFile(downloadPath); err == nil {
// update newLabels[]
labels := data["labels"]
for _, l := range labels.([]interface{}) {
for k, v := range l.(map[string]interface{}) {
builder.newLabels[k] = v.(string)
}
filePath := filepath.Join(downloadPath, metadataFilename)
fd, err := os.Open(filePath)
if fd == nil || err != nil {
return fmt.Errorf("unable to open file '%s' : %v", downloadPath, err)
}
defer fd.Close()
// read the file to a string
str, err := ioutil.ReadAll(fd)
if err != nil {
return fmt.Errorf("error reading file '%s' in to a string: %v", filePath, err)
}
log.V(3).Infof("new Labels File contents : \n%s\n", str)
// string into a map
var data map[string]interface{}
if err = json.Unmarshal([]byte(str), &data); err != nil {
return fmt.Errorf("JSON Unmarshal Error with '%s' file : %v", metadataFilename, err)
}
// update newLabels[]
labels := data["labels"]
for _, l := range labels.([]interface{}) {
for k, v := range l.(map[string]interface{}) {
builder.newLabels[k] = v.(string)
}
}

View File

@@ -1,22 +1,13 @@
package util
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"github.com/openshift/source-to-image/pkg/api"
"github.com/openshift/source-to-image/pkg/api/constants"
"github.com/openshift/source-to-image/pkg/scm/git"
)
const (
// MetadataFilename is the name of the config file defining additional labels to set on the output image.
MetadataFilename = "image_metadata.json"
)
// GenerateOutputImageLabels generate the labels based on the s2i Config
// and source repository informations.
func GenerateOutputImageLabels(info *git.SourceInfo, config *api.Config) map[string]string {
@@ -28,15 +19,6 @@ func GenerateOutputImageLabels(info *git.SourceInfo, config *api.Config) map[str
labels = GenerateLabelsFromConfig(labels, config, namespace)
labels = GenerateLabelsFromSourceInfo(labels, info, namespace)
if data, err := ProcessImageMetadataFile(filepath.Join(config.WorkingDir, constants.SourceConfig)); err == nil {
ll := data["labels"]
for _, l := range ll.([]interface{}) {
for k, v := range l.(map[string]interface{}) {
labels[k] = v.(string)
}
}
}
return labels
}
@@ -86,26 +68,3 @@ func addBuildLabel(to map[string]string, key, value, namespace string) {
}
to[namespace+"build."+key] = value
}
// ProcessImageMetadataFile returns a map of the labels to set on the output image.
func ProcessImageMetadataFile(path string) (map[string]interface{}, error) {
filePath := filepath.Join(path, MetadataFilename)
fd, err := os.Open(filePath)
if fd == nil || err != nil {
return nil, fmt.Errorf("unable to open file '%s' : %v", filePath, err)
}
defer fd.Close()
// read the file to a string
str, err := ioutil.ReadAll(fd)
if err != nil {
return nil, fmt.Errorf("error reading file '%s' in to a string: %v", filePath, err)
}
log.V(3).Infof("new Labels File contents : \n%s\n", str)
var data map[string]interface{}
if err = json.Unmarshal([]byte(str), &data); err != nil {
return nil, fmt.Errorf("JSON Unmarshal Error with '%s' file : %v", MetadataFilename, err)
}
return data, nil
}

View File

@@ -1,62 +0,0 @@
package util
import (
"github.com/openshift/source-to-image/pkg/api"
"github.com/openshift/source-to-image/pkg/api/constants"
"io/ioutil"
"os"
"path/filepath"
"testing"
)
func TestImageMetadataLabels(t *testing.T) {
tests := []struct {
json string
count int
}{
{
json: "{\"labels\": [{\"org.tenkichannel/service\":\"rain-forecast-process\"}]}",
count: 1,
},
{
json: "{\"labels\": [{\"labelkey1\":\"value1\"},{\"labelkey2\":\"value2\"}]}",
count: 2,
},
{
json: "{\"labels\": [{\"labelkey1\":\"value1\",\"labelkey2\":\"value2\"}]}",
count: 2,
},
}
for _, tc := range tests {
tempDir, err := ioutil.TempDir("", "image_metadata")
if err != nil {
t.Fatalf("could not create temp dir: %v", err)
}
defer os.RemoveAll(tempDir)
err = os.Chmod(tempDir, 0777)
if err != nil {
t.Fatalf("could not chmod temp dir: %v", err)
}
err = os.MkdirAll(filepath.Join(tempDir, constants.SourceConfig), 0777)
if err != nil {
t.Fatalf("could not create subdirs: %v", err)
}
path := filepath.Join(tempDir, constants.SourceConfig, MetadataFilename)
err = ioutil.WriteFile(path, []byte(tc.json), 0700)
if err != nil {
t.Fatalf("could not create temp image_metadata.json: %v", err)
}
cfg := &api.Config{
WorkingDir: tempDir,
}
data := GenerateOutputImageLabels(nil, cfg)
if len(data) != tc.count {
t.Fatalf("data from GenerateOutputImageLabels len %d when needed %d for %s", len(data), tc.count, tc.json)
}
}
}