Skip to content

Commit

Permalink
fixes + logs
Browse files Browse the repository at this point in the history
Signed-off-by: alexey.komyakov <[email protected]>
  • Loading branch information
scaps1 committed Oct 24, 2024
1 parent 42b09a5 commit f048c93
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 16 deletions.
2 changes: 0 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ func main() {
bindAddr := flag.String("bind-address", ":8080", "address:port to bind /metrics endpoint to")
namespaceLabels := flag.String("namespace-label", "", "namespace label for checks")
insecureSkipVerify := flag.Bool("skip-registry-cert-verification", false, "whether to skip registries' certificate verification")
ecrImagesExists := flag.Bool("ecr-images-exists", false, "whether images from ECR in your project")
plainHTTP := flag.Bool("allow-plain-http", false, "whether to fallback to HTTP scheme for registries that don't support HTTPS") // named after the ctr cli flag
defaultRegistry := flag.String("default-registry", "", fmt.Sprintf("default registry to use in absence of a fully qualified image name, defaults to %q", name.DefaultRegistry))
flag.Var(&cp, "capath", "path to a file that contains CA certificates in the PEM format") // named after the curl cli flag
Expand Down Expand Up @@ -87,7 +86,6 @@ func main() {
stopCh.Done(),
kubeClient,
*insecureSkipVerify,
*ecrImagesExists,
*plainHTTP,
cp,
forceCheckDisabledControllerKindsParser.ParsedKinds,
Expand Down
29 changes: 15 additions & 14 deletions pkg/registry/checker.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ type registryCheckerConfig struct {
defaultRegistry string
plainHTTP bool
mirrorsMap map[string]string
ecrImagesExists bool
}

type Checker struct {
Expand Down Expand Up @@ -79,7 +78,6 @@ func NewChecker(
stopCh <-chan struct{},
kubeClient *kubernetes.Clientset,
skipVerify bool,
ecrImagesExists bool,
plainHTTP bool,
caPths []string,
forceCheckDisabledControllerKinds []string,
Expand Down Expand Up @@ -131,7 +129,6 @@ func NewChecker(
defaultRegistry: defaultRegistry,
plainHTTP: plainHTTP,
mirrorsMap: mirrorsMap,
ecrImagesExists: ecrImagesExists,
},
}

Expand Down Expand Up @@ -323,9 +320,11 @@ func (rc *Checker) checkImageAvailability(log *logrus.Entry, imageName string, k
return checkImageNameParseErr(log, err)
}

region := parseRegion(ref)
if isImageInEcr(ref, region) && rc.config.ecrImagesExists {
return store.Available
if strings.Contains(ref.Context().RegistryStr(), "amazonaws.com") {
region, _ := parseRegion(ref)
if isImageInEcr(ref, region) {
return store.Available
}
}

imgErr := wait.ExponentialBackoff(wait.Backoff{
Expand Down Expand Up @@ -382,28 +381,30 @@ func parseImageName(image string, defaultRegistry string, plainHTTP bool) (name.
return ref, nil
}

func parseAccountID(reference name.Reference) string {
func parseAccountID(reference name.Reference) (string, error) {
registry := reference.Context().RegistryStr()

parts := strings.Split(registry, ".")
if len(parts) > 0 {
accountID := parts[0]
return accountID
return accountID, nil
}

return ""
logrus.Infof("Uri '%s' doesn't match the default template while parsing account id.", registry)
return "", nil
}

func parseRegion(reference name.Reference) string {
func parseRegion(reference name.Reference) (string, error) {
registry := reference.Context().RegistryStr()

parts := strings.Split(registry, ".")
if len(parts) > 3 {
region := parts[3]
return region
return region, nil
}

return ""
logrus.Infof("Uri '%s' doesn't match the default template while parsing region.", registry)
return "", nil
}

func check(ref name.Reference, kc authn.Keychain, registryTransport http.RoundTripper) (store.AvailabilityMode, error) {
Expand Down Expand Up @@ -452,8 +453,9 @@ func isImageInEcr(ref name.Reference, region string) bool {
}

ecrClient := ecr.NewFromConfig(cfg)
accountID, _ := parseAccountID(ref)
input := &ecr.BatchGetImageInput{
RegistryId: aws.String(parseAccountID(ref)),
RegistryId: aws.String(accountID),
RepositoryName: aws.String(ref.Context().RepositoryStr()),
ImageIds: []types.ImageIdentifier{
{ImageTag: aws.String(ref.Identifier())},
Expand All @@ -467,7 +469,6 @@ func isImageInEcr(ref name.Reference, region string) bool {
}

if len(result.Images) > 0 {
logrus.Infof("Image '%s' found in ECR.", ref.Context().Name())
return true
}

Expand Down

0 comments on commit f048c93

Please sign in to comment.