Skip to content

Commit

Permalink
Allow skipAwait to skip only readiness or deletion
Browse files Browse the repository at this point in the history
  • Loading branch information
blampe committed Aug 1, 2024
1 parent 4f5e926 commit 2ca7228
Show file tree
Hide file tree
Showing 3 changed files with 122 additions and 17 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@
Existing readiness logic is unaffected by this setting.
(https://github.com/pulumi/pulumi-kubernetes/issues/2996)

- The `pulumi.com/skipAwait` annotation now accepts values of "ready" or
"delete" to skip await behavior when creating/updating or when deleting a
resource, respectively.
(https://github.com/pulumi/pulumi-kubernetes/issues/2551)

### Fixed

- Updated logic to accurately detect if a resource is a Patch variant. (https://github.com/pulumi/pulumi-kubernetes/pull/3102)
Expand Down
6 changes: 6 additions & 0 deletions provider/pkg/metadata/annotations.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,9 @@ func GetDeletedCondition(
if IsAnnotationTrue(obj, AnnotationSkipAwait) {
return condition.NewImmediate(logger, obj), nil
}
if strings.EqualFold(GetAnnotationValue(obj, AnnotationSkipAwait), "delete") {
return condition.NewImmediate(logger, obj), nil
}
getter, err := clientset.ResourceClientForObject(obj)
if err != nil {
return nil, err
Expand All @@ -143,6 +146,9 @@ func GetReadyCondition(
if IsAnnotationTrue(obj, AnnotationSkipAwait) {
return condition.NewImmediate(logger, obj), nil
}
if strings.EqualFold(GetAnnotationValue(obj, AnnotationSkipAwait), "ready") {
return condition.NewImmediate(logger, obj), nil
}
if os.Getenv("PULUMI_K8S_AWAIT_ALL") != "true" {
return condition.NewImmediate(nil, obj), nil
}
Expand Down
128 changes: 111 additions & 17 deletions provider/pkg/metadata/annotations_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import (
"testing"

"github.com/pulumi/pulumi-kubernetes/provider/v4/pkg/await/condition"
"github.com/pulumi/pulumi-kubernetes/provider/v4/pkg/logging"
"github.com/pulumi/pulumi/sdk/v3/go/common/resource"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -90,30 +89,99 @@ func TestSetAnnotation(t *testing.T) {
func TestGetReadyCondition(t *testing.T) {
tests := []struct {
name string
uns *unstructured.Unstructured
obj *unstructured.Unstructured
genericEnabled bool
want any
wantErr string
}{
{
name: "no annotation, generic await enabled",
uns: &unstructured.Unstructured{Object: map[string]any{}},
obj: &unstructured.Unstructured{Object: map[string]any{}},
genericEnabled: true,
want: &condition.Ready{},
},
{
name: "no annotation, generic await disabled",
uns: &unstructured.Unstructured{Object: map[string]any{}},
obj: &unstructured.Unstructured{Object: map[string]any{}},
want: condition.Immediate{},
},
{
name: "skipAwait=true, generic await disabled",
obj: &unstructured.Unstructured{Object: map[string]any{
"metadata": map[string]any{
"annotations": map[string]any{
AnnotationSkipAwait: "true",
},
},
}},
want: condition.Immediate{},
},
{
name: "skipAwait=true, generic await enabled",
obj: &unstructured.Unstructured{Object: map[string]any{
"metadata": map[string]any{
"annotations": map[string]any{
AnnotationSkipAwait: "true",
},
},
}},
genericEnabled: true,
want: condition.Immediate{},

Check failure on line 129 in provider/pkg/metadata/annotations_test.go

View workflow job for this annotation

GitHub Actions / lint

File is not `gofmt`-ed with `-s` (gofmt)
},
{
name: "skipAwait=ready, generic await disabled",
obj: &unstructured.Unstructured{Object: map[string]any{
"metadata": map[string]any{
"annotations": map[string]any{
AnnotationSkipAwait: "ready",
},
},
}},
want: condition.Immediate{},
},
{
name: "skipAwait=ready, generic await enabled",
obj: &unstructured.Unstructured{Object: map[string]any{
"metadata": map[string]any{
"annotations": map[string]any{
AnnotationSkipAwait: "ready",
},
},
}},
genericEnabled: true,
want: condition.Immediate{},
},
{
name: "skipAwait=delete, generic await disabled",
obj: &unstructured.Unstructured{Object: map[string]any{
"metadata": map[string]any{
"annotations": map[string]any{
AnnotationSkipAwait: "delete",
},
},
}},
want: condition.Immediate{},
},
{
name: "skipAwait=delete, generic await enabled",
obj: &unstructured.Unstructured{Object: map[string]any{
"metadata": map[string]any{
"annotations": map[string]any{
AnnotationSkipAwait: "delete",
},
},
}},
genericEnabled: true,
want: &condition.Ready{},
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if tt.genericEnabled {
t.Setenv("PULUMI_K8S_AWAIT_ALL", "true")
}
cond, err := GetReadyCondition(context.Background(), nil, nil, nil, tt.uns)
cond, err := GetReadyCondition(context.Background(), nil, nil, nil, tt.obj)
if tt.wantErr != "" {
assert.ErrorContains(t, err, tt.wantErr)
return
Expand All @@ -123,42 +191,68 @@ func TestGetReadyCondition(t *testing.T) {
}
}

func TestSkipAwait(t *testing.T) {
func TestGetDeletedCondition(t *testing.T) {
tests := []struct {
name string
getter func(context.Context, condition.Source, clientGetter, *logging.DedupLogger, *unstructured.Unstructured) (condition.Satisfier, error)
obj *unstructured.Unstructured
want condition.Satisfier
name string
obj *unstructured.Unstructured
want condition.Satisfier
}{
{
name: "skipAwait=true takes priority over delete condition",
name: "skipAwait=true",
obj: &unstructured.Unstructured{Object: map[string]any{
"metadata": map[string]any{
"annotations": map[string]any{
AnnotationSkipAwait: "true",
},
},
}},
getter: GetDeletedCondition,
want: condition.Immediate{},
want: condition.Immediate{},
},
{
name: "skipAwait=delete",
obj: &unstructured.Unstructured{Object: map[string]any{
"metadata": map[string]any{
"annotations": map[string]any{
AnnotationSkipAwait: "delete",
},
},
}},
want: condition.Immediate{},
},
{
name: "skipAwait=ready",
obj: &unstructured.Unstructured{Object: map[string]any{
"metadata": map[string]any{
"annotations": map[string]any{
AnnotationSkipAwait: "ready",
},
},
}},
want: &condition.Deleted{},
},
{
name: "skipAwait=false doesn't take priority over delete condition",
name: "skipAwait=false",
obj: &unstructured.Unstructured{Object: map[string]any{
"metadata": map[string]any{
"annotations": map[string]any{
AnnotationSkipAwait: "false",
},
},
}},
getter: GetDeletedCondition,
want: &condition.Deleted{},
want: &condition.Deleted{},
},
{
name: "skipAwait unset",
obj: &unstructured.Unstructured{Object: map[string]any{
"metadata": map[string]any{},
}},
want: &condition.Deleted{},
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
condition, err := tt.getter(context.Background(), nil, noopClientGetter{}, nil, tt.obj)
condition, err := GetDeletedCondition(context.Background(), nil, noopClientGetter{}, nil, tt.obj)
require.NoError(t, err)

assert.IsType(t, tt.want, condition)
Expand Down

0 comments on commit 2ca7228

Please sign in to comment.