conf.go 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. // Copyright 2015 CNI 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. // http://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. package libcni
  15. import (
  16. "encoding/json"
  17. "fmt"
  18. "io/ioutil"
  19. "os"
  20. "path/filepath"
  21. "sort"
  22. )
  23. type NotFoundError struct {
  24. Dir string
  25. Name string
  26. }
  27. func (e NotFoundError) Error() string {
  28. return fmt.Sprintf(`no net configuration with name "%s" in %s`, e.Name, e.Dir)
  29. }
  30. type NoConfigsFoundError struct {
  31. Dir string
  32. }
  33. func (e NoConfigsFoundError) Error() string {
  34. return fmt.Sprintf(`no net configurations found in %s`, e.Dir)
  35. }
  36. func ConfFromBytes(bytes []byte) (*NetworkConfig, error) {
  37. conf := &NetworkConfig{Bytes: bytes}
  38. if err := json.Unmarshal(bytes, &conf.Network); err != nil {
  39. return nil, fmt.Errorf("error parsing configuration: %s", err)
  40. }
  41. if conf.Network.Type == "" {
  42. return nil, fmt.Errorf("error parsing configuration: missing 'type'")
  43. }
  44. return conf, nil
  45. }
  46. func ConfFromFile(filename string) (*NetworkConfig, error) {
  47. bytes, err := ioutil.ReadFile(filename)
  48. if err != nil {
  49. return nil, fmt.Errorf("error reading %s: %s", filename, err)
  50. }
  51. return ConfFromBytes(bytes)
  52. }
  53. func ConfListFromBytes(bytes []byte) (*NetworkConfigList, error) {
  54. rawList := make(map[string]interface{})
  55. if err := json.Unmarshal(bytes, &rawList); err != nil {
  56. return nil, fmt.Errorf("error parsing configuration list: %s", err)
  57. }
  58. rawName, ok := rawList["name"]
  59. if !ok {
  60. return nil, fmt.Errorf("error parsing configuration list: no name")
  61. }
  62. name, ok := rawName.(string)
  63. if !ok {
  64. return nil, fmt.Errorf("error parsing configuration list: invalid name type %T", rawName)
  65. }
  66. var cniVersion string
  67. rawVersion, ok := rawList["cniVersion"]
  68. if ok {
  69. cniVersion, ok = rawVersion.(string)
  70. if !ok {
  71. return nil, fmt.Errorf("error parsing configuration list: invalid cniVersion type %T", rawVersion)
  72. }
  73. }
  74. disableCheck := false
  75. if rawDisableCheck, ok := rawList["disableCheck"]; ok {
  76. disableCheck, ok = rawDisableCheck.(bool)
  77. if !ok {
  78. return nil, fmt.Errorf("error parsing configuration list: invalid disableCheck type %T", rawDisableCheck)
  79. }
  80. }
  81. list := &NetworkConfigList{
  82. Name: name,
  83. DisableCheck: disableCheck,
  84. CNIVersion: cniVersion,
  85. Bytes: bytes,
  86. }
  87. var plugins []interface{}
  88. plug, ok := rawList["plugins"]
  89. if !ok {
  90. return nil, fmt.Errorf("error parsing configuration list: no 'plugins' key")
  91. }
  92. plugins, ok = plug.([]interface{})
  93. if !ok {
  94. return nil, fmt.Errorf("error parsing configuration list: invalid 'plugins' type %T", plug)
  95. }
  96. if len(plugins) == 0 {
  97. return nil, fmt.Errorf("error parsing configuration list: no plugins in list")
  98. }
  99. for i, conf := range plugins {
  100. newBytes, err := json.Marshal(conf)
  101. if err != nil {
  102. return nil, fmt.Errorf("Failed to marshal plugin config %d: %v", i, err)
  103. }
  104. netConf, err := ConfFromBytes(newBytes)
  105. if err != nil {
  106. return nil, fmt.Errorf("Failed to parse plugin config %d: %v", i, err)
  107. }
  108. list.Plugins = append(list.Plugins, netConf)
  109. }
  110. return list, nil
  111. }
  112. func ConfListFromFile(filename string) (*NetworkConfigList, error) {
  113. bytes, err := ioutil.ReadFile(filename)
  114. if err != nil {
  115. return nil, fmt.Errorf("error reading %s: %s", filename, err)
  116. }
  117. return ConfListFromBytes(bytes)
  118. }
  119. func ConfFiles(dir string, extensions []string) ([]string, error) {
  120. // In part, adapted from rkt/networking/podenv.go#listFiles
  121. files, err := ioutil.ReadDir(dir)
  122. switch {
  123. case err == nil: // break
  124. case os.IsNotExist(err):
  125. return nil, nil
  126. default:
  127. return nil, err
  128. }
  129. confFiles := []string{}
  130. for _, f := range files {
  131. if f.IsDir() {
  132. continue
  133. }
  134. fileExt := filepath.Ext(f.Name())
  135. for _, ext := range extensions {
  136. if fileExt == ext {
  137. confFiles = append(confFiles, filepath.Join(dir, f.Name()))
  138. }
  139. }
  140. }
  141. return confFiles, nil
  142. }
  143. func LoadConf(dir, name string) (*NetworkConfig, error) {
  144. files, err := ConfFiles(dir, []string{".conf", ".json"})
  145. switch {
  146. case err != nil:
  147. return nil, err
  148. case len(files) == 0:
  149. return nil, NoConfigsFoundError{Dir: dir}
  150. }
  151. sort.Strings(files)
  152. for _, confFile := range files {
  153. conf, err := ConfFromFile(confFile)
  154. if err != nil {
  155. return nil, err
  156. }
  157. if conf.Network.Name == name {
  158. return conf, nil
  159. }
  160. }
  161. return nil, NotFoundError{dir, name}
  162. }
  163. func LoadConfList(dir, name string) (*NetworkConfigList, error) {
  164. files, err := ConfFiles(dir, []string{".conflist"})
  165. if err != nil {
  166. return nil, err
  167. }
  168. sort.Strings(files)
  169. for _, confFile := range files {
  170. conf, err := ConfListFromFile(confFile)
  171. if err != nil {
  172. return nil, err
  173. }
  174. if conf.Name == name {
  175. return conf, nil
  176. }
  177. }
  178. // Try and load a network configuration file (instead of list)
  179. // from the same name, then upconvert.
  180. singleConf, err := LoadConf(dir, name)
  181. if err != nil {
  182. // A little extra logic so the error makes sense
  183. if _, ok := err.(NoConfigsFoundError); len(files) != 0 && ok {
  184. // Config lists found but no config files found
  185. return nil, NotFoundError{dir, name}
  186. }
  187. return nil, err
  188. }
  189. return ConfListFromConf(singleConf)
  190. }
  191. func InjectConf(original *NetworkConfig, newValues map[string]interface{}) (*NetworkConfig, error) {
  192. config := make(map[string]interface{})
  193. err := json.Unmarshal(original.Bytes, &config)
  194. if err != nil {
  195. return nil, fmt.Errorf("unmarshal existing network bytes: %s", err)
  196. }
  197. for key, value := range newValues {
  198. if key == "" {
  199. return nil, fmt.Errorf("keys cannot be empty")
  200. }
  201. if value == nil {
  202. return nil, fmt.Errorf("key '%s' value must not be nil", key)
  203. }
  204. config[key] = value
  205. }
  206. newBytes, err := json.Marshal(config)
  207. if err != nil {
  208. return nil, err
  209. }
  210. return ConfFromBytes(newBytes)
  211. }
  212. // ConfListFromConf "upconverts" a network config in to a NetworkConfigList,
  213. // with the single network as the only entry in the list.
  214. func ConfListFromConf(original *NetworkConfig) (*NetworkConfigList, error) {
  215. // Re-deserialize the config's json, then make a raw map configlist.
  216. // This may seem a bit strange, but it's to make the Bytes fields
  217. // actually make sense. Otherwise, the generated json is littered with
  218. // golang default values.
  219. rawConfig := make(map[string]interface{})
  220. if err := json.Unmarshal(original.Bytes, &rawConfig); err != nil {
  221. return nil, err
  222. }
  223. rawConfigList := map[string]interface{}{
  224. "name": original.Network.Name,
  225. "cniVersion": original.Network.CNIVersion,
  226. "plugins": []interface{}{rawConfig},
  227. }
  228. b, err := json.Marshal(rawConfigList)
  229. if err != nil {
  230. return nil, err
  231. }
  232. return ConfListFromBytes(b)
  233. }