forked from grafana/xk6-kubernetes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathservices.go
111 lines (97 loc) · 3.13 KB
/
services.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
// Package services provides implementation of Service resources for Kubernetes
//
// Deprecated: Use the resources package instead.
package services
import (
"context"
"errors"
k8sTypes "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/kubernetes/scheme"
)
// New creates a new instance backed by the provided client
//
// Deprecated: No longer used.
func New(ctx context.Context, client kubernetes.Interface, metaOptions metav1.ListOptions) *Services {
return &Services{
client,
metaOptions,
ctx,
}
}
// Services provides API for manipulating Service resources within a Kubernetes cluster
//
// Deprecated: No longer used in favor of generic resources.
type Services struct {
client kubernetes.Interface
metaOptions metav1.ListOptions
ctx context.Context
}
// Apply creates the Kubernetes resource given the supplied YAML configuration
//
// Deprecated: Use resources.Apply instead.
func (obj *Services) Apply(yaml string, namespace string) (k8sTypes.Service, error) {
decode := scheme.Codecs.UniversalDeserializer().Decode
yamlobj, _, err := decode([]byte(yaml), nil, nil)
service := k8sTypes.Service{}
if err != nil {
return service, err
}
if svc, ok := yamlobj.(*k8sTypes.Service); ok {
service = *svc
} else {
return service, errors.New("YAML was not a Service")
}
svc, err := obj.client.CoreV1().Services(namespace).Create(obj.ctx, &service, metav1.CreateOptions{})
if err != nil {
return k8sTypes.Service{}, err
}
return *svc, nil
}
// Create creates the Kubernetes resource given the supplied object
//
// Deprecated: Use resources.Create instead.
func (obj *Services) Create(
service k8sTypes.Service,
namespace string,
opts metav1.CreateOptions,
) (k8sTypes.Service, error) {
svc, err := obj.client.CoreV1().Services(namespace).Create(obj.ctx, &service, opts)
if err != nil {
return k8sTypes.Service{}, err
}
return *svc, nil
}
// List returns a collection of Services available within the namespace
//
// Deprecated: Use resources.List instead.
func (obj *Services) List(namespace string) ([]k8sTypes.Service, error) {
svcs, err := obj.client.CoreV1().Services(namespace).List(obj.ctx, obj.metaOptions)
if err != nil {
return []k8sTypes.Service{}, err
}
return svcs.Items, nil
}
// Delete removes the named Service from the namespace
//
// Deprecated: Use resources.Delete instead.
func (obj *Services) Delete(name, namespace string, opts metav1.DeleteOptions) error {
return obj.client.CoreV1().Services(namespace).Delete(obj.ctx, name, opts)
}
// Kill removes the named Service from the namespace
//
// Deprecated: Use resources.Delete instead.
func (obj *Services) Kill(name, namespace string, opts metav1.DeleteOptions) error {
return obj.Delete(name, namespace, opts)
}
// Get returns the named Services instance within the namespace if available
//
// Deprecated: Use resources.Get instead.
func (obj *Services) Get(name, namespace string, opts metav1.GetOptions) (k8sTypes.Service, error) {
svc, err := obj.client.CoreV1().Services(namespace).Get(obj.ctx, name, opts)
if err != nil {
return k8sTypes.Service{}, err
}
return *svc, nil
}