@@ -19,8 +19,10 @@ package bundle
19
19
import (
20
20
"context"
21
21
"errors"
22
+ "strings"
22
23
23
24
"github.com/tektoncd/pipeline/pkg/apis/resolution/v1beta1"
25
+ "github.com/tektoncd/pipeline/pkg/remoteresolution/cache"
24
26
"github.com/tektoncd/pipeline/pkg/remoteresolution/resolver/framework"
25
27
"github.com/tektoncd/pipeline/pkg/resolution/common"
26
28
"github.com/tektoncd/pipeline/pkg/resolution/resolver/bundle"
@@ -36,6 +38,16 @@ const (
36
38
37
39
// BundleResolverName is the name that the bundle resolver should be associated with.
38
40
BundleResolverName = "bundleresolver"
41
+
42
+ // ConfigMapName is the bundle resolver's config map
43
+ ConfigMapName = "bundleresolver-config"
44
+
45
+ // CacheModeAlways means always use cache regardless of bundle reference
46
+ CacheModeAlways = "always"
47
+ // CacheModeNever means never use cache regardless of bundle reference
48
+ CacheModeNever = "never"
49
+ // CacheModeAuto means use cache only when bundle reference has a digest
50
+ CacheModeAuto = "auto"
39
51
)
40
52
41
53
var _ framework.Resolver = & Resolver {}
@@ -58,17 +70,18 @@ func (r *Resolver) GetName(context.Context) string {
58
70
59
71
// GetConfigName returns the name of the bundle resolver's configmap.
60
72
func (r * Resolver ) GetConfigName (context.Context ) string {
61
- return bundle . ConfigMapName
73
+ return ConfigMapName
62
74
}
63
75
64
- // GetSelector returns a map of labels to match requests to this Resolver.
76
+ // GetSelector returns the labels that resource requests are required to have for
77
+ // the bundle resolver to process them.
65
78
func (r * Resolver ) GetSelector (context.Context ) map [string ]string {
66
79
return map [string ]string {
67
80
common .LabelKeyResolverType : LabelValueBundleResolverType ,
68
81
}
69
82
}
70
83
71
- // Validate ensures reqolution request spec from a request are as expected.
84
+ // Validate ensures parameters from a request are as expected.
72
85
func (r * Resolver ) Validate (ctx context.Context , req * v1beta1.ResolutionRequestSpec ) error {
73
86
if len (req .Params ) > 0 {
74
87
return bundle .ValidateParams (ctx , req .Params )
@@ -77,11 +90,79 @@ func (r *Resolver) Validate(ctx context.Context, req *v1beta1.ResolutionRequestS
77
90
return errors .New ("cannot validate request. the Validate method has not been implemented." )
78
91
}
79
92
80
- // Resolve uses the given request spec resolve the requested file or resource.
93
+ // ShouldUseCache determines if caching should be used based on the cache mode and bundle reference.
94
+ func ShouldUseCache (cacheMode string , bundleRef string ) bool {
95
+ switch cacheMode {
96
+ case CacheModeAlways :
97
+ return true
98
+ case CacheModeNever :
99
+ return false
100
+ case CacheModeAuto , "" : // default to auto if not specified
101
+ return IsOCIPullSpecByDigest (bundleRef )
102
+ default : // invalid cache mode defaults to auto
103
+ return IsOCIPullSpecByDigest (bundleRef )
104
+ }
105
+ }
106
+
107
+ // Resolve uses the given params to resolve the requested file or resource.
81
108
func (r * Resolver ) Resolve (ctx context.Context , req * v1beta1.ResolutionRequestSpec ) (resolutionframework.ResolvedResource , error ) {
82
109
if len (req .Params ) > 0 {
83
- return bundle .ResolveRequest (ctx , r .kubeClientSet , req )
110
+ if bundle .IsDisabled (ctx ) {
111
+ return nil , errors .New (bundle .DisabledError )
112
+ }
113
+
114
+ origParams := req .Params
115
+
116
+ params , err := bundle .OptionsFromParams (ctx , origParams )
117
+ if err != nil {
118
+ return nil , err
119
+ }
120
+
121
+ // Determine if caching should be used based on cache mode
122
+ useCache := ShouldUseCache (params .Cache , params .Bundle )
123
+
124
+ if useCache {
125
+ // Generate cache key
126
+ cacheKey , err := cache .GenerateCacheKey (LabelValueBundleResolverType , origParams )
127
+ if err != nil {
128
+ return nil , err
129
+ }
130
+
131
+ // Check cache first
132
+ if cached , ok := cache .GetGlobalCache ().Get (cacheKey ); ok {
133
+ if resource , ok := cached .(resolutionframework.ResolvedResource ); ok {
134
+ return resource , nil
135
+ }
136
+ }
137
+ }
138
+
139
+ resource , err := bundle .ResolveRequest (ctx , r .kubeClientSet , req )
140
+ if err != nil {
141
+ return nil , err
142
+ }
143
+
144
+ // Cache the result if caching is enabled
145
+ if useCache {
146
+ cacheKey , _ := cache .GenerateCacheKey (LabelValueBundleResolverType , origParams )
147
+ cache .GetGlobalCache ().Add (cacheKey , resource )
148
+ }
149
+
150
+ return resource , nil
84
151
}
85
152
// Remove this error once resolution of url has been implemented.
86
153
return nil , errors .New ("the Resolve method has not been implemented." )
87
154
}
155
+
156
+ // IsOCIPullSpecByDigest checks if the given OCI pull spec contains a digest.
157
+ // A digest is typically in the format of @sha256:<hash> or :<tag>@sha256:<hash>
158
+ func IsOCIPullSpecByDigest (pullSpec string ) bool {
159
+ // Check for @sha256: pattern
160
+ if strings .Contains (pullSpec , "@sha256:" ) {
161
+ return true
162
+ }
163
+ // Check for :<tag>@sha256: pattern
164
+ if strings .Contains (pullSpec , ":" ) && strings .Contains (pullSpec , "@sha256:" ) {
165
+ return true
166
+ }
167
+ return false
168
+ }
0 commit comments