config.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /*
  2. Copyright 2015 The Kubernetes Authors.
  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. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package rkt
  14. import (
  15. "fmt"
  16. rktapi "github.com/coreos/rkt/api/v1alpha"
  17. "golang.org/x/net/context"
  18. )
  19. // Config stores the global configuration for the rkt runtime.
  20. // Detailed documents can be found at:
  21. // https://github.com/coreos/rkt/blob/master/Documentation/commands.md#global-options
  22. type Config struct {
  23. // The absolute path to the binary, or leave empty to find it in $PATH.
  24. Path string
  25. // The rkt data directory.
  26. Dir string
  27. // The image to use as stage1.
  28. Stage1Image string
  29. // The debug flag for rkt.
  30. Debug bool
  31. // Comma-separated list of security features to disable.
  32. // Allowed values: "none", "image", "tls", "ondisk", "http", "all".
  33. InsecureOptions string
  34. // The local config directory.
  35. LocalConfigDir string
  36. // The user config directory.
  37. UserConfigDir string
  38. // The system config directory.
  39. SystemConfigDir string
  40. }
  41. // buildGlobalOptions returns an array of global command line options.
  42. func (c *Config) buildGlobalOptions() []string {
  43. var result []string
  44. if c == nil {
  45. return result
  46. }
  47. if c.Debug {
  48. result = append(result, "--debug=true")
  49. }
  50. if c.InsecureOptions != "" {
  51. result = append(result, fmt.Sprintf("--insecure-options=%s", c.InsecureOptions))
  52. }
  53. if c.LocalConfigDir != "" {
  54. result = append(result, fmt.Sprintf("--local-config=%s", c.LocalConfigDir))
  55. }
  56. if c.UserConfigDir != "" {
  57. result = append(result, fmt.Sprintf("--user-config=%s", c.UserConfigDir))
  58. }
  59. if c.SystemConfigDir != "" {
  60. result = append(result, fmt.Sprintf("--system-config=%s", c.SystemConfigDir))
  61. }
  62. if c.Dir != "" {
  63. result = append(result, fmt.Sprintf("--dir=%s", c.Dir))
  64. }
  65. return result
  66. }
  67. // getConfig gets configurations from the rkt API service
  68. // and merge it with the existing config. The merge rule is
  69. // that the fields in the provided config will override the
  70. // result that get from the rkt api service.
  71. func (r *Runtime) getConfig(cfg *Config) (*Config, error) {
  72. ctx, cancel := context.WithTimeout(context.Background(), r.requestTimeout)
  73. defer cancel()
  74. resp, err := r.apisvc.GetInfo(ctx, &rktapi.GetInfoRequest{})
  75. if err != nil {
  76. return nil, err
  77. }
  78. flags := resp.Info.GlobalFlags
  79. if flags.Dir != "" {
  80. cfg.Dir = flags.Dir
  81. }
  82. if flags.LocalConfigDir != "" {
  83. cfg.LocalConfigDir = flags.LocalConfigDir
  84. }
  85. if flags.UserConfigDir != "" {
  86. cfg.UserConfigDir = flags.UserConfigDir
  87. }
  88. if flags.SystemConfigDir != "" {
  89. cfg.SystemConfigDir = flags.SystemConfigDir
  90. }
  91. if flags.InsecureFlags != "" {
  92. cfg.InsecureOptions = fmt.Sprintf("%s,%s", cfg.InsecureOptions, flags.InsecureFlags)
  93. }
  94. return cfg, nil
  95. }