Git designed for AI agents.
cli, policystore: the reload signal is caught from the start; every changed setting is named
Federico Elia0d181fc2d310
diff --git a/internal/cli/gitaly/policy_reload.go b/internal/cli/gitaly/policy_reload.goindex 16a41ce..62004b2 100644--- a/internal/cli/gitaly/policy_reload.go+++ b/internal/cli/gitaly/policy_reload.go@@ -19,13 +19,20 @@ var policyReloadSignal = syscall.SIGUSR1// for the reload.var reloaded = func(policystore.Outcome) {}-// reloadPolicyOnSignal reloads the policy of store from the configuration file at configPath every-// time the process receives policyReloadSignal, one reload at a time. running is the [graft]-// section the server started with. The returned function stops it.-func reloadPolicyOnSignal(logger log.Logger, configPath string, running config.Graft, store *policystore.Store) func() {+// notifyPolicyReload returns a channel that receives policyReloadSignal from now on. One signal waits+// in it until reloadPolicyOnSignal reads it, and those that come meanwhile make no more reloads.+func notifyPolicyReload() chan os.Signal {signals := make(chan os.Signal, 1)- done := make(chan struct{})signal.Notify(signals, policyReloadSignal)+ return signals+}++// reloadPolicyOnSignal reloads the policy of store from the configuration file at configPath every+// time signals, which notifyPolicyReload returned, receives the signal, one reload at a time; a+// signal that came before is handled at once. running is the configuration the server started+// with, as the file gave it. The returned function stops it.+func reloadPolicyOnSignal(logger log.Logger, configPath string, running config.Cfg, store *policystore.Store, signals chan os.Signal) func() {+ done := make(chan struct{})go func() {defer close(done)@@ -42,10 +49,11 @@ func reloadPolicyOnSignal(logger log.Logger, configPath string, running config.G}// reloadPolicy reads the configuration file at configPath again and puts its policy in force when it-// is valid; otherwise the policy in force stays. The outcome is logged: an invalid configuration at-// ERROR, a new policy at INFO, and the changes that need a restart and the actors that name no-// configured agent at WARN.-func reloadPolicy(logger log.Logger, configPath string, running config.Graft, store *policystore.Store) policystore.Outcome {+// is valid; otherwise the policy in force stays. running is the configuration the server started+// with: the settings of the file that differ from it, other than the policy, need a restart. The+// outcome is logged: an invalid configuration at ERROR, a new policy at INFO, and the settings that+// need a restart and the actors that name no configured agent at WARN.+func reloadPolicy(logger log.Logger, configPath string, running config.Cfg, store *policystore.Store) policystore.Outcome {logger = logger.WithField("config_path", configPath)next, err := loadConfig(configPath)@@ -53,7 +61,10 @@ func reloadPolicy(logger log.Logger, configPath string, running config.Graft, stif err != nil {outcome = store.Reject()} else {- outcome, err = store.Reload(next.Graft, running)+ outcome, err = store.Reload(next.Graft, running.Graft)+ if err == nil {+ outcome.NeedsRestart = append(policystore.NeedsRestartOutsideGraft(next, running), outcome.NeedsRestart...)+ }}if err != nil {logger.WithError(err).WithFields(log.Fields{@@ -77,7 +88,7 @@ func reloadPolicy(logger log.Logger, configPath string, running config.Graft, stlogger.WithFields(fields).Info("policy reload: the policy is unchanged")}if len(outcome.NeedsRestart) > 0 {- logger.WithField("needs_restart", outcome.NeedsRestart).Warn("policy reload: the configuration changed outside the policy; those changes need a restart and were not applied")+ logger.WithField("needs_restart", outcome.NeedsRestart).Warn("policy reload: these settings changed outside the policy; they need a restart and were not applied")}if len(outcome.UnknownActors) > 0 {logger.WithField("actors", outcome.UnknownActors).Warn("policy reload: actors of the policy that name no configured agent (user IDs of callers with the shared token?)")diff --git a/internal/cli/gitaly/policy_reload_test.go b/internal/cli/gitaly/policy_reload_test.goindex df53ae3..cbd9477 100644--- a/internal/cli/gitaly/policy_reload_test.go+++ b/internal/cli/gitaly/policy_reload_test.go@@ -37,7 +37,7 @@ func reloadSetup(t *testing.T) (config.Cfg, *policystore.Store, testhelper.Loggelogger := testhelper.NewLogger(t)hook := testhelper.AddLoggerHook(logger)reload := func(configPath string) policystore.Outcome {- return reloadPolicy(logger, configPath, cfg.Graft, store)+ return reloadPolicy(logger, configPath, cfg, store)}return cfg, store, hook, reload}@@ -115,6 +115,19 @@ func TestReloadPolicy(t *testing.T) {})}+ t.Run("a changed setting outside [graft] is named as needing a restart", func(t *testing.T) {+ t.Parallel()++ cfg, store, _, reload := reloadSetup(t)+ cfg.Graft.Policy = tighterRules+ cfg.Logging.Level = "debug"++ outcome := reload(testcfg.WriteTemporaryGitalyConfigFile(t, cfg))+ require.Equal(t, policystore.Applied, outcome.Result)+ require.Equal(t, []string{"logging"}, outcome.NeedsRestart)+ require.Equal(t, tighterRules, store.Current().Rules)+ })+t.Run("changed agents need a restart and are not applied; the policy is", func(t *testing.T) {t.Parallel()@@ -129,7 +142,7 @@ func TestReloadPolicy(t *testing.T) {var warned boolfor _, entry := range hook.AllEntries() {if entry.Level == logrus.WarnLevel {- require.Contains(t, entry.Message, "need a restart and were not applied")+ require.Contains(t, entry.Message, "they need a restart and were not applied")require.Equal(t, []string{"graft.agent"}, entry.Data["needs_restart"])warned = true}@@ -143,7 +156,7 @@ func TestReloadPolicy(t *testing.T) {func TestReloadPolicyOnSignal(t *testing.T) {cfg := startupConfig(t)store := policystore.New(cfg.Graft.Policy)- running := cfg.Graft+ running := cfgcfg.Graft.Policy = tighterRulesconfigPath := testcfg.WriteTemporaryGitalyConfigFile(t, cfg)@@ -152,10 +165,14 @@ func TestReloadPolicyOnSignal(t *testing.T) {reloaded = func(outcome policystore.Outcome) { outcomes <- outcome }t.Cleanup(func() { reloaded = original })- stop := reloadPolicyOnSignal(testhelper.NewLogger(t), configPath, running, store)+ // The signal comes before the reloads start, as one may while the server starts: the channel+ // that notifyPolicyReload registered keeps it, and the process does not take the signal's+ // default action, its end.+ signals := notifyPolicyReload()+ require.NoError(t, syscall.Kill(os.Getpid(), policyReloadSignal))+ stop := reloadPolicyOnSignal(testhelper.NewLogger(t), configPath, running, store, signals)defer stop()- require.NoError(t, syscall.Kill(os.Getpid(), policyReloadSignal))select {case outcome := <-outcomes:require.Equal(t, policystore.Applied, outcome.Result)diff --git a/internal/cli/gitaly/serve.go b/internal/cli/gitaly/serve.goindex a53aa2e..8140f99 100644--- a/internal/cli/gitaly/serve.go+++ b/internal/cli/gitaly/serve.go@@ -4,6 +4,7 @@ import ("context""fmt""os"+ "os/signal""runtime/debug""time"@@ -127,6 +128,11 @@ func serveAction(ctx context.Context, cmd *cli.Command) error {cli.ShowSubcommandHelpAndExit(cmd, 2)}+ // The reload signal is caught from the start: its default action ends the process, and an+ // operator or gitaly-wrapper may send it while the server starts.+ policySignals := notifyPolicyReload()+ defer signal.Stop(policySignals)+cfg, logger, err := configure(cmd.Args().First())if err != nil {return exitWithError(logger, "failed to configure Gitaly", err)@@ -155,7 +161,7 @@ func serveAction(ctx context.Context, cmd *cli.Command) error {logger.WithField("version", version.GetVersion()).Info("Starting Gitaly")fips.Check()- if err := run(cmd, cfg, logger); err != nil {+ if err := run(cmd, cfg, logger, policySignals); err != nil {return exitWithError(logger, "unclean Gitaly shutdown", err)}@@ -275,10 +281,14 @@ func preloadLicenseDatabase(logger log.Logger) {}()}-func run(appCtx *cli.Command, cfg config.Cfg, logger log.Logger) error {+func run(appCtx *cli.Command, cfg config.Cfg, logger log.Logger, policySignals chan os.Signal) error {ctx, cancel := context.WithCancel(context.Background())defer cancel()+ // The configuration as the file gave it, before the setup below changes some of it (the+ // runtime directory): a reload of the policy compares the file with it.+ loaded := cfg+// shutdownCtx is cancelled once the graceful shutdown period has elapsed. It lets// services terminate work that intentionally ignores the per-request context, such// as the git-pack-objects processes driven by PackObjectsHookWithSidechannel. Such@@ -589,7 +599,7 @@ func run(appCtx *cli.Command, cfg config.Cfg, logger log.Logger) error {// SIGUSR1 reloads from the configuration file (doc/graft/PLAN12.md).graftPolicy := policystore.New(cfg.Graft.Policy)prometheus.MustRegister(graftPolicy)- stopPolicyReload := reloadPolicyOnSignal(logger, appCtx.Args().First(), cfg.Graft, graftPolicy)+ stopPolicyReload := reloadPolicyOnSignal(logger, appCtx.Args().First(), loaded, graftPolicy, policySignals)defer stopPolicyReload()gitlabClient, err := newGitlabAPIClient(logger, cfg, graftPolicy)diff --git a/internal/graft/e2e/policy_reload_test.go b/internal/graft/e2e/policy_reload_test.goindex 68a708e..485b37a 100644--- a/internal/graft/e2e/policy_reload_test.go+++ b/internal/graft/e2e/policy_reload_test.go@@ -44,6 +44,9 @@ func TestPolicyReloadOnSignal(t *testing.T) {require.NoError(t, n.cmd.Process.Signal(syscall.SIGUSR1))waitFor(`graft_policy_reloads_total{result="applied"}`, 1)require.EqualValues(t, 2, n.counter(t, "graft_policy_generation"))+ // Only the policy changed: the server compares the file with the configuration it started+ // with, as the file gave it, and finds nothing else.+ require.NotContains(t, n.log(t), "they need a restart")rewrite([]config.GraftPolicyRule{{Actor: "*", Refs: []string{"refs/heads/["}, Allow: true}})require.NoError(t, n.cmd.Process.Signal(syscall.SIGUSR1))diff --git a/internal/graft/policystore/store.go b/internal/graft/policystore/store.goindex 01b6385..791bdc0 100644--- a/internal/graft/policystore/store.go+++ b/internal/graft/policystore/store.go@@ -159,11 +159,13 @@ func (s *Store) Reload(next config.Graft, running config.Graft) (Outcome, error)// publish puts snapshot in force and updates the gauges.func (s *Store) publish(snapshot *Snapshot) {- if previous := s.current.Swap(snapshot); previous != nil {- s.info.DeleteLabelValues(previous.SHA256)- }+ previous := s.current.Swap(snapshot)s.generation.Set(float64(snapshot.Generation))+ // The new series first: a scrape between the two sees the hash in force, never none.s.info.WithLabelValues(snapshot.SHA256).Set(1)+ if previous != nil && previous.SHA256 != snapshot.SHA256 {+ s.info.DeleteLabelValues(previous.SHA256)+ }}// Hash returns the SHA-256 of rules in a canonical encoding: two policies with the same rules in@@ -185,18 +187,38 @@ func Hash(rules []config.GraftPolicyRule) string {return hex.EncodeToString(sum[:])}-// needsRestart names what differs between next and running outside the policy: a reload applies the-// policy only.+// needsRestart names the settings of the [graft] section that differ between next and running,+// other than the policy: a reload applies the policy only.func needsRestart(next, running config.Graft) []string {+ return changedSettings("graft.", next, running, "policy")+}++// NeedsRestartOutsideGraft names the settings outside the [graft] section that differ between the+// configurations next and running, by their TOML keys: a reload does not apply them.+func NeedsRestartOutsideGraft(next, running config.Cfg) []string {+ return changedSettings("", next, running, "graft")+}++// changedSettings names, with prefix, the TOML keys of the fields of the structs next and running,+// of one type, whose values differ, skipping the keys in skip.+func changedSettings(prefix string, next, running any, skip ...string) []string {+ nextValue, runningValue := reflect.ValueOf(next), reflect.ValueOf(running)var changed []string- if !slices.Equal(next.Agents, running.Agents) {- changed = append(changed, "graft.agent")- }- nextRest, runningRest := next, running- nextRest.Policy, runningRest.Policy = nil, nil- nextRest.Agents, runningRest.Agents = nil, nil- if !reflect.DeepEqual(nextRest, runningRest) {- changed = append(changed, "graft (other than graft.policy and graft.agent)")+ for i := 0; i < nextValue.NumField(); i++ {+ field := nextValue.Type().Field(i)+ if !field.IsExported() {+ continue+ }+ key, _, _ := strings.Cut(field.Tag.Get("toml"), ",")+ if key == "" {+ key = field.Name+ }+ if slices.Contains(skip, key) {+ continue+ }+ if !reflect.DeepEqual(nextValue.Field(i).Interface(), runningValue.Field(i).Interface()) {+ changed = append(changed, prefix+key)+ }}return changed}diff --git a/internal/graft/policystore/store_test.go b/internal/graft/policystore/store_test.goindex 4fcddf8..88a2ea2 100644--- a/internal/graft/policystore/store_test.go+++ b/internal/graft/policystore/store_test.go@@ -95,7 +95,7 @@ func TestStore_reload(t *testing.T) {outcome, err := s.Reload(next, running)require.NoError(t, err)require.Equal(t, Applied, outcome.Result)- require.Equal(t, []string{"graft.agent", "graft (other than graft.policy and graft.agent)"}, outcome.NeedsRestart)+ require.Equal(t, []string{"graft.agent", "graft.agent_rpcs"}, outcome.NeedsRestart)require.EqualValues(t, 3, s.Current().Generation)})