helpers.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  1. /*
  2. Copyright 2016 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 dockershim
  14. import (
  15. "errors"
  16. "fmt"
  17. "io"
  18. "regexp"
  19. "strconv"
  20. "strings"
  21. "sync/atomic"
  22. dockertypes "github.com/docker/docker/api/types"
  23. dockercontainer "github.com/docker/docker/api/types/container"
  24. dockerfilters "github.com/docker/docker/api/types/filters"
  25. dockernat "github.com/docker/go-connections/nat"
  26. "k8s.io/klog"
  27. utilerrors "k8s.io/apimachinery/pkg/util/errors"
  28. runtimeapi "k8s.io/cri-api/pkg/apis/runtime/v1alpha2"
  29. "k8s.io/kubernetes/pkg/credentialprovider"
  30. "k8s.io/kubernetes/pkg/kubelet/dockershim/libdocker"
  31. "k8s.io/kubernetes/pkg/kubelet/types"
  32. "k8s.io/kubernetes/pkg/security/apparmor"
  33. "k8s.io/kubernetes/pkg/util/parsers"
  34. )
  35. const (
  36. annotationPrefix = "annotation."
  37. securityOptSeparator = '='
  38. )
  39. var (
  40. conflictRE = regexp.MustCompile(`Conflict. (?:.)+ is already in use by container \"?([0-9a-z]+)\"?`)
  41. // this is hacky, but extremely common.
  42. // if a container starts but the executable file is not found, runc gives a message that matches
  43. startRE = regexp.MustCompile(`\\\\\\\"(.*)\\\\\\\": executable file not found`)
  44. defaultSeccompOpt = []dockerOpt{{"seccomp", "unconfined", ""}}
  45. )
  46. // generateEnvList converts KeyValue list to a list of strings, in the form of
  47. // '<key>=<value>', which can be understood by docker.
  48. func generateEnvList(envs []*runtimeapi.KeyValue) (result []string) {
  49. for _, env := range envs {
  50. result = append(result, fmt.Sprintf("%s=%s", env.Key, env.Value))
  51. }
  52. return
  53. }
  54. // makeLabels converts annotations to labels and merge them with the given
  55. // labels. This is necessary because docker does not support annotations;
  56. // we *fake* annotations using labels. Note that docker labels are not
  57. // updatable.
  58. func makeLabels(labels, annotations map[string]string) map[string]string {
  59. merged := make(map[string]string)
  60. for k, v := range labels {
  61. merged[k] = v
  62. }
  63. for k, v := range annotations {
  64. // Assume there won't be conflict.
  65. merged[fmt.Sprintf("%s%s", annotationPrefix, k)] = v
  66. }
  67. return merged
  68. }
  69. // extractLabels converts raw docker labels to the CRI labels and annotations.
  70. // It also filters out internal labels used by this shim.
  71. func extractLabels(input map[string]string) (map[string]string, map[string]string) {
  72. labels := make(map[string]string)
  73. annotations := make(map[string]string)
  74. for k, v := range input {
  75. // Check if the key is used internally by the shim.
  76. internal := false
  77. for _, internalKey := range internalLabelKeys {
  78. if k == internalKey {
  79. internal = true
  80. break
  81. }
  82. }
  83. if internal {
  84. continue
  85. }
  86. // Delete the container name label for the sandbox. It is added in the shim,
  87. // should not be exposed via CRI.
  88. if k == types.KubernetesContainerNameLabel &&
  89. input[containerTypeLabelKey] == containerTypeLabelSandbox {
  90. continue
  91. }
  92. // Check if the label should be treated as an annotation.
  93. if strings.HasPrefix(k, annotationPrefix) {
  94. annotations[strings.TrimPrefix(k, annotationPrefix)] = v
  95. continue
  96. }
  97. labels[k] = v
  98. }
  99. return labels, annotations
  100. }
  101. // generateMountBindings converts the mount list to a list of strings that
  102. // can be understood by docker.
  103. // '<HostPath>:<ContainerPath>[:options]', where 'options'
  104. // is a comma-separated list of the following strings:
  105. // 'ro', if the path is read only
  106. // 'Z', if the volume requires SELinux relabeling
  107. // propagation mode such as 'rslave'
  108. func generateMountBindings(mounts []*runtimeapi.Mount) []string {
  109. result := make([]string, 0, len(mounts))
  110. for _, m := range mounts {
  111. bind := fmt.Sprintf("%s:%s", m.HostPath, m.ContainerPath)
  112. var attrs []string
  113. if m.Readonly {
  114. attrs = append(attrs, "ro")
  115. }
  116. // Only request relabeling if the pod provides an SELinux context. If the pod
  117. // does not provide an SELinux context relabeling will label the volume with
  118. // the container's randomly allocated MCS label. This would restrict access
  119. // to the volume to the container which mounts it first.
  120. if m.SelinuxRelabel {
  121. attrs = append(attrs, "Z")
  122. }
  123. switch m.Propagation {
  124. case runtimeapi.MountPropagation_PROPAGATION_PRIVATE:
  125. // noop, private is default
  126. case runtimeapi.MountPropagation_PROPAGATION_BIDIRECTIONAL:
  127. attrs = append(attrs, "rshared")
  128. case runtimeapi.MountPropagation_PROPAGATION_HOST_TO_CONTAINER:
  129. attrs = append(attrs, "rslave")
  130. default:
  131. klog.Warningf("unknown propagation mode for hostPath %q", m.HostPath)
  132. // Falls back to "private"
  133. }
  134. if len(attrs) > 0 {
  135. bind = fmt.Sprintf("%s:%s", bind, strings.Join(attrs, ","))
  136. }
  137. result = append(result, bind)
  138. }
  139. return result
  140. }
  141. func makePortsAndBindings(pm []*runtimeapi.PortMapping) (dockernat.PortSet, map[dockernat.Port][]dockernat.PortBinding) {
  142. exposedPorts := dockernat.PortSet{}
  143. portBindings := map[dockernat.Port][]dockernat.PortBinding{}
  144. for _, port := range pm {
  145. exteriorPort := port.HostPort
  146. if exteriorPort == 0 {
  147. // No need to do port binding when HostPort is not specified
  148. continue
  149. }
  150. interiorPort := port.ContainerPort
  151. // Some of this port stuff is under-documented voodoo.
  152. // See http://stackoverflow.com/questions/20428302/binding-a-port-to-a-host-interface-using-the-rest-api
  153. var protocol string
  154. switch port.Protocol {
  155. case runtimeapi.Protocol_UDP:
  156. protocol = "/udp"
  157. case runtimeapi.Protocol_TCP:
  158. protocol = "/tcp"
  159. case runtimeapi.Protocol_SCTP:
  160. protocol = "/sctp"
  161. default:
  162. klog.Warningf("Unknown protocol %q: defaulting to TCP", port.Protocol)
  163. protocol = "/tcp"
  164. }
  165. dockerPort := dockernat.Port(strconv.Itoa(int(interiorPort)) + protocol)
  166. exposedPorts[dockerPort] = struct{}{}
  167. hostBinding := dockernat.PortBinding{
  168. HostPort: strconv.Itoa(int(exteriorPort)),
  169. HostIP: port.HostIp,
  170. }
  171. // Allow multiple host ports bind to same docker port
  172. if existedBindings, ok := portBindings[dockerPort]; ok {
  173. // If a docker port already map to a host port, just append the host ports
  174. portBindings[dockerPort] = append(existedBindings, hostBinding)
  175. } else {
  176. // Otherwise, it's fresh new port binding
  177. portBindings[dockerPort] = []dockernat.PortBinding{
  178. hostBinding,
  179. }
  180. }
  181. }
  182. return exposedPorts, portBindings
  183. }
  184. // getApparmorSecurityOpts gets apparmor options from container config.
  185. func getApparmorSecurityOpts(sc *runtimeapi.LinuxContainerSecurityContext, separator rune) ([]string, error) {
  186. if sc == nil || sc.ApparmorProfile == "" {
  187. return nil, nil
  188. }
  189. appArmorOpts, err := getAppArmorOpts(sc.ApparmorProfile)
  190. if err != nil {
  191. return nil, err
  192. }
  193. fmtOpts := fmtDockerOpts(appArmorOpts, separator)
  194. return fmtOpts, nil
  195. }
  196. // dockerFilter wraps around dockerfilters.Args and provides methods to modify
  197. // the filter easily.
  198. type dockerFilter struct {
  199. args *dockerfilters.Args
  200. }
  201. func newDockerFilter(args *dockerfilters.Args) *dockerFilter {
  202. return &dockerFilter{args: args}
  203. }
  204. func (f *dockerFilter) Add(key, value string) {
  205. f.args.Add(key, value)
  206. }
  207. func (f *dockerFilter) AddLabel(key, value string) {
  208. f.Add("label", fmt.Sprintf("%s=%s", key, value))
  209. }
  210. // parseUserFromImageUser splits the user out of an user:group string.
  211. func parseUserFromImageUser(id string) string {
  212. if id == "" {
  213. return id
  214. }
  215. // split instances where the id may contain user:group
  216. if strings.Contains(id, ":") {
  217. return strings.Split(id, ":")[0]
  218. }
  219. // no group, just return the id
  220. return id
  221. }
  222. // getUserFromImageUser gets uid or user name of the image user.
  223. // If user is numeric, it will be treated as uid; or else, it is treated as user name.
  224. func getUserFromImageUser(imageUser string) (*int64, string) {
  225. user := parseUserFromImageUser(imageUser)
  226. // return both nil if user is not specified in the image.
  227. if user == "" {
  228. return nil, ""
  229. }
  230. // user could be either uid or user name. Try to interpret as numeric uid.
  231. uid, err := strconv.ParseInt(user, 10, 64)
  232. if err != nil {
  233. // If user is non numeric, assume it's user name.
  234. return nil, user
  235. }
  236. // If user is a numeric uid.
  237. return &uid, ""
  238. }
  239. // See #33189. If the previous attempt to create a sandbox container name FOO
  240. // failed due to "device or resource busy", it is possible that docker did
  241. // not clean up properly and has inconsistent internal state. Docker would
  242. // not report the existence of FOO, but would complain if user wants to
  243. // create a new container named FOO. To work around this, we parse the error
  244. // message to identify failure caused by naming conflict, and try to remove
  245. // the old container FOO.
  246. // See #40443. Sometimes even removal may fail with "no such container" error.
  247. // In that case we have to create the container with a randomized name.
  248. // TODO(random-liu): Remove this work around after docker 1.11 is deprecated.
  249. // TODO(#33189): Monitor the tests to see if the fix is sufficient.
  250. func recoverFromCreationConflictIfNeeded(client libdocker.Interface, createConfig dockertypes.ContainerCreateConfig, err error) (*dockercontainer.ContainerCreateCreatedBody, error) {
  251. matches := conflictRE.FindStringSubmatch(err.Error())
  252. if len(matches) != 2 {
  253. return nil, err
  254. }
  255. id := matches[1]
  256. klog.Warningf("Unable to create pod sandbox due to conflict. Attempting to remove sandbox %q", id)
  257. if rmErr := client.RemoveContainer(id, dockertypes.ContainerRemoveOptions{RemoveVolumes: true}); rmErr == nil {
  258. klog.V(2).Infof("Successfully removed conflicting container %q", id)
  259. return nil, err
  260. } else {
  261. klog.Errorf("Failed to remove the conflicting container %q: %v", id, rmErr)
  262. // Return if the error is not container not found error.
  263. if !libdocker.IsContainerNotFoundError(rmErr) {
  264. return nil, err
  265. }
  266. }
  267. // randomize the name to avoid conflict.
  268. createConfig.Name = randomizeName(createConfig.Name)
  269. klog.V(2).Infof("Create the container with randomized name %s", createConfig.Name)
  270. return client.CreateContainer(createConfig)
  271. }
  272. // transformStartContainerError does regex parsing on returned error
  273. // for where container runtimes are giving less than ideal error messages.
  274. func transformStartContainerError(err error) error {
  275. if err == nil {
  276. return nil
  277. }
  278. matches := startRE.FindStringSubmatch(err.Error())
  279. if len(matches) > 0 {
  280. return fmt.Errorf("executable not found in $PATH")
  281. }
  282. return err
  283. }
  284. // ensureSandboxImageExists pulls the sandbox image when it's not present.
  285. func ensureSandboxImageExists(client libdocker.Interface, image string) error {
  286. _, err := client.InspectImageByRef(image)
  287. if err == nil {
  288. return nil
  289. }
  290. if !libdocker.IsImageNotFoundError(err) {
  291. return fmt.Errorf("failed to inspect sandbox image %q: %v", image, err)
  292. }
  293. repoToPull, _, _, err := parsers.ParseImageName(image)
  294. if err != nil {
  295. return err
  296. }
  297. keyring := credentialprovider.NewDockerKeyring()
  298. creds, withCredentials := keyring.Lookup(repoToPull)
  299. if !withCredentials {
  300. klog.V(3).Infof("Pulling image %q without credentials", image)
  301. err := client.PullImage(image, dockertypes.AuthConfig{}, dockertypes.ImagePullOptions{})
  302. if err != nil {
  303. return fmt.Errorf("failed pulling image %q: %v", image, err)
  304. }
  305. return nil
  306. }
  307. var pullErrs []error
  308. for _, currentCreds := range creds {
  309. authConfig := dockertypes.AuthConfig(currentCreds)
  310. err := client.PullImage(image, authConfig, dockertypes.ImagePullOptions{})
  311. // If there was no error, return success
  312. if err == nil {
  313. return nil
  314. }
  315. pullErrs = append(pullErrs, err)
  316. }
  317. return utilerrors.NewAggregate(pullErrs)
  318. }
  319. func getAppArmorOpts(profile string) ([]dockerOpt, error) {
  320. if profile == "" || profile == apparmor.ProfileRuntimeDefault {
  321. // The docker applies the default profile by default.
  322. return nil, nil
  323. }
  324. // Return unconfined profile explicitly
  325. if profile == apparmor.ProfileNameUnconfined {
  326. return []dockerOpt{{"apparmor", apparmor.ProfileNameUnconfined, ""}}, nil
  327. }
  328. // Assume validation has already happened.
  329. profileName := strings.TrimPrefix(profile, apparmor.ProfileNamePrefix)
  330. return []dockerOpt{{"apparmor", profileName, ""}}, nil
  331. }
  332. // fmtDockerOpts formats the docker security options using the given separator.
  333. func fmtDockerOpts(opts []dockerOpt, sep rune) []string {
  334. fmtOpts := make([]string, len(opts))
  335. for i, opt := range opts {
  336. fmtOpts[i] = fmt.Sprintf("%s%c%s", opt.key, sep, opt.value)
  337. }
  338. return fmtOpts
  339. }
  340. type dockerOpt struct {
  341. // The key-value pair passed to docker.
  342. key, value string
  343. // The alternative value to use in log/event messages.
  344. msg string
  345. }
  346. // Expose key/value from dockerOpt.
  347. func (d dockerOpt) GetKV() (string, string) {
  348. return d.key, d.value
  349. }
  350. // sharedWriteLimiter limits the total output written across one or more streams.
  351. type sharedWriteLimiter struct {
  352. delegate io.Writer
  353. limit *int64
  354. }
  355. func (w sharedWriteLimiter) Write(p []byte) (int, error) {
  356. if len(p) == 0 {
  357. return 0, nil
  358. }
  359. limit := atomic.LoadInt64(w.limit)
  360. if limit <= 0 {
  361. return 0, errMaximumWrite
  362. }
  363. var truncated bool
  364. if limit < int64(len(p)) {
  365. p = p[0:limit]
  366. truncated = true
  367. }
  368. n, err := w.delegate.Write(p)
  369. if n > 0 {
  370. atomic.AddInt64(w.limit, -1*int64(n))
  371. }
  372. if err == nil && truncated {
  373. err = errMaximumWrite
  374. }
  375. return n, err
  376. }
  377. func sharedLimitWriter(w io.Writer, limit *int64) io.Writer {
  378. if w == nil {
  379. return nil
  380. }
  381. return &sharedWriteLimiter{
  382. delegate: w,
  383. limit: limit,
  384. }
  385. }
  386. var errMaximumWrite = errors.New("maximum write")