-
Notifications
You must be signed in to change notification settings - Fork 181
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Xiaoxuan Wang <[email protected]>
- Loading branch information
Xiaoxuan Wang
committed
Aug 20, 2024
1 parent
86c1e97
commit ef5dacd
Showing
4 changed files
with
269 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,6 +25,7 @@ func Cmd() *cobra.Command { | |
|
||
cmd.AddCommand( | ||
createCmd(), | ||
updateCmd(), | ||
) | ||
return cmd | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,197 @@ | ||
/* | ||
Copyright The ORAS 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 index | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/opencontainers/go-digest" | ||
ocispec "github.com/opencontainers/image-spec/specs-go/v1" | ||
"github.com/spf13/cobra" | ||
"oras.land/oras-go/v2" | ||
"oras.land/oras/cmd/oras/internal/argument" | ||
"oras.land/oras/cmd/oras/internal/command" | ||
oerrors "oras.land/oras/cmd/oras/internal/errors" | ||
"oras.land/oras/cmd/oras/internal/option" | ||
"oras.land/oras/internal/descriptor" | ||
) | ||
|
||
type updateOptions struct { | ||
option.Common | ||
option.Target | ||
|
||
extraRefs []string | ||
addArguments []string | ||
mergeArguments []string | ||
removeArguments []string | ||
} | ||
|
||
func updateCmd() *cobra.Command { | ||
var opts updateOptions | ||
cmd := &cobra.Command{ | ||
Use: "update <name>{:<tag>|@<digest>} {--add/--merge/--remove} {<tag>|<digest>} [...]", | ||
Short: "[Experimental] Update and push an image index", | ||
Long: `[Experimental] Update and push an image index. All manifests should be in the same repository | ||
Example - add one manifest and remove two manifests from an index tagged 'latest': | ||
oras manifest index update localhost:5000/hello:latest --add sha256:xxx --remove sha256:xxx | ||
Example - remove a manifest and merge manifests from indexes tagged as 'index01' and 'index02': | ||
oras manifest index update localhost:5000/hello:latest --remove sha256:xxx --merge index01 --merge index02 | ||
`, | ||
Args: oerrors.CheckArgs(argument.AtLeast(1), "the destination index to update"), | ||
PreRunE: func(cmd *cobra.Command, args []string) error { | ||
refs := strings.Split(args[0], ",") | ||
opts.RawReference = refs[0] | ||
opts.extraRefs = refs[1:] | ||
if err := option.Parse(cmd, &opts); err != nil { | ||
return err | ||
} | ||
// if a digest is given as the index reference, we need to ignore it to successfully push | ||
opts.RawReference, _, _ = strings.Cut(opts.RawReference, "@") | ||
return nil | ||
}, | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
return updateIndex(cmd, opts) | ||
}, | ||
} | ||
option.ApplyFlags(&opts, cmd.Flags()) | ||
cmd.Flags().StringArrayVarP(&opts.addArguments, "add", "", nil, "add manifests to the index") | ||
cmd.Flags().StringArrayVarP(&opts.mergeArguments, "merge", "", nil, "merge the manifests of another index") | ||
cmd.Flags().StringArrayVarP(&opts.removeArguments, "remove", "", nil, "manifests to remove from the index") | ||
return oerrors.Command(cmd, &opts.Target) | ||
} | ||
|
||
func updateIndex(cmd *cobra.Command, opts updateOptions) error { | ||
// if no update flag is used, do nothing | ||
if !cmd.Flags().Changed("add") && !cmd.Flags().Changed("remove") && !cmd.Flags().Changed("merge") { | ||
opts.Println("No update flag is used. There's nothing to update.") | ||
return nil | ||
} | ||
ctx, logger := command.GetLogger(cmd, &opts.Common) | ||
target, err := opts.NewTarget(opts.Common, logger) | ||
if err != nil { | ||
return err | ||
} | ||
if err := opts.EnsureReferenceNotEmpty(cmd, true); err != nil { | ||
return err | ||
} | ||
index, err := fetchIndex(ctx, target, opts) | ||
if err != nil { | ||
return err | ||
} | ||
manifests, err := addManifests(ctx, index.Manifests, target, opts) | ||
if err != nil { | ||
return err | ||
} | ||
manifests, err = mergeIndexes(ctx, manifests, target, opts) | ||
if err != nil { | ||
return err | ||
} | ||
manifests, err = removeManifests(ctx, manifests, target, opts) | ||
if err != nil { | ||
return err | ||
} | ||
desc, content, err := packIndex(&index, manifests) | ||
if err != nil { | ||
return err | ||
} | ||
opts.Println("Updated the index") | ||
return pushIndex(ctx, target, desc, content, opts.Reference, opts.extraRefs, opts.AnnotatedReference(), opts.Printer) | ||
} | ||
|
||
func fetchIndex(ctx context.Context, target oras.ReadOnlyTarget, opts updateOptions) (ocispec.Index, error) { | ||
_, content, err := oras.FetchBytes(ctx, target, opts.Reference, oras.DefaultFetchBytesOptions) | ||
if err != nil { | ||
return ocispec.Index{}, fmt.Errorf("could not find the index %s: %w", opts.Reference, err) | ||
} | ||
opts.Println("Resolved manifest", opts.Reference) | ||
var index ocispec.Index | ||
if err := json.Unmarshal(content, &index); err != nil { | ||
return ocispec.Index{}, err | ||
} | ||
return index, nil | ||
} | ||
|
||
func addManifests(ctx context.Context, manifests []ocispec.Descriptor, target oras.ReadOnlyTarget, opts updateOptions) ([]ocispec.Descriptor, error) { | ||
for _, manifest := range opts.addArguments { | ||
desc, content, err := oras.FetchBytes(ctx, target, manifest, oras.DefaultFetchBytesOptions) | ||
if err != nil { | ||
return nil, fmt.Errorf("could not find the manifest %s: %w", manifest, err) | ||
} | ||
opts.Println("Resolved manifest", manifest) | ||
if descriptor.IsImageManifest(desc) { | ||
desc.Platform, err = getPlatform(ctx, target, content) | ||
if err != nil { | ||
return nil, err | ||
} | ||
} | ||
manifests = append(manifests, desc) | ||
} | ||
return manifests, nil | ||
} | ||
|
||
func mergeIndexes(ctx context.Context, manifests []ocispec.Descriptor, target oras.ReadOnlyTarget, opts updateOptions) ([]ocispec.Descriptor, error) { | ||
for _, index := range opts.mergeArguments { | ||
desc, content, err := oras.FetchBytes(ctx, target, index, oras.DefaultFetchBytesOptions) | ||
if err != nil { | ||
return nil, fmt.Errorf("could not find the index %s: %w", index, err) | ||
} | ||
if desc.MediaType != ocispec.MediaTypeImageIndex { | ||
return nil, fmt.Errorf("%s is not an image index", index) | ||
} | ||
opts.Println("Resolved index", index) | ||
var index ocispec.Index | ||
if err := json.Unmarshal(content, &index); err != nil { | ||
return nil, err | ||
} | ||
manifests = append(manifests, index.Manifests...) | ||
} | ||
return manifests, nil | ||
} | ||
|
||
func removeManifests(ctx context.Context, manifests []ocispec.Descriptor, target oras.ReadOnlyTarget, opts updateOptions) ([]ocispec.Descriptor, error) { | ||
set := make(map[digest.Digest]int) | ||
for _, manifest := range opts.removeArguments { | ||
desc, _, err := oras.FetchBytes(ctx, target, manifest, oras.DefaultFetchBytesOptions) | ||
if err != nil { | ||
return nil, fmt.Errorf("could not find the manifest %s: %w", manifest, err) | ||
} | ||
set[desc.Digest] = set[desc.Digest] + 1 | ||
} | ||
pointer := len(manifests) - 1 | ||
for i := len(manifests) - 1; i >= 0; i-- { | ||
if _, exists := set[manifests[i].Digest]; exists { | ||
val := manifests[i] | ||
// move the item to the end of the slice | ||
for j := i; j < pointer; j++ { | ||
manifests[j] = manifests[j+1] | ||
} | ||
manifests[pointer] = val | ||
pointer = pointer - 1 | ||
set[val.Digest] = set[val.Digest] - 1 | ||
if set[val.Digest] == 0 { | ||
delete(set, val.Digest) | ||
} | ||
} | ||
} | ||
// shrink the slice to remove the manifests | ||
manifests = manifests[:pointer+1] | ||
return manifests, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters