diff --git a/cache-config/t3cutil/getdatacfg.go b/cache-config/t3cutil/getdatacfg.go index 66ffe58ef3..743d4c03f4 100644 --- a/cache-config/t3cutil/getdatacfg.go +++ b/cache-config/t3cutil/getdatacfg.go @@ -894,8 +894,8 @@ func FilterDSS(dsses []tc.DeliveryServiceServer, dsIDs map[int]struct{}, serverI // FilterParams filters params and returns only the parameters which match configFile, name, and value. // If configFile, name, or value is the empty string, it is not filtered. // Returns a slice of parameters. -func FilterParams(params []tc.Parameter, configFile string, name string, value string, omitName string) []tc.Parameter { - filtered := []tc.Parameter{} +func FilterParams(params []tc.ParameterV5, configFile string, name string, value string, omitName string) []tc.ParameterV5 { + filtered := []tc.ParameterV5{} for _, param := range params { if configFile != "" && param.ConfigFile != configFile { continue diff --git a/lib/varnishcfg/custom_vcl.go b/lib/varnishcfg/custom_vcl.go new file mode 100644 index 0000000000..c71c1fe455 --- /dev/null +++ b/lib/varnishcfg/custom_vcl.go @@ -0,0 +1,39 @@ +package varnishcfg + +/* + * 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. + */ + +import ( + "strings" + + "github.com/apache/trafficcontrol/v8/cache-config/t3cutil" +) + +func (v VCLBuilder) configureCustomVCL(vclFile *vclFile) { + params := t3cutil.FilterParams(v.toData.ServerParams, "default.vcl", "", "", "") + for _, param := range params { + if param.Name == "import" { + vclFile.imports = append(vclFile.imports, param.Value) + continue + } + // TODO: support loading vcl files too with `include`?? i.e. `include "custom.vcl";` + lines := strings.Split(param.Value, "\n") + vclFile.subroutines[param.Name] = append(vclFile.subroutines[param.Name], lines...) + } +} diff --git a/lib/varnishcfg/custom_vcl_test.go b/lib/varnishcfg/custom_vcl_test.go new file mode 100644 index 0000000000..e225a5e717 --- /dev/null +++ b/lib/varnishcfg/custom_vcl_test.go @@ -0,0 +1,60 @@ +package varnishcfg + +/* + * 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. + */ + +import ( + "reflect" + "testing" + + "github.com/apache/trafficcontrol/v8/cache-config/t3cutil" + "github.com/apache/trafficcontrol/v8/lib/go-tc" +) + +func TestConfigureCustomVCL(t *testing.T) { + vb := NewVCLBuilder(&t3cutil.ConfigData{ + ServerParams: []tc.ParameterV50{ + {ConfigFile: "default.vcl", Name: "import", Value: "std"}, + {ConfigFile: "default.vcl", Name: "vcl_recv", Value: "set req.url = std.querysort(req.url);"}, + { + ConfigFile: "default.vcl", + Name: "vcl_deliver", + Value: "if (req.status >= 400 && req.status <= 500) {\n\tset req.status = 404;\n}", + }, + }, + }) + + vclFile := newVCLFile(defaultVCLVersion) + vb.configureCustomVCL(&vclFile) + + expectedVCLFile := newVCLFile(defaultVCLVersion) + expectedVCLFile.imports = append(expectedVCLFile.imports, "std") + expectedVCLFile.subroutines["vcl_recv"] = []string{ + "set req.url = std.querysort(req.url);", + } + expectedVCLFile.subroutines["vcl_deliver"] = []string{ + "if (req.status >= 400 && req.status <= 500) {", + " set req.status = 404;", + "}", + } + + if !reflect.DeepEqual(vclFile, expectedVCLFile) { + t.Errorf("got %v want %v", vclFile, expectedVCLFile) + } +} diff --git a/lib/varnishcfg/vclbuilder.go b/lib/varnishcfg/vclbuilder.go index 91b14b5f6b..68f903dd34 100644 --- a/lib/varnishcfg/vclbuilder.go +++ b/lib/varnishcfg/vclbuilder.go @@ -88,5 +88,7 @@ func (vb *VCLBuilder) BuildVCLFile() (string, []string, error) { dirWarnings, err := vb.configureDirectors(&v, parents) warnings = append(warnings, dirWarnings...) + vb.configureCustomVCL(&v) + return fmt.Sprint(v), warnings, err }