This repository has been archived by the owner on Feb 5, 2021. It is now read-only.
/
component.go
106 lines (91 loc) · 2.87 KB
/
component.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
/*
* Copyright (c) 2019 WSO2 Inc. (http:www.wso2.org) All Rights Reserved.
*
* WSO2 Inc. licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except
* in compliance with the License.
* You may obtain a copy of the License at
*
* http:www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package v1alpha2
import (
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"cellery.io/cellery-controller/pkg/apis/mesh/v1alpha2"
"cellery.io/cellery-controller/pkg/ptr"
)
var OwnerReferenceComponentFunc = func(name string) metav1.OwnerReference {
return metav1.OwnerReference{
APIVersion: v1alpha2.SchemeGroupVersion.String(),
Kind: "Component",
Name: name,
Controller: ptr.Bool(true),
BlockOwnerDeletion: ptr.Bool(true),
}
}
type ComponentOption func(*v1alpha2.Component)
func Component(name, namespace string, opt ...ComponentOption) *v1alpha2.Component {
c := &v1alpha2.Component{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Namespace: namespace,
},
}
for _, opt := range opt {
opt(c)
}
return c
}
func ComponentWith(component *v1alpha2.Component, opt ...ComponentOption) *v1alpha2.Component {
for _, opt := range opt {
opt(component)
}
return component
}
func WithComponentType(cType v1alpha2.ComponentType) ComponentOption {
return func(c *v1alpha2.Component) {
c.Spec.Type = cType
}
}
func WithComponentPodSpec(podSpec *corev1.PodSpec) ComponentOption {
return func(c *v1alpha2.Component) {
c.Spec.Template = *podSpec
}
}
func WithComponentPortMaping(name string, protocol v1alpha2.Protocol, port int32, targetContainer string, targetPort int32) ComponentOption {
return func(c *v1alpha2.Component) {
c.Spec.Ports = append(c.Spec.Ports, v1alpha2.PortMapping{
Name: name,
Protocol: protocol,
Port: port,
TargetContainer: targetContainer,
TargetPort: targetPort,
})
}
}
func WithComponentVolumeClaim(shared bool, pvc *corev1.PersistentVolumeClaim) ComponentOption {
return func(c *v1alpha2.Component) {
c.Spec.VolumeClaims = append(c.Spec.VolumeClaims, v1alpha2.VolumeClaim{
Shared: shared,
Template: *pvc,
})
}
}
func WithComponentConfiguration(configMap *corev1.ConfigMap) ComponentOption {
return func(c *v1alpha2.Component) {
c.Spec.Configurations = append(c.Spec.Configurations, *configMap)
}
}
func WithComponentSecret(secret *corev1.Secret) ComponentOption {
return func(c *v1alpha2.Component) {
c.Spec.Secrets = append(c.Spec.Secrets, *secret)
}
}