Skip to content

Commit cfed412

Browse files
committed
extend package inspection a bit more, some more registry tweaks (still not a very safe option)
1 parent 3579450 commit cfed412

File tree

8 files changed

+101
-54
lines changed

8 files changed

+101
-54
lines changed

pkg/build/builder.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,9 +190,11 @@ func (b *builder) bundleImages(opts *types.BuildOptions, parser types.ManifestPa
190190
log.Info("Building private image registry to bundle with the package")
191191
artifacts, err := images.NewImageDownloader().BuildRegistry(&types.BuildRegistryOptions{
192192
Name: opts.Name,
193+
AppVersion: opts.BuildVersion,
193194
Arch: opts.Arch,
194195
Images: imageNames,
195196
PullPolicy: opts.PullPolicy,
197+
// TODO: Make more configurable
196198
})
197199
if err != nil {
198200
return err

pkg/cmd/inspect.go

Lines changed: 46 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -21,41 +21,58 @@ import (
2121

2222
var inspectDetails bool
2323
var inspectManifest string
24+
var inspectConfig string
2425

2526
func init() {
2627
inspectCmd.Flags().BoolVarP(&inspectDetails, "details", "D", false, "Show additional details on package content")
2728
inspectCmd.Flags().StringVarP(&inspectManifest, "manifest", "m", "", "Dump the contents of the specified manifest")
29+
inspectCmd.Flags().StringVarP(&inspectConfig, "config", "c", "", "Dump the contents of the specified config file")
2830

2931
inspectCmd.RegisterFlagCompletionFunc("manifest", completeManifests)
32+
inspectCmd.RegisterFlagCompletionFunc("config", completeConfigs)
3033

3134
rootCmd.AddCommand(inspectCmd)
3235
}
3336

3437
func completeManifests(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
3538
log.Verbose = false
39+
pkg, err := getInspectPackage(args[0])
40+
if err != nil {
41+
return nil, cobra.ShellCompDirectiveError
42+
}
43+
defer pkg.Close()
44+
manifest := pkg.GetMeta().GetManifest()
45+
return manifest.K8sManifests, cobra.ShellCompDirectiveDefault
46+
}
47+
48+
func completeConfigs(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
49+
log.Verbose = false
50+
pkg, err := getInspectPackage(args[0])
51+
if err != nil {
52+
return nil, cobra.ShellCompDirectiveError
53+
}
54+
defer pkg.Close()
55+
manifest := pkg.GetMeta().GetManifest()
56+
return manifest.Etc, cobra.ShellCompDirectiveDefault
57+
}
3658

59+
func getInspectPackage(path string) (types.Package, error) {
3760
var pkgReader io.ReadCloser
3861
var err error
3962

40-
pkgReader, err = os.Open(args[0])
63+
pkgReader, err = os.Open(path)
4164
if err != nil {
42-
return nil, cobra.ShellCompDirectiveError
65+
return nil, err
4366
}
4467

45-
if strings.HasSuffix(args[0], ".zst") {
68+
if strings.HasSuffix(path, ".zst") {
4669
pkgReader, err = v1.Decompress(pkgReader)
4770
if err != nil {
48-
return nil, cobra.ShellCompDirectiveError
71+
return nil, err
4972
}
5073
}
5174

52-
pkg, err := v1.Load(pkgReader)
53-
if err != nil {
54-
return nil, cobra.ShellCompDirectiveError
55-
}
56-
defer pkg.Close()
57-
manifest := pkg.GetMeta().GetManifest()
58-
return manifest.K8sManifests, cobra.ShellCompDirectiveDefault
75+
return v1.Load(pkgReader)
5976
}
6077

6178
var inspectCmd = &cobra.Command{
@@ -66,22 +83,7 @@ var inspectCmd = &cobra.Command{
6683
return []string{"tar"}, cobra.ShellCompDirectiveFilterFileExt
6784
},
6885
RunE: func(cmd *cobra.Command, args []string) error {
69-
var pkgReader io.ReadCloser
70-
var err error
71-
72-
pkgReader, err = os.Open(args[0])
73-
if err != nil {
74-
return err
75-
}
76-
77-
if strings.HasSuffix(args[0], ".zst") {
78-
pkgReader, err = v1.Decompress(pkgReader)
79-
if err != nil {
80-
return err
81-
}
82-
}
83-
84-
pkg, err := v1.Load(pkgReader)
86+
pkg, err := getInspectPackage(args[0])
8587
if err != nil {
8688
return err
8789
}
@@ -106,6 +108,23 @@ var inspectCmd = &cobra.Command{
106108
return nil
107109
}
108110

111+
if inspectConfig != "" {
112+
artifact := &types.Artifact{
113+
Type: types.ArtifactEtc,
114+
Name: inspectConfig,
115+
}
116+
if err := pkg.Get(artifact); err != nil {
117+
return err
118+
}
119+
defer artifact.Body.Close()
120+
body, err := ioutil.ReadAll(artifact.Body)
121+
if err != nil {
122+
return err
123+
}
124+
fmt.Println(string(body))
125+
return nil
126+
}
127+
109128
fmt.Println()
110129
fmt.Println("NAME: ", meta.Name)
111130
fmt.Println("VERSION:", meta.Version)

pkg/cmd/uninstall.go

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,7 @@ import (
77
"github.com/tinyzimmer/k3p/pkg/log"
88
)
99

10-
var (
11-
uninstallName string
12-
)
13-
1410
func init() {
15-
uninstallCmd.Flags().StringVarP(&uninstallName, "name", "n", "", "The name of the package to uninstall (required for docker)")
16-
uninstallCmd.MarkFlagRequired("name")
17-
uninstallCmd.RegisterFlagCompletionFunc("name", completeClusters)
18-
1911
rootCmd.AddCommand(uninstallCmd)
2012
}
2113

@@ -29,9 +21,12 @@ func completeClusters(cmd *cobra.Command, args []string, toComplete string) ([]s
2921
}
3022

3123
var uninstallCmd = &cobra.Command{
32-
Use: "uninstall",
33-
Short: "Uninstall a k3p package (currently only for docker)",
24+
Use: "uninstall",
25+
Short: "Uninstall a k3p package (currently only for docker)",
26+
Args: cobra.ExactArgs(1),
27+
ValidArgsFunction: completeClusters,
3428
RunE: func(cmd *cobra.Command, args []string) error {
29+
uninstallName := args[0]
3530
nodes, err := node.LoadDockerCluster(uninstallName)
3631
if err != nil {
3732
return err

pkg/images/build_registry.go

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,36 @@ const kubenabImage = "docker.bintray.io/kubenab:0.3.4"
2121

2222
var requiredRegistryImages = []string{"registry:2", "busybox", kubenabImage}
2323

24+
func setOptDefaults(opts *types.BuildRegistryOptions) *types.BuildRegistryOptions {
25+
if opts.RegistrySecret == "" {
26+
opts.RegistrySecret = util.GenerateToken(16)
27+
}
28+
29+
if opts.AppVersion == "" {
30+
opts.AppVersion = types.VersionLatest
31+
}
32+
33+
if opts.RegistryNodePort == "" {
34+
opts.RegistryNodePort = "30100"
35+
}
36+
37+
if opts.PullPolicy == "" {
38+
opts.PullPolicy = types.PullPolicyAlways
39+
}
40+
41+
return opts
42+
}
43+
2444
func (d *dockerImageDownloader) BuildRegistry(opts *types.BuildRegistryOptions) ([]*types.Artifact, error) {
45+
opts = setOptDefaults(opts)
46+
2547
cli, err := getDockerClient()
2648
if err != nil {
2749
return nil, err
2850
}
2951
defer cli.Close()
3052

31-
// Generate a secret if one is not provided
32-
if opts.RegistrySecret == "" {
33-
opts.RegistrySecret = util.GenerateToken(16)
34-
}
53+
regDataImgName := fmt.Sprintf("%s-private-registry-data:%s", opts.Name, opts.AppVersion)
3554

3655
// Generate certificates for the registry
3756
// TODO: Allow user to supply certificates
@@ -78,23 +97,26 @@ func (d *dockerImageDownloader) BuildRegistry(opts *types.BuildRegistryOptions)
7897
"TLSCACertificate": string(caCertPem),
7998
"RegistryAuthHtpasswd": string(htpasswd),
8099
"KubenabImage": kubenabImage,
100+
"RegistryDataImage": regDataImgName,
101+
"RegistryNodePort": opts.RegistryNodePort,
81102
})
82103
if err != nil {
83104
return nil, err
84105
}
85106
body := buf.Bytes()
86107
deploymentManifest := &types.Artifact{
87108
Type: types.ArtifactManifest,
88-
Name: "private-registry-deployment.yaml",
109+
Name: fmt.Sprintf("%s-private-registry-deployment.yaml", opts.Name),
89110
Body: ioutil.NopCloser(bytes.NewReader(body)),
90111
Size: int64(len(body)),
91112
}
92113

93114
// Generate a registries.yaml
94115
var yamlBuf bytes.Buffer
95116
err = registriesYamlTmpl.Execute(&yamlBuf, map[string]string{
96-
"Username": "registry",
97-
"Password": opts.RegistrySecret,
117+
"Username": "registry",
118+
"Password": opts.RegistrySecret,
119+
"RegistryNodePort": opts.RegistryNodePort,
98120
})
99121
if err != nil {
100122
return nil, err
@@ -206,13 +228,13 @@ func (d *dockerImageDownloader) BuildRegistry(opts *types.BuildRegistryOptions)
206228
}
207229

208230
// Commit the registry volume container to an image
209-
_, err = cli.ContainerCommit(context.TODO(), volContainerID, dockertypes.ContainerCommitOptions{Reference: "private-registry-data:latest"})
231+
_, err = cli.ContainerCommit(context.TODO(), volContainerID, dockertypes.ContainerCommitOptions{Reference: regDataImgName})
210232
if err != nil {
211233
return nil, err
212234
}
213235

214236
// Save all images for the registry
215-
rdr, err := cli.ImageSave(context.TODO(), append(requiredRegistryImages, "private-registry-data:latest"))
237+
rdr, err := cli.ImageSave(context.TODO(), append(requiredRegistryImages, regDataImgName))
216238
if err != nil {
217239
return nil, err
218240
}
@@ -223,5 +245,5 @@ func (d *dockerImageDownloader) BuildRegistry(opts *types.BuildRegistryOptions)
223245
return nil, err
224246
}
225247

226-
return []*types.Artifact{caCertificate, registriesYamlArtifact, deploymentManifest, registryArtifact}, err
248+
return []*types.Artifact{caCertificate, registriesYamlArtifact, deploymentManifest, registryArtifact}, nil
227249
}

pkg/images/registry_templates.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@ var registriesYamlTmpl = template.Must(template.New("").Funcs(sprig.TxtFuncMap()
1010
mirrors:
1111
registry.private:
1212
endpoint:
13-
- https://localhost:30100
13+
- https://localhost:{{ .RegistryNodePort }}
1414
1515
configs:
16-
"localhost:30100":
16+
"localhost:{{ .RegistryNodePort }}":
1717
auth:
1818
username: {{ .Username }}
1919
password: {{ .Password }}
@@ -88,7 +88,7 @@ spec:
8888
secretName: registry-htpasswd
8989
initContainers:
9090
- name: data-extractor
91-
image: private-registry-data:latest
91+
image: {{ .RegistryDataImage }}
9292
imagePullPolicy: Never
9393
command: ['tar', '-xvz', '--file=/var/registry-data.tgz', '--directory=/var/lib/registry']
9494
volumeMounts:
@@ -178,7 +178,7 @@ spec:
178178
- port: 5000
179179
protocol: TCP
180180
targetPort: 5000
181-
nodePort: 30100
181+
nodePort: {{ .RegistryNodePort }}
182182
---
183183
apiVersion: v1
184184
kind: Service

pkg/images/util.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ func waitForLocalRegistry(port string, timeout time.Duration) error {
260260
continue
261261
}
262262
if res.StatusCode != http.StatusOK {
263-
log.Debug("Non-200 status code from registry catalog, will retry:", err)
263+
log.Debug("Non-200 status code from registry catalog, will retry:", res.StatusCode)
264264
continue
265265
}
266266
log.Debug("Local registry is ready")

pkg/types/registry_options.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ package types
55
type BuildRegistryOptions struct {
66
// A name to use when generating identifiers for various resources
77
Name string
8+
// The version of the application this registry is being built for.
9+
// Defaults to latest.
10+
AppVersion string
811
// A list of images to bundle in the registry
912
Images []string
1013
// Architecture to build the registry for
@@ -14,4 +17,7 @@ type BuildRegistryOptions struct {
1417
// The password to use for authentication to the registry, if this is blank one will
1518
// be generated.
1619
RegistrySecret string
20+
// The node port that the private registry will listen on when installed. Defaults to
21+
// 30100.
22+
RegistryNodePort string
1723
}

pkg/util/util.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,11 @@ var TempDir = os.TempDir()
3232
// the system default, or user-configured path.
3333
func GetTempDir() (string, error) { return ioutil.TempDir(TempDir, "") }
3434

35-
// GetRandomName returns a random name using the docker name generator
36-
func GetRandomName() string { return namesgenerator.GetRandomName(0) }
35+
// GetRandomName returns a random name using the docker name generator. Underscores are converted
36+
// to hyphens for parity with most other strings.
37+
func GetRandomName() string {
38+
return strings.Replace(namesgenerator.GetRandomName(0), "_", "-", -1)
39+
}
3740

3841
// CalculateSHA256Sum calculates the sha256sum of the contents of the given reader.
3942
func CalculateSHA256Sum(rdr io.Reader) (string, error) {

0 commit comments

Comments
 (0)