Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

k8s - allow resource definition using generateName #238

Merged
merged 2 commits into from
Sep 30, 2021

Conversation

abikouo
Copy link
Contributor

@abikouo abikouo commented Sep 17, 2021

SUMMARY

#35

ISSUE TYPE
  • Feature Pull Request
COMPONENT NAME

k8s

ADDITIONAL INFORMATION
- name: create pod using generateName
  k8s:
    namespace: test
    generate_name: pod-
    definition:
       kind: Pod
       spec:
          containers:
          - name: py
            image: python:3.7-alpine

- name: create pod using generateName
  k8s:
    namespace: test
    definition:
       kind: Pod
       metadata:
          generateName: pod-
       spec:
          containers:
          - name: py
            image: python:3.7-alpine

@abikouo abikouo requested review from gravesm and Akasurde September 17, 2021 15:35
Copy link
Member

@gravesm gravesm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are a few problems that will need to be sorted out. In general, we'll need to go through very carefully and make sure that in every instance that we are accessing, or trying to access, the name that we are accounting for the changes here. Some examples of things that are not working, all for different reasons:

k8s:
  kind: Namespace
  generate_name: ns-
k8s:
  kind: ConfigMap
  namespace: whatever
  generate_name: cmap-
  append_hash: yes
k8s:
  definition:
    apiVersion: v1
    kind: Pod
    metadata:
      namespace: whatever
      name: pod1
    spec:
      containers:
        - image: adslfkjadslfkjadslkfjsadf
          name: non-existent-container

k8s:
  definition:
    apiVersion: v1
    kind: Pod
    metadata:
      namespace: whatever
    spec:
      containers:
        - image: busybox
          name: c1
  generate_name: pod-
  wait: yes

In the latter case, because there is no name, we end up waiting on every pod in that namespace. This wait will fail because the first pod has a non-existent image name.

@abikouo
Copy link
Contributor Author

abikouo commented Sep 24, 2021

There are a few problems that will need to be sorted out. In general, we'll need to go through very carefully and make sure that in every instance that we are accessing, or trying to access, the name that we are accounting for the changes here. Some examples of things that are not working, all for different reasons:

k8s:
  kind: Namespace
  generate_name: ns-
k8s:
  kind: ConfigMap
  namespace: whatever
  generate_name: cmap-
  append_hash: yes
k8s:
  definition:
    apiVersion: v1
    kind: Pod
    metadata:
      namespace: whatever
      name: pod1
    spec:
      containers:
        - image: adslfkjadslfkjadslkfjsadf
          name: non-existent-container

k8s:
  definition:
    apiVersion: v1
    kind: Pod
    metadata:
      namespace: whatever
    spec:
      containers:
        - image: busybox
          name: c1
  generate_name: pod-
  wait: yes

In the latter case, because there is no name, we end up waiting on every pod in that namespace. This wait will fail because the first pod has a non-existent image name.

ok for the latter case, I see the issue with the wait mechanisms, I need to check for other scenarios
Thanks @gravesm

@abikouo abikouo force-pushed the k8s_genName branch 2 times, most recently from 1733e32 to 2ffe813 Compare September 24, 2021 16:22
@abikouo
Copy link
Contributor Author

abikouo commented Sep 24, 2021

@gravesm regarding the latter case you just mentioned in your comment above, it is not failing the resource name is updated before calling the wait method,

definition['metadata'].update({'name': k8s_obj['metadata']['name']})
success, result['result'], result['duration'] = self.wait(resource, definition, wait_sleep, wait_timeout, condition=wait_condition)

I have added test case to validate that
- name: Create Pod with failing container
kubernetes.core.k8s:
namespace: '{{ namespace }}'
definition:
apiVersion: v1
kind: Pod
metadata:
name: pod1
spec:
containers:
- image: adslfkjadslfkjadslkfjsadf
name: non-existent-container-image
- name: Create second Pod using wait (it should not wait for the first container)
kubernetes.core.k8s:
namespace: '{{ namespace }}'
definition:
apiVersion: v1
kind: Pod
metadata:
name: pod2
spec:
containers:
- args:
- /bin/sh
- -c
- while true; do echo $(date); sleep 10; done
image: python:3.7-alpine
imagePullPolicy: Always
name: c0
wait: yes
wait_timeout: 10

For others scenarios, code has been fixed and test updated accordingly

@abikouo abikouo requested a review from gravesm September 24, 2021 16:26
@abikouo abikouo requested a review from gravesm September 27, 2021 12:20
@abikouo
Copy link
Contributor Author

abikouo commented Sep 28, 2021

@Akasurde could you please take a look at this one?
Thanks

Copy link

@ansible-zuul ansible-zuul bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@ansible-zuul ansible-zuul bot removed the mergeit label Sep 30, 2021
@ansible-zuul ansible-zuul bot merged commit c655123 into ansible-collections:main Sep 30, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants