"container started" event of a pod from kubernetes using pythons kubernetes library - python-2.7

I've a deployment with one container having postStart hook as shown below
containers:
- name: openvas
image: my-image:test
lifecycle:
postStart:
exec:
command:
- /usr/local/tools/is_service_ready.sh
I'm watching for the events for pods using python's kubernetes library.
when the pod gets deployed, container comes up and postStart script will be executed until postStart script exits successfully. I want to get the event from kubernetes using pythons kubernetes library when CONTAINER comes up.
I tried watching the event, I get the event with status as 'containersReady' only when postStart completes and the POD comes up,it can be seen below.
'status': {'conditions': [{'last_probe_time': None,
'last_transition_time': datetime.datetime(2019, 4, 18, 16, 25, 3, tzinfo=tzlocal()),
'message': None,
'reason': None,
'status': 'True',
'type': 'Initialized'},
{'last_probe_time': None,
'last_transition_time': datetime.datetime(2019, 4, 18, 16, 26, 51, tzinfo=tzlocal()),
'message': None,
'reason': None,
'status': 'True',
'type': 'Ready'},
{'last_probe_time': None,
'last_transition_time': None,
'message': None,
'reason': None,
'status': 'True',
'type': 'ContainersReady'},
{'last_probe_time': None,
'last_transition_time': datetime.datetime(2019, 4, 18, 16, 25, 3, tzinfo=tzlocal()),
'message': None,
'reason': None,
'status': 'True',
'type': 'PodScheduled'}],
'container_statuses': [{'container_id': 'docker://1c39e13dc777a34c38d4194edc23c3668697223746b60276acffe3d62f9f0c44',
'image': 'my-image:test',
'image_id': 'docker://sha256:9903437699d871c1f3af7958a7294fe419ed7b1076cdb8e839687e67501b301b',
'last_state': {'running': None,
'terminated': None,
'waiting': None},
'name': 'samplename',
'ready': True,
'restart_count': 0,
'state': {'running': {'started_at': datetime.datetime(2019, 4, 18, 16, 25, 14, tzinfo=tzlocal())},
'terminated': None,
'waiting': None}}],
and before this I get status 'podScheduled' as 'True'
'status': {'conditions': [{'last_probe_time': None,
'last_transition_time': datetime.datetime(2019, 4, 18, 16, 25, 3, tzinfo=tzlocal()),
'message': None,
'reason': None,
'status': 'True',
'type': 'Initialized'},
{'last_probe_time': None,
'last_transition_time': datetime.datetime(2019, 4, 18, 16, 25, 3, tzinfo=tzlocal()),
'message': 'containers with unready status: [openvas]',
'reason': 'ContainersNotReady',
'status': 'False',
'type': 'Ready'},
{'last_probe_time': None,
'last_transition_time': None,
'message': 'containers with unready status: [openvas]',
'reason': 'ContainersNotReady',
'status': 'False',
'type': 'ContainersReady'},
{'last_probe_time': None,
'last_transition_time': datetime.datetime(2019, 4, 18, 16, 25, 3, tzinfo=tzlocal()),
'message': None,
'reason': None,
'status': 'True',
'type': 'PodScheduled'}],
'container_statuses': [{'container_id': None,
'image': 'ns-openvas:test',
'image_id': '',
'last_state': {'running': None,
'terminated': None,
'waiting': None},
'name': 'openvas',
'ready': False,
'restart_count': 0,
'state': {'running': None,
'terminated': None,
'waiting': {'message': None,
'reason': 'ContainerCreating'}}}],
Anything I can try to get the event when the CONTAINER comes up.

Obviously, with current approach you will never get it working, because, as describe here :
The postStart handler runs asynchronously relative to the Container’s
code, but Kubernetes’ management of the container blocks until the
postStart handler completes. The Container’s status is not set to
RUNNING until the postStart handler completes.
Maybe you should create another pod with is_service_ready.sh script, which will be watching events of the main pod.

Related

Django q process does not get cleared from memory

I have integrated Django Q in my project and i'm running a task from django q but after task ran successfully i can still see the process is in memory is there any way to clear the process from memory after it has finished the job.
Here is the django q settings
Q_CLUSTER = {
'name': 'my_app',
'workers': 8,
'recycle': 500,
'compress': True,
'save_limit': 250,
'queue_limit': 500,
'cpu_affinity': 1,
'label': 'Django Q',
'max_attempts': 1,
'attempt_count': 1,
'catch_up': False,
'redis': {
'host': '127.0.0.1',
'port': 6379,
'db': 0, }}

Celery Django Body Encoding

Hi does anyone know how the body of a celery json is encoded before it is entered in the queue cache (i use Redis in my case).
{'body': 'W1sic2hhd25AdWJ4LnBoIiwge31dLCB7fSwgeyJjYWxsYmFja3MiOiBudWxsLCAiZXJyYmFja3MiOiBudWxsLCAiY2hhaW4iOiBudWxsLCAiY2hvcmQiOiBudWxsfV0=',
'content-encoding': 'utf-8',
'content-type': 'application/json',
'headers': {'lang': 'py',
'task': 'export_users',
'id': '6e506f75-628e-4aa1-9703-c0185c8b3aaa',
'shadow': None,
'eta': None,
'expires': None,
'group': None,
'retries': 0,
'timelimit': [None, None],
'root_id': '6e506f75-628e-4aa1-9703-c0185c8b3aaa',
'parent_id': None,
'argsrepr': "('<email#example.com>', {})",
'kwargsrepr': '{}',
'origin': 'gen187209#ubuntu'},
'properties': {'correlation_id': '6e506f75-628e-4aa1-9703-c0185c8b3aaa',
'reply_to': '403f7314-384a-30a3-a518-65911b7cba5c',
'delivery_mode': 2,
'delivery_info': {'exchange': '', 'routing_key': 'celery'},
'priority': 0,
'body_encoding': 'base64',
'delivery_tag': 'dad6b5d3-c667-473e-a62c-0881a7349684'}}
Just a background I have a nodejs project which needs to trigger my celery (django). Background tasks are all in the django app but the trigger and the details will come from a nodejs app.
Thanks in advance
It may just be simpler to use the nodejs celery client
https://github.com/mher/node-celery/blob/master/celery.js
to invoke a celery task from nodejs.

How to resolve error The specified alg value is not allowed in django

I am trying to decode some JWT string that I have received from an authentication service but I'm getting an error The specified alg value is not allowed. What could be the issue? I verified that the algorithim I should use is HS256.
When I try to decode the JWT string at https://jwt.io/ it's being decoded.
code snippet
try:
print(jwt_value)
decoded = jwt.decode(jwt_value, 'secret', algorithms=['HS256'])
print(decoded)
except Exception as e:
print(e)
JWT Settings
JWT_AUTH = {
# 'JWT_EXPIRATION_DELTA': datetime.timedelta(seconds=36000),
'JWT_ENCODE_HANDLER':
'rest_framework_jwt.utils.jwt_encode_handler',
'JWT_DECODE_HANDLER':
'rest_framework_jwt.utils.jwt_decode_handler',
'JWT_PAYLOAD_HANDLER':
'sbp.custom_jwt.jwt_payload_handler',
'JWT_PAYLOAD_GET_USER_ID_HANDLER':
'rest_framework_jwt.utils.jwt_get_user_id_from_payload_handler',
'JWT_RESPONSE_PAYLOAD_HANDLER':
'sbp.custom_jwt.jwt_response_payload_handler',
'JWT_SECRET_KEY': 'secret',
'JWT_GET_USER_SECRET_KEY': None,
'JWT_PUBLIC_KEY': None,
'JWT_PRIVATE_KEY': None,
'JWT_ALGORITHM': 'HS256',
'JWT_VERIFY': True,
'JWT_VERIFY_EXPIRATION': True,
'JWT_LEEWAY': 0,
'JWT_EXPIRATION_DELTA': datetime.timedelta(seconds=36000),
'JWT_AUDIENCE': None,
'JWT_ISSUER': None,
'JWT_ALLOW_REFRESH': False,
'JWT_REFRESH_EXPIRATION_DELTA': datetime.timedelta(days=7),
'JWT_AUTH_HEADER_PREFIX': 'JWT',
'JWT_AUTH_COOKIE': None,
}

Where is the model instance for a Django SimplyLazyObject instance?

I want to see the model instance corresponding to a SimplyLazyObject that I'm getting so I can print it's __dict__. How can I find the model instance?
e.g.,
ipdb> user
<django.utils.functional.SimpleLazyObject object at 0x10c84d150>
The _wrapped attribute of the SimplyLazyObject instance contains the model instance.
e.g.,
ipdb> user
<django.utils.functional.SimpleLazyObject object at 0x10c84d150>
ipdb> pp(user.__dict__)
{'_setupfunc': <function <lambda> at 0x10c846938>,
'_wrapped': <User: foo#bednark.com>}
ipdb> user._wrapped
<User: foo#bednark.com>
ipdb> type(user._wrapped)
<class 'django.contrib.auth.models.User'>
ipdb> pp(user._wrapped.__dict__)
{'_profile_cache': <UserProfile: foo#bednark.com>,
'_state': <django.db.models.base.ModelState object at 0x10c869290>,
'_username': u'4qInglEqb3HzIFMEuo1H4WkR-HXbWI',
'date_joined': datetime.datetime(2013, 7, 29, 0, 53, 5, 545109),
'email': u'foo#bednark.com',
'first_name': u'Rob',
'id': 24,
'is_active': True,
'is_staff': False,
'is_superuser': False,
'last_login': datetime.datetime(2013, 7, 29, 0, 53, 23, 739774),
'last_name': u'Bednark',
'password': u'pbkdf2_sha256$10000$a1pAQhQuJ2Ng$79CDBtDYWsBxnxShXUSPOTNUVj63cDpaocay6BNzfRs=',
'username': u'foo#bednark.com'}

What are my next debugging steps? InternalError: current transaction is aborted, commands ignored until end of transaction block

UPDATE: The problem wasn't with the previous transaction, it was happening because the database wasn't synced / migrated properly.
I've recently switched my local database to Postgres, and I'm now getting an InternalError.
I understand from this question that the problem likely originates from a previous transaction not executing properly:
"This is what postgres does when a query produces an error and you try to run another query without first rolling back the transaction."
However, from the logs below, it seems like the first query, DEBUG (0.0001), executes fine (I also tested this exact query though the Django DB Shell):
DEBUG (0.001) SELECT "django_site"."id", "django_site"."domain", "django_site"."name"
FROM "django_site" WHERE "django_site"."id" = 12 ; args=(12,)
Full SQL Logs
DEBUG (0.001) SELECT "django_site"."id", "django_site"."domain", "django_site"."name" FROM "django_site" WHERE "django_site"."id" = 12 ; args=(12,)
DEBUG (0.003) INSERT INTO "application_app" ("applied_date", "fname", "lname",
"email_address", "phone_number", "skype_id", "applied_track", "college1",
"field_of_study1", "graduation_month1", "graduation_year1", "degree1",
"degree_other1", "working_during_program", "explain_working_during_program",
"sib_goal", "twitter_link", "linkedin_link", "other_social_link1",
"other_social_link2", "other_social_link3", "applied_class", "applied_location",
"referral", "colossal_failure", "next_week_year_10year", "you_created",
"your_inspiration", "dev_years_of_exp", "dev_fav_lang", "dev_fav_lang_why",
"dev_link_youve_built", "dev_link_github", "dev_fav_resource", "prod_cool_prod",
"prod_fav_designer", "prod_portfolio", "prod_bad_design", "prod_link_dribble",
"mark_ind_trend", "mark_email_to_coworkers", "mark_keep_em_happy",
"mark_article_or_blog", "sales_why_you", "sales_convince_restaurant",
"sales_hardest_door", "sales_sale_within_the_year", "housing_needed",
"program_payment", "any_last_requests") VALUES ('2013-04-20 13:22:06.565691+00:00',
'Brian', 'Dant', 'test#gmail.com', '', '', 'MAR', '', '', 1, NULL, '', '', false,
'', 'GN', '', 'http://linkedin.com/', '', '', '', 'NYCSUM13', '', 'test', 'test',
'test', 'test', 'test', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '',
'', '', '', '', false, 'UF', 'test') RETURNING "application_app"."id"; args=(u'2013-04-20 13:22:06.565691+00:00',
u'Brian', u'Dant', u'test#gmail.com', u'', u'', u'MAR', u'', u'', 1, None, '', u'',
False, u'', u'GN', u'', u'http://linkedin.com/', u'', u'', '', u'NYCSUM13', '',
u'test', u'test', u'test', u'test', u'test', '', '', u'', u'', u'', u'', u'', '',
u'', u'', u'', u'', u'', u'', u'', u'', u'', u'', u'', False, u'UF', u'test')
ERROR Internal Server Error: /
Traceback (most recent call last):
File "/Users/chaz/dev/envs/startupinstitute.com/lib/python2.7/site-packages/django/core/handlers/base.py", line 111, in get_response
response = callback(request, *callback_args, **callback_kwargs)
File "/Users/chaz/dev/projects/startupinstitute.com/apps/application/views.py", line 22, in application
new_app = f.save()
File "/Users/chaz/dev/envs/startupinstitute.com/lib/python2.7/site-packages/django/forms/models.py", line 364, in save
fail_message, commit, construct=False)
File "/Users/chaz/dev/envs/startupinstitute.com/lib/python2.7/site-packages/django/forms/models.py", line 86, in save_instance
instance.save()
File "/Users/chaz/dev/envs/startupinstitute.com/lib/python2.7/site-packages/django/db/models/base.py", line 463, in save
self.save_base(using=using, force_insert=force_insert, force_update=force_update)
File "/Users/chaz/dev/envs/startupinstitute.com/lib/python2.7/site-packages/django/db/models/base.py", line 551, in save_base
result = manager._insert([self], fields=fields, return_id=update_pk, using=using, raw=raw)
File "/Users/chaz/dev/envs/startupinstitute.com/lib/python2.7/site-packages/django/db/models/manager.py", line 203, in _insert
return insert_query(self.model, objs, fields, **kwargs)
File "/Users/chaz/dev/envs/startupinstitute.com/lib/python2.7/site-packages/django/db/models/query.py", line 1593, in insert_query
return query.get_compiler(using=using).execute_sql(return_id)
File "/Users/chaz/dev/envs/startupinstitute.com/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 912, in execute_sql
cursor.execute(sql, params)
File "/Users/chaz/dev/envs/startupinstitute.com/lib/python2.7/site-packages/debug_toolbar/utils/tracking/db.py", line 153, in execute
'iso_level': conn.isolation_level,
InternalError: current transaction is aborted, commands ignored until end of transaction block
[20/Apr/2013 08:22:06] "POST / HTTP/1.1" 500 413131
DEBUG (0.002) SELECT "django_site"."id", "django_site"."domain", "django_site"."name" FROM "django_site" WHERE "django_site"."id" = 12 ; args=(12,)
WARNING Not Found: /favicon.ico
views.py:
def application(request):
if request.method == 'POST':
f = forms.AppForm(request.POST)
selected_track = request.POST['applied_track']
if f.is_valid():
new_app = f.save()
new_app.save()
The problem was that my database wasn't migrated properly. Apparently this error can come from (at least) both a) the previous transaction, or b) the database not being synced properly by south.