forked from kyma-project/lifecycle-manager
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspec_resolver.go
64 lines (51 loc) · 1.9 KB
/
spec_resolver.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package manifest
import (
"context"
"errors"
"fmt"
"github.com/google/go-containerregistry/pkg/authn"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/yaml"
"github.com/kyma-project/lifecycle-manager/api/v1beta2"
declarativev2 "github.com/kyma-project/lifecycle-manager/internal/declarative/v2"
)
type KeyChainLookup interface {
Get(ctx context.Context, imageSpec v1beta2.ImageSpec) (authn.Keychain, error)
}
type PathExtractor interface {
GetPathFromRawManifest(ctx context.Context, imageSpec v1beta2.ImageSpec, keyChain authn.Keychain) (string, error)
}
type SpecResolver struct {
keyChainLookup KeyChainLookup
manifestPathExtractor PathExtractor
}
func NewSpecResolver(kcLookup KeyChainLookup, extractor PathExtractor) *SpecResolver {
return &SpecResolver{
keyChainLookup: kcLookup,
manifestPathExtractor: extractor,
}
}
var ErrRenderModeInvalid = errors.New("render mode is invalid")
func (s *SpecResolver) GetSpec(ctx context.Context, manifest *v1beta2.Manifest) (*declarativev2.Spec, error) {
var imageSpec v1beta2.ImageSpec
if err := yaml.Unmarshal(manifest.Spec.Install.Source.Raw, &imageSpec); err != nil {
return nil, fmt.Errorf("failed to unmarshal data: %w", err)
}
if imageSpec.Type != v1beta2.OciRefType && imageSpec.Type != v1beta2.OciDirType {
return nil, fmt.Errorf("could not determine render mode for %s: %w",
client.ObjectKeyFromObject(manifest), ErrRenderModeInvalid)
}
keyChain, err := s.keyChainLookup.Get(ctx, imageSpec)
if err != nil {
return nil, fmt.Errorf("failed to fetch keyChain: %w", err)
}
rawManifestPath, err := s.manifestPathExtractor.GetPathFromRawManifest(ctx, imageSpec, keyChain)
if err != nil {
return nil, fmt.Errorf("failed to extract raw manifest from layer digest: %w", err)
}
return &declarativev2.Spec{
ManifestName: manifest.Spec.Install.Name,
Path: rawManifestPath,
OCIRef: imageSpec.Ref,
}, nil
}