-
Notifications
You must be signed in to change notification settings - Fork 80
/
utilities.go
113 lines (85 loc) · 2.62 KB
/
utilities.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
// Copyright 2022 Clastix Labs
// SPDX-License-Identifier: Apache-2.0
package utilities
import (
"bytes"
"fmt"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/serializer/json"
kamajiv1alpha1 "github.com/clastix/kamaji/api/v1alpha1"
"github.com/clastix/kamaji/internal/constants"
)
const (
separator = "-"
)
func KamajiLabels(tcpName, resourceName string) map[string]string {
return map[string]string{
constants.ProjectNameLabelKey: constants.ProjectNameLabelValue,
constants.ControlPlaneLabelKey: tcpName,
constants.ControlPlaneLabelResource: resourceName,
}
}
func MergeMaps(maps ...map[string]string) map[string]string {
result := map[string]string{}
for _, m := range maps {
for k, v := range m {
result[k] = v
}
}
return result
}
func AddTenantPrefix(name string, tenantControlPlane *kamajiv1alpha1.TenantControlPlane) string {
return fmt.Sprintf("%s%s%s", tenantControlPlane.GetName(), separator, name)
}
// EncodeToYaml returns the given object in yaml format and the error.
func EncodeToYaml(o runtime.Object) ([]byte, error) {
scheme := runtime.NewScheme()
encoder := json.NewSerializerWithOptions(json.SimpleMetaFactory{}, scheme, scheme, json.SerializerOptions{
Yaml: true,
Pretty: false,
Strict: false,
})
buf := bytes.NewBuffer([]byte{})
if err := encoder.Encode(o, buf); err != nil {
return nil, err
}
return buf.Bytes(), nil
}
func DecodeFromYAML(o string, to runtime.Object) (err error) {
scheme := runtime.NewScheme()
encoder := json.NewSerializerWithOptions(json.SimpleMetaFactory{}, scheme, scheme, json.SerializerOptions{
Yaml: true,
Pretty: false,
Strict: false,
})
if to, _, err = encoder.Decode([]byte(o), nil, to); err != nil { //nolint:ineffassign,staticcheck
return
}
return
}
func DecodeFromJSON(o string, to runtime.Object) (err error) {
scheme := runtime.NewScheme()
encoder := json.NewSerializerWithOptions(json.SimpleMetaFactory{}, scheme, scheme, json.SerializerOptions{
Yaml: false,
Pretty: false,
Strict: false,
})
if to, _, err = encoder.Decode([]byte(o), nil, to); err != nil { //nolint:ineffassign,staticcheck
return
}
return
}
// EncodeToJSON returns the given object in JSON format and the error, respecting the Kubernetes struct tags.
func EncodeToJSON(o runtime.Object) ([]byte, error) {
scheme := runtime.NewScheme()
encoder := json.NewSerializerWithOptions(json.SimpleMetaFactory{}, scheme, scheme, json.SerializerOptions{
Yaml: false,
Pretty: false,
Strict: false,
})
buf := bytes.NewBuffer([]byte{})
if err := encoder.Encode(o, buf); err != nil {
return nil, err
}
return buf.Bytes(), nil
}