Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Cache-Side Config Generator (#3762)
* Add client-side ATS config gen Adds a client-side ATS config gen interceptor to ORT, with an initial parent.config gen and passing everything else to TO. Plan is to add all configs to be client-side generated. * Add TO deliveryserviceserver ids query params Also adds a client function to specify server and DS IDs, as well as a missing client func for the existing deliveryservice?cdn param. * Add ORT atstccfg to ort build scripts * Add ORT args to override hostname, OS check For debugging, or emergencies. * Add ORT using atstccfg instead of TO directly Also changes atstccfg to do things ORT needed: - changed to return the HTTP code as the exit code, on error - added CLI option to return which configs are generated (vs proxied) - added retry num option, for failed TO attempts - changed to use lib/tc-log, and take args for where to log - fixed missing license headers - added integrity check via SHA512 or Content-length headers * Add atstccfg insecure, timeout, cache age flags * Fix merge conflict * Add TO ORT gen profile configs * Add atstccfg changelog * Add atstccfg docs * Add license for vendored pflag * Remove old/wrong comments * Remove unused symbols * Removed docs comma * Fix license files
- Loading branch information
1 parent
fab1f00
commit dc9bdf5
Showing
134 changed files
with
10,994 additions
and
1,175 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
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
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
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
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,38 @@ | ||
package atscfg | ||
|
||
/* | ||
* 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 AstatsSeparator = "=" | ||
const AstatsFileName = "astats.config" | ||
|
||
func MakeAStatsDotConfig( | ||
profileName string, | ||
paramData map[string]string, // GetProfileParamData(tx, profile.ID, AstatsFileName) | ||
toToolName string, // tm.toolname global parameter (TODO: cache itself?) | ||
toURL string, // tm.url global parameter (TODO: cache itself?) | ||
) string { | ||
hdr := GenericHeaderComment(profileName, toToolName, toURL) | ||
txt := GenericProfileConfig(paramData, AstatsSeparator) | ||
if txt == "" { | ||
txt = "\n" // If no params exist, don't send "not found," but an empty file. We know the profile exists. | ||
} | ||
txt = hdr + txt | ||
return txt | ||
} |
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,111 @@ | ||
package atscfg | ||
|
||
/* | ||
* 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 ( | ||
"errors" | ||
"strconv" | ||
"strings" | ||
"time" | ||
|
||
"github.com/apache/trafficcontrol/lib/go-tc" | ||
) | ||
|
||
const HeaderCommentDateFormat = "Mon Jan 2 15:04:05 MST 2006" | ||
|
||
type ServerInfo struct { | ||
CacheGroupID int | ||
CDN tc.CDNName | ||
CDNID int | ||
DomainName string | ||
HostName string | ||
ID int | ||
IP string | ||
ParentCacheGroupID int | ||
ParentCacheGroupType string | ||
ProfileID ProfileID | ||
ProfileName string | ||
Port int | ||
SecondaryParentCacheGroupID int | ||
SecondaryParentCacheGroupType string | ||
Type string | ||
} | ||
|
||
func (s *ServerInfo) IsTopLevelCache() bool { | ||
return (s.ParentCacheGroupType == tc.CacheGroupOriginTypeName || s.ParentCacheGroupID == InvalidID) && | ||
(s.SecondaryParentCacheGroupType == tc.CacheGroupOriginTypeName || s.SecondaryParentCacheGroupID == InvalidID) | ||
} | ||
|
||
func HeaderCommentWithTOVersionStr(name string, nameVersionStr string) string { | ||
return "# DO NOT EDIT - Generated for " + name + " by " + nameVersionStr + " on " + time.Now().Format(HeaderCommentDateFormat) + "\n" | ||
} | ||
|
||
func GetNameVersionStringFromToolNameAndURL(toolName string, url string) string { | ||
return toolName + " (" + url + ")" | ||
} | ||
|
||
func GenericHeaderComment(name string, toolName string, url string) string { | ||
return HeaderCommentWithTOVersionStr(name, GetNameVersionStringFromToolNameAndURL(toolName, url)) | ||
} | ||
|
||
// GetATSMajorVersionFromATSVersion returns the major version of the given profile's package trafficserver parameter. | ||
// The atsVersion is typically a Parameter on the Server's Profile, with the configFile "package" name "trafficserver". | ||
// Returns an error if atsVersion is empty or not a number. | ||
func GetATSMajorVersionFromATSVersion(atsVersion string) (int, error) { | ||
if len(atsVersion) == 0 { | ||
return 0, errors.New("ats version missing") | ||
} | ||
atsMajorVer, err := strconv.Atoi(atsVersion[:1]) | ||
if err != nil { | ||
return 0, errors.New("ats version parameter '" + atsVersion + "' is not a number") | ||
} | ||
return atsMajorVer, nil | ||
} | ||
|
||
type DeliveryServiceID int | ||
type ProfileID int | ||
type ServerID int | ||
|
||
// GenericProfileConfig generates a generic profile config text, from the profile's parameters with the given config file name. | ||
// This does not include a header comment, because a generic config may not use a number sign as a comment. | ||
// If you need a header comment, it can be added manually via ats.HeaderComment, or automatically with WithProfileDataHdr. | ||
func GenericProfileConfig( | ||
paramData map[string]string, // GetProfileParamData(tx, profileID, fileName) | ||
separator string, | ||
) string { | ||
text := "" | ||
for name, val := range paramData { | ||
name = trimParamUnderscoreNumSuffix(name) | ||
text += name + separator + val + "\n" | ||
} | ||
return text | ||
} | ||
|
||
// trimParamUnderscoreNumSuffix removes any trailing "__[0-9]+" and returns the trimmed string. | ||
func trimParamUnderscoreNumSuffix(paramName string) string { | ||
underscorePos := strings.LastIndex(paramName, `__`) | ||
if underscorePos == -1 { | ||
return paramName | ||
} | ||
if _, err := strconv.ParseFloat(paramName[underscorePos+2:], 64); err != nil { | ||
return paramName | ||
} | ||
return paramName[:underscorePos] | ||
} |
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,52 @@ | ||
package atscfg | ||
|
||
/* | ||
* 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" | ||
"testing" | ||
) | ||
|
||
func TestGenericHeaderComment(t *testing.T) { | ||
objName := "foo" | ||
toolName := "bar" | ||
toURL := "url" | ||
|
||
txt := GenericHeaderComment(objName, toolName, toURL) | ||
|
||
testComment(t, txt, objName, toolName, toURL) | ||
} | ||
|
||
func testComment(t *testing.T, txt string, objName string, toolName string, toURL string) { | ||
commentLine := strings.SplitN(txt, "\n", 2)[0] // SplitN always returns at least 1 element, no need to check len before indexing | ||
|
||
if !strings.HasPrefix(strings.TrimSpace(commentLine), "#") { | ||
t.Errorf("expected comment on first line, actual: '" + commentLine + "'") | ||
} | ||
if !strings.Contains(commentLine, toURL) { | ||
t.Errorf("expected toolName '" + toolName + "' in comment, actual: '" + commentLine + "'") | ||
} | ||
if !strings.Contains(commentLine, toURL) { | ||
t.Errorf("expected toURL '" + toURL + "' in comment, actual: '" + commentLine + "'") | ||
} | ||
if !strings.Contains(commentLine, objName) { | ||
t.Errorf("expected profile '" + objName + "' in comment, actual: '" + commentLine + "'") | ||
} | ||
} |
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,53 @@ | ||
package atscfg | ||
|
||
/* | ||
* 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" | ||
) | ||
|
||
func MakeATSDotRules( | ||
profileName string, | ||
paramData map[string]string, // GetProfileParamData(tx, profile.ID, StorageFileName) | ||
toToolName string, // tm.toolname global parameter (TODO: cache itself?) | ||
toURL string, // tm.url global parameter (TODO: cache itself?) | ||
) string { | ||
text := GenericHeaderComment(profileName, toToolName, toURL) | ||
|
||
drivePrefix := strings.TrimPrefix(paramData["Drive_Prefix"], `/dev/`) | ||
drivePostfix := strings.Split(paramData["Drive_Letters"], ",") | ||
|
||
for _, l := range drivePostfix { | ||
l = strings.TrimSpace(l) | ||
if l == "" { | ||
continue | ||
} | ||
text += `KERNEL=="` + drivePrefix + l + `", OWNER="ats"` + "\n" | ||
} | ||
if ramPrefix, ok := paramData["RAM_Drive_Prefix"]; ok { | ||
ramPrefix = strings.TrimPrefix(ramPrefix, `/dev/`) | ||
ramPostfix := strings.Split(paramData["RAM_Drive_Letters"], ",") | ||
for _, l := range ramPostfix { | ||
text += `KERNEL=="` + ramPrefix + l + `", OWNER="ats"` + "\n" | ||
} | ||
} | ||
|
||
return text | ||
} |
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,52 @@ | ||
package atscfg | ||
|
||
/* | ||
* 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" | ||
"testing" | ||
) | ||
|
||
func TestMakeATSDotRules(t *testing.T) { | ||
profileName := "myProfile" | ||
toolName := "myToolName" | ||
toURL := "https://myto.example.net" | ||
paramData := map[string]string{ | ||
"Drive_Prefix": "/dev/sd", | ||
"Drive_Letters": "a,b,c,d,e", | ||
"RAM_Drive_Prefix": "/dev/ra", | ||
"RAM_Drive_Letters": "f,g,h", | ||
} | ||
|
||
txt := MakeATSDotRules(profileName, paramData, toolName, toURL) | ||
|
||
testComment(t, txt, profileName, toolName, toURL) | ||
|
||
if count := strings.Count(txt, "\n"); count != 9 { // one line for each drive letter, plus 1 comment | ||
t.Errorf("expected one line for each drive letter plus a comment, actual: '%v' count %v", txt, count) | ||
} | ||
|
||
if !strings.Contains(txt, "sda") { | ||
t.Errorf("expected sda for drive letter, actual: '%v'", txt) | ||
} | ||
if !strings.Contains(txt, "rah") { | ||
t.Errorf("expected sda for drive letter, actual: '%v'", txt) | ||
} | ||
} |
Oops, something went wrong.