From 6369eebd2224487131bac8d7f5f7e8d7cfb71803 Mon Sep 17 00:00:00 2001 From: Glyn Normington Date: Fri, 21 Jun 2019 02:16:54 +0100 Subject: [PATCH] Apply relocation mapping to invocation image Fixes /~https://github.com/deislabs/duffle/issues/785 --- cmd/duffle/main.go | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/cmd/duffle/main.go b/cmd/duffle/main.go index c0d76ebc..3a32abb2 100644 --- a/cmd/duffle/main.go +++ b/cmd/duffle/main.go @@ -1,6 +1,7 @@ package main import ( + "encoding/json" "fmt" "io/ioutil" "os" @@ -133,7 +134,14 @@ func (d *driverWithRelocationMapping) Run(op *driver.Operation) error { // if there is a relocation mapping, ensure it is mounted if d.relMapping != "" { op.Files["/cnab/app/relocation-mapping.json"] = d.relMapping + + var err error + op, err = d.relocateInvocationImage(op) + if err != nil { + return err + } } + return d.driver.Run(op) } @@ -141,6 +149,24 @@ func (d *driverWithRelocationMapping) Handles(it string) bool { return d.driver.Handles(it) } +func (d *driverWithRelocationMapping) relocateInvocationImage(op *driver.Operation) (*driver.Operation, error) { + relMap := make(map[string]string) + err := json.Unmarshal([]byte(d.relMapping), &relMap) + if err != nil { + return nil, err + } + + // TODO: copy op before mutating it + + var ok bool + op.Image, ok = relMap[op.Image] + if !ok { + return nil, fmt.Errorf("invocation image %s not present in relocation mapping %v", op.Image, relMap) + } + + return op, nil +} + func loadRelMapping(relMap string) (string, error) { if relMap != "" { data, err := ioutil.ReadFile(relMap)