Skip to content

Conversation

@hanlins
Copy link

@hanlins hanlins commented Mar 26, 2022

The original logic is if we can get the user cache directory(i.e. os.UserCacheDir()), then create the cache directory under that user cache directory. Otherwise, create the cache directory under the temp directory.

Basically os.UserCacheDir could return a path that the current user doesn't have permission for (typically in container environment). os.UserCacheDir typically returns the $HOME. If $HOME is set but the user doesn't have permission to create directories in $HOME, it will panic. This is somewhat confusing since it could also fallback on the temp directory.

The update logic will simply attempt to create directory under os.UserCacheDir(). If os.UserCacheDir returns error or user encounters error creating directories under it, it will fallback onto temp directory and retry.

Here's the manual verification:

➜  test cat main.go
package main

import (
	"fmt"
	"os"
	"path/filepath"
)

func main() {
	const (
		eventTestDir = "kubebuilder-envtest"
		permission   = 0750
	)
	if baseDir, err := os.UserCacheDir(); err == nil {
		cacheDir := filepath.Join(baseDir, eventTestDir)
		fmt.Println("#1", cacheDir)
		if err := os.MkdirAll(cacheDir, permission); err == nil {
			return
		}
	}
	cacheDir := filepath.Join(os.TempDir(), eventTestDir)
	fmt.Println("#2", cacheDir)
	if err := os.MkdirAll(cacheDir, permission); err != nil {
		panic(err)
	}

}
➜  test GO111MODULE=off go build .
➜  test # if HOME directory doesn't have permission issue
➜  test HOME=$(pwd)/somepath ./test
#1 /home/ubuntu/test/somepath/.cache/kubebuilder-envtest
➜  test # if HOME directory has permission issue
➜  test rm -rf somepath/.cache
➜  test sudo chown root:root somepath
➜  test HOME=$(pwd)/somepath ./test
#1 /home/ubuntu/test/somepath/.cache/kubebuilder-envtest
#2 /tmp/kubebuilder-envtest

Fixes #1845

@k8s-ci-robot k8s-ci-robot added the cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. label Mar 26, 2022
@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: hanlins
To complete the pull request process, please assign apelisse after the PR has been reviewed.
You can assign the PR to them by writing /assign @apelisse in a comment when ready.

The full list of commands accepted by this bot can be found here.

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@k8s-ci-robot k8s-ci-robot requested review from FillZpp and mengqiy March 26, 2022 00:18
@k8s-ci-robot k8s-ci-robot added the size/S Denotes a PR that changes 10-29 lines, ignoring generated files. label Mar 26, 2022
@hanlins

This comment was marked as outdated.

2 similar comments
@hanlins

This comment was marked as outdated.

@hanlins

This comment was marked as outdated.

@hanlins hanlins force-pushed the fix/1845/fallback-temp-dir branch 2 times, most recently from f79e8cb to 0fbadc7 Compare March 31, 2022 20:11
@hanlins

This comment was marked as outdated.

1 similar comment
@hanlins
Copy link
Author

hanlins commented Mar 31, 2022

/test pull-controller-runtime-test-master

@hanlins hanlins force-pushed the fix/1845/fallback-temp-dir branch from 0fbadc7 to 5d67204 Compare March 31, 2022 23:24
@hanlins
Copy link
Author

hanlins commented Apr 4, 2022

/assign controller-runtime-maintainers

@k8s-ci-robot
Copy link
Contributor

@hanlins: GitHub didn't allow me to assign the following users: controller-runtime-maintainers.

Note that only kubernetes-sigs members, repo collaborators and people who have commented on this issue/PR can be assigned. Additionally, issues/PRs can only have 10 assignees at the same time.
For more information please see the contributor guide

Details

In response to this:

/assign controller-runtime-maintainers

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository.

@hanlins
Copy link
Author

hanlins commented Apr 4, 2022

/assign @vincepri

@vincepri
Copy link
Member

vincepri commented Apr 4, 2022

@hanlins There is already a PR out for this issue, see #1800

@alvaroaleman
Copy link
Member

This was fixed in #1800

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. size/S Denotes a PR that changes 10-29 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Panic creating kubebuilder-envtest cache directory

4 participants