github.com/sealerio/sealer@v0.11.1-0.20240507115618-f4f89c5853ae/pkg/imageengine/buildah/rmi.go (about) 1 // Copyright © 2022 Alibaba Group Holding Ltd. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package buildah 16 17 import ( 18 "context" 19 20 "github.com/containers/common/libimage" 21 "github.com/hashicorp/go-multierror" 22 "github.com/pkg/errors" 23 "github.com/sirupsen/logrus" 24 25 "github.com/sealerio/sealer/pkg/define/options" 26 ) 27 28 func (engine *Engine) RemoveImage(opts *options.RemoveImageOptions) error { 29 if len(opts.ImageNamesOrIDs) == 0 && !opts.Prune { 30 return errors.Errorf("image name or ID must be specified") 31 } 32 if len(opts.ImageNamesOrIDs) > 0 && opts.Prune { 33 return errors.Errorf("when using the --prune switch, you may not pass any images names or IDs") 34 } 35 options := &libimage.RemoveImagesOptions{ 36 Filters: []string{"readonly=false"}, 37 } 38 39 if opts.Prune { 40 options.Filters = append(options.Filters, "dangling=true") 41 } 42 options.Force = opts.Force 43 44 // take it as image first 45 rmiReports, rmiErrors := engine.ImageRuntime().RemoveImages(context.Background(), opts.ImageNamesOrIDs, options) 46 for _, r := range rmiReports { 47 for _, u := range r.Untagged { 48 logrus.Infof("untagged: %s", u) 49 } 50 } 51 for _, r := range rmiReports { 52 if r.Removed { 53 logrus.Infof("%s", r.ID) 54 } 55 } 56 57 if len(rmiErrors) == 0 { 58 return nil 59 } 60 61 // take it as manifestList and try again 62 options.LookupManifest = true 63 rmiReports, rmiErrors2 := engine.ImageRuntime().RemoveImages(context.Background(), opts.ImageNamesOrIDs, options) 64 for _, r := range rmiReports { 65 for _, u := range r.Untagged { 66 logrus.Infof("untagged: %s", u) 67 } 68 } 69 for _, r := range rmiReports { 70 if r.Removed { 71 logrus.Infof("%s", r.ID) 72 } 73 } 74 75 if len(rmiErrors2) == 0 { 76 return nil 77 } 78 79 var multiE *multierror.Error 80 multiE = multierror.Append(multiE, append(rmiErrors, rmiErrors2...)...) 81 return multiE.ErrorOrNil() 82 }