-
Notifications
You must be signed in to change notification settings - Fork 13
/
get.go
140 lines (113 loc) · 3.65 KB
/
get.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
// SPDX-FileCopyrightText: 2020 SAP SE or an SAP affiliate company and Gardener contributors.
//
// SPDX-License-Identifier: Apache-2.0
package blueprints
import (
"bytes"
"context"
"errors"
"fmt"
"os"
"path/filepath"
lsv1alpha1 "github.com/gardener/landscaper/apis/core"
"github.com/gardener/landscaper/pkg/components/model/tar"
"github.com/go-logr/logr"
"github.com/mandelsoft/vfs/pkg/memoryfs"
"github.com/mandelsoft/vfs/pkg/vfs"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"k8s.io/apimachinery/pkg/runtime/serializer"
"sigs.k8s.io/yaml"
"github.com/gardener/component-cli/ociclient"
"github.com/gardener/component-cli/ociclient/cache"
lsinstall "github.com/gardener/landscaper/apis/core/v1alpha1"
"github.com/gardener/landscaper/pkg/api"
"github.com/gardener/landscapercli/pkg/logger"
"github.com/gardener/landscapercli/cmd/constants"
)
type showOptions struct {
// ref is the oci reference where the definition should eb uploaded.
ref string
// allowPlainHttp allows the fallback to http if the oci registry does not support https
allowPlainHttp bool
// cacheDir defines the oci cache directory
cacheDir string
}
// NewGetCommand shows definitions and their configuration.
func NewGetCommand(ctx context.Context) *cobra.Command {
opts := &showOptions{}
cmd := &cobra.Command{
Use: "get OCI_ARTIFACT_REF",
Args: cobra.MinimumNArgs(1),
Example: "landscaper-cli blueprints get my-registry/my-repository:v1.0.0",
Short: "command to download a blueprint from an oci registry",
Long: "The get command downloads a Blueprint from an OCI registry. The reference to the OCI artifact " +
"consists of the base URL of the OCI registry, the repository (namespace), and the tag.",
Run: func(cmd *cobra.Command, args []string) {
if err := opts.Complete(args); err != nil {
fmt.Println(err.Error())
os.Exit(1)
}
if err := opts.run(ctx, logger.Log); err != nil {
fmt.Println(err.Error())
os.Exit(1)
}
},
}
opts.AddFlags(cmd.Flags())
return cmd
}
func (o *showOptions) run(ctx context.Context, log logr.Logger) error {
cache, err := cache.NewCache(log, cache.WithBasePath(o.cacheDir))
if err != nil {
return err
}
ociClient, err := ociclient.NewClient(log, ociclient.WithCache(cache), ociclient.AllowPlainHttp(o.allowPlainHttp))
if err != nil {
return err
}
manifest, err := ociClient.GetManifest(ctx, o.ref)
if err != nil {
return err
}
var data bytes.Buffer
if err := ociClient.Fetch(ctx, o.ref, manifest.Layers[0], &data); err != nil {
return err
}
memFS := memoryfs.New()
if err := tar.ExtractTarGzip(ctx, &data, memFS, tar.ToPath("/")); err != nil {
return err
}
defData, err := vfs.ReadFile(memFS, lsinstall.BlueprintFileName)
if err != nil {
return err
}
def := &lsv1alpha1.Blueprint{}
if _, _, err := serializer.NewCodecFactory(api.LandscaperScheme).UniversalDecoder().Decode(defData, nil, def); err != nil {
return err
}
out, err := yaml.Marshal(def)
if err != nil {
return err
}
fmt.Println(string(out))
return nil
}
func (o *showOptions) Complete(args []string) error {
o.ref = args[0]
landscaperCliHomeDir, err := constants.LandscaperCliHomeDir()
if err != nil {
return err
}
o.cacheDir = filepath.Join(landscaperCliHomeDir, "blueprints")
if err := os.MkdirAll(o.cacheDir, os.ModePerm); err != nil {
return fmt.Errorf("unable to create cache directory %s: %w", o.cacheDir, err)
}
if len(o.cacheDir) == 0 {
return errors.New("a oci cache directory must be defined")
}
return nil
}
func (o *showOptions) AddFlags(fs *pflag.FlagSet) {
fs.BoolVar(&o.allowPlainHttp, "allow-plain-http", false, "allows the fallback to http if the oci registry does not support https")
}