forked from kyma-project/kyma
/
k8sresources_client.go
117 lines (94 loc) · 4.23 KB
/
k8sresources_client.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
package testkit
import (
"fmt"
"github.com/kyma-project/kyma/components/application-operator/pkg/apis/applicationconnector/v1alpha1"
"github.com/kyma-project/kyma/components/application-operator/pkg/client/clientset/versioned"
"github.com/kyma-project/kyma/components/application-registry/pkg/apis/istio/v1alpha2"
istioclient "github.com/kyma-project/kyma/components/application-registry/pkg/client/clientset/versioned"
v1core "k8s.io/api/core/v1"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/rand"
"k8s.io/client-go/kubernetes"
restclient "k8s.io/client-go/rest"
)
type K8sResourcesClient interface {
GetService(name string, options v1.GetOptions) (*v1core.Service, error)
GetSecret(name string, options v1.GetOptions) (*v1core.Secret, error)
GetDenier(name string, options v1.GetOptions) (*v1alpha2.Denier, error)
GetRule(name string, options v1.GetOptions) (*v1alpha2.Rule, error)
GetChecknothing(name string, options v1.GetOptions) (*v1alpha2.Checknothing, error)
GetApplicationServices(name string, options v1.GetOptions) (*v1alpha1.Application, error)
CreateDummyApplication(namePrefix string, options v1.GetOptions, skipInstallation bool) (*v1alpha1.Application, error)
DeleteApplication(name string, options *v1.DeleteOptions) error
}
type k8sResourcesClient struct {
coreClient *kubernetes.Clientset
istioClient *istioclient.Clientset
applicationClient *versioned.Clientset
namespace string
applicationName string
}
func NewK8sInClusterResourcesClient(namespace string) (K8sResourcesClient, error) {
k8sConfig, err := restclient.InClusterConfig()
if err != nil {
return nil, err
}
return initClient(k8sConfig, namespace)
}
func initClient(k8sConfig *restclient.Config, namespace string) (K8sResourcesClient, error) {
coreClientset, err := kubernetes.NewForConfig(k8sConfig)
if err != nil {
return nil, err
}
applicationClientset, err := versioned.NewForConfig(k8sConfig)
if err != nil {
return nil, err
}
istioClientset, err := istioclient.NewForConfig(k8sConfig)
if err != nil {
return nil, err
}
return &k8sResourcesClient{
coreClient: coreClientset,
istioClient: istioClientset,
applicationClient: applicationClientset,
namespace: namespace,
}, nil
}
func (c *k8sResourcesClient) GetService(name string, options v1.GetOptions) (*v1core.Service, error) {
return c.coreClient.CoreV1().Services(c.namespace).Get(name, options)
}
func (c *k8sResourcesClient) GetSecret(name string, options v1.GetOptions) (*v1core.Secret, error) {
return c.coreClient.CoreV1().Secrets(c.namespace).Get(name, options)
}
func (c *k8sResourcesClient) GetDenier(name string, options v1.GetOptions) (*v1alpha2.Denier, error) {
return c.istioClient.IstioV1alpha2().Deniers(c.namespace).Get(name, options)
}
func (c *k8sResourcesClient) GetRule(name string, options v1.GetOptions) (*v1alpha2.Rule, error) {
return c.istioClient.IstioV1alpha2().Rules(c.namespace).Get(name, options)
}
func (c *k8sResourcesClient) GetChecknothing(name string, options v1.GetOptions) (*v1alpha2.Checknothing, error) {
return c.istioClient.IstioV1alpha2().Checknothings(c.namespace).Get(name, options)
}
func (c *k8sResourcesClient) GetApplicationServices(name string, options v1.GetOptions) (*v1alpha1.Application, error) {
return c.applicationClient.ApplicationconnectorV1alpha1().Applications().Get(name, options)
}
func (c *k8sResourcesClient) CreateDummyApplication(namePrefix string, options v1.GetOptions, skipInstallation bool) (*v1alpha1.Application, error) {
spec := v1alpha1.ApplicationSpec{
Services: []v1alpha1.Service{},
SkipInstallation: skipInstallation,
}
dummyAppName := addRandomPostfix(namePrefix)
dummyApp := &v1alpha1.Application{
TypeMeta: v1.TypeMeta{Kind: "Application", APIVersion: v1alpha1.SchemeGroupVersion.String()},
ObjectMeta: v1.ObjectMeta{Name: dummyAppName},
Spec: spec,
}
return c.applicationClient.ApplicationconnectorV1alpha1().Applications().Create(dummyApp)
}
func addRandomPostfix(s string) string {
return fmt.Sprintf(s+"-%s", rand.String(5))
}
func (c *k8sResourcesClient) DeleteApplication(name string, options *v1.DeleteOptions) error {
return c.applicationClient.ApplicationconnectorV1alpha1().Applications().Delete(name, options)
}