Skip to content

Commit 43458b5

Browse files
Don't set TargetOptions before initialize
Following on from [28deaa4], if a deployment fails to initialize we should avoid updating the `TargetOptions` in the state.
1 parent b62efc1 commit 43458b5

File tree

2 files changed

+17
-11
lines changed

2 files changed

+17
-11
lines changed

internal/server/router_test.go

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -222,8 +222,10 @@ func TestRouter_DeploymentsWithErrorsDoNotUpdateService(t *testing.T) {
222222
serviceOptions := defaultServiceOptions
223223
serviceOptions.Hosts = []string{"example.com"}
224224

225+
targetOptions := defaultTargetOptions
226+
225227
assert.NoFileExists(t, router.statePath)
226-
require.NoError(t, router.DeployService("service1", []string{target}, defaultEmptyReaders, serviceOptions, defaultTargetOptions, DefaultDeployTimeout, DefaultDrainTimeout))
228+
require.NoError(t, router.DeployService("service1", []string{target}, defaultEmptyReaders, serviceOptions, targetOptions, DefaultDeployTimeout, DefaultDrainTimeout))
227229
ensureServiceIsHealthy()
228230
require.FileExists(t, router.statePath)
229231

@@ -239,23 +241,30 @@ func TestRouter_DeploymentsWithErrorsDoNotUpdateService(t *testing.T) {
239241
sm.Set(service)
240242
}
241243
persistedOptions := sm.Get("service1").options
244+
persistedTargetOptions := sm.Get("service1").targetOptions
245+
242246
assert.Equal(t, serviceOptions.TLSPrivateKeyPath, persistedOptions.TLSPrivateKeyPath)
243247
assert.Equal(t, serviceOptions.TLSCertificatePath, persistedOptions.TLSCertificatePath)
244248
assert.Equal(t, serviceOptions.TLSEnabled, persistedOptions.TLSEnabled)
245249
assert.Equal(t, serviceOptions.ErrorPagePath, persistedOptions.ErrorPagePath)
250+
assert.Equal(t, targetOptions.BufferRequests, persistedTargetOptions.BufferRequests)
246251
}
247252

248253
t.Run("custom TLS that is not valid", func(t *testing.T) {
249254
newServiceOptions := ServiceOptions{TLSEnabled: true, TLSCertificatePath: "not valid", TLSPrivateKeyPath: "not valid"}
250-
require.Error(t, router.DeployService("service1", []string{target}, defaultEmptyReaders, newServiceOptions, defaultTargetOptions, DefaultDeployTimeout, DefaultDrainTimeout))
255+
newTargetOptions := TargetOptions{BufferRequests: true, HealthCheckConfig: defaultHealthCheckConfig}
256+
257+
require.Error(t, router.DeployService("service1", []string{target}, defaultEmptyReaders, newServiceOptions, newTargetOptions, DefaultDeployTimeout, DefaultDrainTimeout))
251258

252259
ensureServiceIsHealthy()
253260
ensureStateWasNotSaved()
254261
})
255262

256263
t.Run("custom error pages that are not valid", func(t *testing.T) {
257264
newServiceOptions := ServiceOptions{ErrorPagePath: "not valid"}
258-
require.Error(t, router.DeployService("service1", []string{target}, defaultEmptyReaders, newServiceOptions, defaultTargetOptions, DefaultDeployTimeout, DefaultDrainTimeout))
265+
newTargetOptions := TargetOptions{BufferRequests: true, HealthCheckConfig: defaultHealthCheckConfig}
266+
267+
require.Error(t, router.DeployService("service1", []string{target}, defaultEmptyReaders, newServiceOptions, newTargetOptions, DefaultDeployTimeout, DefaultDrainTimeout))
259268

260269
ensureServiceIsHealthy()
261270
ensureStateWasNotSaved()

internal/server/service.go

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -134,20 +134,17 @@ type Service struct {
134134
func NewService(name string, options ServiceOptions, targetOptions TargetOptions) (*Service, error) {
135135
service := &Service{
136136
name: name,
137-
targetOptions: targetOptions,
138137
pauseController: NewPauseController(),
139138
}
140139

141-
if err := service.initialize(options); err != nil {
140+
if err := service.initialize(options, targetOptions); err != nil {
142141
return nil, err
143142
}
144143
return service, nil
145144
}
146145

147146
func (s *Service) UpdateOptions(options ServiceOptions, targetOptions TargetOptions) error {
148-
s.targetOptions = targetOptions
149-
150-
return s.initialize(options)
147+
return s.initialize(options, targetOptions)
151148
}
152149

153150
func (s *Service) Dispose() {
@@ -266,7 +263,6 @@ func (s *Service) UnmarshalJSON(data []byte) error {
266263
s.name = ms.Name
267264
s.pauseController = ms.PauseController
268265
s.rolloutController = ms.RolloutController
269-
s.targetOptions = ms.TargetOptions
270266

271267
activeTargets, err := NewTargetList(ms.ActiveTargets, ms.ActiveReaders, ms.TargetOptions)
272268
if err != nil {
@@ -284,7 +280,7 @@ func (s *Service) UnmarshalJSON(data []byte) error {
284280
s.rollout.MarkAllHealthy()
285281
}
286282

287-
return s.initialize(ms.Options)
283+
return s.initialize(ms.Options, ms.TargetOptions)
288284
}
289285

290286
func (s *Service) Stop(drainTimeout time.Duration, message string) error {
@@ -325,7 +321,7 @@ func (s *Service) Resume() error {
325321

326322
// Private
327323

328-
func (s *Service) initialize(options ServiceOptions) error {
324+
func (s *Service) initialize(options ServiceOptions, targetOptions TargetOptions) error {
329325
certManager, err := s.createCertManager(options)
330326
if err != nil {
331327
return err
@@ -337,6 +333,7 @@ func (s *Service) initialize(options ServiceOptions) error {
337333
}
338334

339335
s.options = options
336+
s.targetOptions = targetOptions
340337
s.certManager = certManager
341338
s.middleware = middleware
342339

0 commit comments

Comments
 (0)