Skip to content

Commit 375f5fa

Browse files
authored
Change leader election logic (#158)
* Change leader election logic
1 parent 6cecdfa commit 375f5fa

24 files changed

+698
-75
lines changed

.golangci.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ linters:
4646
- tagliatelle
4747
- varnamelen
4848
- nilnil
49+
- godot
4950

5051
issues:
5152
exclude-rules:

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ All notable changes to this project will be documented in this file.
55
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
66
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).
77

8+
## [unpublished]
9+
- Add ability to specify key in failover password secret
10+
- Improve leader election logic
11+
812
## [1.0.0-rc1]
913

1014
### Added

apis/v1beta1/cluster_types.go

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package v1beta1
22

33
import (
44
"github.com/tarantool/tarantool-operator/pkg/api"
5-
corev1 "k8s.io/api/core/v1"
65
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
76
)
87

@@ -121,7 +120,7 @@ type FailoverEtcd2 struct {
121120

122121
// Password for etcd2 connection
123122
// +optional
124-
Password corev1.SecretReference `json:"password"`
123+
Password SecretKeyReference `json:"password"`
125124

126125
// LockDelay - Timeout (in seconds), determines lock’s time-to-live (default: 10)
127126
// +kubebuilder:default=10
@@ -141,8 +140,8 @@ func (in *FailoverEtcd2) GetUsername() string {
141140
return in.Username
142141
}
143142

144-
func (in *FailoverEtcd2) GetPassword() corev1.SecretReference {
145-
return in.Password
143+
func (in *FailoverEtcd2) GetPassword() api.SecretKeyReference {
144+
return &in.Password
146145
}
147146

148147
func (in *FailoverEtcd2) GetLockDelay() int32 {
@@ -162,15 +161,41 @@ type FailoverStateboard struct {
162161

163162
// Password for etcd2 connection
164163
// +optional
165-
Password corev1.SecretReference `json:"password"`
164+
Password SecretKeyReference `json:"password"`
166165
}
167166

168167
func (in *FailoverStateboard) GetURI() string {
169168
return in.URI
170169
}
171170

172-
func (in *FailoverStateboard) GetPassword() corev1.SecretReference {
173-
return in.Password
171+
func (in *FailoverStateboard) GetPassword() api.SecretKeyReference {
172+
return &in.Password
173+
}
174+
175+
// SecretKeyReference represents a reference to filed in Secret. It has enough information to retrieve a value secret in any namespace.
176+
// +structType=atomic
177+
type SecretKeyReference struct {
178+
// Namespace defines the space within which the secret name must be unique.
179+
// +optional
180+
Namespace string `json:"namespace"`
181+
// Name is unique within a namespace to reference a secret resource.
182+
// +optional
183+
Name string `json:"name"`
184+
// Key is unique within a Secret to reference a value.
185+
// +optional
186+
Key string `json:"key"`
187+
}
188+
189+
func (in *SecretKeyReference) GetNamespace() string {
190+
return in.Namespace
191+
}
192+
193+
func (in *SecretKeyReference) GetName() string {
194+
return in.Name
195+
}
196+
197+
func (in *SecretKeyReference) GetKey() string {
198+
return in.Key
174199
}
175200

176201
// ClusterPhase is a label for the condition of a Cluster at the current time.

apis/v1beta1/role_types.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -124,17 +124,17 @@ type RoleStatus struct {
124124
// Phase of roles
125125
// +kubebuilder:default=Pending
126126
Phase RolePhase `json:"phase"`
127+
128+
// ReadyPods a string in format "ready_pods_count/total_pods_count" for printable column
129+
ReadyPods string `json:"readyPods"`
127130
}
128131

129132
// Role is the Schema for the roles API
130133
// +kubebuilder:object:root=true
131134
// +kubebuilder:subresource:status
132135
// +kubebuilder:storageversion
133136
// +kubebuilder:printcolumn:name="Phase",type="string",JSONPath=".status.phase",priority=0
134-
// +kubebuilder:printcolumn:name="Replicasets",type="number",JSONPath=".status.replicasets",priority=1
135-
// +kubebuilder:printcolumn:name="Ready replicasets",type="number",JSONPath=".status.readyReplicasets",priority=1
136-
// +kubebuilder:printcolumn:name="Replicas",type="number",JSONPath=".status.replicas",priority=1
137-
// +kubebuilder:printcolumn:name="Ready replicas",type="number",JSONPath=".status.readyReplicas",priority=1
137+
// +kubebuilder:printcolumn:name="Pods",type="number",JSONPath=".status.readyPods",priority=1
138138
// +kubebuilder:printcolumn:name="Weight",type="number",JSONPath=".status.weight",priority=0
139139
// +kubebuilder:printcolumn:name="Age",type="date",JSONPath=".metadata.creationTimestamp",priority=0
140140
// +k8s:openapi-gen=true
@@ -171,6 +171,10 @@ func (in *Role) ResetStatus() {
171171
in.Status = RoleStatus{}
172172
}
173173

174+
func (in *Role) SetReadyPodsCount(count int32) {
175+
in.Status.ReadyPods = fmt.Sprintf("%d/%d", count, in.GetReplicasets()*in.GetReplicas())
176+
}
177+
174178
func (in *Role) SetPhase(phase RolePhase) {
175179
in.Status.Phase = phase
176180
}

apis/v1beta1/zz_generated.deepcopy.go

Lines changed: 15 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

config/crd/bases/tarantool.io_clusters.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,8 @@ spec:
111111
type: integer
112112
password:
113113
properties:
114+
key:
115+
type: string
114116
name:
115117
type: string
116118
namespace:
@@ -152,6 +154,8 @@ spec:
152154
properties:
153155
password:
154156
properties:
157+
key:
158+
type: string
155159
name:
156160
type: string
157161
namespace:

config/crd/bases/tarantool.io_roles.yaml

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3459,20 +3459,8 @@ spec:
34593459
- jsonPath: .status.phase
34603460
name: Phase
34613461
type: string
3462-
- jsonPath: .status.replicasets
3463-
name: Replicasets
3464-
priority: 1
3465-
type: number
3466-
- jsonPath: .status.readyReplicasets
3467-
name: Ready replicasets
3468-
priority: 1
3469-
type: number
3470-
- jsonPath: .status.replicas
3471-
name: Replicas
3472-
priority: 1
3473-
type: number
3474-
- jsonPath: .status.readyReplicas
3475-
name: Ready replicas
3462+
- jsonPath: .status.readyPods
3463+
name: Pods
34763464
priority: 1
34773465
type: number
34783466
- jsonPath: .status.weight
@@ -6833,8 +6821,11 @@ spec:
68336821
phase:
68346822
default: Pending
68356823
type: string
6824+
readyPods:
6825+
type: string
68366826
required:
68376827
- phase
6828+
- readyPods
68386829
type: object
68396830
required:
68406831
- metadata

controllers/cartridgeconfig_controller.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ func NewCartridgeConfigReconciler(mgr Manager) *CartridgeConfigReconciler {
7474
Client: k8sClient,
7575
Recorder: eventsRecorder,
7676
ResourcesManager: resourcesManager,
77+
Topology: luaTopology,
7778
},
7879
ResourcesManager: resourcesManager,
7980
EventsRecorder: eventsRecorder,

controllers/cluster_controller.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ func NewClusterReconciler(mgr Manager) *ClusterReconciler {
8989
Client: k8sClient,
9090
Recorder: eventsRecorder,
9191
ResourcesManager: resourcesManager,
92+
Topology: luaTopology,
9293
},
9394
ResourcesManager: resourcesManager,
9495
EventsRecorder: eventsRecorder,
@@ -144,6 +145,7 @@ func (r *ClusterReconciler) SetupWithManager(mgr Manager) error {
144145
return NewControllerManagedBy(mgr).
145146
For(&Cluster{}).
146147
Owns(&v1.Service{}).
148+
Watches(&source.Kind{Type: &v1.Secret{}}, &handler.EnqueueRequestForOwner{OwnerType: &Cluster{}, IsController: false}).
147149
Watches(&source.Kind{Type: &Role{}}, handler.EnqueueRequestsFromMapFunc(func(obj client.Object) []reconcile.Request {
148150
role, ok := obj.(*Role)
149151
if !ok {

controllers/cluster_controller_test.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ var _ = Describe("cluster_controller unit testing", func() {
7070
Client: fakeClient,
7171
Recorder: eventsRecorder,
7272
ResourcesManager: resourcesManager,
73+
Topology: fakeTopologyService,
7374
},
7475
ResourcesManager: resourcesManager,
7576
LabelsManager: labelsManager,
@@ -122,13 +123,17 @@ var _ = Describe("cluster_controller unit testing", func() {
122123
It("Must bootstrap cluster if all roles ready", func() {
123124
cartridge.
124125
WithAllRolesInPhase(v1beta1.RoleWaitingForBootstrap).
125-
WithAllPodsReady()
126+
WithAllPodsRunning()
126127

127128
fakeTopologyService.
128129
On("BootstrapVshard", mock.Anything, mock.Anything).
129130
Return(nil).
130131
Once()
131132

133+
fakeTopologyService.
134+
On("IsCartridgeStarted", mock.Anything, mock.Anything).
135+
Return(true, nil)
136+
132137
fakeClient := cartridge.BuildFakeClient()
133138

134139
resourcesManager := &ResourcesManager{
@@ -158,6 +163,7 @@ var _ = Describe("cluster_controller unit testing", func() {
158163
Client: fakeClient,
159164
Recorder: eventsRecorder,
160165
ResourcesManager: resourcesManager,
166+
Topology: fakeTopologyService,
161167
},
162168
ResourcesManager: resourcesManager,
163169
LabelsManager: labelsManager,
@@ -208,6 +214,7 @@ var _ = Describe("cluster_controller unit testing", func() {
208214
Client: fakeClient,
209215
Recorder: eventsRecorder,
210216
ResourcesManager: resourcesManager,
217+
Topology: fakeTopologyService,
211218
},
212219
ResourcesManager: resourcesManager,
213220
LabelsManager: labelsManager,
@@ -260,6 +267,7 @@ var _ = Describe("cluster_controller unit testing", func() {
260267
Client: fakeClient,
261268
Recorder: eventsRecorder,
262269
ResourcesManager: resourcesManager,
270+
Topology: fakeTopologyService,
263271
},
264272
ResourcesManager: resourcesManager,
265273
LabelsManager: labelsManager,

0 commit comments

Comments
 (0)