diff --git a/salt/manager/tools/sbin/so-push-drainer b/salt/manager/tools/sbin/so-push-drainer index beee274d9..a33b75751 100644 --- a/salt/manager/tools/sbin/so-push-drainer +++ b/salt/manager/tools/sbin/so-push-drainer @@ -16,6 +16,7 @@ For each intent file under /opt/so/state/push_pending/*.json whose last_touch is older than debounce_seconds, this script: * concatenates the actions lists from every ready intent * dedupes by (state or __highstate__, tgt, tgt_type) + * narrows each target to minions that have completed a highstate * drops actions whose target matches no accepted minion (e.g. a role with no nodes yet) * holds everything for a later pass while salt-master has only just started * dispatches a single `salt-run state.orchestrate orch.push_batch --async` @@ -58,6 +59,9 @@ DISPATCHED_DIR = '/opt/so/state/push_dispatched' HIGHSTATE_SENTINEL = '__highstate__' +# Grain written by setup and removed by salt.lasthighstate at the end of the node's first highstate. +PENDING_GRAIN = 'first_highstate_pending' + RESULT_CHECK_DELAY = 30 RESULT_RECHECK_MAX = 300 RESULT_MAX_AGE = 7200 @@ -126,9 +130,18 @@ def _dedupe_actions(actions): return deduped +def _skip_pending(action): + """Narrows a compound target to minions that have completed a highstate. A newly accepted node matches + its role's targets before orch.deploy_newnode has highstated it, and a single state fails there.""" + if action.get('tgt_type', 'compound') != 'compound': + return action + return dict(action, tgt='( {} ) and not G@{}:True'.format(action.get('tgt'), PENDING_GRAIN)) + + def _targeted(actions, log): """Drops actions whose target matches no accepted minion (e.g. a role with no nodes yet), which the orchestration would otherwise report as failed with 'No minions responded'.""" + actions = [_skip_pending(action) for action in actions] try: ckminions = salt.utils.minions.CkMinions(salt.config.master_config(MASTER_CONFIG)) except Exception: diff --git a/salt/manager/tools/sbin/so-push-drainer_test.py b/salt/manager/tools/sbin/so-push-drainer_test.py index 062358100..80623f8ab 100644 --- a/salt/manager/tools/sbin/so-push-drainer_test.py +++ b/salt/manager/tools/sbin/so-push-drainer_test.py @@ -34,6 +34,7 @@ JID = '20260930171554259426' ASYNC_STDERR = ('[WARNING ] Running in asynchronous mode. Results of this execution may be collected ' 'by attaching to the master event bus or by examining the master job cache, if ' 'configured. This execution is running under tag salt/run/{}\n'.format(JID)) +NOT_PENDING = ' and not G@first_highstate_pending:True' CONFLICT = ('The function "state.sls" is running as PID 372218 and was started at ' '2026, Sep 30 17:15:40.466233 with jid 20260930171540466233') @@ -490,8 +491,19 @@ class TestTargeted(DrainerTestCase): self.CkMinions = patcher.start() self.addCleanup(patcher.stop) + def test_skip_pending(self): + self.assertEqual(drainer._skip_pending({'state': 'soc', 'tgt': 'G@role:so-eval or G@role:so-manager'}), + {'state': 'soc', 'tgt': '( G@role:so-eval or G@role:so-manager )' + NOT_PENDING}) + self.assertEqual(drainer._skip_pending({'highstate': True, 'tgt': '*', 'tgt_type': 'compound'}), + {'highstate': True, 'tgt': '( * )' + NOT_PENDING, 'tgt_type': 'compound'}) + listed = {'state': 'soc', 'tgt': 'manager_manager', 'tgt_type': 'list'} + self.assertEqual(drainer._skip_pending(listed), listed) + def test_drops_unmatched(self): - matches = {'G@role:so-sensor': ['sensor_sensor'], 'manager_manager': ['manager_manager']} + sensor = '( G@role:so-sensor )' + NOT_PENDING + heavynode = '( G@role:so-heavynode )' + NOT_PENDING + idh = '( G@role:so-idh )' + NOT_PENDING + matches = {sensor: ['sensor_sensor'], 'manager_manager': ['manager_manager']} self.ckminions.check_minions.side_effect = lambda tgt, tgt_type: {'minions': matches.get(tgt, [])} actions = [ {'state': 'strelka', 'tgt': 'G@role:so-sensor'}, @@ -499,28 +511,27 @@ class TestTargeted(DrainerTestCase): {'highstate': True, 'tgt': 'G@role:so-idh'}, {'state': 'soc', 'tgt': 'manager_manager', 'tgt_type': 'list'}, ] - self.assertEqual(drainer._targeted(actions, self.log), [actions[0], actions[3]]) + self.assertEqual(drainer._targeted(actions, self.log), [{'state': 'strelka', 'tgt': sensor}, actions[3]]) self.CkMinions.assert_called_once() self.assertEqual([c.args for c in self.ckminions.check_minions.call_args_list], [ - ('G@role:so-sensor', 'compound'), ('G@role:so-heavynode', 'compound'), - ('G@role:so-idh', 'compound'), ('manager_manager', 'list'), + (sensor, 'compound'), (heavynode, 'compound'), (idh, 'compound'), ('manager_manager', 'list'), ]) info = self.logged('info') - self.assertIn('skipping suricata: no minions match tgt=G@role:so-heavynode', info) - self.assertIn('skipping highstate: no minions match tgt=G@role:so-idh', info) + self.assertIn('skipping suricata: no minions match tgt=' + heavynode, info) + self.assertIn('skipping highstate: no minions match tgt=' + idh, info) self.assertNotIn('strelka', info) def test_master_config_error_keeps_actions(self): actions = [{'state': 'soc', 'tgt': '*'}] with patch.object(drainer.salt.config, 'master_config', side_effect=OSError('no config')): - self.assertEqual(drainer._targeted(actions, self.log), actions) + self.assertEqual(drainer._targeted(actions, self.log), [{'state': 'soc', 'tgt': '( * )' + NOT_PENDING}]) self.log.exception.assert_called_once() self.CkMinions.assert_not_called() def test_check_error_keeps_action(self): actions = [{'state': 'soc', 'tgt': 'G@role:'}] self.ckminions.check_minions.side_effect = RuntimeError('bad target') - self.assertEqual(drainer._targeted(actions, self.log), actions) + self.assertEqual(drainer._targeted(actions, self.log), [{'state': 'soc', 'tgt': '( G@role: )' + NOT_PENDING}]) self.log.exception.assert_called_once() diff --git a/salt/reactor/pillar_push_map.yaml b/salt/reactor/pillar_push_map.yaml index b49397c30..65e4fe0fa 100644 --- a/salt/reactor/pillar_push_map.yaml +++ b/salt/reactor/pillar_push_map.yaml @@ -3,6 +3,7 @@ # # One pillar directory can map to multiple (state, tgt) actions. # tgt is a raw salt compound expression. tgt_type is always "compound". +# so-push-drainer excludes nodes that have not completed their first highstate. # Per-action `batch` / `batch_wait` override the orch defaults (10% / 15s). # An action with `highstate: True` triggers state.highstate instead of # state.apply -- see salt/orch/push_batch.sls. diff --git a/salt/salt/lasthighstate.sls b/salt/salt/lasthighstate.sls index 88d060483..2c7ca9554 100644 --- a/salt/salt/lasthighstate.sls +++ b/salt/salt/lasthighstate.sls @@ -2,3 +2,9 @@ lasthighstate: file.touch: - name: /opt/so/log/salt/lasthighstate - order: 9001 + +# set by setup; so-push-drainer does not target a node that still has it +first_highstate_pending: + grains.absent: + - destructive: True + - order: 9001 diff --git a/setup/so-functions b/setup/so-functions index 0381b6252..fed7d9096 100755 --- a/setup/so-functions +++ b/setup/so-functions @@ -519,6 +519,8 @@ configure_minion() { info "Configuring minion type as $minion_type" logCmd "mkdir -p /etc/salt/minion.d" echo "role: so-$minion_type" > /etc/salt/grains + # so-push-drainer leaves the node alone until salt.lasthighstate removes this + echo "first_highstate_pending: True" >> /etc/salt/grains local minion_config=/etc/salt/minion