/
recources.go
129 lines (108 loc) · 3.39 KB
/
recources.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
/*
Copyright 2023 The Nephio Authors.
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 resources
import (
"context"
"sync"
"github.com/nephio-project/nephio/controllers/pkg/resource"
"github.com/nokia/k8s-ipam/pkg/meta"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/runtime/schema"
"sigs.k8s.io/controller-runtime/pkg/client"
)
type Resources interface {
AddNewResource(o client.Object)
GetExistingResources(ctx context.Context) error
APIApply(ctx context.Context) error
GetNewResources() map[corev1.ObjectReference]client.Object
}
type Config struct {
CR client.Object
MatchingLabels client.MatchingLabels
Owns []schema.GroupVersionKind
}
func New(c resource.APIPatchingApplicator, cfg Config) Resources {
return &resources{
APIPatchingApplicator: c,
cfg: cfg,
newResources: map[corev1.ObjectReference]client.Object{},
existingResources: map[corev1.ObjectReference]client.Object{},
}
}
type resources struct {
resource.APIPatchingApplicator
cfg Config
m sync.RWMutex
newResources map[corev1.ObjectReference]client.Object
existingResources map[corev1.ObjectReference]client.Object
}
func (r *resources) AddNewResource(o client.Object) {
r.m.Lock()
defer r.m.Unlock()
r.newResources[corev1.ObjectReference{
APIVersion: o.GetResourceVersion(),
Kind: o.GetObjectKind().GroupVersionKind().Kind,
Namespace: o.GetNamespace(),
Name: o.GetName(),
}] = o
}
func (r *resources) GetExistingResources(ctx context.Context) error {
r.m.Lock()
defer r.m.Unlock()
for _, gvk := range r.cfg.Owns {
objs := meta.GetUnstructuredListFromGVK(&gvk)
opts := []client.ListOption{
r.cfg.MatchingLabels,
}
if err := r.List(ctx, objs, opts...); err != nil {
return err
}
for _, o := range objs.Items {
for _, ref := range o.GetOwnerReferences() {
if ref.UID == r.cfg.CR.GetUID() {
r.existingResources[corev1.ObjectReference{APIVersion: o.GetAPIVersion(), Kind: o.GetKind(), Name: o.GetName(), Namespace: o.GetNamespace()}] = &o
}
}
}
}
return nil
}
// APIApply
// step 1. remove the exisiting resources that overlap with the new resources
// step 2. delete the exisiting resources that are no longer needed
// step 3. apply the new resources to the api server
func (r *resources) APIApply(ctx context.Context) error {
r.m.Lock()
defer r.m.Unlock()
for ref := range r.newResources {
delete(r.existingResources, ref)
}
// step2. delete the exisiting resource that are no longer needed
for _, o := range r.existingResources {
if err := r.Delete(ctx, o); err != nil {
return err
}
}
// step3. apply the new resources to the api server
for _, o := range r.newResources {
if err := r.Apply(ctx, o); err != nil {
return err
}
}
return nil
}
func (r *resources) GetNewResources() map[corev1.ObjectReference]client.Object {
r.m.RLock()
defer r.m.RUnlock()
return r.newResources
}