/
utils.go
195 lines (166 loc) · 5.64 KB
/
utils.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
// Copyright Mia srl
// SPDX-License-Identifier: Apache-2.0
//
// 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 deploy
import (
"context"
"encoding/json"
"fmt"
"github.com/mia-platform/mlp/pkg/resourceutil"
batchv1 "k8s.io/api/batch/v1"
corev1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
)
const (
resourceSecretName = "resources-deployed"
resourceField = "resources"
)
var (
gvrSecrets = schema.GroupVersionResource{Group: "", Version: "v1", Resource: "secrets"}
gvrNamespaces = schema.GroupVersionResource{Group: "", Version: "v1", Resource: "namespaces"}
gvrJobs = schema.GroupVersionResource{Group: batchv1.SchemeGroupVersion.Group, Version: batchv1.SchemeGroupVersion.Version, Resource: "jobs"}
)
// ResourceList is the base block used to build the secret containing
// the resources deployed in the cluster.
type ResourceList struct {
Gvk *schema.GroupVersionKind `json:"kind"` //nolint:tagliatelle
Resources []string `json:"resources"`
}
// makeResourceMap groups the resources list by kind and embeds them in a `ResourceList` struct
func makeResourceMap(resources []resourceutil.Resource) map[string]*ResourceList {
res := make(map[string]*ResourceList)
for _, r := range resources {
if _, ok := res[r.GroupVersionKind.Kind]; !ok {
res[r.GroupVersionKind.Kind] = &ResourceList{
Gvk: r.GroupVersionKind,
Resources: []string{},
}
}
res[r.GroupVersionKind.Kind].Resources = append(res[r.GroupVersionKind.Kind].Resources, r.Object.GetName())
}
return res
}
// Resources secrets created with helper/builer version of mlp is incompatible with newer versions
// this function convert old format in the new one
func convertSecretFormat(resources []byte) (map[string]*ResourceList, error) {
type oldResourceList struct {
Kind string `json:"kind"`
Mapping schema.GroupVersionResource
Resources []string `json:"resources"`
}
oldres := make(map[string]*oldResourceList)
err := json.Unmarshal(resources, &oldres)
if err != nil {
return nil, err
}
res := make(map[string]*ResourceList)
for k, v := range oldres {
res[k] = &ResourceList{
Gvk: &schema.GroupVersionKind{
Group: v.Mapping.Group,
Version: v.Mapping.Version,
Kind: k,
},
Resources: v.Resources}
}
return res, nil
}
// getOldResourceMap fetches the last set of resources deployed into the namespace from
// `resourceSecretName` secret.
func getOldResourceMap(clients *k8sClients, namespace string) (map[string]*ResourceList, error) {
var secret corev1.Secret
secretUnstr, err := clients.dynamic.Resource(gvrSecrets).
Namespace(namespace).Get(context.Background(), resourceSecretName, metav1.GetOptions{})
if err != nil {
if apierrors.IsNotFound(err) {
return map[string]*ResourceList{}, nil
}
return nil, err
}
err = runtime.DefaultUnstructuredConverter.
FromUnstructured(secretUnstr.Object, &secret)
if err != nil {
return nil, err
}
res := make(map[string]*ResourceList)
resources := secret.Data[resourceField]
if err := json.Unmarshal(resources, &res); err != nil {
var convertError error
res, convertError = convertSecretFormat(resources)
if convertError != nil {
return nil, fmt.Errorf("error unmarshalling resource map in secret %s: %s in namespace %s. Try to convert format from v0, but fails", resourceSecretName, err, namespace)
}
}
return res, nil
}
// deletedResources returns the resources not contained in the last deploy
func deletedResources(actual, old map[string]*ResourceList) map[string]*ResourceList {
res := make(map[string]*ResourceList)
// get diff on already existing resources, the new ones
// are added with the new secret.
for key := range old {
if _, ok := res[key]; !ok {
res[key] = &ResourceList{
Gvk: old[key].Gvk,
}
}
if _, ok := actual[key]; ok {
res[key].Resources = diffResourceArray(actual[key].Resources, old[key].Resources)
} else {
res[key].Resources = old[key].Resources
}
}
// Remove entries with empty diff
for kind, resourceGroup := range res {
if len(resourceGroup.Resources) == 0 {
delete(res, kind)
}
}
return res
}
// diffResourceArray returns the old values missing in the new slice
func diffResourceArray(actual, old []string) []string {
res := []string{}
for _, oValue := range old {
if !contains(actual, oValue) {
res = append(res, oValue)
}
}
return res
}
// contains takes a string slice and search for an element in it.
func contains(res []string, s string) bool {
for _, item := range res {
if item == s {
return true
}
}
return false
}
// convert runtime object to unstructured.Unstructured
func fromRuntimeObjtoUnstruct(obj runtime.Object, gvk schema.GroupVersionKind) (*unstructured.Unstructured, error) {
currentObj := &unstructured.Unstructured{}
currentObj.SetGroupVersionKind(gvk)
interfCurrentObj, err := runtime.DefaultUnstructuredConverter.ToUnstructured(&obj)
if err != nil {
return nil, err
}
return &unstructured.Unstructured{
Object: interfCurrentObj,
}, nil
}