limited commented on code in PR #7669: URL: https://github.com/apache/trafficcontrol/pull/7669#discussion_r1279468470
########## lib/varnishcfg/vcl.go: ########## @@ -0,0 +1,99 @@ +package varnishcfg + +import "fmt" + +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +const defaultVCLVersion = "4.1" + +// vclFile contains all VCL components +type vclFile struct { + version string + imports []string + acls map[string][]string + backends map[string]backend + subroutines map[string][]string +} + +func newVCLFile(version string) vclFile { + return vclFile{ + version: version, + imports: make([]string, 0), + acls: make(map[string][]string), + backends: make(map[string]backend), + subroutines: make(map[string][]string), + } +} + +func (v vclFile) String() string { + txt := fmt.Sprintf("vcl %s;\n", v.version) + for _, i := range v.imports { + txt += fmt.Sprintf("import %s;\n", i) + } + + for name, backend := range v.backends { + txt += fmt.Sprintf("backend %s {\n", name) + txt += fmt.Sprint(backend) + txt += fmt.Sprint("}\n") + } + // varnishd will fail if there are no backends defined + if len(v.backends) == 0 { + txt += fmt.Sprint("backend default none;\n") + } + + for name, acl := range v.acls { + txt += fmt.Sprintf("acl %s {\n", name) + for _, entry := range acl { + txt += fmt.Sprintf("\t%s\n", entry) + } + txt += fmt.Sprint("}\n") + } + + // has to be before other subroutines for variables initialization + txt += fmt.Sprint("sub vcl_init {\n") + for _, entry := range v.subroutines["vcl_init"] { Review Comment: are we guaranteed to always have a `vcl_init` or do we need to check for presence first? ########## lib/varnishcfg/backends.go: ########## @@ -0,0 +1,172 @@ +package varnishcfg + +import ( + "fmt" + "strings" + + "github.com/apache/trafficcontrol/lib/go-atscfg" + "github.com/apache/trafficcontrol/lib/go-tc" +) + +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +func (v *VCLBuilder) configureDirectors(vclFile *vclFile, parents *atscfg.ParentAbstraction) ([]string, error) { + warnings := []string{} + + vclFile.imports = append(vclFile.imports, "directors") + var err error + requestFQDNs := make([]string, 0) + + for _, svc := range parents.Services { + addBackends(vclFile.backends, append(svc.Parents, svc.SecondaryParents...), svc.DestDomain, svc.Port) + addDirectors(vclFile.subroutines, svc) + + requestFQDNs = []string{svc.DestDomain} + + if v.toData.Server.Type == tc.CacheTypeEdge.String() { + dsRegexes := atscfg.MakeDSRegexMap(v.toData.DeliveryServiceRegexes) + anyCastPartners := atscfg.GetAnyCastPartners(v.toData.Server, v.toData.Servers) + requestFQDNs, err = atscfg.GetDSRequestFQDNs( + &svc.DS, + dsRegexes[tc.DeliveryServiceName(*svc.DS.XMLID)], + v.toData.Server, + anyCastPartners, + v.toData.CDN.DomainName, + ) + if err != nil { + warnings = append(warnings, "error getting ds '"+*svc.DS.XMLID+"' request fqdns, skipping! Error: "+err.Error()) + continue + } + } + + assignBackends(vclFile.subroutines, svc, requestFQDNs) + } + + return warnings, nil +} + +func assignBackends(subroutines map[string][]string, svc *atscfg.ParentAbstractionService, requestFQDNs []string) { + lines := make([]string, 0) + + conditions := make([]string, 0) + for _, fqdn := range requestFQDNs { + conditions = append(conditions, fmt.Sprintf(`req.http.host == "%s"`, fqdn)) + } + + lines = append(lines, fmt.Sprintf("if (%s) {", strings.Join(conditions, " || "))) + lines = append(lines, fmt.Sprintf("\tset req.backend_hint = %s.backend();", svc.Name)) + + // only change request host from edge servers which typically has multiple request FQDNs or + // one request FQDN that is not the origin. + if len(requestFQDNs) > 1 || (len(requestFQDNs) == 1 && requestFQDNs[0] != svc.DestDomain) { + lines = append(lines, fmt.Sprintf("\tset req.http.host = \"%s\";", svc.DestDomain)) Review Comment: Might want to consider moving this to vcl_backend_fetch, and doing `set bereq.http.host=...` so that the host header is only changed on the request to the backend, and the original host header is preserved in the client request processing ########## lib/varnishcfg/backends.go: ########## @@ -0,0 +1,172 @@ +package varnishcfg + +import ( + "fmt" + "strings" + + "github.com/apache/trafficcontrol/lib/go-atscfg" + "github.com/apache/trafficcontrol/lib/go-tc" +) + +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +func (v *VCLBuilder) configureDirectors(vclFile *vclFile, parents *atscfg.ParentAbstraction) ([]string, error) { + warnings := []string{} + + vclFile.imports = append(vclFile.imports, "directors") + var err error + requestFQDNs := make([]string, 0) + + for _, svc := range parents.Services { + addBackends(vclFile.backends, append(svc.Parents, svc.SecondaryParents...), svc.DestDomain, svc.Port) + addDirectors(vclFile.subroutines, svc) + + requestFQDNs = []string{svc.DestDomain} + + if v.toData.Server.Type == tc.CacheTypeEdge.String() { + dsRegexes := atscfg.MakeDSRegexMap(v.toData.DeliveryServiceRegexes) + anyCastPartners := atscfg.GetAnyCastPartners(v.toData.Server, v.toData.Servers) + requestFQDNs, err = atscfg.GetDSRequestFQDNs( + &svc.DS, + dsRegexes[tc.DeliveryServiceName(*svc.DS.XMLID)], + v.toData.Server, + anyCastPartners, + v.toData.CDN.DomainName, + ) + if err != nil { + warnings = append(warnings, "error getting ds '"+*svc.DS.XMLID+"' request fqdns, skipping! Error: "+err.Error()) + continue + } + } + + assignBackends(vclFile.subroutines, svc, requestFQDNs) + } + + return warnings, nil +} + +func assignBackends(subroutines map[string][]string, svc *atscfg.ParentAbstractionService, requestFQDNs []string) { + lines := make([]string, 0) + + conditions := make([]string, 0) + for _, fqdn := range requestFQDNs { + conditions = append(conditions, fmt.Sprintf(`req.http.host == "%s"`, fqdn)) + } + + lines = append(lines, fmt.Sprintf("if (%s) {", strings.Join(conditions, " || "))) + lines = append(lines, fmt.Sprintf("\tset req.backend_hint = %s.backend();", svc.Name)) + + // only change request host from edge servers which typically has multiple request FQDNs or + // one request FQDN that is not the origin. + if len(requestFQDNs) > 1 || (len(requestFQDNs) == 1 && requestFQDNs[0] != svc.DestDomain) { + lines = append(lines, fmt.Sprintf("\tset req.http.host = \"%s\";", svc.DestDomain)) + } + + lines = append(lines, "}") + + if _, ok := subroutines["vcl_recv"]; !ok { + subroutines["vcl_recv"] = make([]string, 0) + } + subroutines["vcl_recv"] = append(subroutines["vcl_recv"], lines...) +} + +func addBackends(backends map[string]backend, parents []*atscfg.ParentAbstractionServiceParent, originDomain string, originPort int) { + for _, parent := range parents { + backendName := fmt.Sprintf("%s", getBackendName(parent.FQDN, parent.Port)) + if _, ok := backends[backendName]; ok { + continue + } + backends[backendName] = backend{ + host: parent.FQDN, + port: parent.Port, + } + } + backendName := fmt.Sprintf("%s", getBackendName(originDomain, originPort)) + if _, ok := backends[backendName]; ok { + return Review Comment: Why skip this silently? Seems like it would be an error if the parent and origin backend names conflict? ########## infrastructure/cdn-in-a-box/varnish/systemctl.sh: ########## @@ -0,0 +1,94 @@ +#!/bin/bash + +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. + +VARNISHD_EXECUTABLE="/usr/sbin/varnishd" + +is_varnishd_running() { + pgrep -x "$(basename "$VARNISHD_EXECUTABLE")" >/dev/null +} + +start_varnishd() { + if is_varnishd_running; then + echo "varnishd is already running." + else + echo "Starting varnishd..." + "$VARNISHD_EXECUTABLE" -f /opt/trafficserver/etc/trafficserver/default.vcl Review Comment: Consider moving default.vcl into a different directory, not `trafficserver` related? ########## lib/varnishcfg/backends.go: ########## @@ -0,0 +1,172 @@ +package varnishcfg + +import ( + "fmt" + "strings" + + "github.com/apache/trafficcontrol/lib/go-atscfg" + "github.com/apache/trafficcontrol/lib/go-tc" +) + +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +func (v *VCLBuilder) configureDirectors(vclFile *vclFile, parents *atscfg.ParentAbstraction) ([]string, error) { + warnings := []string{} + + vclFile.imports = append(vclFile.imports, "directors") + var err error + requestFQDNs := make([]string, 0) + + for _, svc := range parents.Services { + addBackends(vclFile.backends, append(svc.Parents, svc.SecondaryParents...), svc.DestDomain, svc.Port) + addDirectors(vclFile.subroutines, svc) + + requestFQDNs = []string{svc.DestDomain} + + if v.toData.Server.Type == tc.CacheTypeEdge.String() { + dsRegexes := atscfg.MakeDSRegexMap(v.toData.DeliveryServiceRegexes) + anyCastPartners := atscfg.GetAnyCastPartners(v.toData.Server, v.toData.Servers) + requestFQDNs, err = atscfg.GetDSRequestFQDNs( + &svc.DS, + dsRegexes[tc.DeliveryServiceName(*svc.DS.XMLID)], + v.toData.Server, + anyCastPartners, + v.toData.CDN.DomainName, + ) + if err != nil { + warnings = append(warnings, "error getting ds '"+*svc.DS.XMLID+"' request fqdns, skipping! Error: "+err.Error()) + continue + } + } + + assignBackends(vclFile.subroutines, svc, requestFQDNs) + } + + return warnings, nil +} + +func assignBackends(subroutines map[string][]string, svc *atscfg.ParentAbstractionService, requestFQDNs []string) { + lines := make([]string, 0) + + conditions := make([]string, 0) + for _, fqdn := range requestFQDNs { + conditions = append(conditions, fmt.Sprintf(`req.http.host == "%s"`, fqdn)) + } + + lines = append(lines, fmt.Sprintf("if (%s) {", strings.Join(conditions, " || "))) + lines = append(lines, fmt.Sprintf("\tset req.backend_hint = %s.backend();", svc.Name)) + + // only change request host from edge servers which typically has multiple request FQDNs or + // one request FQDN that is not the origin. + if len(requestFQDNs) > 1 || (len(requestFQDNs) == 1 && requestFQDNs[0] != svc.DestDomain) { + lines = append(lines, fmt.Sprintf("\tset req.http.host = \"%s\";", svc.DestDomain)) + } + + lines = append(lines, "}") + + if _, ok := subroutines["vcl_recv"]; !ok { + subroutines["vcl_recv"] = make([]string, 0) + } + subroutines["vcl_recv"] = append(subroutines["vcl_recv"], lines...) +} + +func addBackends(backends map[string]backend, parents []*atscfg.ParentAbstractionServiceParent, originDomain string, originPort int) { + for _, parent := range parents { + backendName := fmt.Sprintf("%s", getBackendName(parent.FQDN, parent.Port)) + if _, ok := backends[backendName]; ok { + continue + } + backends[backendName] = backend{ + host: parent.FQDN, + port: parent.Port, + } + } + backendName := fmt.Sprintf("%s", getBackendName(originDomain, originPort)) + if _, ok := backends[backendName]; ok { + return + } + backends[backendName] = backend{ + host: originDomain, + port: originPort, + } +} + +func addDirectors(subroutines map[string][]string, svc *atscfg.ParentAbstractionService) { + lines := make([]string, 0) + fallbackDirectorLines := make([]string, 0) + fallbackDirectorLines = append(fallbackDirectorLines, fmt.Sprintf("new %s = directors.fallback();", svc.Name)) + + if len(svc.Parents) != 0 { + lines = append(lines, addBackendsToDirector(svc.Name+"_primary", svc.RetryPolicy, svc.Parents)...) + fallbackDirectorLines = append(fallbackDirectorLines, fmt.Sprintf("%s.add_backend(%s_primary.backend());", svc.Name, svc.Name)) + } + if len(svc.SecondaryParents) != 0 { + lines = append(lines, addBackendsToDirector(svc.Name+"_secondary", svc.RetryPolicy, svc.SecondaryParents)...) + fallbackDirectorLines = append(fallbackDirectorLines, fmt.Sprintf("%s.add_backend(%s_secondary.backend());", svc.Name, svc.Name)) + } + fallbackDirectorLines = append(fallbackDirectorLines, fmt.Sprintf("%s.add_backend(%s);", svc.Name, getBackendName(svc.DestDomain, svc.Port))) + + lines = append(lines, fallbackDirectorLines...) + + if _, ok := subroutines["vcl_init"]; !ok { + subroutines["vcl_init"] = make([]string, 0) + } + subroutines["vcl_init"] = append(subroutines["vcl_init"], lines...) +} + +func addBackendsToDirector(name string, retryPolicy atscfg.ParentAbstractionServiceRetryPolicy, parents []*atscfg.ParentAbstractionServiceParent) []string { + lines := make([]string, 0) + directorType := getDirectorType(retryPolicy) + sticky := "" + if directorType == "fallback" && retryPolicy == atscfg.ParentAbstractionServiceRetryPolicyLatched { Review Comment: Consider adding a separate `sticky` return from getDirectorType so all that evaluation happens in the same place -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
