Skip to content

Conversation

cronik
Copy link
Contributor

@cronik cronik commented May 21, 2025

Add PodContainerSource extension point to lookup container working directory. This allows plugins to use ContainerExecDecorator with alternate container sources, like ephemeral containers.

Testing done

Submitter checklist

  • Make sure you are opening from a topic/feature/bugfix branch (right side) and not your main branch!
  • Ensure that the pull request title represents the desired changelog entry
  • Please describe what you did
  • Link to relevant issues in GitHub or Jira
  • Link to relevant pull requests, esp. upstream and downstream changes
  • Ensure you have provided tests that demonstrate the feature works or the issue is fixed

@cronik cronik requested a review from a team as a code owner May 21, 2025 03:12
Copy link
Member

@Vlatombe Vlatombe left a comment

Choose a reason for hiding this comment

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

Some questions, looks good in general

* this implementation.
* @see PodSpec#getContainers()
*/
@Extension(ordinal = Double.MAX_VALUE) // this extension goes always first
Copy link
Member

Choose a reason for hiding this comment

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

Is it really needed? I think it's easier to leave the default and let implementations pick whether they should appear before or later.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Since containers are unique I suppose it doesn't really matter which goes first. Maybe a minor optimization 1 way or the other. I'll remove it.

* @param containerName name of container to lookup
* @return working directory path if container found and working dir specified, otherwise empty
*/
public abstract Optional<String> getContainerWorkingDir(@NonNull Pod pod, @NonNull String containerName);
Copy link
Member

Choose a reason for hiding this comment

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

Wondering whether we have any guarantee that a given name can't be used by both containers and ephemeral containers, as it would create an ambiguity...

Copy link
Member

Choose a reason for hiding this comment

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

Also, would public abstract Optional<Container> getContainerWorkingDir(Pod, String) be more flexible?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Wondering whether we have any guarantee that a given name can't be used by both containers and ephemeral containers, as it would create an ambiguity...

https://kubernetes.io/docs/reference/kubernetes-api/workload-resources/pod-v1/#Container

Name of the container specified as a DNS_LABEL. Each container in a pod must have a unique name (DNS_LABEL). Cannot be updated.

This is confirmed if you attempt to apply a spec with duplicate names. It doesn't matter if they are defined in containers, initContainers, or ephemeralContainers.

❯ kubectl apply -f deployment-with-dup-container-names.yaml
The Deployment "foo" is invalid: spec.template.spec.initContainers[0].name: Duplicate value: "a"

Also, would public abstract Optional<Container> getContainerWorkingDir(Pod, String) be more flexible?

Yes, I will refactor to return the Container instance.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Also, would public abstract Optional<Container> getContainerWorkingDir(Pod, String) be more flexible?

I take back what I said 😄 . While it makes sense at first, fabric8 models Container and EphemeralContainer as different types. From the previous PR it sounded like the whole purpose of this extension is to isolate ephemeral container references to extension plugins, so this extension is to abstract over those differences.

In a future PR I will be adding 1 more method to PodContainerSource to get the container status. Statuses are not properties of Container or EphemeralContainer, they are accesses via pod.getStatus().getContainerStatuses().

public abstract Optional<ContainerStatus> getContainerStatus(@NonNull Pod pod, @NonNull String containerName);

These 2 methods will allow my extension to reuse ContainerExecDecorator, as shown in this PR, and display ephemeral container logs (future PR that updates KubernetesComputer#goContainerLog).

ContainerExecDecorator does A LOT of complicated stuff which would be a shame to replicate and maintain separately given this small abstraction makes it reusable. Hopefully we can figure out an acceptable solution.

Copy link
Member

Choose a reason for hiding this comment

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

fabric8 models Container and EphemeralContainer as different types

😞

Indeed, I don't see anything better.

@cronik cronik force-pushed the feature/ephemeral-containers-working-dir branch from 90b029c to f9e5ac9 Compare May 22, 2025 23:26
Copy link
Member

@Vlatombe Vlatombe left a comment

Choose a reason for hiding this comment

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

Some small fixups, but LGTM otherwise.


@Override
public Optional<String> getContainerWorkingDir(@NotNull Pod pod, @NotNull String containerName) {
return pod.getSpec().getEphemeralContainers().stream()
Copy link
Member

Choose a reason for hiding this comment

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

In a real pod, pod.getSpec().getEphemeralContainers() could be null (just mentioning for your production extension)

Add PodContainerSource extension point to lookup container working directory. This allows plugins to use ContainerExecDecorator with alternate
container sources, like ephemeral containers.
@cronik cronik force-pushed the feature/ephemeral-containers-working-dir branch from f9e5ac9 to 17c5a69 Compare May 24, 2025 13:56
@Vlatombe Vlatombe changed the title Add PodContainerSource Extension Point Add PodContainerSource Extension Point May 26, 2025
@Vlatombe Vlatombe merged commit e05368a into jenkinsci:master May 26, 2025
9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants