-
Notifications
You must be signed in to change notification settings - Fork 38.7k
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
Part 2 of plural ports: make endpoints a struct #4580
Changes from all 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 |
---|---|---|
|
@@ -742,12 +742,25 @@ type Service struct { | |
} | ||
|
||
// Endpoints is a collection of endpoints that implement the actual service, for example: | ||
// Name: "mysql", Endpoints: ["10.10.1.1:1909", "10.10.2.2:8834"] | ||
// Name: "mysql", Endpoints: [{"ip": "10.10.1.1", "port": 1909}, {"ip": "10.10.2.2", "port": 8834}] | ||
type Endpoints struct { | ||
TypeMeta `json:",inline"` | ||
ObjectMeta `json:"metadata,omitempty"` | ||
|
||
Endpoints []string `json:"endpoints,omitempty"` | ||
// Optional: The IP protocol for these endpoints. Supports "TCP" and | ||
// "UDP". Defaults to "TCP". | ||
Protocol Protocol `json:"protocol,omitempty"` | ||
Endpoints []Endpoint `json:"endpoints,omitempty"` | ||
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. nit: 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. It gets worse as this PR series evolves - EndpointsList.Items[x].Endpoints[y].Ports[z].IP I didn't choose the name, I just rolled with what was there. I'm happy to change it if you have a solid suggestion :) 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. HostPorts? 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. The real fix (IMO) would be to rename the struct to ServiceEndpoints or something, but ain't nobody got time to rename REST objects :) HostPorts is wrong because it implies something like Container[x].Ports[y].HostPort, which is something else. PodPorts is wrong because endpoints are supposed to be only loosely coupled to kubernetes, and may not even be pods. Targets? Destinations? I didn't find any of these to be significantly better than Endpoints, so I left it as it was. |
||
} | ||
|
||
// Endpoint is a single IP endpoint of a service. | ||
type Endpoint struct { | ||
// Required: The IP of this endpoint. | ||
// TODO: This should allow hostname or IP, see #4447. | ||
IP string `json:"ip"` | ||
|
||
// Required: The destination port to access. | ||
Port int `json:"port"` | ||
} | ||
|
||
// EndpointsList is a list of endpoints. | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,6 +18,7 @@ package v1beta1 | |
|
||
import ( | ||
"fmt" | ||
"net" | ||
"strconv" | ||
|
||
newer "github.com/GoogleCloudPlatform/kubernetes/pkg/api" | ||
|
@@ -1113,6 +1114,48 @@ func init() { | |
out.TimeoutSeconds = in.TimeoutSeconds | ||
return nil | ||
}, | ||
func(in *newer.Endpoints, out *Endpoints, s conversion.Scope) error { | ||
if err := s.Convert(&in.TypeMeta, &out.TypeMeta, 0); err != nil { | ||
return err | ||
} | ||
if err := s.Convert(&in.ObjectMeta, &out.TypeMeta, 0); err != nil { | ||
return err | ||
} | ||
if err := s.Convert(&in.Protocol, &out.Protocol, 0); err != nil { | ||
return err | ||
} | ||
for i := range in.Endpoints { | ||
ep := &in.Endpoints[i] | ||
out.Endpoints = append(out.Endpoints, net.JoinHostPort(ep.IP, strconv.Itoa(ep.Port))) | ||
} | ||
return nil | ||
}, | ||
func(in *Endpoints, out *newer.Endpoints, s conversion.Scope) error { | ||
if err := s.Convert(&in.TypeMeta, &out.TypeMeta, 0); err != nil { | ||
return err | ||
} | ||
if err := s.Convert(&in.TypeMeta, &out.ObjectMeta, 0); err != nil { | ||
return err | ||
} | ||
if err := s.Convert(&in.Protocol, &out.Protocol, 0); err != nil { | ||
return err | ||
} | ||
for i := range in.Endpoints { | ||
out.Endpoints = append(out.Endpoints, newer.Endpoint{}) | ||
ep := &out.Endpoints[i] | ||
host, port, err := net.SplitHostPort(in.Endpoints[i]) | ||
if err != nil { | ||
return err | ||
} | ||
ep.IP = host | ||
pn, err := strconv.Atoi(port) | ||
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. you can simplify this to: if ep.Port, err = strconv.Atoi(port); err != nil {
return err
} 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. Nope. In the relentless goal of a dumber, I mean SIMPLER grammar, Go has done away with the idea of an lvalue and just has a "name" token. ep.Port is not a name. There's a bug open against go to allow this syntax, but it's at least a year old and not fixed. |
||
if err != nil { | ||
return err | ||
} | ||
ep.Port = pn | ||
} | ||
return nil | ||
}, | ||
) | ||
if err != nil { | ||
// If one of the conversion functions is malformed, detect it immediately. | ||
|
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.
Protocol needs to go into the Endpoint struct. Example: DNS service listening on both UDP 53 and TCP 53.
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.
yes, it will migrate, but I wanted to make clean commits that each are plausibly correct on their own.