-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement server management logic and add tests for ProduceServers fu…
…nction
- Loading branch information
Showing
2 changed files
with
548 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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,144 @@ | ||
package nginx | ||
|
||
import ( | ||
"sort" | ||
"strings" | ||
|
||
nginxv1alpha1 "github.com/tsuru/nginx-operator/api/v1alpha1" | ||
"github.com/tsuru/rpaas-operator/api/v1alpha1" | ||
) | ||
|
||
type Server struct { | ||
Name string `json:"names"` | ||
TLS bool `json:"tls"` | ||
TLSSecretName string `json:"secretName"` | ||
Default bool `json:"default,omitempty"` | ||
Wildcard bool `json:"wildcard,omitempty"` | ||
|
||
Locations []v1alpha1.Location `json:"locations,omitempty"` | ||
} | ||
|
||
func ProduceServers(spec *v1alpha1.RpaasInstanceSpec) []*Server { | ||
defaultServer := &Server{ | ||
Default: true, | ||
} | ||
|
||
mapServerNames := make(map[string]*Server) | ||
defaultLocationsIndex := map[string]int{} | ||
|
||
for _, server := range spec.Locations { | ||
if server.ServerName != "" { | ||
mapServerNames[server.ServerName] = &Server{ | ||
Name: server.ServerName, | ||
} | ||
} | ||
} | ||
|
||
for _, location := range spec.Locations { | ||
if location.ServerName != "" { | ||
continue | ||
} | ||
defaultServer.Locations = append(defaultServer.Locations, *location.DeepCopy()) | ||
defaultLocationsIndex[location.Path] = len(defaultLocationsIndex) | ||
|
||
for _, server := range mapServerNames { | ||
server.Locations = append(server.Locations, location) | ||
} | ||
|
||
} | ||
|
||
for _, location := range spec.Locations { | ||
if location.ServerName == "" { | ||
continue | ||
} | ||
|
||
if index, ok := defaultLocationsIndex[location.Path]; ok { | ||
mapServerNames[location.ServerName].Locations[index] = *location.DeepCopy() | ||
} else { | ||
mapServerNames[location.ServerName].Locations = append(mapServerNames[location.ServerName].Locations, *location.DeepCopy()) | ||
} | ||
|
||
} | ||
|
||
wildCardTLS := map[string]nginxv1alpha1.NginxTLS{} | ||
|
||
for _, tls := range spec.TLS { | ||
for _, host := range tls.Hosts { | ||
if isWildCard(host) { | ||
wildCardTLS[host] = tls | ||
} | ||
} | ||
} | ||
|
||
var appendTLSServer = func(host string, tlsSecretName string) { | ||
if _, ok := mapServerNames[host]; !ok { | ||
mapServerNames[host] = &Server{ | ||
Name: host, | ||
Locations: deepCopyLocations(defaultServer.Locations), | ||
} | ||
} | ||
|
||
server := mapServerNames[host] | ||
server.TLS = true | ||
server.TLSSecretName = tlsSecretName | ||
server.Wildcard = isWildCard(host) | ||
|
||
} | ||
|
||
for _, tls := range spec.TLS { | ||
for _, host := range tls.Hosts { | ||
appendTLSServer(host, tls.SecretName) | ||
} | ||
} | ||
|
||
for host, server := range mapServerNames { | ||
if server.TLS { | ||
continue | ||
} | ||
|
||
// find possible wildcard | ||
possibleWildcard := "*." + strings.SplitN(host, ".", 2)[1] | ||
if tls, ok := wildCardTLS[possibleWildcard]; ok { | ||
appendTLSServer(host, tls.SecretName) | ||
} | ||
} | ||
|
||
exactServers := []*Server{} | ||
wildcardServers := []*Server{} | ||
|
||
for _, server := range mapServerNames { | ||
if server.Wildcard { | ||
wildcardServers = append(wildcardServers, server) | ||
} else { | ||
exactServers = append(exactServers, server) | ||
} | ||
} | ||
|
||
sortServers(exactServers) | ||
sortServers(wildcardServers) | ||
|
||
result := []*Server{defaultServer} | ||
result = append(result, exactServers...) | ||
result = append(result, wildcardServers...) | ||
|
||
return result | ||
} | ||
|
||
func sortServers(servers []*Server) { | ||
sort.Slice(servers, func(i, j int) bool { | ||
return servers[i].Name < servers[j].Name | ||
}) | ||
} | ||
|
||
func deepCopyLocations(locations []v1alpha1.Location) []v1alpha1.Location { | ||
if len(locations) == 0 { | ||
return nil | ||
} | ||
copiedLocations := make([]v1alpha1.Location, len(locations)) | ||
copy(copiedLocations, locations) | ||
return copiedLocations | ||
} | ||
|
||
func isWildCard(host string) bool { | ||
return strings.HasPrefix(host, "*.") | ||
} |
Oops, something went wrong.