github.com/sealerio/sealer@v0.11.1-0.20240507115618-f4f89c5853ae/pkg/imageengine/buildah/config.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  	"strings"
    19  
    20  	"github.com/containers/buildah"
    21  	"github.com/pkg/errors"
    22  
    23  	"github.com/sealerio/sealer/pkg/define/options"
    24  )
    25  
    26  func (engine *Engine) Config(opts *options.ConfigOptions) error {
    27  	if len(opts.ContainerID) == 0 {
    28  		return errors.Errorf("container ID must be specified")
    29  	}
    30  	name := opts.ContainerID
    31  
    32  	ctx := getContext()
    33  	store := engine.ImageStore()
    34  	builder, err := OpenBuilder(ctx, store, name)
    35  	if err != nil {
    36  		return errors.Wrapf(err, "error reading build container %q", name)
    37  	}
    38  
    39  	if err := updateConfig(builder, opts); err != nil {
    40  		return err
    41  	}
    42  	return builder.Save()
    43  }
    44  
    45  func updateConfig(builder *buildah.Builder, iopts *options.ConfigOptions) error {
    46  	if len(iopts.Annotations) != 0 {
    47  		for _, annotationSpec := range iopts.Annotations {
    48  			annotation := strings.SplitN(annotationSpec, "=", 2)
    49  			switch {
    50  			case len(annotation) > 1:
    51  				builder.SetAnnotation(annotation[0], annotation[1])
    52  			case annotation[0] == "-":
    53  				builder.ClearAnnotations()
    54  			case strings.HasSuffix(annotation[0], "-"):
    55  				builder.UnsetAnnotation(strings.TrimSuffix(annotation[0], "-"))
    56  			default:
    57  				builder.SetAnnotation(annotation[0], "")
    58  			}
    59  		}
    60  	}
    61  	return nil
    62  }