-
-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: apply proxy-replacement for removal images too
Signed-off-by: Christian Kotzbauer <[email protected]> #290
- Loading branch information
1 parent
a4e9d0d
commit fdb76e5
Showing
5 changed files
with
114 additions
and
82 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package kubernetes | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/ckotzbauer/libk8soci/pkg/oci" | ||
parser "github.com/novln/docker-parser" | ||
"github.com/novln/docker-parser/docker" | ||
"github.com/sirupsen/logrus" | ||
) | ||
|
||
func ApplyProxyRegistry(img *oci.RegistryImage, log bool, registryProxyMap map[string]string) error { | ||
imageRef, err := parser.Parse(img.ImageID) | ||
if err != nil { | ||
logrus.WithError(err).Errorf("Could not parse image %s", img.ImageID) | ||
return err | ||
} | ||
|
||
for registryToReplace, proxyRegistry := range registryProxyMap { | ||
if imageRef.Registry() == registryToReplace { | ||
shortName := strings.TrimPrefix(imageRef.ShortName(), docker.DefaultRepoPrefix) | ||
fullName := fmt.Sprintf("%s/%s", imageRef.Registry(), shortName) | ||
if strings.HasPrefix(imageRef.Tag(), "sha256") { | ||
fullName = fmt.Sprintf("%s@%s", fullName, imageRef.Tag()) | ||
} else { | ||
fullName = fmt.Sprintf("%s:%s", fullName, imageRef.Tag()) | ||
} | ||
|
||
img.ImageID = strings.ReplaceAll(fullName, registryToReplace, proxyRegistry) | ||
img.Image = strings.ReplaceAll(img.Image, registryToReplace, proxyRegistry) | ||
|
||
if log { | ||
logrus.Debugf("Applied Registry-Proxy %s", img.ImageID) | ||
} | ||
|
||
break | ||
} | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package kubernetes_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/ckotzbauer/libk8soci/pkg/oci" | ||
"github.com/ckotzbauer/sbom-operator/internal/kubernetes" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
type mapTestData struct { | ||
input string | ||
expected string | ||
dataMap map[string]string | ||
} | ||
|
||
func TestApplyProxyRegistry(t *testing.T) { | ||
tests := []mapTestData{ | ||
{ | ||
input: "alpine:3.17", | ||
expected: "alpine:3.17", | ||
dataMap: make(map[string]string), | ||
}, | ||
{ | ||
input: "docker.io/alpine:3.17", | ||
expected: "ghcr.io/alpine:3.17", | ||
dataMap: map[string]string{"docker.io": "ghcr.io"}, | ||
}, | ||
{ | ||
input: "alpine:3.17", | ||
expected: "ghcr.io/alpine:3.17", | ||
dataMap: map[string]string{"docker.io": "ghcr.io"}, | ||
}, | ||
{ | ||
input: "alpine:3.17", | ||
expected: "alpine:3.17", | ||
dataMap: map[string]string{"ghcr.io": "docker.io"}, | ||
}, | ||
{ | ||
input: "alpine:3.17", | ||
expected: "my.registry.com:5000/alpine:3.17", | ||
dataMap: map[string]string{"docker.io": "my.registry.com:5000"}, | ||
}, | ||
{ | ||
input: "my.registry.com:5000/alpine:3.17", | ||
expected: "ghcr.io/alpine:3.17", | ||
dataMap: map[string]string{"my.registry.com:5000": "ghcr.io"}, | ||
}, | ||
} | ||
|
||
for _, v := range tests { | ||
t.Run(v.input, func(t *testing.T) { | ||
img := &oci.RegistryImage{ImageID: v.input, Image: v.input} | ||
err := kubernetes.ApplyProxyRegistry(img, false, v.dataMap) | ||
assert.NoError(t, err) | ||
assert.Equal(t, v.expected, img.ImageID) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters