github.com/MerlinKodo/gvisor@v0.0.0-20231110090155-957f62ecf90e/pkg/shim/runsc/utils.go (about) 1 // Copyright 2018 The containerd Authors. 2 // Copyright 2018 The gVisor Authors. 3 // 4 // Licensed under the Apache License, Version 2.0 (the "License"); 5 // you may not use this file except in compliance with the License. 6 // You may obtain a copy of the License at 7 // 8 // https://www.apache.org/licenses/LICENSE-2.0 9 // 10 // Unless required by applicable law or agreed to in writing, software 11 // distributed under the License is distributed on an "AS IS" BASIS, 12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 // See the License for the specific language governing permissions and 14 // limitations under the License. 15 16 package runsc 17 18 import ( 19 "bytes" 20 "strings" 21 "sync" 22 ) 23 24 var bytesBufferPool = sync.Pool{ 25 New: func() any { 26 return bytes.NewBuffer(nil) 27 }, 28 } 29 30 func getBuf() *bytes.Buffer { 31 return bytesBufferPool.Get().(*bytes.Buffer) 32 } 33 34 func putBuf(b *bytes.Buffer) { 35 b.Reset() 36 bytesBufferPool.Put(b) 37 } 38 39 // pathLikeFlags are runsc flags which refer to paths to files. 40 var pathLikeFlags = []string{ 41 "log", 42 "panic-log", 43 "debug-log", 44 "coverage-report", 45 "profile-block", 46 "profile-cpu", 47 "profile-heap", 48 "profile-mutex", 49 "trace", 50 } 51 52 // replaceID replaces %ID% in `path` with the given sandbox ID. 53 func replaceID(id string, path string) string { 54 return strings.Replace(path, "%ID%", id, -1) 55 } 56 57 // EmittedPaths returns a list of file paths that the sandbox may need to 58 // create using the given configuration. Useful to create parent directories. 59 func EmittedPaths(id string, config map[string]string) []string { 60 var paths []string 61 for _, cfgFlag := range pathLikeFlags { 62 if path, ok := config[cfgFlag]; ok { 63 paths = append(paths, replaceID(id, path)) 64 } 65 } 66 return paths 67 } 68 69 // FormatRunscPaths fills in %ID% in path-like flags. 70 func FormatRunscPaths(id string, config map[string]string) { 71 for _, cfgFlag := range pathLikeFlags { 72 if path, ok := config[cfgFlag]; ok { 73 config[cfgFlag] = replaceID(id, path) 74 } 75 } 76 } 77 78 // FormatShimLogPath creates the file path to the log file. It replaces %ID% 79 // in the path with the provided "id". It also uses a default log name if the 80 // path ends with '/'. 81 func FormatShimLogPath(path string, id string) string { 82 if strings.HasSuffix(path, "/") { 83 // Default format: <path>/runsc-shim-<ID>.log 84 path += "runsc-shim-%ID%.log" 85 } 86 return replaceID(id, path) 87 }