-
Notifications
You must be signed in to change notification settings - Fork 110
/
identified_resources_list.go
124 lines (98 loc) · 3.13 KB
/
identified_resources_list.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
// Copyright 2024 The Carvel Authors.
// SPDX-License-Identifier: Apache-2.0
package resources
import (
"sort"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/runtime/schema"
)
type IdentifiedResourcesListOpts struct {
IgnoreCachedResTypes bool
GKsScope []schema.GroupKind
ResourceNamespaces []string
}
func (r IdentifiedResources) List(labelSelector labels.Selector, resRefs []ResourceRef, opts IdentifiedResourcesListOpts) ([]Resource, error) {
defer r.logger.DebugFunc("List").Finish()
resTypes, err := r.resourceTypes.All(opts.IgnoreCachedResTypes)
if err != nil {
return nil, err
}
// TODO non-listable types
resTypes = Listable(resTypes)
// TODO eliminating events
resTypes = NonMatching(resTypes, ResourceRef{
schema.GroupVersionResource{Version: "v1", Resource: "events"},
})
// TODO eliminating component statuses
resTypes = NonMatching(resTypes, ResourceRef{
schema.GroupVersionResource{Version: "v1", Resource: "componentstatuses"},
})
// https://github.com/carvel-dev/kapp/issues/748
// TODO provide a way to exclude resource via configuration
resTypes = NonMatchingGK(resTypes, schema.GroupKind{Group: "cilium.io", Kind: "CiliumIdentity"})
if len(opts.GKsScope) > 0 {
resTypes = MatchingAnyGK(resTypes, opts.GKsScope)
}
if len(resRefs) > 0 {
resTypes = MatchingAny(resTypes, resRefs)
}
allOpts := AllOpts{
ListOpts: &metav1.ListOptions{
LabelSelector: labelSelector.String(),
},
ResourceNamespaces: opts.ResourceNamespaces,
}
resources, err := r.resources.All(resTypes, allOpts)
if err != nil {
return nil, err
}
// Check returned resources against label selector
// in case of Kubernetes APIs returned resources that do not match.
// This can happen if custom aggregated APIs did not implement label selector filtering.
// (https://github.com/carvel-dev/kapp/issues/160)
var filteredResources []Resource
for _, res := range resources {
if labelSelector.Matches(labels.Set(res.Labels())) {
filteredResources = append(filteredResources, res)
}
}
resources = filteredResources
// Mark resources that were not created by kapp as transient
for i, res := range resources {
if !NewIdentityAnnotation(res).Valid() {
res.MarkTransient(true)
resources[i] = res
}
}
return r.pickPreferredVersions(resources)
}
func (r IdentifiedResources) pickPreferredVersions(resources []Resource) ([]Resource, error) {
var result []Resource
uniqueByID := map[string][]Resource{}
for _, res := range resources {
uniqueByID[res.UID()] = append(uniqueByID[res.UID()], res)
}
for _, rs := range uniqueByID {
var matched bool
for _, res := range rs {
idAnn := NewIdentityAnnotation(res)
if idAnn.MatchesVersion() {
err := idAnn.RemoveMod().Apply(res)
if err != nil {
return nil, err
}
result = append(result, res)
matched = true
break
}
}
if !matched {
// Sort to have some stability
sort.Slice(rs, func(i, j int) bool { return rs[i].APIVersion() < rs[j].APIVersion() })
// TODO use preferred version from the api
result = append(result, rs[0])
}
}
return result, nil
}