/
shootstate_list.go
158 lines (138 loc) · 5.37 KB
/
shootstate_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
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
// Copyright (c) 2018 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 helper
import (
gardencorev1alpha1 "github.com/gardener/gardener/pkg/apis/core/v1alpha1"
autoscalingv1 "k8s.io/api/autoscaling/v1"
apiequality "k8s.io/apimachinery/pkg/api/equality"
"k8s.io/apimachinery/pkg/labels"
)
// ExtensionResourceStateList is a list of ExtensionResourceStates
type ExtensionResourceStateList []gardencorev1alpha1.ExtensionResourceState
// Get retrieves an ExtensionResourceState for given kind, name and purpose from a list of ExtensionResourceStates
// If no ExtensionResourceStates can be found, nil is returned.
func (e *ExtensionResourceStateList) Get(kind string, name, purpose *string) *gardencorev1alpha1.ExtensionResourceState {
for _, obj := range *e {
if matchesExtensionResourceState(&obj, kind, name, purpose) {
return &obj
}
}
return nil
}
// Delete removes an ExtensionResourceState from the list by kind, name and purpose
func (e *ExtensionResourceStateList) Delete(kind string, name, purpose *string) {
for i := len(*e) - 1; i >= 0; i-- {
if matchesExtensionResourceState(&(*e)[i], kind, name, purpose) {
*e = append((*e)[:i], (*e)[i+1:]...)
return
}
}
}
// Upsert either inserts or updates an already existing ExtensionResourceState with kind, name and purpose in the list
func (e *ExtensionResourceStateList) Upsert(extensionResourceState *gardencorev1alpha1.ExtensionResourceState) {
for i, obj := range *e {
if matchesExtensionResourceState(&obj, extensionResourceState.Kind, extensionResourceState.Name, extensionResourceState.Purpose) {
(*e)[i].State = extensionResourceState.State
(*e)[i].Resources = extensionResourceState.Resources
return
}
}
*e = append(*e, *extensionResourceState)
}
func matchesExtensionResourceState(extensionResourceState *gardencorev1alpha1.ExtensionResourceState, kind string, name, purpose *string) bool {
if extensionResourceState.Kind == kind && apiequality.Semantic.DeepEqual(extensionResourceState.Name, name) && apiequality.Semantic.DeepEqual(extensionResourceState.Purpose, purpose) {
return true
}
return false
}
// GardenerResourceDataList is a list of GardenerResourceData
type GardenerResourceDataList []gardencorev1alpha1.GardenerResourceData
// Delete deletes an item from the list
func (g *GardenerResourceDataList) Delete(name string) {
for i := len(*g) - 1; i >= 0; i-- {
if (*g)[i].Name == name {
*g = append((*g)[:i], (*g)[i+1:]...)
return
}
}
}
// Get returns the item from the list
func (g *GardenerResourceDataList) Get(name string) *gardencorev1alpha1.GardenerResourceData {
for _, resourceDataEntry := range *g {
if resourceDataEntry.Name == name {
return &resourceDataEntry
}
}
return nil
}
// Select uses the provided label selector to find data entries with matching labels.
func (g *GardenerResourceDataList) Select(labelSelector labels.Selector) []*gardencorev1alpha1.GardenerResourceData {
var results []*gardencorev1alpha1.GardenerResourceData
for _, resourceDataEntry := range *g {
if labelSelector.Matches(labels.Set(resourceDataEntry.Labels)) {
results = append(results, resourceDataEntry.DeepCopy())
}
}
return results
}
// Upsert inserts a new element or updates an existing one
func (g *GardenerResourceDataList) Upsert(data *gardencorev1alpha1.GardenerResourceData) {
for i, obj := range *g {
if obj.Name == data.Name {
(*g)[i].Type = data.Type
(*g)[i].Data = data.Data
(*g)[i].Labels = data.Labels
return
}
}
*g = append(*g, *data)
}
// DeepCopy makes a deep copy of a GardenerResourceDataList
func (g GardenerResourceDataList) DeepCopy() GardenerResourceDataList {
res := GardenerResourceDataList{}
for _, obj := range g {
res = append(res, *obj.DeepCopy())
}
return res
}
// ResourceDataList is a list of ResourceData
type ResourceDataList []gardencorev1alpha1.ResourceData
// Delete deletes an item from the list
func (r *ResourceDataList) Delete(ref *autoscalingv1.CrossVersionObjectReference) {
for i := len(*r) - 1; i >= 0; i-- {
if apiequality.Semantic.DeepEqual((*r)[i].CrossVersionObjectReference, *ref) {
*r = append((*r)[:i], (*r)[i+1:]...)
return
}
}
}
// Get returns the item from the list
func (r *ResourceDataList) Get(ref *autoscalingv1.CrossVersionObjectReference) *gardencorev1alpha1.ResourceData {
for _, obj := range *r {
if apiequality.Semantic.DeepEqual(obj.CrossVersionObjectReference, *ref) {
return &obj
}
}
return nil
}
// Upsert inserts a new element or updates an existing one
func (r *ResourceDataList) Upsert(data *gardencorev1alpha1.ResourceData) {
for i, obj := range *r {
if apiequality.Semantic.DeepEqual(obj.CrossVersionObjectReference, data.CrossVersionObjectReference) {
(*r)[i].Data = data.Data
return
}
}
*r = append(*r, *data)
}