/
generate.go
145 lines (133 loc) · 3.46 KB
/
generate.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package generate
import (
"github.com/coreos/go-semver/semver"
apps "k8s.io/api/apps/v1beta1"
core "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"github.com/jetstack/navigator/pkg/apis/navigator/v1alpha1"
)
type PilotConfig struct {
Name, Namespace string
Cluster, NodePool string
Documents *int64
Version string
}
func Pilot(c PilotConfig) *v1alpha1.Pilot {
if c.Namespace == "" {
c.Namespace = "default"
}
labels := map[string]string{}
labels[v1alpha1.ElasticsearchClusterNameLabel] = c.Cluster
labels[v1alpha1.ElasticsearchNodePoolNameLabel] = c.NodePool
var version *semver.Version
if c.Version != "" {
version = semver.New(c.Version)
}
return &v1alpha1.Pilot{
ObjectMeta: metav1.ObjectMeta{
Name: c.Name,
Namespace: c.Namespace,
Labels: labels,
},
Status: v1alpha1.PilotStatus{
Elasticsearch: &v1alpha1.ElasticsearchPilotStatus{
Documents: c.Documents,
Version: version,
},
},
}
}
type ClusterConfig struct {
Name, Namespace string
NodePools []v1alpha1.ElasticsearchClusterNodePool
Version string
ClusterConfig v1alpha1.NavigatorClusterConfig
Health *v1alpha1.ElasticsearchClusterHealth
}
func Cluster(c ClusterConfig) *v1alpha1.ElasticsearchCluster {
if c.Namespace == "" {
c.Namespace = "default"
}
return &v1alpha1.ElasticsearchCluster{
ObjectMeta: metav1.ObjectMeta{
Name: c.Name,
Namespace: c.Namespace,
},
Spec: v1alpha1.ElasticsearchClusterSpec{
NavigatorClusterConfig: c.ClusterConfig,
Version: *semver.New(c.Version),
NodePools: c.NodePools,
},
Status: v1alpha1.ElasticsearchClusterStatus{
Health: c.Health,
},
}
}
type StatefulSetConfig struct {
Name, Namespace string
Replicas *int32
Version, Image string
Partition *int32
CurrentRevision, UpdateRevision string
CurrentReplicas, UpdatedReplicas int32
ReadyReplicas int32
}
func StatefulSet(c StatefulSetConfig) *apps.StatefulSet {
if c.Namespace == "" {
c.Namespace = "default"
}
return &apps.StatefulSet{
ObjectMeta: metav1.ObjectMeta{
Name: c.Name,
Namespace: c.Namespace,
Annotations: map[string]string{
v1alpha1.ElasticsearchNodePoolVersionAnnotation: c.Version,
},
},
Spec: apps.StatefulSetSpec{
Replicas: c.Replicas,
UpdateStrategy: apps.StatefulSetUpdateStrategy{
RollingUpdate: &apps.RollingUpdateStatefulSetStrategy{
Partition: c.Partition,
},
},
Template: core.PodTemplateSpec{
Spec: core.PodSpec{
Containers: []core.Container{
{
Image: c.Image,
},
},
},
},
},
Status: apps.StatefulSetStatus{
CurrentRevision: c.CurrentRevision,
UpdateRevision: c.UpdateRevision,
CurrentReplicas: c.CurrentReplicas,
UpdatedReplicas: c.UpdatedReplicas,
ReadyReplicas: c.ReadyReplicas,
},
}
}
type CassandraClusterConfig struct {
Name, Namespace string
}
func CassandraCluster(c CassandraClusterConfig) *v1alpha1.CassandraCluster {
return &v1alpha1.CassandraCluster{
ObjectMeta: metav1.ObjectMeta{
Name: c.Name,
Namespace: c.Namespace,
},
}
}
type CassandraClusterNodePoolConfig struct {
Name string
Replicas int32
}
func CassandraClusterNodePool(c CassandraClusterNodePoolConfig) *v1alpha1.CassandraClusterNodePool {
return &v1alpha1.CassandraClusterNodePool{
Name: c.Name,
Replicas: &c.Replicas,
}
}