This repository has been archived by the owner on Jan 11, 2024. It is now read-only.
/
serviceplan.go
155 lines (139 loc) · 4.11 KB
/
serviceplan.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
package clientset
import (
"encoding/json"
"errors"
platform "kolihub.io/koli/pkg/apis/core/v1alpha1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/watch"
"k8s.io/client-go/rest"
)
// ServicePlanGetter has a method to return an ServicePlanInterface.
// A group's client should implement this interface.
type ServicePlanGetter interface {
ServicePlan(namespace string) ServicePlanInterface
}
// ServicePlanInterface has methods to work with ServicePlan resources.
type ServicePlanInterface interface {
List(opts *metav1.ListOptions) (*platform.PlanList, error)
Get(name string) (*platform.Plan, error)
Delete(name string, options *metav1.DeleteOptions) error
Create(data *platform.Plan) (*platform.Plan, error)
Update(data *platform.Plan) (*platform.Plan, error)
Watch(opts *metav1.ListOptions) (watch.Interface, error)
}
// servicePlan implements ServicePlanInterface
type servicePlan struct {
client rest.Interface
namespace string
resource *metav1.APIResource
}
// Get gets the resource with the specified name.
func (s *servicePlan) Get(name string) (*platform.Plan, error) {
sps := &platform.Plan{}
err := s.client.Get().
NamespaceIfScoped(s.namespace, s.resource.Namespaced).
Resource(s.resource.Name).
Name(name).
Do().
Into(sps)
return sps, err
}
// List returns a list of objects for this resource.
func (s *servicePlan) List(opts *metav1.ListOptions) (*platform.PlanList, error) {
spList := &platform.PlanList{}
err := s.client.Get().
NamespaceIfScoped(s.namespace, s.resource.Namespaced).
Resource(s.resource.Name).
// VersionedParams(opts).
Do().
Into(spList)
return spList, err
}
// Delete deletes the resource with the specified name.
func (s *servicePlan) Delete(name string, opts *metav1.DeleteOptions) error {
return s.client.Delete().
NamespaceIfScoped(s.namespace, s.resource.Namespaced).
Resource(s.resource.Name).
Name(name).
// TODO: https://github.com/kubernetes/kubernetes/issues/37278
// error: no kind "DeleteOptions" is registered for version "<3PR>"
// Body(opts).
Do().
Error()
}
// Create creates the provided resource.
func (s *servicePlan) Create(data *platform.Plan) (*platform.Plan, error) {
sps := &platform.Plan{}
err := s.client.Post().
NamespaceIfScoped(s.namespace, s.resource.Namespaced).
Resource(s.resource.Name).
Body(data).
Do().
Into(sps)
return sps, err
}
// Update updates the provided resource.
func (s *servicePlan) Update(data *platform.Plan) (*platform.Plan, error) {
sps := &platform.Plan{}
if len(data.GetName()) == 0 {
return data, errors.New("object missing name")
}
err := s.client.Put().
NamespaceIfScoped(s.namespace, s.resource.Namespaced).
Resource(s.resource.Name).
Name(data.GetName()).
Body(data).
Do().
Into(sps)
return sps, err
}
// Watch returns a watch.Interface that watches the resource.
func (s *servicePlan) Watch(opts *metav1.ListOptions) (watch.Interface, error) {
stream, err := s.client.Get().
Prefix("watch").
NamespaceIfScoped(s.namespace, s.resource.Namespaced).
Resource(s.resource.Name).
VersionedParams(opts, metav1.ParameterCodec).
Stream()
if err != nil {
return nil, err
}
return watch.NewStreamWatcher(&servicePlanDecoder{
dec: json.NewDecoder(stream),
close: stream.Close,
}), nil
}
// Patch updates the provided resource
func (s *servicePlan) Patch(name string, pt types.PatchType, data []byte) (*platform.Plan, error) {
sps := &platform.Plan{}
err := s.client.Patch(pt).
NamespaceIfScoped(s.namespace, s.resource.Namespaced).
Resource(s.resource.Name).
Name(name).
Body(data).
Do().
Into(sps)
return sps, err
}
// provides a decoder for watching 3PR resources
type servicePlanDecoder struct {
dec *json.Decoder
close func() error
}
// Close decoder
func (d *servicePlanDecoder) Close() {
d.close()
}
// Decode data
func (d *servicePlanDecoder) Decode() (watch.EventType, runtime.Object, error) {
var e struct {
Type watch.EventType
Object platform.Plan
}
if err := d.dec.Decode(&e); err != nil {
return watch.Error, nil, err
}
return e.Type, &e.Object, nil
}