From 7174ee32f97c066f664ca0eed9c51d96d95217fc Mon Sep 17 00:00:00 2001 From: Ramkumar Chinchani <45800463+rchincha@users.noreply.github.com> Date: Sat, 25 Feb 2023 12:57:57 -0800 Subject: [PATCH] feat(publish): add ability to select specific layers to publish (#418) Currently, publish cmd will push every layer in a stacker.yaml file by default. This patch adds support to push only required layers. Signed-off-by: Ramkumar Chinchani --- cmd/stacker/publish.go | 5 +++++ pkg/stacker/publisher.go | 14 ++++++++++++++ test/publish.bats | 14 ++++++++++++++ 3 files changed, 33 insertions(+) diff --git a/cmd/stacker/publish.go b/cmd/stacker/publish.go index 3609cffc..0a0da3a3 100644 --- a/cmd/stacker/publish.go +++ b/cmd/stacker/publish.go @@ -69,6 +69,10 @@ var publishCmd = cli.Command{ Usage: "set the output layer type (supported values: tar, squashfs); can be supplied multiple times", Value: &cli.StringSlice{"tar"}, }, + cli.StringSliceFlag{ + Name: "layer", + Usage: "layer to be published; can be specified multiple times", + }, }, Before: beforePublish, } @@ -120,6 +124,7 @@ func doPublish(ctx *cli.Context) error { Progress: shouldShowProgress(ctx), SkipTLS: ctx.Bool("skip-tls"), LayerTypes: layerTypes, + Layers: ctx.StringSlice("layer"), } var stackerFiles []string diff --git a/pkg/stacker/publisher.go b/pkg/stacker/publisher.go index b7ee4762..8195c12b 100644 --- a/pkg/stacker/publisher.go +++ b/pkg/stacker/publisher.go @@ -30,6 +30,7 @@ type PublishArgs struct { Progress bool SkipTLS bool LayerTypes []types.LayerType + Layers []string } // Publisher is responsible for publishing the layers based on stackerfiles @@ -117,6 +118,19 @@ func (p *Publisher) Publish(file string) error { log.Infof("will not publish: %s build_only %s", file, name) continue } + if len(p.opts.Layers) > 0 { + found := false + for _, lname := range p.opts.Layers { + if lname == name { + found = true + break + } + } + + if !found { + continue + } + } // Verify layer is in build cache _, ok, err = buildCache.Lookup(name) diff --git a/test/publish.bats b/test/publish.bats index f9d83bc5..b2968215 100644 --- a/test/publish.bats +++ b/test/publish.bats @@ -116,6 +116,20 @@ function teardown() { [ -f dest/layer2_test1/rootfs/root/import1 ] } +@test "publish selected multiple layers" { + stacker recursive-build -d ocibuilds + stacker publish -d ocibuilds --url oci:oci_publish --tag test1 --layer layer1 --layer layer6 + + # Unpack published image and check content + umoci unpack --image oci_publish:layer1_test1 dest/layer1_test1 + [ -f dest/layer1_test1/rootfs/root/import1 ] + umoci unpack --image oci_publish:layer6_test1 dest/layer6_test1 + [ -f dest/layer6_test1/rootfs/root/ls_out ] + # since we did not publish this layer, shouldn't be found + run umoci unpack --image oci_publish:layer2_test1 dest/layer2_test1 + [ "$status" -ne 0 ] +} + @test "publish single layer with docker url" { stacker build -f ocibuilds/sub1/stacker.yaml stacker publish -f ocibuilds/sub1/stacker.yaml --url docker://docker-reg.fake.com/ --username user --password pass --tag test1 --show-only