github.com/opencontainers/umoci@v0.4.8-0.20240508124516-656e4836fb0d/new.go (about) 1 /* 2 * umoci: Umoci Modifies Open Containers' Images 3 * Copyright (C) 2016-2020 SUSE LLC 4 * 5 * Licensed under the Apache License, Version 2.0 (the "License"); 6 * you may not use this file except in compliance with the License. 7 * You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 18 package umoci 19 20 import ( 21 "context" 22 "runtime" 23 "time" 24 25 "github.com/apex/log" 26 imeta "github.com/opencontainers/image-spec/specs-go" 27 ispec "github.com/opencontainers/image-spec/specs-go/v1" 28 "github.com/opencontainers/umoci/oci/casext" 29 igen "github.com/opencontainers/umoci/oci/config/generate" 30 "github.com/pkg/errors" 31 ) 32 33 // NewImage creates a new empty image (tag) in the existing layout. 34 func NewImage(engineExt casext.Engine, tagName string) error { 35 // Create a new manifest. 36 log.WithFields(log.Fields{ 37 "tag": tagName, 38 }).Debugf("creating new manifest") 39 40 // Create a new image config. 41 g := igen.New() 42 createTime := time.Now() 43 44 // Set all of the defaults we need. 45 g.SetCreated(createTime) 46 g.SetOS(runtime.GOOS) 47 g.SetArchitecture(runtime.GOARCH) 48 g.ClearHistory() 49 50 // Make sure we have no diffids. 51 g.SetRootfsType("layers") 52 g.ClearRootfsDiffIDs() 53 54 // Update config and create a new blob for it. 55 config := g.Image() 56 configDigest, configSize, err := engineExt.PutBlobJSON(context.Background(), config) 57 if err != nil { 58 return errors.Wrap(err, "put config blob") 59 } 60 61 log.WithFields(log.Fields{ 62 "digest": configDigest, 63 "size": configSize, 64 }).Debugf("umoci: added new config") 65 66 // Create a new manifest that just points to the config and has an 67 // empty layer set. FIXME: Implement ManifestList support. 68 manifest := ispec.Manifest{ 69 Versioned: imeta.Versioned{ 70 SchemaVersion: 2, // FIXME: This is hardcoded at the moment. 71 }, 72 MediaType: ispec.MediaTypeImageManifest, 73 Config: ispec.Descriptor{ 74 MediaType: ispec.MediaTypeImageConfig, 75 Digest: configDigest, 76 Size: configSize, 77 }, 78 Layers: []ispec.Descriptor{}, 79 } 80 81 manifestDigest, manifestSize, err := engineExt.PutBlobJSON(context.Background(), manifest) 82 if err != nil { 83 return errors.Wrap(err, "put manifest blob") 84 } 85 86 log.WithFields(log.Fields{ 87 "digest": manifestDigest, 88 "size": manifestSize, 89 }).Debugf("umoci: added new manifest") 90 91 // Now create a new reference, and either add it to the engine or spew it 92 // to stdout. 93 94 descriptor := ispec.Descriptor{ 95 // FIXME: Support manifest lists. 96 MediaType: ispec.MediaTypeImageManifest, 97 Digest: manifestDigest, 98 Size: manifestSize, 99 } 100 101 log.Infof("new image manifest created: %s", descriptor.Digest) 102 103 if err := engineExt.UpdateReference(context.Background(), tagName, descriptor); err != nil { 104 return errors.Wrap(err, "add new tag") 105 } 106 107 log.Infof("created new tag for image manifest: %s", tagName) 108 return nil 109 }