Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow awaiters to be skipped by setting an annotation #417

Merged
merged 2 commits into from
Feb 12, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
100 changes: 58 additions & 42 deletions pkg/await/await.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"fmt"

"github.com/golang/glog"
"github.com/pulumi/pulumi-kubernetes/pkg/metadata"
"github.com/pulumi/pulumi-kubernetes/pkg/clients"
"github.com/pulumi/pulumi-kubernetes/pkg/openapi"
"github.com/pulumi/pulumi-kubernetes/pkg/retry"
Expand Down Expand Up @@ -131,19 +132,22 @@ func Creation(c CreateConfig) (*unstructured.Unstructured, error) {
// logic is blank, simply do nothing instead of logging.
id := fmt.Sprintf("%s/%s", c.Inputs.GetAPIVersion(), c.Inputs.GetKind())
if awaiter, exists := awaiters[id]; exists {
if awaiter.awaitCreation != nil {
conf := createAwaitConfig{
host: c.Host,
ctx: c.Context,
urn: c.URN,
clientSet: c.ClientSet,
// TODO(lblackstone): maybe pass create output into input here?
currentInputs: c.Inputs,
currentOutputs: outputs,
}
waitErr := awaiter.awaitCreation(conf)
if waitErr != nil {
return nil, waitErr
if metadata.SkipAwaitLogic(c.Inputs) {
glog.V(1).Infof("Skipping await logic for %v", c.Inputs.GetName())
} else {
if awaiter.awaitCreation != nil {
conf := createAwaitConfig{
host: c.Host,
ctx: c.Context,
urn: c.URN,
clientSet: c.ClientSet,
currentInputs: c.Inputs,
currentOutputs: outputs,
}
waitErr := awaiter.awaitCreation(conf)
if waitErr != nil {
return nil, waitErr
}
}
}
} else {
Expand Down Expand Up @@ -173,18 +177,22 @@ func Read(c ReadConfig) (*unstructured.Unstructured, error) {

id := fmt.Sprintf("%s/%s", outputs.GetAPIVersion(), outputs.GetKind())
if awaiter, exists := awaiters[id]; exists {
if awaiter.awaitRead != nil {
conf := createAwaitConfig{
host: c.Host,
ctx: c.Context,
urn: c.URN,
clientSet: c.ClientSet,
currentInputs: c.Inputs,
currentOutputs: outputs,
}
waitErr := awaiter.awaitRead(conf)
if waitErr != nil {
return nil, waitErr
if metadata.SkipAwaitLogic(c.Inputs) {
glog.V(1).Infof("Skipping await logic for %v", c.Inputs.GetName())
} else {
if awaiter.awaitRead != nil {
conf := createAwaitConfig{
host: c.Host,
ctx: c.Context,
urn: c.URN,
clientSet: c.ClientSet,
currentInputs: c.Inputs,
currentOutputs: outputs,
}
waitErr := awaiter.awaitRead(conf)
if waitErr != nil {
return nil, waitErr
}
}
}
}
Expand Down Expand Up @@ -284,22 +292,26 @@ func Update(c UpdateConfig) (*unstructured.Unstructured, error) {
// is blank, simply do nothing instead of logging.
id := fmt.Sprintf("%s/%s", c.Inputs.GetAPIVersion(), c.Inputs.GetKind())
if awaiter, exists := awaiters[id]; exists {
if awaiter.awaitUpdate != nil {
conf := updateAwaitConfig{
createAwaitConfig: createAwaitConfig{
host: c.Host,
ctx: c.Context,
urn: c.URN,
clientSet: c.ClientSet,
currentInputs: c.Inputs,
currentOutputs: currentOutputs,
},
lastInputs: c.Previous,
lastOutputs: liveOldObj,
}
waitErr := awaiter.awaitUpdate(conf)
if waitErr != nil {
return nil, waitErr
if metadata.SkipAwaitLogic(c.Inputs) {
glog.V(1).Infof("Skipping await logic for %v", c.Inputs.GetName())
} else {
if awaiter.awaitUpdate != nil {
conf := updateAwaitConfig{
createAwaitConfig: createAwaitConfig{
host: c.Host,
ctx: c.Context,
urn: c.URN,
clientSet: c.ClientSet,
currentInputs: c.Inputs,
currentOutputs: currentOutputs,
},
lastInputs: c.Previous,
lastOutputs: liveOldObj,
}
waitErr := awaiter.awaitUpdate(conf)
if waitErr != nil {
return nil, waitErr
}
}
}
} else {
Expand Down Expand Up @@ -391,7 +403,11 @@ func Deletion(c DeleteConfig) error {
var waitErr error
id := fmt.Sprintf("%s/%s", c.Inputs.GetAPIVersion(), c.Inputs.GetKind())
if awaiter, exists := awaiters[id]; exists && awaiter.awaitDeletion != nil {
waitErr = awaiter.awaitDeletion(c.Context, client, c.Name)
if metadata.SkipAwaitLogic(c.Inputs) {
glog.V(1).Infof("Skipping await logic for %v", c.Inputs.GetName())
} else {
waitErr = awaiter.awaitDeletion(c.Context, client, c.Name)
}
} else {
for {
select {
Expand Down
63 changes: 63 additions & 0 deletions pkg/metadata/annotations.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// Copyright 2016-2019, Pulumi Corporation.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package metadata

import (
"strings"

"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
)

const (
AnnotationTrue = "true"
AnnotationFalse = "false"

AnnotationPrefix = "pulumi.com/"

AnnotationAutonamed = AnnotationPrefix + "autonamed"
AnnotationSkipAwait = AnnotationPrefix + "skipAwait"
)

// Annotations for internal Pulumi use only.
var internalAnnotationPrefixes = []string{AnnotationAutonamed}

func IsInternalAnnotation(key string) bool {
for _, annotationPrefix := range internalAnnotationPrefixes {
if strings.HasPrefix(key, annotationPrefix) {
return true
}
}

return false
}

func SetAnnotation(obj *unstructured.Unstructured, key, value string) {
annotations := obj.GetAnnotations()
if annotations == nil {
annotations = map[string]string{}
}
annotations[key] = value
obj.SetAnnotations(annotations)
}

func SetAnnotationTrue(obj *unstructured.Unstructured, key string) {
SetAnnotation(obj, key, AnnotationTrue)
}

func IsAnnotationTrue(obj *unstructured.Unstructured, key string) bool {
annotations := obj.GetAnnotations()
value := annotations[key]
return value == AnnotationTrue
}
66 changes: 66 additions & 0 deletions pkg/metadata/naming.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// Copyright 2016-2019, Pulumi Corporation.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package metadata

import (
"fmt"
"math/rand"
"time"

"github.com/pulumi/pulumi/pkg/tokens"
"github.com/pulumi/pulumi/pkg/util/contract"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
)

var dns1123Alphabet = []rune("abcdefghijklmnopqrstuvwxyz0123456789")

// AssignNameIfAutonamable generates a name for an object. Uses DNS-1123-compliant characters.
// All auto-named resources get the annotation `pulumi.com/autonamed` for tooling purposes.
func AssignNameIfAutonamable(obj *unstructured.Unstructured, base tokens.QName) {
contract.Assert(base != "")
if obj.GetName() == "" {
obj.SetName(fmt.Sprintf("%s-%s", base, randString(8)))
SetAnnotationTrue(obj, AnnotationAutonamed)
}
}

// AdoptOldNameIfUnnamed checks if `newObj` has a name, and if not, "adopts" the name of `oldObj`
// instead. If `oldObj` was autonamed, then we mark `newObj` as autonamed, too.
func AdoptOldNameIfUnnamed(newObj, oldObj *unstructured.Unstructured) {
contract.Assert(oldObj.GetName() != "")
if newObj.GetName() == "" {
newObj.SetName(oldObj.GetName())
if IsAutonamed(oldObj) {
SetAnnotationTrue(newObj, AnnotationAutonamed)
}
}
}

func IsAutonamed(obj *unstructured.Unstructured) bool {
return IsAnnotationTrue(obj, AnnotationAutonamed)
}

func randString(n int) string {
b := make([]rune, n)
for i := range b {
b[i] = dns1123Alphabet[rand.Intn(len(dns1123Alphabet))]
}
return string(b)
}

// Seed RNG to get different random names at each suffix.
func init() {
rand.Seed(time.Now().UTC().UnixNano())
}
36 changes: 24 additions & 12 deletions pkg/provider/naming_test.go → pkg/metadata/naming_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,18 @@
// Copyright 2016-2018, Pulumi Corporation. All rights reserved.
// Copyright 2016-2019, Pulumi Corporation.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package provider
package metadata

import (
"strings"
Expand All @@ -14,16 +26,16 @@ import (
func TestAssignNameIfAutonamable(t *testing.T) {
// o1 has no name, so autonaming succeeds.
o1 := &unstructured.Unstructured{}
assignNameIfAutonamable(o1, "foo")
assert.True(t, isAutonamed(o1))
AssignNameIfAutonamable(o1, "foo")
assert.True(t, IsAutonamed(o1))
assert.True(t, strings.HasPrefix(o1.GetName(), "foo-"))

// o2 has a name, so autonaming fails.
o2 := &unstructured.Unstructured{
Object: map[string]interface{}{"metadata": map[string]interface{}{"name": "bar"}},
}
assignNameIfAutonamable(o2, "foo")
assert.False(t, isAutonamed(o2))
AssignNameIfAutonamable(o2, "foo")
assert.False(t, IsAutonamed(o2))
assert.Equal(t, "bar", o2.GetName())
}

Expand All @@ -35,23 +47,23 @@ func TestAdoptName(t *testing.T) {
"name": "old1",
// NOTE: annotations needs to be a `map[string]interface{}` rather than `map[string]string`
// or the k8s utility functions fail.
"annotations": map[string]interface{}{annotationInternalAutonamed: "true"},
"annotations": map[string]interface{}{AnnotationAutonamed: "true"},
}},
}
new1 := &unstructured.Unstructured{
Object: map[string]interface{}{"metadata": map[string]interface{}{"name": "new1"}},
}
adoptOldNameIfUnnamed(new1, old1)
AdoptOldNameIfUnnamed(new1, old1)
assert.Equal(t, "old1", old1.GetName())
assert.True(t, isAutonamed(old1))
assert.True(t, IsAutonamed(old1))
assert.Equal(t, "new1", new1.GetName())
assert.False(t, isAutonamed(new1))
assert.False(t, IsAutonamed(new1))

// new2 is unnamed and therefore DOES adopt old1's name.
new2 := &unstructured.Unstructured{
Object: map[string]interface{}{},
}
adoptOldNameIfUnnamed(new2, old1)
AdoptOldNameIfUnnamed(new2, old1)
assert.Equal(t, "old1", new2.GetName())
assert.True(t, isAutonamed(new2))
assert.True(t, IsAutonamed(new2))
}
23 changes: 23 additions & 0 deletions pkg/metadata/overrides.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Copyright 2016-2019, Pulumi Corporation.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package metadata

import (
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
)

func SkipAwaitLogic(obj *unstructured.Unstructured) bool {
return IsAnnotationTrue(obj, AnnotationSkipAwait)
}
Loading