-
Notifications
You must be signed in to change notification settings - Fork 150
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
Support explicit target protocol annotation for load balancers #751
base: master
Are you sure you want to change the base?
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 |
---|---|---|
|
@@ -22,6 +22,7 @@ import ( | |
"fmt" | ||
"net/http" | ||
"reflect" | ||
"slices" | ||
"sort" | ||
"strconv" | ||
"strings" | ||
|
@@ -80,6 +81,9 @@ const ( | |
|
||
var ( | ||
errLBNotFound = errors.New("loadbalancer not found") | ||
|
||
supportedEntryProtocols = []string{protocolTCP, protocolHTTP, protocolHTTPS, protocolHTTP2, protocolHTTP3} | ||
supportedTargetProtocols = []string{protocolHTTP, protocolHTTPS, protocolHTTP2, protocolHTTP3} | ||
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. To confirm, are TCP->TCP and UDP->UDP not supported? |
||
) | ||
|
||
func buildK8sTag(val string) string { | ||
|
@@ -733,13 +737,24 @@ func buildHTTP3ForwardingRule(ctx context.Context, service *v1.Service, godoClie | |
return nil, errors.New("certificate ID is required for HTTP3") | ||
} | ||
|
||
var targetProtocol string | ||
if targetProtocol, err = getTargetProtocol(service); err != nil { | ||
return nil, err | ||
} | ||
if targetProtocol == protocolTCP || targetProtocol == protocolUDP { | ||
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. Do we need this explicit check still given that Similar question for other occurrences of this check. |
||
return nil, errors.New("target protocol annotation is not supported for TCP or UDP") | ||
} | ||
if targetProtocol == "" { | ||
targetProtocol = protocolHTTP | ||
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 looks like we don't need this anymore either given that |
||
} | ||
|
||
for _, port := range service.Spec.Ports { | ||
if port.Port == int32(http3Port) { | ||
return &godo.ForwardingRule{ | ||
EntryProtocol: protocolHTTP3, | ||
EntryPort: http3Port, | ||
CertificateID: certificateID, | ||
TargetProtocol: protocolHTTP, | ||
TargetProtocol: targetProtocol, | ||
TargetPort: int(port.NodePort), | ||
}, nil | ||
} | ||
|
@@ -753,7 +768,7 @@ func buildHTTP3ForwardingRule(ctx context.Context, service *v1.Service, godoClie | |
func buildForwardingRules(ctx context.Context, service *v1.Service, godoClient *godo.Client) ([]godo.ForwardingRule, error) { | ||
var forwardingRules []godo.ForwardingRule | ||
|
||
defaultProtocol, err := getProtocol(service) | ||
defaultProtocol, err := getEntryProtocol(service) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
@@ -928,6 +943,17 @@ func buildTLSForwardingRule(forwardingRule *godo.ForwardingRule, service *v1.Ser | |
return errors.New("either certificate id should be set or tls pass through enabled, not both") | ||
} | ||
|
||
var ( | ||
targetProtocol string | ||
err error | ||
) | ||
if targetProtocol, err = getTargetProtocol(service); err != nil { | ||
return err | ||
} | ||
if targetProtocol == protocolTCP || targetProtocol == protocolUDP { | ||
return errors.New("target protocol annotation is not supported for TCP or UDP") | ||
} | ||
|
||
if tlsPassThrough { | ||
forwardingRule.TlsPassthrough = tlsPassThrough | ||
// We don't explicitly set the TargetProtocol here since in buildForwardingRule | ||
|
@@ -936,7 +962,11 @@ func buildTLSForwardingRule(forwardingRule *godo.ForwardingRule, service *v1.Ser | |
// to match the EntryProtocol. | ||
} else { | ||
forwardingRule.CertificateID = certificateID | ||
forwardingRule.TargetProtocol = protocolHTTP | ||
if targetProtocol != "" { | ||
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. Similarly, I don't think we need the if statement since |
||
forwardingRule.TargetProtocol = targetProtocol | ||
} else { | ||
forwardingRule.TargetProtocol = protocolHTTP | ||
} | ||
} | ||
|
||
return nil | ||
|
@@ -968,17 +998,40 @@ func buildStickySessions(service *v1.Service) (*godo.StickySessions, error) { | |
}, nil | ||
} | ||
|
||
// getProtocol returns the desired protocol of service. | ||
func getProtocol(service *v1.Service) (string, error) { | ||
protocol, ok := service.Annotations[annDOProtocol] | ||
func getEntryProtocol(service *v1.Service) (string, error) { | ||
protocol, err := getProtocol(service, annDOProtocol, supportedEntryProtocols) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
if protocol == "" { | ||
protocol = protocolTCP | ||
} | ||
|
||
return protocol, nil | ||
} | ||
|
||
func getTargetProtocol(service *v1.Service) (string, error) { | ||
protocol, err := getProtocol(service, annDOTargetProtocol, supportedTargetProtocols) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
if protocol == "" { | ||
protocol = protocolHTTP | ||
} | ||
|
||
return protocol, nil | ||
} | ||
|
||
func getProtocol(service *v1.Service, annotation string, supportedProtocols []string) (string, error) { | ||
protocol, ok := service.Annotations[annotation] | ||
if !ok { | ||
return protocolTCP, nil | ||
return "", nil | ||
} | ||
|
||
switch protocol { | ||
case protocolTCP, protocolHTTP, protocolHTTPS, protocolHTTP2, protocolHTTP3: | ||
default: | ||
return "", fmt.Errorf("invalid protocol %q specified in annotation %q", protocol, annDOProtocol) | ||
if !slices.Contains(supportedProtocols, protocol) { | ||
return "", fmt.Errorf("invalid protocol %q specified in annotation %q", protocol, annotation) | ||
} | ||
|
||
return protocol, 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.
Is this just an extra blank now? Was it accidental?
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.
Nit whitespace adjustment really, I just noticed that the last version update didn't have a tab prepended to new lines stanza as other updates. I can revert this.
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.
No this is great. Thanks for cleaning up.