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 "kops get assets" command #11617

Merged
merged 6 commits into from
May 30, 2021
Merged
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions cmd/kops/BUILD.bazel

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions cmd/kops/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ func NewCmdGet(f *util.Factory, out io.Writer) *cobra.Command {
cmd.PersistentFlags().StringVarP(&options.output, "output", "o", options.output, "output format. One of: table, yaml, json")

// create subcommands
cmd.AddCommand(NewCmdGetAssets(f, out, options))
cmd.AddCommand(NewCmdGetCluster(f, out, options))
cmd.AddCommand(NewCmdGetInstanceGroups(f, out, options))
cmd.AddCommand(NewCmdGetSecrets(f, out, options))
Expand Down
191 changes: 191 additions & 0 deletions cmd/kops/get_assets.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,191 @@
/*
Copyright 2020 The Kubernetes Authors.

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 main

import (
"context"
"encoding/json"
"fmt"
"io"

"k8s.io/kubectl/pkg/util/i18n"
"k8s.io/kubectl/pkg/util/templates"
"sigs.k8s.io/yaml"

"k8s.io/kops/util/pkg/tables"

"github.com/spf13/cobra"
"k8s.io/kops/cmd/kops/util"
"k8s.io/kops/upup/pkg/fi/cloudup"
)

type Image struct {
Canonical string `json:"canonical"`
Download string `json:"download"`
}

type File struct {
Canonical string `json:"canonical"`
Download string `json:"download"`
SHA string `json:"sha"`
}

type AssetResult struct {
// Images are the image assets we use (output).
Images []*Image `json:"images,omitempty"`
// FileAssets are the file assets we use (output).
Files []*File `json:"files,omitempty"`
}

func NewCmdGetAssets(f *util.Factory, out io.Writer, options *GetOptions) *cobra.Command {
getAssetsShort := i18n.T(`Display assets for cluster.`)

getAssetsLong := templates.LongDesc(i18n.T(`
Display assets for cluster.`))

getAssetsExample := templates.Examples(i18n.T(`
# Display all assets.
kops get assets
`))

cmd := &cobra.Command{
Use: "assets",
Short: getAssetsShort,
Long: getAssetsLong,
Example: getAssetsExample,
Run: func(cmd *cobra.Command, args []string) {
ctx := context.TODO()

if err := rootCommand.ProcessArgs(args); err != nil {
exitWithError(err)
}

err := RunGetAssets(ctx, f, out, options)
if err != nil {
exitWithError(err)
}
},
}

return cmd
}

func RunGetAssets(ctx context.Context, f *util.Factory, out io.Writer, options *GetOptions) error {

clusterName := rootCommand.ClusterName()
options.clusterName = clusterName
if clusterName == "" {
return fmt.Errorf("--name is required")
}

updateClusterResults, err := RunUpdateCluster(ctx, f, clusterName, out, &UpdateClusterOptions{
Target: cloudup.TargetDryRun,
Phase: string(cloudup.PhaseStageAssets),
Quiet: true,
})
if err != nil {
return err
}

result := AssetResult{
Images: make([]*Image, 0, len(updateClusterResults.ImageAssets)),
Files: make([]*File, 0, len(updateClusterResults.FileAssets)),
}

seen := map[string]bool{}
for _, imageAsset := range updateClusterResults.ImageAssets {
image := Image{
Canonical: imageAsset.CanonicalLocation,
Download: imageAsset.DownloadLocation,
}
if !seen[image.Canonical] {
result.Images = append(result.Images, &image)
seen[image.Canonical] = true
}
}
seen = map[string]bool{}
for _, fileAsset := range updateClusterResults.FileAssets {
file := File{
Canonical: fileAsset.CanonicalURL.String(),
Download: fileAsset.DownloadURL.String(),
SHA: fileAsset.SHAValue,
}
if !seen[file.Canonical] {
result.Files = append(result.Files, &file)
seen[file.Canonical] = true
}
}

switch options.output {
case OutputTable:
if err = imageOutputTable(result.Images, out); err != nil {
return err
}
return fileOutputTable(result.Files, out)
case OutputYaml:
y, err := yaml.Marshal(result)
if err != nil {
return fmt.Errorf("unable to marshal YAML: %v", err)
}
if _, err := out.Write(y); err != nil {
return fmt.Errorf("error writing to output: %v", err)
}
case OutputJSON:
j, err := json.Marshal(result)
if err != nil {
return fmt.Errorf("unable to marshal JSON: %v", err)
}
if _, err := out.Write(j); err != nil {
return fmt.Errorf("error writing to output: %v", err)
}
default:
return fmt.Errorf("unsupported output format: %q", options.output)
}

return nil
}

func imageOutputTable(images []*Image, out io.Writer) error {
fmt.Println("")
t := &tables.Table{}
t.AddColumn("CANONICAL", func(i *Image) string {
return i.Canonical
})
t.AddColumn("DOWNLOAD", func(i *Image) string {
return i.Download
})

columns := []string{"CANONICAL", "DOWNLOAD"}
return t.Render(images, out, columns...)
}

func fileOutputTable(files []*File, out io.Writer) error {
fmt.Println("")
t := &tables.Table{}
t.AddColumn("CANONICAL", func(f *File) string {
return f.Canonical
})
t.AddColumn("DOWNLOAD", func(f *File) string {
return f.Download
})
t.AddColumn("SHA", func(f *File) string {
return f.SHA
})

columns := []string{"CANONICAL", "DOWNLOAD", "SHA"}
return t.Render(files, out, columns...)
}
12 changes: 11 additions & 1 deletion cmd/kops/update_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import (
"k8s.io/klog/v2"
"k8s.io/kops/cmd/kops/util"
"k8s.io/kops/pkg/apis/kops"
"k8s.io/kops/pkg/assets"
"k8s.io/kops/pkg/kubeconfig"
"k8s.io/kops/upup/pkg/fi"
"k8s.io/kops/upup/pkg/fi/cloudup"
Expand Down Expand Up @@ -65,6 +66,7 @@ type UpdateClusterOptions struct {
SSHPublicKey string
RunTasksOptions fi.RunTasksOptions
AllowKopsDowngrade bool
Quiet bool

CreateKubecfg bool
admin time.Duration
Expand Down Expand Up @@ -139,6 +141,11 @@ type UpdateClusterResults struct {

// TaskMap is the map of tasks that we built (output)
TaskMap map[string]fi.Task

// ImageAssets are the image assets we use (output).
ImageAssets []*assets.ImageAsset
// FileAssets are the file assets we use (output).
FileAssets []*assets.FileAsset
}

func RunUpdateCluster(ctx context.Context, f *util.Factory, clusterName string, out io.Writer, c *UpdateClusterOptions) (*UpdateClusterResults, error) {
Expand Down Expand Up @@ -280,6 +287,7 @@ func RunUpdateCluster(ctx context.Context, f *util.Factory, clusterName string,
Phase: phase,
TargetName: targetName,
LifecycleOverrides: lifecycleOverrideMap,
Quiet: c.Quiet,
}

if err := applyCmd.Run(ctx); err != nil {
Expand All @@ -288,8 +296,10 @@ func RunUpdateCluster(ctx context.Context, f *util.Factory, clusterName string,

results.Target = applyCmd.Target
results.TaskMap = applyCmd.TaskMap
results.ImageAssets = applyCmd.ImageAssets
results.FileAssets = applyCmd.FileAssets

if isDryrun {
if isDryrun && !c.Quiet {
target := applyCmd.Target.(*fi.DryRunTarget)
if target.HasChanges() {
fmt.Fprintf(out, "Must specify --yes to apply changes\n")
Expand Down
1 change: 1 addition & 0 deletions docs/cli/kops_get.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

54 changes: 54 additions & 0 deletions docs/cli/kops_get_assets.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading