|
| 1 | +/* |
| 2 | +Copyright 2020 The Kubernetes Authors All rights reserved. |
| 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 | + http://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 | + |
| 17 | +package docker |
| 18 | + |
| 19 | +import ( |
| 20 | + "encoding/json" |
| 21 | + "os/exec" |
| 22 | + "path" |
| 23 | + |
| 24 | + "github.com/golang/glog" |
| 25 | + "github.com/opencontainers/go-digest" |
| 26 | + "k8s.io/minikube/pkg/minikube/assets" |
| 27 | + "k8s.io/minikube/pkg/minikube/command" |
| 28 | +) |
| 29 | + |
| 30 | +const ( |
| 31 | + referenceStorePath = "/var/lib/docker/image/overlay2/repositories.json" |
| 32 | +) |
| 33 | + |
| 34 | +// Storage keeps track of reference stores |
| 35 | +type Storage struct { |
| 36 | + refStores []ReferenceStore |
| 37 | + runner command.Runner |
| 38 | +} |
| 39 | + |
| 40 | +// ReferenceStore stores references to images in repositories.json |
| 41 | +// used by the docker daemon to name images |
| 42 | +// taken from "github.com/docker/docker/reference/store.go" |
| 43 | +type ReferenceStore struct { |
| 44 | + Repositories map[string]repository |
| 45 | +} |
| 46 | + |
| 47 | +type repository map[string]digest.Digest |
| 48 | + |
| 49 | +// NewStorage returns a new storage type |
| 50 | +func NewStorage(runner command.Runner) *Storage { |
| 51 | + return &Storage{ |
| 52 | + runner: runner, |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +// Save saves the current reference store in memory |
| 57 | +func (s *Storage) Save() error { |
| 58 | + // get the contents of repositories.json in minikube |
| 59 | + // if this command fails, assume the file doesn't exist |
| 60 | + rr, err := s.runner.RunCmd(exec.Command("sudo", "cat", referenceStorePath)) |
| 61 | + if err != nil { |
| 62 | + glog.Infof("repositories.json doesn't exist: %v", err) |
| 63 | + return nil |
| 64 | + } |
| 65 | + contents := rr.Stdout.Bytes() |
| 66 | + var rs ReferenceStore |
| 67 | + if err := json.Unmarshal(contents, &rs); err != nil { |
| 68 | + return err |
| 69 | + } |
| 70 | + s.refStores = append(s.refStores, rs) |
| 71 | + return nil |
| 72 | +} |
| 73 | + |
| 74 | +// Update merges all reference stores and updates repositories.json |
| 75 | +func (s *Storage) Update() error { |
| 76 | + // in case we didn't overwrite respoitories.json, do nothing |
| 77 | + if len(s.refStores) == 1 { |
| 78 | + return nil |
| 79 | + } |
| 80 | + // merge reference stores |
| 81 | + merged := s.mergeReferenceStores() |
| 82 | + |
| 83 | + // write to file in minikube |
| 84 | + contents, err := json.Marshal(merged) |
| 85 | + if err != nil { |
| 86 | + return err |
| 87 | + } |
| 88 | + |
| 89 | + asset := assets.NewMemoryAsset(contents, path.Dir(referenceStorePath), path.Base(referenceStorePath), "0644") |
| 90 | + return s.runner.Copy(asset) |
| 91 | +} |
| 92 | + |
| 93 | +func (s *Storage) mergeReferenceStores() ReferenceStore { |
| 94 | + merged := ReferenceStore{ |
| 95 | + Repositories: map[string]repository{}, |
| 96 | + } |
| 97 | + // otherwise, merge together reference stores |
| 98 | + for _, rs := range s.refStores { |
| 99 | + for k, v := range rs.Repositories { |
| 100 | + merged.Repositories[k] = v |
| 101 | + } |
| 102 | + } |
| 103 | + return merged |
| 104 | +} |
0 commit comments