Skip to content

Commit

Permalink
Adding funk library to the project (#1237)
Browse files Browse the repository at this point in the history
which implements map, filter, etc. methods for slices and maps. The first commit replaces existing implementations of the `contains` method through the codebase.
  • Loading branch information
Aleksey Dukhovniy committed Jan 6, 2020
1 parent c85e68c commit fa80cbd
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 50 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ require (
github.com/spf13/cobra v0.0.5
github.com/spf13/pflag v1.0.5
github.com/stretchr/testify v1.4.0
github.com/thoas/go-funk v0.5.0
github.com/xlab/treeprint v0.0.0-20181112141820-a009c3971eca
go.uber.org/atomic v1.4.0 // indirect
go.uber.org/zap v1.10.0 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -451,6 +451,8 @@ github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/thoas/go-funk v0.5.0 h1:XXFUVqX6xnIDqXxENFHBFS1X5AoT0EDs7HJq2krRfD8=
github.com/thoas/go-funk v0.5.0/go.mod h1:+IWnUfUmFO1+WVYQWQtIJHeRRdaIyyYglZN7xzUPe4Q=
github.com/tmc/grpc-websocket-proxy v0.0.0-20170815181823-89b8d40f7ca8 h1:ndzgwNDnKIqyCvHTXaCqh9KlOWKvBry6nuXMJmonVsE=
github.com/tmc/grpc-websocket-proxy v0.0.0-20170815181823-89b8d40f7ca8/go.mod h1:ncp9v5uamzpCO7NfCPTXjqaC+bZgJeR0sMTm6dMHP7U=
github.com/tsenart/deadcode v0.0.0-20160724212837-210d2dc333e9 h1:vY5WqiEon0ZSTGM3ayVVi+twaHKHDFUVloaQ/wug9/c=
Expand Down
14 changes: 3 additions & 11 deletions pkg/apis/kudo/v1beta1/instance_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"log"
"reflect"

"github.com/thoas/go-funk"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
apimachinerytypes "k8s.io/apimachinery/pkg/types"
Expand Down Expand Up @@ -505,15 +506,6 @@ func parameterDiff(old, new map[string]string) map[string]string {
return diff
}

func contains(values []string, s string) bool {
for _, value := range values {
if value == s {
return true
}
}
return false
}

func remove(values []string, s string) (result []string) {
for _, value := range values {
if value == s {
Expand All @@ -528,7 +520,7 @@ func remove(values []string, s string) (result []string) {
// hasn't been added yet, the instance has a cleanup plan and the cleanup plan
// didn't run yet. Returns true if the cleanup finalizer has been added.
func (i *Instance) TryAddFinalizer() bool {
if !contains(i.ObjectMeta.Finalizers, instanceCleanupFinalizerName) {
if !funk.ContainsString(i.ObjectMeta.Finalizers, instanceCleanupFinalizerName) {
if planStatus := i.PlanStatus(CleanupPlanName); planStatus != nil {
// avoid adding a finalizer again if a reconciliation is requested
// after it has just been removed but the instance isn't deleted yet
Expand All @@ -546,7 +538,7 @@ func (i *Instance) TryAddFinalizer() bool {
// been added, the instance has a cleanup plan and the cleanup plan completed.
// Returns true if the cleanup finalizer has been removed.
func (i *Instance) TryRemoveFinalizer() bool {
if contains(i.ObjectMeta.Finalizers, instanceCleanupFinalizerName) {
if funk.ContainsString(i.ObjectMeta.Finalizers, instanceCleanupFinalizerName) {
if planStatus := i.PlanStatus(CleanupPlanName); planStatus != nil {
if planStatus.Status.IsTerminal() {
i.ObjectMeta.Finalizers = remove(i.ObjectMeta.Finalizers, instanceCleanupFinalizerName)
Expand Down
23 changes: 7 additions & 16 deletions pkg/controller/instance/instance_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"reflect"
"time"

"github.com/thoas/go-funk"
appsv1 "k8s.io/api/apps/v1"
batchv1 "k8s.io/api/batch/v1"
corev1 "k8s.io/api/core/v1"
Expand Down Expand Up @@ -86,27 +87,17 @@ func (r *Reconciler) SetupWithManager(
return requests
})

// hasAnnotation returns true if an annotation with the passed key is found in the map
hasAnnotation := func(key string, annotations map[string]string) bool {
if annotations == nil {
return false
}
for k := range annotations {
if k == key {
return true
}
}
return false
}

// resPredicate ignores DeleteEvents for pipe-pods only (marked with task.PipePodAnnotation). This is due to an
// inherent race that was described in detail in #1116 (https://github.com/kudobuilder/kudo/issues/1116)
// tl;dr: pipe task will delete the pipe pod at the end of the execution. this would normally trigger another
// tl;dr: pipe-task will delete the pipe pod at the end of the execution. this would normally trigger another
// Instance reconciliation which might end up copying pipe files twice. we avoid this by explicitly ignoring
// DeleteEvents for pipe-pods.
resPredicate := predicate.Funcs{
CreateFunc: func(event.CreateEvent) bool { return true },
DeleteFunc: func(e event.DeleteEvent) bool { return !hasAnnotation(task.PipePodAnnotation, e.Meta.GetAnnotations()) },
CreateFunc: func(event.CreateEvent) bool { return true },
DeleteFunc: func(e event.DeleteEvent) bool {
return e.Meta.GetAnnotations() != nil &&
funk.Contains(e.Meta.GetAnnotations(), task.PipePodAnnotation)
},
UpdateFunc: func(event.UpdateEvent) bool { return true },
GenericFunc: func(event.GenericEvent) bool { return true },
}
Expand Down
14 changes: 3 additions & 11 deletions pkg/kudoctl/packages/verifier/template/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package template
import (
"testing"

"github.com/thoas/go-funk"
"gotest.tools/assert"

"github.com/kudobuilder/kudo/pkg/kudoctl/packages"
Expand Down Expand Up @@ -45,14 +46,14 @@ spec:
assert.Equal(t, 4, len(nodes.parameters))
params := []string{"Foo", "JVM_OPT_AVAILABLE_PROCESSORS", "AUTHORIZATION_ENABLED", "CUSTOM_CASSANDRA_YAML_BASE64"}
for _, param := range params {
if !contains(nodes.parameters, param) {
if !funk.ContainsString(nodes.parameters, param) {
t.Fatalf("missing %q parameter", param)
}
}
assert.Equal(t, 2, len(nodes.implicitParams))
implicits := []string{"Name", "Namespace"}
for _, param := range implicits {
if !contains(nodes.implicitParams, param) {
if !funk.ContainsString(nodes.implicitParams, param) {
t.Fatalf("missing %q implicit parameter", param)
}
}
Expand All @@ -79,12 +80,3 @@ metadata:

assert.Equal(t, `template file "example.yaml" reports the following error: template: example.yaml:6: unexpected {{end}}`, *nodes.error)
}

func contains(arr []string, str string) bool {
for _, a := range arr {
if a == str {
return true
}
}
return false
}
15 changes: 3 additions & 12 deletions pkg/test/kind_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,14 @@ import (

"github.com/docker/docker/api/types"
dockerClient "github.com/docker/docker/client"
"github.com/thoas/go-funk"
"sigs.k8s.io/kind/pkg/apis/config/v1alpha3"
"sigs.k8s.io/kind/pkg/cluster/nodes"
)

const (
kindTestContext = "test"
testImage = "busybox:latest"
testImage = "docker.io/library/busybox:latest"
)

// Tests that Docker images are added to the nodes of a KIND cluster with the
Expand Down Expand Up @@ -79,7 +80,7 @@ func TestAddContainers(t *testing.T) {
t.Errorf("failed to list node images: %v", err)
}

if !contains(images, testImage) {
if !funk.ContainsString(images, testImage) {
t.Errorf("failed to find image %s on node %s", testImage, node.String())
}
}
Expand All @@ -97,13 +98,3 @@ func nodeImages(node nodes.Node) ([]string, error) {

return strings.Split(stdout.String(), "\n"), nil
}

func contains(values []string, value string) bool {
for _, v := range values {
if strings.Contains(v, value) {
return true
}
}

return false
}

0 comments on commit fa80cbd

Please sign in to comment.