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

fix #4355: adding logic to set/validate the container name #4505

Merged
merged 3 commits into from Nov 7, 2022

Conversation

shawkins
Copy link
Contributor

@shawkins shawkins commented Oct 18, 2022

Description

Fixes #4355 - direct or indirect (copy, upload) to exec / attach will now validate or set the container parameter in the same was as the kubectl client. This requires always fetching the pod prior to the exec / attach call and will always add the container argument.

We do not yet need to do this for log operations as we don't have a similar option to specify "don't get from all" without specifying a container name.

Type of change

  • Bug fix (non-breaking change which fixes an issue)
  • Feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change
  • Chore (non-breaking change which doesn't affect codebase;
    test, version modification, documentation, etc.)

Checklist

  • Code contributed by me aligns with current project license: Apache 2.0
  • I Added CHANGELOG entry regarding this change
  • I have implemented unit tests to cover my changes
  • I have added/updated the javadocs and other documentation accordingly
  • No new bugs, code smells, etc. in SonarCloud report
  • I tested my code in Kubernetes
  • I tested my code in OpenShift

@@ -315,10 +315,10 @@ private URL getURL(String operation, String[] commands) throws MalformedURLExcep
*/
String validateOrDefaultContainerId(String name) {
Pod pod = this.require();
List<Container> containers = pod.getSpec().getContainers();
if (containers.isEmpty()) {
if (pod.getSpec() == null || pod.getSpec().getContainers() == null || pod.getSpec().getContainers().isEmpty()) {
throw new KubernetesClientException("Pod has no containers!");
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@manusa see also

if you want to guard against invalid Pods/specs, all of that logic makes the same assumptions.

Copy link
Member

@manusa manusa Nov 7, 2022

Choose a reason for hiding this comment

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

I'm not sure that this is possible, I added this after implementing a test that failed (using the mockserver).
If it is possible to create a Pod without spec or containers (in real k8s -which I doubt), then we should definitely handle this

Copy link
Contributor Author

Choose a reason for hiding this comment

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

No, I don't believe it was possible. But I wasn't sure how pervasive you were trying to cover those cases.

Copy link
Member

@manusa manusa Nov 7, 2022

Choose a reason for hiding this comment

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

So my thoughts:

  • I don't think this resource can exist in a real cluster (Pod | PodTemplate) -I did a very quick test-
  • I added this here because in the MockServer this resource can exist.
  • I don't think it hurts having these guards here, although the most correct approach would be to create a valid resource in the test and remove these.
  • I think it's fine leaving this here, but I can remove if you think it's inconsistent or that this might be confusing.

Please let me know what do you prefer

Copy link
Contributor Author

Choose a reason for hiding this comment

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

How about a comment explaining that it's for making testing easier. There's less danger it will be unnecessarily copied later that way.

Copy link
Member

Choose a reason for hiding this comment

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

sounds good

Signed-off-by: Marc Nuri <marc@marcnuri.com>
Signed-off-by: Marc Nuri <marc@marcnuri.com>
@sonarcloud
Copy link

sonarcloud bot commented Nov 7, 2022

SonarCloud Quality Gate failed.    Quality Gate failed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 0 Code Smells

55.8% 55.8% Coverage
0.0% 0.0% Duplication

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

exec and other operations should support a default container
2 participants