Merge pull request #442 from thockin/release-3.x

Port small fixes from v4 branch
This commit is contained in:
Kubernetes Prow Robot 2021-08-18 08:44:08 -07:00 committed by GitHub
commit f52e17ea2c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 23 additions and 43 deletions

View File

@ -101,9 +101,9 @@ docker run -d \
| GIT_SYNC_PERMISSIONS | `--change-permissions` | the file permissions to apply to the checked-out files (0 will not change permissions at all) | 0 |
| GIT_SYNC_SPARSE_CHECKOUT_FILE | `--sparse-checkout-file` | the location of an optional [sparse-checkout](https://git-scm.com/docs/git-sparse-checkout#_sparse_checkout) file, same syntax as a .gitignore file. | "" |
| GIT_SYNC_HOOK_COMMAND | `--sync-hook-command` | DEPRECATED: use --exechook-command instead | "" |
| GIT_EXECHOOK_COMMAND | `--exechook-command` | the command executed with the syncing repository as its working directory after syncing a new hash of the remote repository. it is subject to the sync time out and will extend period between syncs. (doesn't support the command arguments) | "" |
| GIT_EXECHOOK_TIMEOUT | `--exechook-timeout` | the timeout for the sync hook command | 30 (seconds) |
| GIT_EXECHOOK_BACKOFF | `--exechook-backoff` | the time to wait before retrying a failed sync hook command
| GIT_SYNC_EXECHOOK_COMMAND | `--exechook-command` | the command executed with the syncing repository as its working directory after syncing a new hash of the remote repository. it is subject to the sync time out and will extend period between syncs. (doesn't support the command arguments) | "" |
| GIT_SYNC_EXECHOOK_TIMEOUT | `--exechook-timeout` | the timeout for the sync hook command | 30 (seconds) |
| GIT_SYNC_EXECHOOK_BACKOFF | `--exechook-backoff` | the time to wait before retrying a failed sync hook command
| GIT_SYNC_WEBHOOK_URL | `--webhook-url` | the URL for a webook notification when syncs complete | "" |
| GIT_SYNC_WEBHOOK_METHOD | `--webhook-method` | the HTTP method for the webhook | "POST" |
| GIT_SYNC_WEBHOOK_SUCCESS_STATUS | `--webhook-success-status` | the HTTP status code indicating a successful webhook (-1 disables success checks to make webhooks fire-and-forget) | 200 |

View File

@ -77,12 +77,12 @@ var flChmod = flag.Int("change-permissions", envInt("GIT_SYNC_PERMISSIONS", 0),
"the file permissions to apply to the checked-out files (0 will not change permissions at all)")
var flSyncHookCommand = flag.String("sync-hook-command", envString("GIT_SYNC_HOOK_COMMAND", ""),
"DEPRECATED: use --exechook-command instead")
var flExechookCommand = flag.String("exechook-command", envString("GIT_EXECHOOK_COMMAND", ""),
var flExechookCommand = flag.String("exechook-command", envString("GIT_SYNC_EXECHOOK_COMMAND", ""),
"a command to be executed (without arguments, with the syncing repository as its working directory) after syncing a new hash of the remote repository. "+
"It is subject to --timeout out and will extend period between syncs.")
var flExechookTimeout = flag.Duration("exechook-timeout", envDuration("GIT_EXECHOOK_TIMEOUT", time.Second*30),
var flExechookTimeout = flag.Duration("exechook-timeout", envDuration("GIT_SYNC_EXECHOOK_TIMEOUT", time.Second*30),
"the timeout for the command")
var flExechookBackoff = flag.Duration("exechook-backoff", envDuration("GIT_EXECHOOK_BACKOFF", time.Second*3),
var flExechookBackoff = flag.Duration("exechook-backoff", envDuration("GIT_SYNC_EXECHOOK_BACKOFF", time.Second*3),
"the time to wait before retrying a failed command")
var flSparseCheckoutFile = flag.String("sparse-checkout-file", envString("GIT_SYNC_SPARSE_CHECKOUT_FILE", ""),
"the path to a sparse-checkout file.")

View File

@ -54,18 +54,18 @@ func NewExechook(cmdrunner *cmd.Runner, command, gitroot string, args []string,
}
// Name describes hook, implements Hook.Name
func (w *Exechook) Name() string {
func (h *Exechook) Name() string {
return "exechook"
}
// Do runs exechook.command, implements Hook.Do
func (c *Exechook) Do(ctx context.Context, hash string) error {
ctx, cancel := context.WithTimeout(ctx, c.timeout)
func (h *Exechook) Do(ctx context.Context, hash string) error {
ctx, cancel := context.WithTimeout(ctx, h.timeout)
defer cancel()
worktreePath := filepath.Join(c.gitRoot, hash)
worktreePath := filepath.Join(h.gitRoot, hash)
c.logger.V(0).Info("running exechook", "command", c.command, "timeout", c.timeout)
_, err := c.cmdrunner.Run(ctx, worktreePath, c.command, c.args...)
h.logger.V(0).Info("running exechook", "command", h.command, "timeout", h.timeout)
_, err := h.cmdrunner.Run(ctx, worktreePath, h.command, h.args...)
return err
}

View File

@ -29,24 +29,19 @@ import (
// A logger embeds logr.Logger
type Logger struct {
log logr.Logger
logr.Logger
root string
errorFile string
}
// NewLogger returns glogr implemented logr.Logger.
func New(root string, errorFile string) *Logger {
return &Logger{log: glogr.New(), root: root, errorFile: errorFile}
}
// Info implements logr.Logger.Info.
func (l *Logger) Info(msg string, keysAndValues ...interface{}) {
l.log.Info(msg, keysAndValues...)
return &Logger{Logger: glogr.New(), root: root, errorFile: errorFile}
}
// Error implements logr.Logger.Error.
func (l *Logger) Error(err error, msg string, kvList ...interface{}) {
l.log.Error(err, msg, kvList...)
l.Logger.WithCallDepth(1).Error(err, msg, kvList...)
if l.errorFile == "" {
return
}
@ -71,7 +66,7 @@ func (l *Logger) Error(err error, msg string, kvList ...interface{}) {
}
jb, err := json.Marshal(payload)
if err != nil {
l.log.Error(err, "can't encode error payload")
l.Logger.Error(err, "can't encode error payload")
content := fmt.Sprintf("%v", err)
l.writeContent([]byte(content))
} else {
@ -79,21 +74,6 @@ func (l *Logger) Error(err error, msg string, kvList ...interface{}) {
}
}
// V implements logr.Logger.V.
func (l *Logger) V(level int) logr.Logger {
return l.log.V(level)
}
// WithValues implements logr.Logger.WithValues.
func (l *Logger) WithValues(keysAndValues ...interface{}) logr.Logger {
return l.log.WithValues(keysAndValues...)
}
// WithName implements logr.Logger.WithName.
func (l *Logger) WithName(name string) logr.Logger {
return l.log.WithName(name)
}
// ExportError exports the error to the error file if --export-error is enabled.
func (l *Logger) ExportError(content string) {
if l.errorFile == "" {
@ -112,7 +92,7 @@ func (l *Logger) DeleteErrorFile() {
if os.IsNotExist(err) {
return
}
l.log.Error(err, "can't delete the error-file", "filename", errorFile)
l.Logger.Error(err, "can't delete the error-file", "filename", errorFile)
}
}
@ -121,32 +101,32 @@ func (l *Logger) writeContent(content []byte) {
if _, err := os.Stat(l.root); os.IsNotExist(err) {
fileMode := os.FileMode(0755)
if err := os.Mkdir(l.root, fileMode); err != nil {
l.log.Error(err, "can't create the root directory", "root", l.root)
l.Logger.Error(err, "can't create the root directory", "root", l.root)
return
}
}
tmpFile, err := ioutil.TempFile(l.root, "tmp-err-")
if err != nil {
l.log.Error(err, "can't create temporary error-file", "directory", l.root, "prefix", "tmp-err-")
l.Logger.Error(err, "can't create temporary error-file", "directory", l.root, "prefix", "tmp-err-")
return
}
defer func() {
if err := tmpFile.Close(); err != nil {
l.log.Error(err, "can't close temporary error-file", "filename", tmpFile.Name())
l.Logger.Error(err, "can't close temporary error-file", "filename", tmpFile.Name())
}
}()
if _, err = tmpFile.Write(content); err != nil {
l.log.Error(err, "can't write to temporary error-file", "filename", tmpFile.Name())
l.Logger.Error(err, "can't write to temporary error-file", "filename", tmpFile.Name())
return
}
errorFile := filepath.Join(l.root, l.errorFile)
if err := os.Rename(tmpFile.Name(), errorFile); err != nil {
l.log.Error(err, "can't rename to error-file", "temp-file", tmpFile.Name(), "error-file", errorFile)
l.Logger.Error(err, "can't rename to error-file", "temp-file", tmpFile.Name(), "error-file", errorFile)
return
}
if err := os.Chmod(errorFile, 0644); err != nil {
l.log.Error(err, "can't change permissions on the error-file", "error-file", errorFile)
l.Logger.Error(err, "can't change permissions on the error-file", "error-file", errorFile)
}
}