Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add cache list support #2272

Merged
merged 5 commits into from
Dec 8, 2017
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 73 additions & 0 deletions cmd/minikube/cmd/cache_list.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
Copyright 2017 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 cmd

import (
"fmt"
"os"
"text/template"

"github.com/spf13/cobra"
cmdConfig "k8s.io/minikube/cmd/minikube/cmd/config"
"k8s.io/minikube/pkg/minikube/constants"
)

const cacheListFormat = "- {{.CacheImageName}}\n"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: do we need the "-" character here? It would make automatic parsing of stdout a little tricker (like if you wanted to use this in a bash for loop, etc.).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I ignored this usage scenario, I'll remove this character to bring about this results:

$ minikube cache add redis ubuntu
$ minikube cache delete $(minikube cache list)


type CacheListTemplate struct {
CacheImageName string
}

// listCacheCmd represents the cache list command
var listCacheCmd = &cobra.Command{
Use: "list",
Short: "List all available images from the local cache.",
Long: "List all available images from the local cache.",
Run: func(cmd *cobra.Command, args []string) {
// list images from config file
images, err := cmdConfig.ListConfigMap(constants.Cache)
if err != nil {
fmt.Fprintf(os.Stderr, "Error listing image entries from config: %s\n", err)
os.Exit(1)
}
if err := cacheList(images); err != nil {
fmt.Fprintf(os.Stderr, "Error listing images: %s\n", err)
os.Exit(1)
}
},
}

func init() {
cacheCmd.AddCommand(listCacheCmd)
}

func cacheList(images map[string]interface{}) error {
for imageName := range images {
tmpl, err := template.New("list").Parse(cacheListFormat)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you move this up out of the for loop? It looks like the format here is always a constant, so there's no need to do it in each iteration of the loop.

Copy link
Contributor Author

@kairen kairen Dec 8, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added a new flag to custom format for cache list, for future work, we could extend the CacheListTemplate to add more image detail, such as below:

var cacheListFormat  = "{{.CacheImageName}}:{{.CacheImageTag}}\n"

type CacheListTemplate struct {
	CacheImageName   string
        CacheImageTag    string
}

But we need to ensure minikube cache add <image> use latest tag to save in config.

This <Image> does not have a tag.

For example:

$ minikube cache add redis
$ cat .minikube/config/config.json
{
    "WantReportError": true,
    "WantReportErrorPrompt": false,
    "cache": {
        "redis:latest": null
    },
    "ingress": false
}

if err != nil {
fmt.Fprintf(os.Stderr, "Error creating list template: %s\n", err)
os.Exit(1)
}
listTmplt := CacheListTemplate{imageName}
err = tmpl.Execute(os.Stdout, listTmplt)
if err != nil {
fmt.Fprintf(os.Stderr, "Error executing list template: %s\n", err)
os.Exit(1)
}
}
return nil
}
15 changes: 15 additions & 0 deletions cmd/minikube/cmd/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,21 @@ func configurableFields() string {
return strings.Join(fields, "\n")
}

// ListConfigMap list entries from config file
func ListConfigMap(name string) (map[string]interface{}, error) {
configFile, err := config.ReadConfig()
if err != nil {
return nil, err
}
newImages := make(map[string]interface{})
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would a "slice" of images work instead of a map? It looks like you only set "nil" as the value.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No problem, I'll do it!

if values, ok := configFile[name].(map[string]interface{}); ok {
for key := range values {
newImages[key] = nil
}
}
return newImages, nil
}

// AddToConfigMap adds entries to a map in the config file
func AddToConfigMap(name string, images []string) error {
s, err := findSetting(name)
Expand Down
13 changes: 12 additions & 1 deletion docs/cache.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,15 @@

Minikube supports caching non-minikube images using the `minikube cache` command. Images can be added to the cache by running `minikube cache add <img>`, and deleted by running `minikube cache delete <img>`.

Images in the cache will be loaded on `minikube start`.
Images in the cache will be loaded on `minikube start`. If you want to list all available cached images, you can use `minikube cache list` command to list. Below is an example of this functionality:

```shell
# cache a image into $HOME/.minikube/cache/images
$ minikube cache add ubuntu:16.04
$ minikube cache add redis:3

# list cached images
$ minikube cache list
- redis:3
- ubuntu:16.04
```