-
Notifications
You must be signed in to change notification settings - Fork 1.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Lockdown /tekton/step folders to their own steps. #4352
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,7 @@ package main | |
import ( | ||
"log" | ||
"os" | ||
"path/filepath" | ||
|
||
"github.com/tektoncd/pipeline/pkg/entrypoint" | ||
) | ||
|
@@ -13,11 +14,16 @@ type realPostWriter struct{} | |
var _ entrypoint.PostWriter = (*realPostWriter)(nil) | ||
|
||
// Write creates a file and writes content to that file if content is specified | ||
// assumption here is the underlying directory structure already exists | ||
func (*realPostWriter) Write(file string, content string) { | ||
if file == "" { | ||
return | ||
} | ||
|
||
// Create directory if it doesn't already exist | ||
if err := os.MkdirAll(filepath.Dir(file), os.ModePerm); err != nil { | ||
log.Fatalf("Error creating parent directory of %q: %v", file, err) | ||
} | ||
|
||
Comment on lines
+21
to
+26
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you add tests for this new behaviour? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done. |
||
f, err := os.Create(file) | ||
if err != nil { | ||
log.Fatalf("Creating %q: %v", file, err) | ||
|
@@ -29,26 +35,3 @@ func (*realPostWriter) Write(file string, content string) { | |
} | ||
} | ||
} | ||
|
||
// CreateDirWithSymlink creates the specified directory and a symbolic link to that directory | ||
func (*realPostWriter) CreateDirWithSymlink(source, link string) { | ||
if source == "" { | ||
return | ||
} | ||
if err := os.MkdirAll(source, 0770); err != nil { | ||
log.Fatalf("Creating directory %q: %v", source, err) | ||
} | ||
|
||
if link == "" { | ||
return | ||
} | ||
// create a symlink if it does not exist | ||
if _, err := os.Stat(link); os.IsNotExist(err) { | ||
// check if a source exist before creating a symbolic link | ||
if _, err := os.Stat(source); err == nil { | ||
if err := os.Symlink(source, link); err != nil { | ||
log.Fatalf("Creating a symlink %q: %v", link, err) | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package subcommands | ||
|
||
import ( | ||
"log" | ||
"os" | ||
"path/filepath" | ||
"strconv" | ||
) | ||
|
||
// StepInitCommand is the name of the /tekton/steps initialization command. | ||
const StepInitCommand = "step-init" | ||
|
||
var ( | ||
// root is the location of the Tekton root directory. | ||
// Included as a global variable to allow overriding for tests. | ||
tektonRoot = "/tekton" | ||
) | ||
|
||
// stepInit sets up the /tekton/steps directory for the pod. | ||
// This expects the list of steps (in order matching the Task spec). | ||
func stepInit(steps []string) error { | ||
// Setup step directory symlinks - step data is written to a /tekton/run/<step>/status | ||
// folder corresponding to each step - this is only mounted RW for the matching user step | ||
// (and RO for all other steps). | ||
// /tekton/steps provides a convenience symlink so that Tekton utilities to reference steps | ||
// by name or index. | ||
// NOTE: /tekton/steps may be removed in the future. Prefer using /tekton/run directly if | ||
// possible. | ||
|
||
// Create directory if it doesn't already exist | ||
stepDir := filepath.Join(tektonRoot, "steps") | ||
if err := os.MkdirAll(stepDir, os.ModePerm); err != nil { | ||
log.Fatalf("Error creating steps directory %q: %v", stepDir, err) | ||
} | ||
|
||
for i, s := range steps { | ||
run := filepath.Join(tektonRoot, "run", strconv.Itoa(i), "status") | ||
if err := os.Symlink(run, filepath.Join(stepDir, s)); err != nil { | ||
return err | ||
} | ||
if err := os.Symlink(run, filepath.Join(stepDir, strconv.Itoa(i))); err != nil { | ||
return err | ||
} | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package subcommands | ||
|
||
import ( | ||
"io/ioutil" | ||
"os" | ||
"path/filepath" | ||
"testing" | ||
) | ||
|
||
func TestStepInit(t *testing.T) { | ||
tmp, err := ioutil.TempDir("", "step-init-*") | ||
if err != nil { | ||
t.Fatalf("error creating temp directory: %v", err) | ||
} | ||
defer os.RemoveAll(tmp) | ||
|
||
// Override tektonRoot for testing. | ||
tektonRoot = tmp | ||
|
||
// Create step directory so that symlinks can be successfully created. | ||
// This is typically done by volume mounts, so it needs to be done manually | ||
// in tests. | ||
stepDir := filepath.Join(tmp, "steps") | ||
if err := os.Mkdir(stepDir, os.ModePerm); err != nil { | ||
t.Fatalf("error creating step directory: %v", err) | ||
} | ||
|
||
steps := []string{"a", "b"} | ||
if err := stepInit(steps); err != nil { | ||
t.Fatalf("stepInit: %v", err) | ||
} | ||
|
||
// Map of symlinks to expected /tekton/run folders. | ||
// Expected format: | ||
// Key: /tekton/steps/<key> | ||
// Value: /tekton/run/<value>/status | ||
wantLinks := map[string]string{ | ||
"a": "0", | ||
"0": "0", | ||
"b": "1", | ||
"1": "1", | ||
} | ||
|
||
direntry, err := os.ReadDir(stepDir) | ||
if err != nil { | ||
t.Fatalf("os.ReadDir: %v", err) | ||
} | ||
for _, de := range direntry { | ||
t.Run(de.Name(), func(t *testing.T) { | ||
l, err := os.Readlink(filepath.Join(stepDir, de.Name())) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
want, ok := wantLinks[de.Name()] | ||
if !ok { | ||
t.Fatalf("unexpected symlink: %s", de.Name()) | ||
} | ||
if wantDir := filepath.Join(tmp, "run", want, "status"); l != wantDir { | ||
t.Errorf("want %s, got %s", wantDir, l) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
NIT: it looks like the "=" lost its alignment?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🤷 gofmt is doing this, so I'm not going to fight it. My guess is the string concat above is resetting the alignment, and
stepMetadataDirLink
just happened to be the right length to align this block with the flags above.