-
Notifications
You must be signed in to change notification settings - Fork 450
/
registry.go
156 lines (134 loc) · 5.15 KB
/
registry.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
// Copyright 2020 SAP SE or an SAP affiliate company. All rights reserved. This file is licensed under the Apache Software License, v. 2 except as noted otherwise in the LICENSE file
//
// 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 managedresources
import (
"fmt"
"reflect"
"sort"
"strings"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/runtime/serializer"
"k8s.io/apimachinery/pkg/runtime/serializer/json"
"k8s.io/apimachinery/pkg/util/sets"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/client/apiutil"
)
// Registry stores objects and their serialized form. It allows to compute a map of all registered objects that can be
// used as part of a Secret's data which is referenced by a ManagedResource.
type Registry struct {
scheme *runtime.Scheme
codec runtime.Codec
nameToObject map[string]*object
}
type object struct {
obj client.Object
serialization []byte
}
// NewRegistry returns a new registry for resources. The given scheme, codec, and serializer must know all the resource
// types that will later be added to the registry.
func NewRegistry(scheme *runtime.Scheme, codec serializer.CodecFactory, serializer *json.Serializer) *Registry {
var groupVersions schema.GroupVersions
for k := range scheme.AllKnownTypes() {
groupVersions = append(groupVersions, k.GroupVersion())
}
// Use set to remove duplicates
groupVersions = sets.New(groupVersions...).UnsortedList()
// Sort groupVersions to ensure groupVersions.Identifier() is stable key
// for the map in https://github.com/kubernetes/apimachinery/blob/v0.26.1/pkg/runtime/serializer/versioning/versioning.go#L94
sort.Slice(groupVersions, func(i, j int) bool {
if groupVersions[i].Group == groupVersions[j].Group {
return groupVersions[i].Version < groupVersions[j].Version
}
return groupVersions[i].Group < groupVersions[j].Group
})
return &Registry{
scheme: scheme,
codec: codec.CodecForVersions(serializer, serializer, groupVersions, groupVersions),
nameToObject: make(map[string]*object),
}
}
// Add adds the given object to the registry. It computes a filename based on its type, namespace, and name. It serializes
// the object to YAML and stores both representations (object and serialization) in the registry.
func (r *Registry) Add(objs ...client.Object) error {
for _, obj := range objs {
if obj == nil || reflect.ValueOf(obj) == reflect.Zero(reflect.TypeOf(obj)) {
continue
}
objectName, err := r.objectName(obj)
if err != nil {
return err
}
filename := objectName + ".yaml"
if _, ok := r.nameToObject[filename]; ok {
return fmt.Errorf("duplicate filename in registry: %q", filename)
}
serializationYAML, err := runtime.Encode(r.codec, obj)
if err != nil {
return err
}
r.nameToObject[filename] = &object{
obj: obj,
serialization: serializationYAML,
}
}
return nil
}
// AddSerialized adds the provided serialized YAML for the registry. The provided filename will be used as key.
func (r *Registry) AddSerialized(filename string, serializationYAML []byte) {
r.nameToObject[filename] = &object{serialization: serializationYAML}
}
// SerializedObjects returns a map whose keys are filenames and whose values are serialized objects.
func (r *Registry) SerializedObjects() map[string][]byte {
out := make(map[string][]byte, len(r.nameToObject))
for name, object := range r.nameToObject {
out[name] = object.serialization
}
return out
}
// AddAllAndSerialize calls Add() for all the given objects before calling SerializedObjects().
func (r *Registry) AddAllAndSerialize(objects ...client.Object) (map[string][]byte, error) {
if err := r.Add(objects...); err != nil {
return nil, err
}
return r.SerializedObjects(), nil
}
// RegisteredObjects returns a map whose keys are filenames and whose values are objects.
func (r *Registry) RegisteredObjects() map[string]client.Object {
out := make(map[string]client.Object, len(r.nameToObject))
for name, object := range r.nameToObject {
out[name] = object.obj
}
return out
}
// String returns the string representation of the registry.
func (r *Registry) String() string {
out := make([]string, 0, len(r.nameToObject))
for name, object := range r.nameToObject {
out = append(out, fmt.Sprintf("* %s:\n%s", name, object.serialization))
}
return strings.Join(out, "\n\n")
}
func (r *Registry) objectName(obj client.Object) (string, error) {
gvk, err := apiutil.GVKForObject(obj, r.scheme)
if err != nil {
return "", err
}
return fmt.Sprintf(
"%s__%s__%s",
strings.ToLower(gvk.Kind),
obj.GetNamespace(),
strings.Replace(obj.GetName(), ":", "_", -1),
), nil
}