webhook_common: Extract only relevant parts for the validation

Since we currently validate only the image name and the tag,
ignore registry, vendor and extract only relevant parts.

Closes #605

Signed-off-by: Oleg Zhurakivskyy <oleg.zhurakivskyy@intel.com>
This commit is contained in:
Oleg Zhurakivskyy 2021-03-30 12:41:09 +00:00
parent 60f23f40f0
commit 7d40758d14

View File

@ -15,6 +15,7 @@
package v1 package v1
import ( import (
"path/filepath"
"strings" "strings"
"github.com/pkg/errors" "github.com/pkg/errors"
@ -23,18 +24,19 @@ import (
// common functions for webhooks // common functions for webhooks
func validatePluginImage(image, expectedName string, expectedMinVersion *version.Version) error { func validatePluginImage(image, expectedImageName string, expectedMinVersion *version.Version) error {
parts := strings.SplitN(image, ":", 2) // Ignore registry, vendor and extract the image name with the tag
parts := strings.SplitN(filepath.Base(image), ":", 2)
if len(parts) != 2 { if len(parts) != 2 {
return errors.Errorf("incorrect image field %q", image) return errors.Errorf("incorrect image field %q", image)
} }
namespacedName := parts[0]
imageName := parts[0]
versionStr := parts[1] versionStr := parts[1]
parts = strings.Split(namespacedName, "/") if imageName != expectedImageName {
name := parts[len(parts)-1] return errors.Errorf("incorrect image name %q. Make sure you use '<vendor>/%s:<version>'", imageName, expectedImageName)
if name != expectedName {
return errors.Errorf("incorrect image name %q. Make sure you use '<vendor>/%s:<version>'", name, expectedName)
} }
ver, err := version.ParseSemantic(versionStr) ver, err := version.ParseSemantic(versionStr)