-
Notifications
You must be signed in to change notification settings - Fork 325
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
[NET-7534] v2: Make port names in consul-k8s compatible with NET-5586 #3528
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -73,6 +73,20 @@ func PortValue(pod corev1.Pod, value string) (int32, error) { | |
return int32(raw), err | ||
} | ||
|
||
// WorkloadPortName returns the container port's name if it has one, and if not, constructs a name from the port number | ||
// and adds a constant prefix. The port name must be 1-15 characters and must have at least 1 alpha character. | ||
func WorkloadPortName(port *corev1.ContainerPort) string { | ||
name := port.Name | ||
var isNum bool | ||
if _, err := strconv.Atoi(name); err == nil { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. codegolf, nonblocking, unnecessary optimization: You don't have to try to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah right, makes sense, I may skip for now because just trying to get the tests green to merge before code freeze 🤞 |
||
isNum = true | ||
} | ||
if name == "" || isNum { | ||
name = "cslport-" + strconv.Itoa(int(port.ContainerPort)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should we use the constant There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yup updated thanks |
||
} | ||
return name | ||
} | ||
|
||
// TransparentProxyEnabled returns true if transparent proxy should be enabled for this pod. | ||
// It returns an error when the annotation value cannot be parsed by strconv.ParseBool or if we are unable | ||
// to read the pod's namespace label when it exists. | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,6 +10,7 @@ import ( | |
"fmt" | ||
"net/http" | ||
"strconv" | ||
"strings" | ||
|
||
mapset "github.com/deckarep/golang-set" | ||
"github.com/go-logr/logr" | ||
|
@@ -251,6 +252,16 @@ func (w *MeshWebhook) Handle(ctx context.Context, req admission.Request) admissi | |
|
||
w.Log.Info("received pod", "name", req.Name, "ns", req.Namespace) | ||
|
||
// Validate that none of the pod ports start with the prefix "cslport-" as that may result in conflicts with ports | ||
// created by the pod controller when creating workloads. | ||
for _, c := range pod.Spec.Containers { | ||
for _, p := range c.Ports { | ||
if strings.HasPrefix(p.Name, constants.UnnamedWorkloadPortNamePrefix) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. not necessarily for this PR but are we tracking anywhere restrictions like this that we should document for end users? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For this particular case I'd not expect users to run into this and would expect if they do that it's easily discoverable through the pod not being accepted for injection. It definitely feels like a thing that'd be glazed over in any docs. Any other restrictions (so far) are enforced through kube validations |
||
return admission.Errored(http.StatusInternalServerError, fmt.Errorf("error creating pod: port names cannot be prefixed with \"cslport-\" as that prefix is reserved")) | ||
} | ||
} | ||
} | ||
|
||
// Add our volume that will be shared by the init container and | ||
// the sidecar for passing data in the pod. | ||
pod.Spec.Volumes = append(pod.Spec.Volumes, w.containerVolume()) | ||
|
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.
can we add some unit tests for this function?
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.
ah yep i forgot to add unit tests on this side after refactoring