forked from kyma-project/kyma
/
replicaset_service.go
129 lines (103 loc) · 3.85 KB
/
replicaset_service.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
package k8s
import (
"fmt"
"github.com/kyma-project/kyma/components/console-backend-service/pkg/resource"
apps "k8s.io/api/apps/v1"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/util/validation/field"
appsv1 "k8s.io/client-go/kubernetes/typed/apps/v1"
"github.com/kyma-project/kyma/components/console-backend-service/internal/pager"
"k8s.io/client-go/tools/cache"
)
type replicaSetService struct {
client appsv1.AppsV1Interface
informer cache.SharedIndexInformer
}
func newReplicaSetService(informer cache.SharedIndexInformer, client appsv1.AppsV1Interface) *replicaSetService {
notifier := resource.NewNotifier()
informer.AddEventHandler(notifier)
return &replicaSetService{
client: client,
informer: informer,
}
}
func (svc *replicaSetService) Find(name, namespace string) (*apps.ReplicaSet, error) {
key := fmt.Sprintf("%s/%s", namespace, name)
item, exists, err := svc.informer.GetStore().GetByKey(key)
if err != nil || !exists {
return nil, err
}
replicaSet, ok := item.(*apps.ReplicaSet)
if !ok {
return nil, fmt.Errorf("Incorrect item type: %T, should be: *ReplicaSet", item)
}
svc.ensureTypeMeta(replicaSet)
return replicaSet, nil
}
func (svc *replicaSetService) List(namespace string, pagingParams pager.PagingParams) ([]*apps.ReplicaSet, error) {
items, err := pager.FromIndexer(svc.informer.GetIndexer(), "namespace", namespace).Limit(pagingParams)
if err != nil {
return nil, err
}
var replicaSets []*apps.ReplicaSet
for _, item := range items {
replicaSet, ok := item.(*apps.ReplicaSet)
if !ok {
return nil, fmt.Errorf("Incorrect item type: %T, should be: *ReplicaSet", item)
}
svc.ensureTypeMeta(replicaSet)
replicaSets = append(replicaSets, replicaSet)
}
return replicaSets, nil
}
func (svc *replicaSetService) Update(name, namespace string, update apps.ReplicaSet) (*apps.ReplicaSet, error) {
err := svc.checkUpdatePreconditions(name, namespace, update)
if err != nil {
return nil, err
}
updated, err := svc.client.ReplicaSets(namespace).Update(&update)
if err != nil {
return nil, err
}
svc.ensureTypeMeta(updated)
return updated, nil
}
func (svc *replicaSetService) Delete(name, namespace string) error {
return svc.client.ReplicaSets(namespace).Delete(name, nil)
}
func (svc *replicaSetService) checkUpdatePreconditions(name string, namespace string, update apps.ReplicaSet) error {
errorList := field.ErrorList{}
if name != update.Name {
errorList = append(errorList, field.Invalid(field.NewPath("metadata.name"), update.Name, fmt.Sprintf("name of updated object does not match the original (%s)", name)))
}
if namespace != update.Namespace {
errorList = append(errorList, field.Invalid(field.NewPath("metadata.namespace"), update.Namespace, fmt.Sprintf("namespace of updated object does not match the original (%s)", namespace)))
}
typeMeta := svc.replicaSetTypeMeta()
if update.Kind != typeMeta.Kind {
errorList = append(errorList, field.Invalid(field.NewPath("kind"), update.Kind, "ReplicaSet's kind should not be changed"))
}
if update.APIVersion != typeMeta.APIVersion {
errorList = append(errorList, field.Invalid(field.NewPath("apiVersion"), update.APIVersion, "ReplicaSet's apiVersion should not be changed"))
}
if len(errorList) > 0 {
return errors.NewInvalid(schema.GroupKind{
Group: "",
Kind: "ReplicaSet",
}, name, errorList)
}
return nil
}
// Kubernetes API used by client-go doesn't provide kind and apiVersion so we have to add it here
// See: https://github.com/kubernetes/kubernetes/issues/3030
func (svc *replicaSetService) ensureTypeMeta(replicaSet *apps.ReplicaSet) {
replicaSet.TypeMeta = svc.replicaSetTypeMeta()
}
func (svc *replicaSetService) replicaSetTypeMeta() metav1.TypeMeta {
return metav1.TypeMeta{
Kind: "ReplicaSet",
APIVersion: "apps/v1",
}
}