diff --git a/pkg/builder/builder.go b/pkg/builder/builder.go index fcc3fa5c80616215097c95d059f19aeb307c7052..4e5fa60c5b95fcc7e68301ca5ca2e7bc59f2260f 100644 --- a/pkg/builder/builder.go +++ b/pkg/builder/builder.go @@ -12,32 +12,32 @@ package builder import ( - "bufio" - "context" - "encoding/json" - "errors" - "fmt" - "io" - "os" - "os/exec" - "path/filepath" - "strings" - - "gitee.com/openeuler/ktib/pkg/options" - cpier "github.com/containers/image/v5/copy" - v5manifest "github.com/containers/image/v5/manifest" - - //"github.com/containers/image/v5/docker/reference" - "github.com/containers/image/v5/signature" - "github.com/containers/image/v5/transports/alltransports" - "github.com/containers/image/v5/types" - "github.com/containers/storage" - "github.com/containers/storage/pkg/archive" - "github.com/containers/storage/pkg/ioutils" - "github.com/opencontainers/go-digest" - v1 "github.com/opencontainers/image-spec/specs-go/v1" - "github.com/opencontainers/runtime-tools/generate" - "github.com/sirupsen/logrus" + "bufio" + "context" + "encoding/json" + "errors" + "fmt" + "io" + "os" + "os/exec" + "path/filepath" + "strings" + + "gitee.com/openeuler/ktib/pkg/options" + cpier "github.com/containers/image/v5/copy" + v5manifest "github.com/containers/image/v5/manifest" + + //"github.com/containers/image/v5/docker/reference" + "github.com/containers/image/v5/signature" + "github.com/containers/image/v5/transports/alltransports" + "github.com/containers/image/v5/types" + "github.com/containers/storage" + "github.com/containers/storage/pkg/archive" + "github.com/containers/storage/pkg/ioutils" + "github.com/opencontainers/go-digest" + v1 "github.com/opencontainers/image-spec/specs-go/v1" + "github.com/opencontainers/runtime-tools/generate" + "github.com/sirupsen/logrus" ) const ( @@ -129,7 +129,7 @@ func FindBuilder(store storage.Store, name string) (*Builder, error) { } statefile := filepath.Join(cdir, stateFile) - buildstate, err := os.ReadFile(statefile) + buildstate, err := os.ReadFile(statefile) if err != nil && os.IsNotExist(err) { return nil, err } @@ -154,7 +154,7 @@ func FindAllBuilders(store storage.Store) ([]*Builder, error) { if err != nil { return nil, err } - buildstate, err := os.ReadFile(filepath.Join(cdir, stateFile)) + buildstate, err := os.ReadFile(filepath.Join(cdir, stateFile)) if err != nil && os.IsNotExist(err) { return nil, err } @@ -346,7 +346,7 @@ func (b *Builder) Commit(exportTo string) error { return fmt.Errorf("storing blob to file %v: %w", tar, err) } if err := wt.Flush(); err != nil { - return fmt.Errorf("Can not flush bufio: %w", err) + return fmt.Errorf("can not flush bufio: %v", err) } diffrdcloser.Close() @@ -575,7 +575,7 @@ func (b *Builder) Run(args []string, ops options.RUNOption) error { } // Currently, the cni component is not supported to create container networks, so the host network is still used. if err = g.RemoveLinuxNamespace("network"); err != nil { - return fmt.Errorf("error removing network namespace for run", err) + return fmt.Errorf("error removing network namespace for run: %v", err) } if err := b.Mount(""); err != nil { return err @@ -630,7 +630,7 @@ func (b *Builder) SetLabel(containerID string, labels map[string]string) error { configPath := filepath.Join(configDir, "ktib.json") // 读取配置文件 - data, err := os.ReadFile(configPath) + data, err := os.ReadFile(configPath) if err != nil { return err } @@ -645,20 +645,34 @@ func (b *Builder) SetLabel(containerID string, labels map[string]string) error { if config["Labels"] == nil { config["Labels"] = make(map[string]string) } - labelsMap := config["Labels"].(map[string]string) + // 获取现有标签 + existingLabels := make(map[string]string) + + if rawLabels, ok := config["Labels"].(map[string]interface{}); ok { + // JSON反序列化产生的类型 + for k, v := range rawLabels { + existingLabels[k] = fmt.Sprintf("%v", v) + } + } else if stringLabels, ok := config["Labels"].(map[string]string); ok { + existingLabels = stringLabels + } + + // 合并新标签 for key, value := range labels { - labelsMap[key] = value + existingLabels[key] = value } - config["Labels"] = labelsMap + + // 更新配置 + config["Labels"] = existingLabels // 更新配置文件 newData, err := json.Marshal(config) if err != nil { return err } - if err := os.WriteFile(configPath, newData, 0644); err != nil { - return err - } + if err := os.WriteFile(configPath, newData, 0644); err != nil { + return err + } fmt.Printf("成功为容器 %s 设置标签: %v\n", containerID, labels) return nil