-
Notifications
You must be signed in to change notification settings - Fork 0
/
store.go
297 lines (268 loc) · 9.59 KB
/
store.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
/*
Copyright 2024 Nokia.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package catalog
import (
"context"
"fmt"
"github.com/henderiw/apiserver-store/pkg/storebackend"
memstore "github.com/henderiw/apiserver-store/pkg/storebackend/memory"
"github.com/henderiw/logger/log"
"github.com/kform-dev/kform/pkg/recorder"
"github.com/kform-dev/kform/pkg/recorder/diag"
pkgv1alpha1 "github.com/pkgserver-dev/pkgserver/apis/pkg/v1alpha1"
"github.com/pkgserver-dev/pkgserver/apis/pkgrevid"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime/schema"
"sigs.k8s.io/kustomize/kyaml/yaml"
)
const (
APIServiceName = "apiService"
)
type APIType int
const (
APIType_Core APIType = iota
APIType_Package
)
func (r APIType) String() string {
switch r {
case APIType_Core:
return "core"
case APIType_Package:
return "package"
}
return "unknown"
}
type API struct {
Type APIType
Kind string
Resource string
PkgRevID pkgrevid.PackageRevID
Versions map[string][]string // key is version and value is pkgRev
}
type Store struct {
recorder recorder.Recorder[diag.Diagnostic]
// key: gk (ns:group, name:kind) -> Package(NS/Pkg), resource, kind, versions
catalogAPIStore storebackend.Storer[*API]
// key: gr (ns: group, name: resource) -> kind
catalogGRMap storebackend.Storer[string]
// key: pkg/pkgRevName -> per gvk dependencies
catalogPkgStore storebackend.Storer[*Dependency]
}
func NewStore() *Store {
return &Store{
catalogAPIStore: memstore.NewStore[*API](),
catalogGRMap: memstore.NewStore[string](),
catalogPkgStore: memstore.NewStore[*Dependency](),
}
}
func (r *Store) InitializeRecorder(recorder recorder.Recorder[diag.Diagnostic]) {
r.recorder = recorder
}
func (r *Store) DeletePkgRev(ctx context.Context, cr *pkgv1alpha1.PackageRevision) {
log := log.FromContext(ctx)
// delete the pkgRev from all the references
r.catalogAPIStore.UpdateWithFn(ctx, func(ctx context.Context, key storebackend.Key, api *API) *API {
// delete the pkgRevs from the api versions if this is a package API
if api.Type == APIType_Package {
for v, pkgRevs := range api.Versions {
for i, pkgRevName := range pkgRevs {
if pkgRevName == cr.Name {
// delete the entry
api.Versions[v] = append(pkgRevs[:i], pkgRevs[i+1:]...)
}
}
// if no more pkgrevs use this version we can delete the api
if len(api.Versions[v]) == 0 {
delete(api.Versions, v)
}
}
}
// TODO do we need to delete the api altogether if there is no more ref
return api
})
// dont delete the catalogGRMap since this should never change
// delete the pkgrevision
key := getKey(cr.Spec.PackageRevID.PkgString(), cr.Spec.PackageRevID.Revision)
if err := r.catalogPkgStore.Delete(ctx, key); err != nil {
log.Error("cannot delete pkgrev from pkgStore")
r.recorder.Record(diag.DiagFromErr(err))
}
}
func (r *Store) UpdatePkgRevAPI(ctx context.Context, cr *pkgv1alpha1.PackageRevision, outputs []*yaml.RNode) {
log := log.FromContext(ctx)
// we gather the apis that are referenced in this package revision
// k8s api extensions are defined on the basis of CRD(s) or APIServices
apiReferences := newAPIreferences(r.recorder)
apiReferences.gatherAPIs(ctx, outputs)
// dont proceed if there was an error found
if r.recorder.Get().HasError() {
return
}
// update the gr map and sort the entries before updating the apiStore
for _, gvkr := range apiReferences.list() {
gvkr := gvkr
log.Debug("gvkr", "group", gvkr.group, "version", gvkr.version, "kind", gvkr.kind, "resource", gvkr.resource)
// TODO check if there is a change
// update the catalog Group, Resource -> Kind mapping
if err := r.catalogGRMap.Update(ctx, getKey(gvkr.group, gvkr.resource), gvkr.kind); err != nil {
r.recorder.Record(diag.DiagFromErr(err))
}
// update the apiStore
r.catalogAPIStore.UpdateWithKeyFn(ctx, getKey(gvkr.group, gvkr.kind), func(ctx context.Context, api *API) *API {
if api == nil {
// no entry was found
versions := map[string][]string{}
versions[gvkr.version] = []string{cr.Name}
return &API{
Type: APIType_Package,
Kind: gvkr.kind,
Resource: gvkr.resource,
PkgRevID: cr.Spec.PackageRevID,
Versions: versions, // hold all the version
}
}
api.Type = APIType_Package
api.PkgRevID = cr.Spec.PackageRevID
// update an existing entry
if len(api.Versions) == 0 { // initialize versions if none exist
api.Versions = map[string][]string{}
}
if len(api.Versions[gvkr.version]) == 0 { // initialize versions entry if none exist
api.Versions[gvkr.version] = []string{}
}
found := false
for _, pkgRevName := range api.Versions[gvkr.version] {
if pkgRevName == cr.Name {
found = true
continue
}
}
if !found {
api.Versions[gvkr.version] = append(api.Versions[gvkr.version], cr.Name)
}
return api
})
}
}
// UpdateAPIfromAPIResources updates the API store from watching the apiResources on the k8s API
func (r *Store) UpdateAPIfromAPIResources(ctx context.Context, apiResources []*metav1.APIResourceList) {
log := log.FromContext(ctx)
for _, apiResourceList := range apiResources {
gv, err := schema.ParseGroupVersion(apiResourceList.GroupVersion)
if err != nil {
continue
}
for _, apiResource := range apiResourceList.APIResources {
log.Debug("UpdateAPIfromAPIResources", "group", gv.Group, "version", gv.Version, "kind", apiResource.Kind, "resource", apiResource.Name)
// update gr map
r.catalogGRMap.Update(ctx, getKey(gv.Group, apiResource.Name), apiResource.Kind)
// update api store
key := getKey(gv.Group, apiResource.Kind)
r.catalogAPIStore.UpdateWithKeyFn(ctx, key, func(ctx context.Context, api *API) *API {
if api == nil {
// no entry was found
versions := map[string][]string{}
versions[gv.Version] = []string{}
return &API{
Type: APIType_Core,
Kind: apiResource.Kind,
Resource: apiResource.Name,
Versions: versions, // hold all the version
}
}
// when an api is non nil, it means the entry exists and this is likely
// filled with a api from a package, core resources don't change within
// a deployment of k8s
return api
})
}
}
}
func (r *Store) UpdatePkgRevDependencies(ctx context.Context, cr *pkgv1alpha1.PackageRevision, packages, inputs, resources []*yaml.RNode) {
log := log.FromContext(ctx)
dr := newDependencyResolver(
r.recorder,
r.catalogAPIStore,
r.catalogGRMap,
cr.Spec.PackageRevID,
)
dep := dr.resolve(ctx, packages, inputs, resources)
if err := r.catalogPkgStore.Update(ctx, getKey(cr.Spec.PackageRevID.PkgString(), cr.Spec.PackageRevID.Revision), dep); err != nil {
log.Error("cannot update pkg dependencies")
}
}
func (r *Store) GetPkgRevDependencies(ctx context.Context, cr *pkgv1alpha1.PackageRevision) (bool, []pkgrevid.Upstream) {
log := log.FromContext(ctx)
// the catalog should be fetched using the upstream identifier in the package
r.catalogPkgStore.List(ctx, func(ctx context.Context, key storebackend.Key, dep *Dependency) {
log.Debug("pkgrevs", "key ns", key.Namespace, "key name", key.Name, "resolution", dep.HasResolutionError(), "deps", dep.ListPkgDependencies())
})
dep, err := r.catalogPkgStore.Get(ctx, getKey(cr.Spec.Upstream.PkgString(), cr.Spec.Upstream.Revision))
if err != nil {
log.Error("failed to fetch catalog package info", "pkgId", cr.Spec.Upstream.PkgString(), "revision", cr.Spec.Upstream.Revision, "error", err)
return false, nil
}
log.Debug("get pkgRev dependencies", "resolution error", dep.HasResolutionError(), "dependencies", dep.ListPkgDependencies())
//dep.PrintPkgDependencies()
//dep.PrintResolutionErrors()
return !dep.HasResolutionError(), dep.ListPkgDependencies()
}
func (r *Store) Print(ctx context.Context) {
/*
fmt.Println("****** API resources ******")
r.catalogAPIStore.List(ctx, func(ctx context.Context, key storebackend.Key, api *API) {
fmt.Printf("type: %s, group: %s, kind: %s, resource: %s, versions: %v\n", api.Type.String(), key.Namespace, key.Name, api.Resource, api.Versions)
})
fmt.Println("****************************")
fmt.Println("******** GR MAPPING ********")
r.catalogGRMap.List(ctx, func(ctx context.Context, key storebackend.Key, kind string) {
fmt.Println(key.Namespace, key.Name, kind)
})
fmt.Println("****************************")
*/
fmt.Println("**** Package Catalog ****")
r.catalogPkgStore.List(ctx, func(ctx context.Context, key storebackend.Key, dep *Dependency) {
fmt.Println("package", key.Namespace, "revision", key.Name)
dep.PrintResolutionErrors()
dep.PrintCoreDependencies()
dep.PrintPkgDependencies()
})
fmt.Println("****************************")
}
/*
func (r *apiDiscoverer) PrintPkgResources(ctx context.Context) {
}
*/
/*
func getApiVersionKind(krmResource any) (string, string) {
apiVersion := ""
kind := ""
switch x := krmResource.(type) {
case map[string]any:
for k, v := range x {
//fmt.Println("output filename:", k, v)
if k == "apiVersion" {
if reflect.TypeOf(v).Kind() == reflect.String {
apiVersion = v.(string)
}
}
if k == "kind" {
if reflect.TypeOf(v).Kind() == reflect.String {
kind = v.(string)
}
}
}
}
return apiVersion, kind
}
*/