Skip to content
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

fix(stack): unable to delete invalid stack [EE-5753] #11813

Open
wants to merge 10 commits into
base: develop
Choose a base branch
from

Conversation

oscarzhou-portainer
Copy link
Collaborator

closes EE-5753

Copy link
Contributor

@chiptus chiptus left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking good. use a common function for isExist and simplify your comments.

api/http/handler/stacks/stack_delete.go Show resolved Hide resolved
// to the Kubernetes cluster API endpoint via the Portainer endpoint proxy.
// See @https://github.com/portainer/portainer/blob/release/2.20/app/kubernetes/views/applications/applicationsController.js#L96
for _, manifest := range manifestFiles {
if _, statErr := os.Stat(manifest); os.IsNotExist(statErr) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we have a function for it filesystem.FileExist

if err != nil {
return errors.Wrap(err, "failed to create env file")
projectPath := stack.ProjectPath
if _, err := os.Stat(projectPath); os.IsNotExist(err) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we have a function for it filesystem.FileExist

// copyConfigEnvVarsIfExists write the environment variables from stack configuration to target file
func copyConfigEnvVarsIfExists(w io.Writer, envs []portainer.Pair) {
for _, v := range envs {
io.WriteString(w, fmt.Sprintf("%s=%s\n", v.Name, v.Value))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should handle the error of this operation.

Nitpick: maybe use fmt.Fprintf() directly?

}

return "stack.env", nil
}

// copyDefaultEnvFile copies the default .env file if it exists to the provided writer
func copyDefaultEnvFile(stack *portainer.Stack, w io.Writer) {
defaultEnvFile, err := os.Open(path.Join(path.Join(stack.ProjectPath, path.Dir(stack.EntryPoint)), ".env"))
func copyDefaultEnvFileIfExists(w io.Writer, defaultEnvFilePath string) error {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
func copyDefaultEnvFileIfExists(w io.Writer, defaultEnvFilePath string) error {
func copyDefaultEnvFile(w io.Writer, defaultEnvFilePath string) error {

I feel like IfExists doesn't add any value. it copied the file into the writer

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When there is no .env file, we skip the copy and don't return error. That's the reason the IfExists was added

Comment on lines 188 to 189
// copyConfigEnvVarsIfExists write the environment variables from stack configuration to target file
func copyConfigEnvVarsIfExists(w io.Writer, envs []portainer.Pair) error {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// copyConfigEnvVarsIfExists write the environment variables from stack configuration to target file
func copyConfigEnvVarsIfExists(w io.Writer, envs []portainer.Pair) error {
// copyConfigEnvVars write the environment variables from stack configuration to the writer
func copyConfigEnvVars(w io.Writer, envs []portainer.Pair) error {

Copy link
Contributor

@chiptus chiptus left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Copy link
Contributor

@chiptus chiptus left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM


if err != nil {
for _, manifest := range manifestFiles {
if exists, err := filesystem.FileExists(manifest); err != nil || !exists {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

to prevent overriding of the original error (L248), rename this variable

Suggested change
if exists, err := filesystem.FileExists(manifest); err != nil || !exists {
if exists, fileExistsErr := filesystem.FileExists(manifest); fileExistsErr != nil || !exists {

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants