Commit Graph

201 Commits

Author SHA1 Message Date
Tim Hockin a3d9b673a8 Port #372 to v4 dev branch (from SpencerMalone)
Wanted to finally tackle #54, I sidestepped the problem of how to handle the volume of flags that might be required by instead specifying a sparsecheckout file.

The workflow as I've had has been...
- Do a local sparse checkout, add the files you want ignored (or included on if you did a cone pattern https://git-scm.com/docs/git-sparse-checkout#_cone_pattern_set)
- Grab your .git/info/sparecheckout file, and reserve it for later use with this new flag

It's not quite as easy as specifying it all from a CLI, but I think it's a reasonable first pass.

Here are some logs of it being run on https://github.com/SpencerMalone/logstash-output-prometheus:

```
test-repo % cat sparseconfig
!/*
!/*/
README.md
test-repo % docker run --rm -d \
    -v $(pwd)/git-data:/tmp/git \
    -v $(pwd):/test \
    docker.io/registry/git-sync:tag__linux_amd64  \
        --repo=https://github.com/SpencerMalone/logstash-output-prometheus.git \
        --branch=master \
        --sparse-checkout-file=/test/sparseconfig
41494548dd64caf0ff8f7b75e4d3a86014cfaefc40ff31b14ba19accf99aa82f
test-repo % ls git-data/db86200b1ab158ce9ad403d06de2301b15333601
README.md
```

As you can see, I ignored everything but the `README.md`, and sure enough only got that file in my final checkout.
2021-04-23 09:01:05 -07:00
Tim Hockin a8a7ffb5a2 Sort the flags in the manual output 2021-04-21 21:48:55 -07:00
Nan Yu 53f3fa17a0 Export the error details to an error file
The current git-sync process outputs the error information to standard
out, which is inaccessible from outside the container. Users have to
dump the logs using kubectl logs in order to check the error details in
the git-sync process. This commit exports the error details to a file,
which provides users the capability to check the errors directly from
other sidecar containers.

proposal: https://github.com/kubernetes/git-sync/issues/326
2021-04-21 16:42:28 -07:00
Nan Yu f2581cff93 Export the error details to an error file
The current git-sync process outputs the error information to standard
out, which is inaccessible from outside the container. Users have to
dump the logs using kubectl logs in order to check the error details in
the git-sync process. This commit exports the error details to a file,
which provides users the capability to check the errors directly from
other sidecar containers.

proposal: https://github.com/kubernetes/git-sync/issues/326
2021-04-09 11:33:16 -07:00
Kubernetes Prow Robot 13af14e3bf
Merge pull request #370 from thockin/exec-hook-after-symlink-v4
Move sync-hook to after symlink flip
2021-04-07 16:47:42 -07:00
Tim Hockin d6c1fd3c6b Move sync-hook to after symlink flip 2021-04-05 17:02:00 -07:00
Tim Hockin dcb918f846 WIP: startup tests 2021-03-29 16:48:50 -07:00
Kubernetes Prow Robot 7b594781f8
Merge pull request #344 from thockin/fix-pid1-exit-code-v4
Fix exit non-zero exit codes when running as pid1
2021-03-15 09:39:06 -07:00
Tim Hockin 45b28530a7 Fix exit non-zero exit codes when running as pid1
(v4 branch)

Prior to this we would swallow the exit code and always exit(0).
2021-03-15 09:28:26 -07:00
Tim Hockin 83b4dd21de Add --git-config flag
This allows arbitrary git configs to be passed in.  For example:

`git config --global http.postBuffer 1048576000`

`git config --global http.sslCAInfo /path/to/cert/file`

`git config --global http.sslVerify false`

This flag takes a comma-separated list of `key:val` pairs.  The key part
is passed to `git config` and must be a valid gitconfig section header
and variable name.  The val part can be either a quoted or unquoted
value.  For all values the following escape sequences are supported:
   * `\n` => [newline]
   * `\t` => [tab]
   * `\"` => `"`
   * `\,` => `,`
   * `\\` => `\`

Within unquoted values, commas MUST be escaped. Within quoted values,
commas MAY be escaped, but are not required to be.  Any other escape
sequence is an error.

Example:

`--git-config=foo.one:val1,foo.two:"quoted val",foo.three:12345`

This commit exposed a bug in runCommand() which modified its args when
they had an embedded space.
2021-03-15 09:20:02 -07:00
Tim Hockin cc094858b3 Fix tests on master - reset needs "--" arg
Now that the worktree dirs are the same name as the hash, `git reset`
doesn't know if we are asking for reset-to-hash or reset-the-dir.
Adding "--" makes that unambiguous.

Tests pass now.
2021-03-15 08:59:34 -07:00
Tim Hockin e5a438e446 Change the symlink targets to just the SHA
This allows users to call readlink() on the link and learn the current
checked out SHA.
2021-03-11 22:11:35 -08:00
Kubernetes Prow Robot 6f8ecc6caa
Merge pull request #323 from thockin/log-uid-gid
Log info about UID, GID, and HOME for debug
2021-01-07 05:59:45 -08:00
Tim Hockin e2bc4c3ea0 Log info about UID, GID, and HOME for debug 2021-01-06 13:52:08 -08:00
Tim Hockin e7eecc31a1 Allow octal and hex values for int flags
This is specifcally relevant to --change-permissions.
2021-01-06 13:46:25 -08:00
Tim Hockin c410bb5610 Add a main struct, part 8
This commit encapsulates the authURL parameter.
2020-11-10 10:21:20 -08:00
Tim Hockin 4d808d47ed Add a main struct, part 7
This commit encapsulates the chmod and link parameters.
2020-11-10 10:21:20 -08:00
Tim Hockin 70dd821e7b Add a main struct, part 6
This commit encapsulates the submodules parameter.
2020-11-10 10:21:20 -08:00
Tim Hockin 8b321e3940 Add a main struct, part 5
This commit encapsulates the depth parameter.
2020-11-10 10:21:20 -08:00
Tim Hockin 3a1212da36 Add a main struct, part 4
This commit encapsulates the branch and rev parameters.
2020-11-10 10:21:20 -08:00
Tim Hockin b44dab6817 Add a main struct, part 3
This commit encapsulates the repo parameter.
2020-11-10 10:21:20 -08:00
Tim Hockin a80afb427d Add a main struct, part 2
This commit encapsulates the root parameter.

This exposed a bug where we do not reset the root of the workspace.
2020-11-10 10:21:20 -08:00
Tim Hockin 2dd4705c1b Add a main struct, part 1
Start the process of encapsulating most of the flags and not using them
as global variables.  This commit JUST does the git command flag, which
is now only accessed from main()
2020-11-10 10:21:20 -08:00
Tim Hockin 047593b40a Clarify logging vs printf for fatal messages
Also log when HTTP serving fails.
2020-11-10 09:15:17 -08:00
Tim Hockin 24c06a5b48 Normalize the root path
This makes sure there's never any confusion about whether it is an
absolute path or has symlinks or whatever.  Add e2e cases to cover.
2020-11-09 09:20:24 -08:00
Tim Hockin ea469c1ed4 Add a test for tabs in manual 2020-11-09 08:53:29 -08:00
Tim Hockin 0e802450ee Don't allow --dest to start with a dot
BREAKING CHANGE

It's useful to reserve names that start with a dot.
2020-11-09 08:36:10 -08:00
Tim Hockin af2ae5b533 Make the --root flag required, no default
BREAKING CHANGE

The default of $HOME has caused problems for people playing with
git-sync, so this change makes it a required argument.
2020-11-09 08:30:29 -08:00
Tim Hockin 7d7cbdacff Add tests for other env funcs 2020-11-08 10:47:07 -08:00
Tim Hockin fa0e8696e2 Change time-related flags to durations
Add '--period' to replace '--wait', which is now obsolete.

Add '--sync-timeout' to replace '--timeout', which is now obsolete.

Both of these new flags take a Go-style time string, rather than a bare
number. For example "1s" for 1 second or "1m" for one minute.

The old flags have been kept and will take precedence if specified.
2020-11-05 16:46:09 -08:00
Tim Hockin 20696689f3 Add help and manual flags, use pflag
THIS IS A BREAKING CHANGE

Switch flags implementation to use pflag.  This means that long flags
like `-username` must now use 2 dashes: `--username`.

The `-v` flag (verbose) used to accept `-v` or `--v`.  Now it only
accepts `-v.

The `--help` and `-h` flags are new.

The `--man` flag is new (print a man-page like help message).

Several glog flags which used to be exposed (e.g. --logtostderr) are no
longer exposed.  Logs always go to stderr.
2020-10-31 00:50:51 -07:00
unknowingknow 26cd942b2a change to a command name with no args 2020-10-02 19:29:03 +09:00
unknowingknow e1c4717de6 add an option to run the command when the repository is updated 2020-10-02 19:28:25 +09:00
Tim Hockin 44e94a16b0 Fix webhook regression: allow -1 to ignore status 2020-09-25 11:17:56 -07:00
Kubernetes Prow Robot 66320a6b92
Merge pull request #243 from killuazhu/kyle-relax-wait
Relax wait between sync
2020-09-25 10:30:26 -07:00
Tim Hockin 4225d000e0 Clean up askpass function to take url as arg 2020-09-07 17:16:56 -07:00
Kubernetes Prow Robot ed57c29d83
Merge pull request #275 from thockin/7-askpass-metric
Add metric for askpass, noop sync != success
2020-09-07 17:11:42 -07:00
Tim Hockin 9cef5f8ced Improve flag input checking and errors 2020-09-05 10:30:25 -07:00
Kubernetes Prow Robot f67c13654e
Merge pull request #277 from thockin/9-better-logs
Better logs and errors
2020-09-05 10:27:40 -07:00
Kubernetes Prow Robot 9c56b25591
Merge pull request #276 from thockin/8-use-filepath
use filepath.Join, drop dep on path
2020-09-05 10:25:41 -07:00
Tim Hockin 04c85ee1dc Add metric for askpass, noop sync != success 2020-09-05 10:24:08 -07:00
Kubernetes Prow Robot 37f74f46fe
Merge pull request #274 from thockin/6-better-run-logs
Improve logs when running a command
2020-09-05 10:21:41 -07:00
Kubernetes Prow Robot 7d63180e78
Merge pull request #272 from thockin/4-move-setFlagDefaults
Move setFlagDefaults closer to flag code
2020-09-05 10:17:41 -07:00
Tim Hockin e43fb2513e Better logs and errors 2020-09-04 20:14:51 -07:00
Tim Hockin 07b1ab7572 use filepth.Join, drop dep on path 2020-09-04 20:13:22 -07:00
Tim Hockin 5ac2975ec4 Improve logs when running a command 2020-09-04 20:11:34 -07:00
Tim Hockin af755fbe99 Move setFlagDefaults closer to flag code 2020-09-04 20:07:47 -07:00
Tim Hockin 89aad82a31 Minor tweaks to submodules strings & consts
Rename consts internally and shorten help string a bit.
2020-09-04 20:06:44 -07:00
Tim Hockin 366e08ff40 Trivial comment change 2020-09-04 20:03:04 -07:00
alix.cook11 dcf169c10a
add option to NOT recursively clone submodules (#246)
* add option to NOT recursively clone submodules

* Add "shallow" and "off" modes to submodule clone

* update readme to reflect new flag

* wording and such feedback
2020-07-30 21:29:07 -07:00
windayski f44e140358
fix typo
fix typo
2020-06-30 16:21:33 +08:00
Kyle Zhu 1546e3415f
Reduce wait time 2020-04-24 17:32:32 -04:00
Kyle Zhu c1d4d77216
Relax wait between sync 2020-04-24 15:21:09 -04:00
Tim Hockin 82cd91958b Split stdout/stderr in runCommand()
Sometimes git emits things on stderr that are not errors but not part of
the programmatic output either.  Don't combine the output.
2020-03-20 13:44:33 -07:00
Tim Hockin 95a1690e6f Fix 2 bugs in pid1
1) Was calling Wait() on the child process, but also calling Wait4,
which would race, causing an occasional error or panic.

2) In testing (1), I observed occasional hangs.  Tracing it down to a
SIGWINCH, which masked a SIGCHLD, causing it to hang.  Both seem fixed.

Added a manual test script.
2020-03-20 13:44:27 -07:00
Reuben Thomas-Davis 4a75452461 avoid accessing Request in webhook.go until possible error due to invalid url has been handled 2020-03-16 19:49:59 +00:00
Tim Hockin ef8f5cf703 Bump to debian-base 2.0.0 (buster)
This changed `git fetch` to require a `-f` the way we use it.

Also fix a flaky test case for SSH.
2020-01-31 09:20:54 -08:00
Tim Hockin 72deefcec3 Make health check meaningful 2020-01-09 17:20:11 -08:00
Tim Hockin 5154ace66d Add --add-user to write UID/GID to passwd 2020-01-03 16:16:15 -08:00
Chuanying Du b0bdc02e8b manually merge https://github.com/kubernetes/git-sync/pull/217 2019-12-18 20:05:51 -08:00
Chuanying Du 31f276dd58 fix comments 2019-12-18 10:59:20 -08:00
Chuanying Du 6c6c354c72 update docs from auth-url to askpass-url 2019-12-17 11:02:03 -08:00
Chuanying Du 67a0788aa2 Rename to GIT_ASKPASS_URL and also update related examples. 2019-12-17 10:48:20 -08:00
Chuanying Du 9cae624f8c Add a new authentiate method GIT_SYNC_AUTH_URL.
It specifies a HTTP URL which will return username&password which will
be used to authenticate access to the git repo.

This is mainly used for git repo accecpt dynamic password (for example
oauth bare token). Because the dynamic password might expire very soon,
so it's added to the main syncRepo loop.

Typical usage case is work with a sidecar called gce-node-auth on GKE,
it uses the GCE service account's oauth token as password to access
Cloud Source Repo.

Please see the repo below for how it worked.
https://github.com/cydu-cloud/gce-node-auth/blob/master/git-sync-with-gce-node-auth.yaml
2019-12-16 23:45:37 -08:00
Kubernetes Prow Robot a2a8f72733
Merge pull request #211 from thockin/log-cleanups
Log before running commands, not after
2019-11-27 23:33:03 -08:00
Tim Hockin eac27f505e Log before actions 2019-11-26 13:45:18 -08:00
Tim Hockin fc700779be Log chmod operation 2019-11-26 13:45:18 -08:00
Tim Hockin e97eff010f trivial comments 2019-11-26 13:42:15 -08:00
Tim Hockin 287a0e5464 var renames for readability 2019-11-26 13:42:15 -08:00
Tim Hockin 415303f900 Rename for clarity 2019-11-26 13:42:15 -08:00
Kubernetes Prow Robot 5d7622e6ec
Merge pull request #208 from thockin/code-move-cleanups
Code move cleanups
2019-11-26 01:05:10 -08:00
Kubernetes Prow Robot cbad455c2b
Merge pull request #205 from thockin/git-cat-file
Make revIsHash a bit safer
2019-11-23 02:41:49 -08:00
Tim Hockin 74d3e9daab move code for readability 2019-11-21 21:57:53 -08:00
Tim Hockin d980b6d9d6 Move prune logic for cohesion 2019-11-21 21:57:53 -08:00
Tim Hockin dc5bdcb461 Better flag help strings 2019-11-21 21:47:15 -08:00
Tim Hockin 1b295ad217 Make revIsHash a bit safer 2019-11-21 08:55:30 -08:00
Tim Hockin f433b067ca Rename snake_case variable 2019-11-21 08:48:36 -08:00
Tim Hockin 5daf3a42f7 Clean up webhook a bit
Return hash so we don't need to recalculate.

Rename some funcs.
2019-11-17 13:22:38 -08:00
Kubernetes Prow Robot 7353b157b2
Merge pull request #193 from peick/http-header-webhook
Sent git hash and branch in the HTTP header of the webhook.
2019-10-29 09:16:46 -07:00
Kubernetes Prow Robot 012d0bfbf0
Merge pull request #194 from thockin/fix-pid1
Handle running as PID 1
2019-10-28 15:52:43 -07:00
Michael Peick 5cb2198689 Remove unnecessary comment. 2019-10-28 21:58:44 +01:00
Michael Peick 440eac730e Make hash value updates and reads atomic using mutex. 2019-10-28 21:57:03 +01:00
Kubernetes Prow Robot 26f21906be
Merge pull request #197 from hypnoglow/fix-nits
Fix minor issues from linters
2019-10-28 13:26:44 -07:00
Kubernetes Prow Robot 4bb587e083
Merge pull request #190 from hypnoglow/switch-to-runcommand
Replace exec.Command with runCommand
2019-10-28 13:24:43 -07:00
Igor Zibarev bff7e7bf43 Replace exec.Command with runCommand 2019-10-28 22:16:56 +03:00
Igor Zibarev 7a3c0679d5 Fix minor issues from linters 2019-10-28 22:05:39 +03:00
Tim Hockin 2a42fba009 Handle running as pid 1
This detects when it is running as pid 1, and becomes an init process.
Specifically this means handling SIGCHLD and reaping processes
(otherwise they become zombies) and forwarding signals to "real"
process.

We fork and re-exec ourselves so that we only get *this* SIGCHLD for
orphaned processes (re-parented to 1) and not the real events from
running things like git or ssh.
2019-10-27 09:45:27 -07:00
Michael Peick c3d026bf2b Cleanup test. 2019-10-27 14:47:18 +01:00
Michael Peick f0ec409582 Populate last hash value in case webhook is slower than the main loop. 2019-10-27 14:33:40 +01:00
Michael Peick e214aeb4d6 Rename http header "Git-Sync-Hash" into "Gitsync-Hash". 2019-10-26 13:34:39 +02:00
Michael Peick 1eb223e2cc Do not send branch information as http header. 2019-10-26 13:34:16 +02:00
Michael Peick 9ecc67fb3a Sent git hash and branch in the HTTP header of the webhook. 2019-10-25 10:42:37 +02:00
Michal Lula 94818756af 🔧 add depth flag support and cover with tests 2019-10-21 10:40:51 +02:00
Michal Lula d263429c6d 🎨 add todo comment for --depth flag support 2019-10-18 17:50:04 +02:00
Michal Lula 41c8572ad6 add tests 2019-10-11 17:58:47 +02:00
Michal Lula 3f06a3ba4b :spakrles: add git submodules support 2019-10-02 16:41:28 +02:00
Filip Filmar bbf6d60f36 git-sync: removes special exit on first error
Old code used to exit at any error seen on first sync attempt.  This
didn't prove useful in practice, so removing that special case.

This may make git-sync slower to recover after user fixes a
non-retryable error, as now flMaxSyncFailures are needed before the pod
fails.  It may make sense in practice.

Fixes #161, in a different way than is proposed in PR #162.
2019-04-02 10:06:00 -07:00
Tim Hockin 4493070350 Add --version flag 2019-03-25 08:54:44 -07:00
Tim Hockin 831eae8f9f Use official logr rather than thockin 2019-03-25 08:54:44 -07:00
Tim Hockin 51f19b81c9 Fix SSH
* Create a git-sync user to run as, with an entry in /etc/passwd and
writable homedir

* Remove our own validation of key perms - let SSH do that.

* Update docs,
2019-02-07 16:32:01 -08:00