This repository has been archived by the owner on Mar 23, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
custom_resource.go
107 lines (92 loc) · 2.64 KB
/
custom_resource.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
package kubekit
import (
"fmt"
"reflect"
"strings"
"k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1beta1"
apiextv1beta1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1beta1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
)
// CustomResource describes the configuration values for a
// CustomResourceDefinition.
type CustomResource struct {
Name string
Plural string
Group string
Version string
Aliases []string
Scope v1beta1.ResourceScope
Object runtime.Object
Validation *v1beta1.CustomResourceValidation
}
// GetName returns the CustomResource Name. If no name is specified, the
// lowercased kind is used.
func (c CustomResource) GetName() string {
if c.Name != "" {
return c.Name
}
return strings.ToLower(c.Kind())
}
// GetPlural returns the pluralisation of the CustomResource. If no Plural value
// is specified, an 's' will be added to the Name.
func (c CustomResource) GetPlural() string {
if c.Plural != "" {
return c.Plural
}
return fmt.Sprintf("%ss", c.GetName())
}
// FullName returns the combined name of the Plural and the Group.
func (c CustomResource) FullName() string {
return c.GetPlural() + "." + c.Group
}
// GroupVersion returns the GroupVersion Schema representation of this
// CustomResource.
func (c CustomResource) GroupVersion() schema.GroupVersion {
return schema.GroupVersion{
Group: c.Group,
Version: c.Version,
}
}
// GroupVersionKind returns the GroupVersionKind Schema representation of this
// CustomResource.
func (c CustomResource) GroupVersionKind() schema.GroupVersionKind {
return c.GroupVersion().WithKind(c.Kind())
}
// Kind returns the Type Name of the CustomResource Object.
func (c CustomResource) Kind() string {
return TypeName(c.Object)
}
// Definition returns the CustomResourceDefinition that is linked to this
// CustomResource.
func (c CustomResource) Definition() *apiextv1beta1.CustomResourceDefinition {
return &apiextv1beta1.CustomResourceDefinition{
ObjectMeta: metav1.ObjectMeta{
Name: c.FullName(),
},
Spec: apiextv1beta1.CustomResourceDefinitionSpec{
Group: c.Group,
Version: c.Version,
Scope: c.Scope,
Names: apiextv1beta1.CustomResourceDefinitionNames{
Plural: c.Plural,
ShortNames: c.Aliases,
Kind: c.Kind(),
},
Validation: c.Validation,
},
}
}
// TypeName returns the Type Name of a given object.
func TypeName(o interface{}) string {
val := reflect.ValueOf(o)
var name string
switch val.Kind() {
case reflect.Ptr:
name = val.Elem().Type().Name()
default:
name = val.Type().Name()
}
return name
}