saltstack / saltstack/salt

[BUG] jobs.list_jobs shows "Target": unknown-target when using pgjsonb returner

Open
#57,524 13 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug needs-testcase severity-medium
Dominant language
Python
Stars
15.7k
Forks
5.6k
Avg merge
2d 44m
Merged PRs (30d)
80

Description

Description
A clear and concise description of what the bug is.

Hello Colleagues, I get an issue related to pgjsonb returner. The problem is: when I try to list jobs with the following command:

salt-run jobs.list_jobs search_function='pkg.update'

I get "Target": unknown-target

I suppose the issue in jsonb database load column.

CREATE TABLE jids (
jid varchar(255) NOT NULL primary key,
load jsonb NOT NULL
);

Filename: "/usr/local/lib/python3.6/site-packages/salt/returners/pgjsonb.py"

If you check jsonb schema, it has load column, which has an id property in json, as a name of target.
But other returners have tgt property instead of id, like postgres_local_cache.

if you pass a list of minions to salt -L 'minion1, minion2' the database load column has an id property with one minion and not a list, but it should be a list (array).

Also I checked jid utils file
Filename: /usr/local/lib/python3.6/site-packages/salt/utils/jid.py

it has the format_job_instance method, which also includes tgt, and if it is empty it shows unknown-target, and it shows unknown-target in pgjsonb returner, because it doesn't have tgt property, it uses id.

def format_job_instance(job):
'''
Format the job instance correctly
'''
ret = {'Function': job.get('fun', 'unknown-function'),
'Arguments': list(job.get('arg', [])),
# unlikely but safeguard from invalid returns
'Target': job.get('tgt', 'unknown-target'),
'Target-type': job.get('tgt_type', 'list'),
'User': job.get('user', 'root')}

Setup
(Please provide relevant configs and/or SLS files (be sure to remove sensitive info).
Filename: "/usr/local/lib/python3.6/site-packages/salt/returners/pgjsonb.py"

Steps to Reproduce the behavior
(Include debug logs if possible and relevant)

Expected behavior
A clear and concise description of what you expected to happen.

jobs.list_jobs and jobs.print_job should return Target: [minion1, minion2]. and not unknown-target.

jobs.print_job is also affected and should be changed to something similar

Screenshots
If applicable, add screenshots to help explain your problem.

Versions Report

salt --versions-report (Provided by running salt --versions-report. Please also mention any differences in master/minion versions.)
PASTE HERE

Additional context
Add any other context about the problem here.

As a workaround I changed get_jids method in file /usr/local/lib/python3.6/site-packages/salt/returners/pgjsonb.py to below and it seems it is working, but I suppose it should be tgt as a target and not id.

def get_jids():
'''
Return a list of all job ids
'''
with _get_serv(ret=None, commit=True) as cur:

    #sql = '''SELECT jid, load
    #        FROM jids'''
    **sql = '''SELECT j.jid as jid, j.load as load, jsonb_agg(sr.id) as tgt FROM jids j
            join salt_returns sr
            using(jid)
            group by j.jid, j.load'''**

    cur.execute(sql)
    data = cur.fetchall()
    ret = {}
    **for** jid, load, tgt in data:
        **load["tgt"] = tgt**
        ret[jid] = salt.utils.jid.format_jid_instance(jid, load)
    return ret

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with salt/returners/pgjsonb.py, especially get_jids, and compare its load data with salt/utils/jid.py's format_job_instance. Reproduce with jobs.list_jobs search_function='pkg.update' and check jobs.print_job as well. Done means both commands report the target, including a list such as [minion1, minion2], instead of unknown-target.

Written by the indexing model from the issue text.

Assessment

Tech stack
postgresql, python
Domain
cli, databases
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.