Skip to content

Commit

Permalink
Skip wait for Pods on headless Service
Browse files Browse the repository at this point in the history
The await logic for Service resources normally waits for selected Pods to be ready before marking the Service ready. This doesn't work correctly for StatefulSet resources, which use a "headless" Service (i.e., Service without a ClusterIP) to reference its Pods. This led to a circular dependency if the StatefulSet referenced the Service name in the Pulumi program, because the StatefulSet would not be created until after the Service was ready, but the Service was waiting on the StatefulSet to create Pods. This change fixes this dependency by skipping the Pod check for any headless Service, rather than only headless Services that don't have Pods selected.
  • Loading branch information
lblackstone committed Jun 27, 2023
1 parent 54c4096 commit 788fb9f
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 27 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

- [sdk/python] Fix bug with class methods for YAML transformations (https://github.com/pulumi/pulumi-kubernetes/pull/2469)
- Fix StatefulSet await logic for OnDelete update (https://github.com/pulumi/pulumi-kubernetes/pull/2473)
- Skip wait for Pods on headless Service (https://github.com/pulumi/pulumi-kubernetes/pull/2475)

## 3.29.1 (June 14, 2023)

Expand Down
19 changes: 2 additions & 17 deletions provider/pkg/await/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,7 @@ func (sia *serviceInitAwaiter) processEndpointEvent(event watch.Event, settledCh

func (sia *serviceInitAwaiter) errorMessages() []string {
messages := make([]string, 0)
if sia.emptyHeadlessOrExternalName() {
if sia.isHeadlessService() || sia.isExternalNameService() {
return messages
}

Expand Down Expand Up @@ -389,21 +389,6 @@ func (sia *serviceInitAwaiter) isExternalNameService() bool {
return sia.serviceType == string(v1.ServiceTypeExternalName)
}

// emptyHeadlessOrExternalName checks whether the current `Service` is either an "empty" headless
// `Service`[1] (i.e., it targets 0 `Pod`s) or a `Service` with `.spec.type: ExternalName` (which
// also targets 0 `Pod`s). This is useful to know when deciding whether to wait for a `Service` to
// target some number of `Pod`s.
//
// [1]: https://kubernetes.io/docs/concepts/services-networking/service/#headless-services
func (sia *serviceInitAwaiter) emptyHeadlessOrExternalName() bool {
selectorI, _ := openapi.Pluck(sia.service.Object, "spec", "selector")
selector, _ := selectorI.(map[string]any)

headlessEmpty := len(selector) == 0 && sia.isHeadlessService()
return headlessEmpty || sia.isExternalNameService()

}

// hasHeadlessServicePortBug checks whether the current `Service` is affected by a bug [1][2]
// that prevents endpoints from being populated when ports are not specified for a headless
// or external name Service.
Expand Down Expand Up @@ -434,7 +419,7 @@ func (sia *serviceInitAwaiter) hasHeadlessServicePortBug(version cluster.ServerV
// shouldWaitForPods determines whether to wait for Pods to be ready before marking the Service ready.
func (sia *serviceInitAwaiter) shouldWaitForPods(version cluster.ServerVersion) bool {
// For these special cases, skip the wait for Pod logic.
if sia.emptyHeadlessOrExternalName() || sia.hasHeadlessServicePortBug(version) {
if sia.isExternalNameService() || sia.isHeadlessService() || sia.hasHeadlessServicePortBug(version) {
sia.endpointsReady = true
return false
}
Expand Down
12 changes: 2 additions & 10 deletions provider/pkg/await/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ func Test_Core_Service(t *testing.T) {
},
},
{
description: "Should fail if non-empty headless service doesn't target any Pods",
description: "Should succeed if non-empty headless service doesn't target any Pods",
serviceInput: headlessNonemptyServiceInput,
version: cluster.ServerVersion{Major: 1, Minor: 12},
do: func(services, endpoints chan watch.Event, settled chan struct{}, timeout chan time.Time) {
Expand All @@ -211,11 +211,6 @@ func Test_Core_Service(t *testing.T) {
// Finally, time out.
timeout <- time.Now()
},
expectedError: &timeoutError{
object: headlessNonemptyServiceOutput("default", "foo-4setj4y6"),
subErrors: []string{
"Service does not target any Pods. Selected Pods may not be ready, or " +
"field '.spec.selector' may not match labels on any Pods"}},
},
}

Expand Down Expand Up @@ -284,13 +279,10 @@ func Test_Core_Service_Read(t *testing.T) {
version: cluster.ServerVersion{Major: 1, Minor: 11},
},
{
description: "Read fail if headless non-empty Service doesn't target any Pods",
description: "Read succeed if headless non-empty Service doesn't target any Pods",
serviceInput: headlessNonemptyServiceInput,
service: headlessNonemptyServiceInput,
version: cluster.ServerVersion{Major: 1, Minor: 12},
expectedSubErrors: []string{
"Service does not target any Pods. Selected Pods may not be ready, or " +
"field '.spec.selector' may not match labels on any Pods"},
},
}

Expand Down

0 comments on commit 788fb9f

Please sign in to comment.