github.com/nicocha30/gvisor-ligolo@v0.0.0-20230726075806-989fa2c0a413/pkg/shim/utils/utils.go (about) 1 // Copyright 2018 The gVisor Authors. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // https://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 // Package utils container miscellaneous utility function used by the shim. 16 package utils 17 18 import ( 19 "encoding/json" 20 "io/ioutil" 21 "path/filepath" 22 23 specs "github.com/opencontainers/runtime-spec/specs-go" 24 ) 25 26 const configFilename = "config.json" 27 28 // ReadSpec reads OCI spec from the bundle directory. 29 func ReadSpec(bundle string) (*specs.Spec, error) { 30 b, err := ioutil.ReadFile(filepath.Join(bundle, configFilename)) 31 if err != nil { 32 return nil, err 33 } 34 var spec specs.Spec 35 if err := json.Unmarshal(b, &spec); err != nil { 36 return nil, err 37 } 38 return &spec, nil 39 } 40 41 // WriteSpec writes OCI spec to the bundle directory. 42 func WriteSpec(bundle string, spec *specs.Spec) error { 43 b, err := json.Marshal(spec) 44 if err != nil { 45 return err 46 } 47 return ioutil.WriteFile(filepath.Join(bundle, configFilename), b, 0666) 48 } 49 50 // IsSandbox checks whether a container is a sandbox container. 51 func IsSandbox(spec *specs.Spec) bool { 52 t, ok := spec.Annotations[ContainerTypeAnnotation] 53 return !ok || t == containerTypeSandbox 54 } 55 56 // UserLogPath gets user log path from OCI annotation. 57 func UserLogPath(spec *specs.Spec) string { 58 sandboxLogDir := spec.Annotations[sandboxLogDirAnnotation] 59 if sandboxLogDir == "" { 60 return "" 61 } 62 return filepath.Join(sandboxLogDir, "gvisor.log") 63 } 64 65 // PanicLogPath gets the panic log path from OCI annotation. 66 func PanicLogPath(spec *specs.Spec) string { 67 if spec == nil { 68 return "" 69 } 70 sandboxLogDir := spec.Annotations[sandboxLogDirAnnotation] 71 if sandboxLogDir == "" { 72 return "" 73 } 74 return filepath.Join(sandboxLogDir, "gvisor_panic.log") 75 }