-
Notifications
You must be signed in to change notification settings - Fork 227
inject proxy env vars into requesting workload resources #224
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| package resourcebuilder | ||
|
|
||
| import ( | ||
| "fmt" | ||
|
|
||
| corev1 "k8s.io/api/core/v1" | ||
| ) | ||
|
|
||
| // updatePodSpecWithProxy mutates the input podspec with proxy env vars for all init containers and containers | ||
| // matching the container names. | ||
| func updatePodSpecWithProxy(podSpec *corev1.PodSpec, containerNames []string, httpProxy, httpsProxy, noProxy string) error { | ||
| hasProxy := len(httpsProxy) > 0 || len(httpProxy) > 0 || len(noProxy) > 0 | ||
|
abhinavdahiya marked this conversation as resolved.
|
||
| if !hasProxy { | ||
| return nil | ||
| } | ||
|
|
||
| for _, containerName := range containerNames { | ||
| found := false | ||
| for i := range podSpec.Containers { | ||
| if podSpec.Containers[i].Name != containerName { | ||
| continue | ||
| } | ||
| found = true | ||
|
|
||
| podSpec.Containers[i].Env = append(podSpec.Containers[i].Env, corev1.EnvVar{Name: "HTTP_PROXY", Value: httpProxy}) | ||
|
abhinavdahiya marked this conversation as resolved.
|
||
| podSpec.Containers[i].Env = append(podSpec.Containers[i].Env, corev1.EnvVar{Name: "HTTPS_PROXY", Value: httpsProxy}) | ||
| podSpec.Containers[i].Env = append(podSpec.Containers[i].Env, corev1.EnvVar{Name: "NO_PROXY", Value: noProxy}) | ||
| } | ||
| for i := range podSpec.InitContainers { | ||
| if podSpec.InitContainers[i].Name != containerName { | ||
| continue | ||
| } | ||
| found = true | ||
|
|
||
| podSpec.InitContainers[i].Env = append(podSpec.InitContainers[i].Env, corev1.EnvVar{Name: "HTTP_PROXY", Value: httpProxy}) | ||
| podSpec.InitContainers[i].Env = append(podSpec.InitContainers[i].Env, corev1.EnvVar{Name: "HTTPS_PROXY", Value: httpsProxy}) | ||
| podSpec.InitContainers[i].Env = append(podSpec.InitContainers[i].Env, corev1.EnvVar{Name: "NO_PROXY", Value: noProxy}) | ||
| } | ||
|
|
||
| if !found { | ||
| return fmt.Errorf("requested injection for non-existent container: %q", containerName) | ||
| } | ||
| } | ||
|
|
||
| return nil | ||
|
|
||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,112 @@ | ||
| package resourcebuilder | ||
|
|
||
| import ( | ||
| "reflect" | ||
| "testing" | ||
|
|
||
| "k8s.io/apimachinery/pkg/util/diff" | ||
|
|
||
| corev1 "k8s.io/api/core/v1" | ||
| ) | ||
|
|
||
| func TestUpdatePodSpecWithProxy(t *testing.T) { | ||
| tests := []struct { | ||
| name string | ||
|
|
||
| input *corev1.PodSpec | ||
| containerNames []string | ||
| httpProxy, httpsProxy, noProxy string | ||
|
|
||
| expectedErr string | ||
| expected *corev1.PodSpec | ||
| }{ | ||
| { | ||
| name: "no proxy info", | ||
| input: &corev1.PodSpec{ | ||
| Containers: []corev1.Container{ | ||
| { | ||
| Name: "foo", | ||
| }, | ||
| }, | ||
| }, | ||
| expected: &corev1.PodSpec{ | ||
| Containers: []corev1.Container{ | ||
| { | ||
| Name: "foo", | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| { | ||
| name: "proxy info", | ||
| containerNames: []string{"foo", "init-foo"}, | ||
| httpsProxy: "httpsProxy-val", | ||
| noProxy: "noProxy-val", | ||
| input: &corev1.PodSpec{ | ||
| InitContainers: []corev1.Container{ | ||
| { | ||
| Name: "init-foo", | ||
| }, | ||
| { | ||
| Name: "init-bar", | ||
| }, | ||
| }, | ||
| Containers: []corev1.Container{ | ||
| { | ||
| Name: "foo", | ||
| }, | ||
| { | ||
| Name: "bar", | ||
| }, | ||
| }, | ||
| }, | ||
| expected: &corev1.PodSpec{ | ||
| InitContainers: []corev1.Container{ | ||
| { | ||
| Name: "init-foo", | ||
| Env: []corev1.EnvVar{ | ||
| {Name: "HTTP_PROXY", Value: ""}, | ||
| {Name: "HTTPS_PROXY", Value: "httpsProxy-val"}, | ||
| {Name: "NO_PROXY", Value: "noProxy-val"}, | ||
| }, | ||
| }, | ||
| { | ||
| Name: "init-bar", | ||
| }, | ||
| }, | ||
| Containers: []corev1.Container{ | ||
| { | ||
| Name: "foo", | ||
| Env: []corev1.EnvVar{ | ||
| {Name: "HTTP_PROXY", Value: ""}, | ||
| {Name: "HTTPS_PROXY", Value: "httpsProxy-val"}, | ||
| {Name: "NO_PROXY", Value: "noProxy-val"}, | ||
| }, | ||
| }, | ||
| { | ||
| Name: "bar", | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| } | ||
|
|
||
| for _, test := range tests { | ||
| t.Run(test.name, func(t *testing.T) { | ||
| err := updatePodSpecWithProxy(test.input, test.containerNames, test.httpProxy, test.httpsProxy, test.noProxy) | ||
| switch { | ||
| case err == nil && len(test.expectedErr) == 0: | ||
| case err != nil && len(test.expectedErr) == 0: | ||
| t.Fatal(err) | ||
| case err == nil && len(test.expectedErr) != 0: | ||
| t.Fatal(err) | ||
| case err != nil && len(test.expectedErr) != 0 && err.Error() != test.expectedErr: | ||
| t.Fatal(err) | ||
| } | ||
|
|
||
| if !reflect.DeepEqual(test.input, test.expected) { | ||
| t.Error(diff.ObjectDiff(test.input, test.expected)) | ||
| } | ||
| }) | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not terrible.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Which operators would use this? The ones that need all of their traffic to go through the proxy, including service network (I assume status.noProxy must ALWAYS contain service network or you cause an outage?)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
auth operator for example....
the noProxy will be provided by network operator which should set these (I asked him same question during arch call ;-)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The image registry operator may use this. We already have similar logic inside of our reconciliation loop. The expectation is that noProxy covers the API server. And our operator needs to interact with external services like S3.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's correct. I plan to enforce the default noProxy values as part of openshift/cluster-network-operator#245
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@danehans the registry operator communicates to cloud apis even when the cluster is not running on that particular cloud.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@bparees the registry operator is then responsible for augmenting the cluster-wide
noProxywith additional no proxy values, correct? This is the WIP implementation of the cloud metadata ip.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@danehans I don't know if that is desirable, particularly if a customer wants their baremetal cluster to have all outbound traffic go through their proxy (and yet still use s3 or other cloud provider storage as their backend).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
? My point is that the registry operator has a need to use the proxy configuration when it is talking to the cloudapis that exist outside the cloudprovider the cluster is running on. NoProxy is irrelevant.
@dmage was observing that the registry operator has a use for this proxy env injection. You said they didn't need it because they are talking to cloud apis, but that is incorrect. They are talking to cloudapis, but not cloudapis from the cluster's cloud provider, therefore they will need to go through the proxy (assuming the customer's network topology requires it, which would be our assumption since it's external traffic like any other).
If the customer does not want their AWS api calls going through the proxy (and assuming their network topology doesn't require it), the admin would have to add the AWS api endpoints to their NoProxy value.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@adambkaplan the cloud metadata ip will not be set for vsphere, baremetal and non platform types.