From 594f5bf3136f49dfd4735d8606ba09d652001aac Mon Sep 17 00:00:00 2001 From: gabemontero Date: Tue, 10 Dec 2019 11:11:13 -0500 Subject: [PATCH] Revert "support .s2i/image_metadata.json with --as-dockerfile" This reverts commit 38d2b729fd1d1358397f538775c84074f48dadea. --- .gitignore | 5 -- pkg/api/constants/scripts.go | 5 +- pkg/build/strategies/sti/postexecutorstep.go | 41 +++++++++---- pkg/util/labels.go | 41 ------------- pkg/util/labels_test.go | 62 -------------------- 5 files changed, 32 insertions(+), 122 deletions(-) delete mode 100644 pkg/util/labels_test.go diff --git a/.gitignore b/.gitignore index 597931542..f55f51fda 100644 --- a/.gitignore +++ b/.gitignore @@ -5,8 +5,3 @@ .vscode/ cmd/s2i/debug *~ -.idea -upload -*.classpath -*.project -*.settings diff --git a/pkg/api/constants/scripts.go b/pkg/api/constants/scripts.go index 84508308a..8f18e919b 100644 --- a/pkg/api/constants/scripts.go +++ b/pkg/api/constants/scripts.go @@ -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" diff --git a/pkg/build/strategies/sti/postexecutorstep.go b/pkg/build/strategies/sti/postexecutorstep.go index c06521a2c..19c7417e8 100644 --- a/pkg/build/strategies/sti/postexecutorstep.go +++ b/pkg/build/strategies/sti/postexecutorstep.go @@ -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) } } diff --git a/pkg/util/labels.go b/pkg/util/labels.go index 30990f877..5b24c0839 100644 --- a/pkg/util/labels.go +++ b/pkg/util/labels.go @@ -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 -} diff --git a/pkg/util/labels_test.go b/pkg/util/labels_test.go deleted file mode 100644 index 3232acadf..000000000 --- a/pkg/util/labels_test.go +++ /dev/null @@ -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) - } - - } - -}