fix patch command logic

Signed-off-by: Anubhav Gupta <mail.anubhav06@gmail.com>
This commit is contained in:
Anubhav Gupta
2023-09-01 09:43:09 +05:30
parent 18b4d3cbec
commit 53f5667694
10 changed files with 59 additions and 43 deletions

View File

@@ -4,9 +4,11 @@ import (
"context"
"errors"
"fmt"
"strings"
"time"
ref "github.com/distribution/distribution/reference"
"github.com/docker/distribution/reference"
"github.com/kubescape/go-logger"
"github.com/kubescape/kubescape/v2/core/cautils"
@@ -47,7 +49,6 @@ func GetPatchCmd(ks meta.IKubescape) *cobra.Command {
patchCmd.PersistentFlags().StringVarP(&patchInfo.PatchedImageTag, "tag", "t", "", "Tag for the patched image. Defaults to '<image-tag>-patched' ")
patchCmd.PersistentFlags().StringVarP(&patchInfo.BuildkitAddress, "address", "a", "unix:///run/buildkit/buildkitd.sock", "Address of buildkitd service, defaults to local buildkitd.sock")
patchCmd.PersistentFlags().DurationVar(&patchInfo.Timeout, "timeout", 5*time.Minute, "Timeout for the operation, defaults to '5m'")
patchCmd.PersistentFlags().BoolVarP(&patchInfo.IncludeReport, "report", "r", false, "Generate & save a before and after report for the image (json format))")
patchCmd.PersistentFlags().StringVarP(&patchInfo.Username, "username", "u", "", "Username for registry login")
patchCmd.PersistentFlags().StringVarP(&patchInfo.Password, "password", "p", "", "Password for registry login")
@@ -62,10 +63,15 @@ func validateImagePatchInfo(patchInfo *metav1.PatchInfo) error {
return errors.New("image tag is required")
}
// Check if image is in canonical format (required by copacetic for patching images)
// Convert image to canonical format (required by copacetic for patching images)
patchInfoImage, err := cautils.Normalize_image_name(patchInfo.Image)
if err != nil {
return nil
}
patchInfo.Image = patchInfoImage
// Parse the image full name to get image name and tag
named, err := ref.ParseNamed(patchInfo.Image)
if err != nil{
if err != nil {
return err
}
@@ -87,8 +93,18 @@ func validateImagePatchInfo(patchInfo *metav1.PatchInfo) error {
}
}
patchInfo.ImageName = named.Name()
// Extract the "image" name from the canonical Image URL
// If it's an official docker image, we store just the "image-name". Else if a docker repo then we store as "repo/image". Else complete URL
ref, _ := reference.ParseNormalizedNamed(patchInfoImage)
imageName := named.Name()
if strings.Contains(imageName, "docker.io/library/") {
imageName = reference.Path(ref)
imageName = imageName[strings.LastIndex(imageName, "/")+1:]
} else if strings.Contains(imageName, "docker.io/") {
imageName = reference.Path(ref)
}
patchInfo.ImageName = imageName
return nil
}