forked from jenkins-x/jx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
crds.go
113 lines (98 loc) · 3.63 KB
/
crds.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
package kube
import (
jenkinsio "github.com/jenkins-x/jx/pkg/apis/jenkins.io"
"k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1beta1"
apiextensionsclientset "k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
// RegisterEnvironmentCRD ensures that the CRD is registered for Environments
func RegisterEnvironmentCRD(apiClient apiextensionsclientset.Interface) error {
name := "environments." + jenkinsio.GroupName
names := &v1beta1.CustomResourceDefinitionNames{
Kind: "Environment",
ListKind: "EnvironmentList",
Plural: "environments",
Singular: "environment",
ShortNames: []string{"env"},
}
return registerCRD(apiClient, name, names)
}
// RegisterEnvironmentRoleBindingCRD ensures that the CRD is registered for Environments
func RegisterEnvironmentRoleBindingCRD(apiClient apiextensionsclientset.Interface) error {
name := "environmentrolebindings." + jenkinsio.GroupName
names := &v1beta1.CustomResourceDefinitionNames{
Kind: "EnvironmentRoleBinding",
ListKind: "EnvironmentRoleBindingList",
Plural: "environmentrolebindings",
Singular: "environmentrolebinding",
ShortNames: []string{"envrolebindings", "envrb"},
}
return registerCRD(apiClient, name, names)
}
// RegisterGitServiceCRD ensures that the CRD is registered for GitServices
func RegisterGitServiceCRD(apiClient apiextensionsclientset.Interface) error {
name := "gitservices." + jenkinsio.GroupName
names := &v1beta1.CustomResourceDefinitionNames{
Kind: "GitService",
ListKind: "GitServiceList",
Plural: "gitservices",
Singular: "gitservice",
ShortNames: []string{"gits"},
}
return registerCRD(apiClient, name, names)
}
// RegisterPipelineActivityCRD ensures that the CRD is registered for PipelineActivity
func RegisterPipelineActivityCRD(apiClient apiextensionsclientset.Interface) error {
name := "pipelineactivities." + jenkinsio.GroupName
names := &v1beta1.CustomResourceDefinitionNames{
Kind: "PipelineActivity",
ListKind: "PipelineActivityList",
Plural: "pipelineactivities",
Singular: "pipelineactivity",
ShortNames: []string{"activity", "act"},
}
return registerCRD(apiClient, name, names)
}
// RegisterReleaseCRD ensures that the CRD is registered for Release
func RegisterReleaseCRD(apiClient apiextensionsclientset.Interface) error {
name := "releases." + jenkinsio.GroupName
names := &v1beta1.CustomResourceDefinitionNames{
Kind: "Release",
ListKind: "ReleaseList",
Plural: "releases",
Singular: "release",
ShortNames: []string{"rel"},
}
return registerCRD(apiClient, name, names)
}
// RegisterUserCRD ensures that the CRD is registered for User
func RegisterUserCRD(apiClient apiextensionsclientset.Interface) error {
name := "users." + jenkinsio.GroupName
names := &v1beta1.CustomResourceDefinitionNames{
Kind: "User",
ListKind: "UserList",
Plural: "users",
Singular: "user",
ShortNames: []string{"usr"},
}
return registerCRD(apiClient, name, names)
}
func registerCRD(apiClient apiextensionsclientset.Interface, name string, names *v1beta1.CustomResourceDefinitionNames) error {
_, err := apiClient.ApiextensionsV1beta1().CustomResourceDefinitions().Get(name, metav1.GetOptions{})
if err == nil {
return nil
}
crd := &v1beta1.CustomResourceDefinition{
ObjectMeta: metav1.ObjectMeta{
Name: name,
},
Spec: v1beta1.CustomResourceDefinitionSpec{
Group: jenkinsio.GroupName,
Version: jenkinsio.Version,
Scope: v1beta1.NamespaceScoped,
Names: *names,
},
}
_, err = apiClient.ApiextensionsV1beta1().CustomResourceDefinitions().Create(crd)
return err
}