-
Notifications
You must be signed in to change notification settings - Fork 303
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
Add annotation for exposing NEGs #284
Conversation
/assign freehan |
/ok-to-test |
pkg/neg/controller.go
Outdated
glog.V(2).Infof("Applying annotation to service: %s", annotation) | ||
|
||
service.Annotations[negVisibilityAnnotationKey] = annotation | ||
c.serviceLister.Update(service) |
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.
do not omit the error
pkg/neg/controller.go
Outdated
res.NetworkEndpointGroups[port] = c.namer.NEG(namespace, name, port) | ||
} | ||
|
||
return fmt.Sprintf("%+v", res), nil |
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.
Use a struct to represent the annotation schema. And then use json.Marshal to convert it into json.
pkg/annotations/service.go
Outdated
// To enable this feature, the value of the annotation must be "true". | ||
// This annotation should be specified on services that are backing ingresses. | ||
// WARNING: The feature will NOT be effective in the following circumstances: | ||
// 1. NEG feature is not enabled in feature gate. | ||
// 2. Service is not referenced in any ingress. | ||
// 3. Adding this annotation on ingress. | ||
NetworkEndpointGroupAlphaAnnotation = "alpha.cloud.google.com/load-balancer-neg" |
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.
let us keep the boolean trigger for now.
Add a separate annotation for expose neg
4a83279
to
80b03d8
Compare
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.
need to remove visibility annotation or make it empty when trigger annotations are removed.
pkg/annotations/service.go
Outdated
// ExposeNegAnnotation is the format of the annotation associated with the | ||
// cloud.google.com/use-neg key. | ||
type ExposeNegAnnotation struct { | ||
SvcPorts map[string]NegAttributes |
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.
You can add json tags: https://eager.io/blog/go-and-json/
pkg/annotations/service.go
Outdated
// NEGAnnotation is the annotation key to specify standalone NEGs associated | ||
// with the service. This should be a valid JSON string, for example: | ||
// {"SvcPorts":{"80":{"Enabled":true}}} | ||
NEGAnnotation = "cloud.google.com/use-neg" |
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.
expose-load-balancer-neg
pkg/annotations/service.go
Outdated
func (svc Service) NEGEnabled() bool { | ||
// NEGIngress returns true if the annotation is to be applied on | ||
// Ingress-referenced ports | ||
func (svc Service) NEGIngress() bool { | ||
v, ok := svc.v[NetworkEndpointGroupAlphaAnnotation] |
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.
change to something like:
NEGEnabledForIngress
pkg/annotations/service.go
Outdated
v, ok := svc.v[NetworkEndpointGroupAlphaAnnotation] | ||
return ok && v == "true" | ||
} | ||
|
||
// NEGEnabled is true if the service uses NEGs. | ||
func (svc Service) NEGEnabled() bool { |
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.
NEGExposed
pkg/annotations/service.go
Outdated
|
||
var portMap ExposeNegAnnotation | ||
if err := json.Unmarshal([]byte(v), &portMap); err != nil { | ||
return nil, fmt.Errorf("NEG annotation %s is not well-formed", v) |
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.
Add a TODO or something to point to the documentation.
pkg/neg/controller.go
Outdated
@@ -265,6 +294,30 @@ func (c *Controller) synced() bool { | |||
c.ingressSynced() | |||
} | |||
|
|||
type negSvcState struct { |
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.
Same here: use json tag
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.
Use json tag here.
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.
negServiceState
@@ -40,6 +41,11 @@ const ( | |||
// 3. Adding this annotation on ingress. | |||
NetworkEndpointGroupAlphaAnnotation = "alpha.cloud.google.com/load-balancer-neg" |
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.
cloud.google.com/use-load-balancer-neg
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.
this was the original annotation for NEG + Ingress - is it ok to change it?
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 is okay
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.
we should change to beta?
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.
note that if we put beta, it will stay forever right now (new deprecation rules)
/assign |
70f9758
to
6a2cd8d
Compare
pkg/annotations/service.go
Outdated
|
||
// NEGAnnotation is the annotation key to specify standalone NEGs associated | ||
// with the service. This should be a valid JSON string, for example: | ||
// {"service_ports":{"80":{"enabled":true}}} |
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.
Let us do this:
{"service_ports":{"80":{}}}
pkg/annotations/service.go
Outdated
@@ -57,6 +63,18 @@ const ( | |||
ProtocolHTTP2 AppProtocol = "HTTP2" | |||
) | |||
|
|||
// ExposeNegAnnotation is the format of the annotation associated with the | |||
// NEGAnnotation key. | |||
type ExposeNegAnnotation struct { |
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.
Make it empty for now. Leave a comment to say there will be future extensions in this struct
f57641d
to
9dfff4c
Compare
pkg/neg/controller.go
Outdated
if err == nil { | ||
svcPorts = svcPorts.Union(negSvcPorts) | ||
} else { | ||
glog.Warning("Failed to parse %v annotation on Service, err: %v. Ignoring the annotation.", annotations.NEGAnnotation, err) |
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.
use event recorder to record and error event to the service object
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.
Also check if the service port actually exists. If not, record an event.
Add a TODO to say that will move the validation logic to validation webhook.
pkg/neg/controller.go
Outdated
@@ -265,6 +294,30 @@ func (c *Controller) synced() bool { | |||
c.ingressSynced() | |||
} | |||
|
|||
type negSvcState struct { |
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.
Use json tag here.
pkg/annotations/service.go
Outdated
// ExposeNegAnnotation is the format of the annotation associated with the | ||
// NEGAnnotation key. ServicePorts present in this map will be NEG-enabled. | ||
type ExposeNegAnnotation struct { | ||
SvcPorts map[string]NegAttributes `json:"service_ports,omitempty"` |
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 get rid of the service_ports
key?
"cloud.google.com/expose-load-balancer-neg": "{"80":{}, "8080": {}}"
27998a4
to
a4aa68a
Compare
pkg/annotations/service.go
Outdated
NetworkEndpointGroupAlphaAnnotation = "cloud.google.com/use-load-balancer-neg" | ||
|
||
// NEGAnnotation is the annotation key to specify standalone NEGs associated | ||
// with the service. This should be a valid JSON string, for example: |
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 you put where the actual schema struct is defined?
pkg/annotations/service.go
Outdated
func (svc Service) NEGEnabled() bool { | ||
// NEGEnabledForIngress returns true if the annotation is to be applied on | ||
// Ingress-referenced ports | ||
func (svc Service) NEGEnabledForIngress() bool { |
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.
is using this as a value type on purpose? (svc *Service)
pkg/annotations/service.go
Outdated
// NEGExposed is true if the service exposes NEGs | ||
func (svc Service) NEGExposed() bool { | ||
v, ok := svc.v[NEGAnnotation] | ||
return ok && len(v) > 0 |
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.
wouldn't len(v) be == 0 if it didn't exist?
pkg/annotations/service.go
Outdated
return nil, fmt.Errorf("No NEG ServicePorts specified") | ||
} | ||
|
||
// TODO: add link to documentation |
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 doc?
pkg/annotations/service.go
Outdated
// TODO: add link to documentation | ||
var portMap ExposeNegAnnotation | ||
if err := json.Unmarshal([]byte(v), &portMap); err != nil { | ||
return nil, fmt.Errorf("NEG annotation %s is not well-formed", v) |
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.
...well-formed: %v", v, err
might as well report the error so people know how to fix
pkg/neg/controller.go
Outdated
if annotations.FromService(service).NEGExposed() { | ||
negSvcPorts, err := annotations.FromService(service).NEGServicePorts() | ||
if err == nil { | ||
knownPorts := sets.NewString() |
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.
This feels like it should be a helper function.
pkg/neg/controller.go
Outdated
negSvcPorts, err := annotations.FromService(service).NEGServicePorts() | ||
if err == nil { | ||
knownPorts := sets.NewString() | ||
for _, sp := range service.Spec.Ports { |
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.
This block that collects the service ports should be a helper function and unit tested
pkg/neg/controller.go
Outdated
|
||
if len(svcPorts) > 0 { | ||
annotation, err := c.negVisibilityAnnotation(namespace, name, svcPorts.List()) | ||
service.Annotations[negVisibilityAnnotationKey] = annotation |
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.
I would like this to be part of the annotations pkg to keep annotation related code together
pkg/neg/controller.go
Outdated
@@ -265,6 +294,30 @@ func (c *Controller) synced() bool { | |||
c.ingressSynced() | |||
} | |||
|
|||
type negSvcState struct { |
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.
negServiceState
pkg/neg/controller.go
Outdated
// associated with the given ports. | ||
// NetworkEndpointGroups is a mapping between ServicePort and NEG name | ||
// Zones is a list of zones where the NEGs exist. | ||
func (c *Controller) negVisibilityAnnotation(namespace, name string, ports []string) (string, error) { |
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.
move the logic to annotations
73252dc
to
85f09ef
Compare
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.
rename the pkg/neg/annotation.go to pkg/neg/utils.go
NEG: true, | ||
Http2: true, | ||
NEG: true, | ||
NEGExposed: true, |
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.
default to false
?
pkg/annotations/service.go
Outdated
|
||
// NEGStatusKey is the annotation key whose value is the status of the NEGs | ||
// on the Service, and is applied by the NEG Controller. | ||
NEGStatusKey = "cloud.google.com/neg" |
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.
cloud.google.com/neg-status
pkg/annotations/service.go
Outdated
// with the service. This should be a valid JSON string, as defined in | ||
// ExposeNegAnnotation. | ||
// example: {"80":{},"443":{}} | ||
ExposeNEGAnnotationKey = "cloud.google.com/expose-load-balancer-neg" |
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.
cloud.google.com/neg
pkg/annotations/service.go
Outdated
|
||
// NegAttributes houses the attributes of the NEGs that are associated with the | ||
// service. Future extensions to the Expose NEGs annotation should be added here. | ||
type NegAttributes struct{} |
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.
Name string
pkg/neg/annotations.go
Outdated
|
||
// NegServiceState contains name and zone of the Network Endpoint Group | ||
// resources associated with this service | ||
type NegServiceState struct { |
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.
rename to NegStatus
pkg/neg/annotations.go
Outdated
// associated with the given ports. | ||
// NetworkEndpointGroups is a mapping between ServicePort and NEG name | ||
// Zones is a list of zones where the NEGs exist. | ||
func GenNegServiceState(zones []string, portToNegs PortNameMap) NegServiceState { |
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.
GetNegStatus
testCases := []struct { | ||
desc string | ||
previousPortMap PortNameMap | ||
portMap PortNameMap |
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.
add a boolean speicify expect service update or not
let me find an example for you.
0a7c879
to
4bb2b9c
Compare
/lgtm |
|
/lgtm |
Automatic merge from submit-queue (batch tested with PRs 65338, 64535). If you want to cherry-pick this change to another branch, please follow the instructions <a href="https://github.com/kubernetes/community/blob/master/contributors/devel/cherry-picks.md">here</a>. [GCE] e2e test for expose neg on gce ingress **What this PR does / why we need it**: - Adds e2e test for the expose NEG annotation (which allows for standalone NEGs) **Special notes for your reviewer**: Note, kubernetes/ingress-gce#350 must be merged first before this is merged. `[Unreleased]` tag is on this PR because it depends on code from kubernetes/ingress-gce#350 and kubernetes/ingress-gce#284 being in an Ingress release. Will update this test and test-infra once this is released in the next Ingress. **Release note**: ```release-note NONE ```
This PR adds a new annotation for exposing standalone NEGs on Services without requiring an Ingress.
Additionally:
/pkg/neg
(PortNameMap type)To do in later PRs: