-
Notifications
You must be signed in to change notification settings - Fork 4.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6985 from priyawadhwa/no-overwrite-tar
Merge repositories.json after extracting preloaded tarball so that reference store isn't lost
- Loading branch information
Showing
11 changed files
with
299 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
/* | ||
Copyright 2020 The Kubernetes Authors All rights reserved. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package docker | ||
|
||
import ( | ||
"encoding/json" | ||
"os/exec" | ||
"path" | ||
|
||
"github.com/golang/glog" | ||
"github.com/opencontainers/go-digest" | ||
"k8s.io/minikube/pkg/minikube/assets" | ||
"k8s.io/minikube/pkg/minikube/command" | ||
) | ||
|
||
const ( | ||
referenceStorePath = "/var/lib/docker/image/overlay2/repositories.json" | ||
) | ||
|
||
// Storage keeps track of reference stores | ||
type Storage struct { | ||
refStores []ReferenceStore | ||
runner command.Runner | ||
} | ||
|
||
// ReferenceStore stores references to images in repositories.json | ||
// used by the docker daemon to name images | ||
// taken from "github.com/docker/docker/reference/store.go" | ||
type ReferenceStore struct { | ||
Repositories map[string]repository | ||
} | ||
|
||
type repository map[string]digest.Digest | ||
|
||
// NewStorage returns a new storage type | ||
func NewStorage(runner command.Runner) *Storage { | ||
return &Storage{ | ||
runner: runner, | ||
} | ||
} | ||
|
||
// Save saves the current reference store in memory | ||
func (s *Storage) Save() error { | ||
// get the contents of repositories.json in minikube | ||
// if this command fails, assume the file doesn't exist | ||
rr, err := s.runner.RunCmd(exec.Command("sudo", "cat", referenceStorePath)) | ||
if err != nil { | ||
glog.Infof("repositories.json doesn't exist: %v", err) | ||
return nil | ||
} | ||
contents := rr.Stdout.Bytes() | ||
var rs ReferenceStore | ||
if err := json.Unmarshal(contents, &rs); err != nil { | ||
return err | ||
} | ||
s.refStores = append(s.refStores, rs) | ||
return nil | ||
} | ||
|
||
// Update merges all reference stores and updates repositories.json | ||
func (s *Storage) Update() error { | ||
// in case we didn't overwrite respoitories.json, do nothing | ||
if len(s.refStores) == 1 { | ||
return nil | ||
} | ||
// merge reference stores | ||
merged := s.mergeReferenceStores() | ||
|
||
// write to file in minikube | ||
contents, err := json.Marshal(merged) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
asset := assets.NewMemoryAsset(contents, path.Dir(referenceStorePath), path.Base(referenceStorePath), "0644") | ||
return s.runner.Copy(asset) | ||
} | ||
|
||
func (s *Storage) mergeReferenceStores() ReferenceStore { | ||
merged := ReferenceStore{ | ||
Repositories: map[string]repository{}, | ||
} | ||
// otherwise, merge together reference stores | ||
for _, rs := range s.refStores { | ||
for k, v := range rs.Repositories { | ||
merged.Repositories[k] = v | ||
} | ||
} | ||
return merged | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
/* | ||
Copyright 2020 The Kubernetes Authors All rights reserved. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package docker | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/google/go-cmp/cmp" | ||
) | ||
|
||
func TestMergeReferenceStores(t *testing.T) { | ||
initial := ReferenceStore{ | ||
Repositories: map[string]repository{ | ||
"image1": repository{ | ||
"r1": "d1", | ||
"r2": "d2", | ||
}, | ||
"image2": repository{ | ||
"r1": "d1", | ||
"r2": "d2", | ||
}, | ||
}, | ||
} | ||
|
||
afterPreload := ReferenceStore{ | ||
Repositories: map[string]repository{ | ||
"image1": repository{ | ||
"r1": "updated", | ||
"r2": "updated", | ||
}, | ||
"image3": repository{ | ||
"r3": "d3", | ||
}, | ||
}, | ||
} | ||
|
||
expected := ReferenceStore{ | ||
Repositories: map[string]repository{ | ||
"image1": repository{ | ||
"r1": "updated", | ||
"r2": "updated", | ||
}, | ||
"image2": repository{ | ||
"r1": "d1", | ||
"r2": "d2", | ||
}, | ||
"image3": repository{ | ||
"r3": "d3", | ||
}, | ||
}, | ||
} | ||
|
||
s := &Storage{ | ||
refStores: []ReferenceStore{initial, afterPreload}, | ||
} | ||
|
||
actual := s.mergeReferenceStores() | ||
if diff := cmp.Diff(actual, expected); diff != "" { | ||
t.Errorf("Actual: %v, Expected: %v, Diff: %s", actual, expected, diff) | ||
} | ||
} |
Oops, something went wrong.