1
0
Fork 0
mirror of https://github.com/kyverno/kyverno.git synced 2024-12-14 11:57:48 +00:00
kyverno/pkg/engine/adapters/rclient.go
Vishal Choudhary c5298cdf85
chore: use sigstore/cosign 2.2.2 on main (#9179)
* chore: use official cosign on main

Signed-off-by: Vishal Choudhary <vishal.choudhary@nirmata.com>

* cleanup: remove redundant if check

Signed-off-by: Vishal Choudhary <vishal.choudhary@nirmata.com>

* cleanup: extra require statement

Signed-off-by: Vishal Choudhary <vishal.choudhary@nirmata.com>

---------

Signed-off-by: Vishal Choudhary <vishal.choudhary@nirmata.com>
2023-12-18 06:41:26 +00:00

56 lines
1.7 KiB
Go

package adapters
import (
"context"
"fmt"
"github.com/google/go-containerregistry/pkg/name"
engineapi "github.com/kyverno/kyverno/pkg/engine/api"
"github.com/kyverno/kyverno/pkg/registryclient"
)
type rclientAdapter struct {
registryclient.Client
}
func RegistryClient(client registryclient.Client) engineapi.RegistryClient {
if client == nil {
return nil
}
return &rclientAdapter{client}
}
func (a *rclientAdapter) ForRef(ctx context.Context, ref string) (*engineapi.ImageData, error) {
desc, err := a.Client.FetchImageDescriptor(ctx, ref)
if err != nil {
return nil, fmt.Errorf("failed to fetch image descriptor: %s, error: %v", ref, err)
}
parsedRef, err := name.ParseReference(ref)
if err != nil {
return nil, fmt.Errorf("failed to parse image reference: %s, error: %v", ref, err)
}
image, err := desc.Image()
if err != nil {
return nil, fmt.Errorf("failed to resolve image reference: %s, error: %v", ref, err)
}
// We need to use the raw config and manifest to avoid dropping unknown keys
// which are not defined in GGCR structs.
rawManifest, err := image.RawManifest()
if err != nil {
return nil, fmt.Errorf("failed to fetch manifest for image reference: %s, error: %v", ref, err)
}
rawConfig, err := image.RawConfigFile()
if err != nil {
return nil, fmt.Errorf("failed to fetch config for image reference: %s, error: %v", ref, err)
}
data := engineapi.ImageData{
Image: ref,
ResolvedImage: fmt.Sprintf("%s@%s", parsedRef.Context().Name(), desc.Digest.String()),
Registry: parsedRef.Context().RegistryStr(),
Repository: parsedRef.Context().RepositoryStr(),
Identifier: parsedRef.Identifier(),
Manifest: rawManifest,
Config: rawConfig,
}
return &data, nil
}