Repository: knative/pkg
Pull request #1618 (2020-08-13 06:35:34)

Add a helper func for BucketSet
Add `BucketSet.HasBucket` func to get whether the BucketSet has the given BucketName. This will be used to filter endpoints created for leader election buckets.

/assign @vagababov 

knative-prow-robot: [APPROVALNOTIFIER] This PR is **NOT APPROVED**

This pull-request has been approved by: *<a href="https://github.com/knative/pkg/pull/1618#" title="Author self-approved">yanweiguo</a>*
To complete the [pull request process](https://git.k8s.io/community/contributors/guide/owners.md#the-code-review-process), please assign **vagababov**
You can assign the PR to them by writing `/assign @vagababov` in a comment when ready.

The full list of commands accepted by this bot can be found [here](https://go.k8s.io/bot-commands?repo=knative%2Fpkg).

<details open>
Needs approval from an approver in each of these files:

- **[hash/OWNERS](https://github.com/knative/pkg/blob/master/hash/OWNERS)**

Approvers can indicate their approval by writing `/approve` in a comment
Approvers can cancel approval by writing `/approve cancel` in a comment
</details>
<!-- META={"approvers":["vagababov"]} -->
knative-prow-robot: [APPROVALNOTIFIER] This PR is **APPROVED**

This pull-request has been approved by: *<a href="https://github.com/knative/pkg/pull/1618#pullrequestreview-466896240" title="LGTM">vagababov</a>*, *<a href="https://github.com/knative/pkg/pull/1618#" title="Author self-approved">yanweiguo</a>*

The full list of commands accepted by this bot can be found [here](https://go.k8s.io/bot-commands?repo=knative%2Fpkg).

The pull request process is described [here](https://git.k8s.io/community/contributors/guide/owners.md#the-code-review-process)

<details >
Needs approval from an approver in each of these files:

- ~~[hash/OWNERS](https://github.com/knative/pkg/blob/master/hash/OWNERS)~~ [vagababov]

Approvers can indicate their approval by writing `/approve` in a comment
Approvers can cancel approval by writing `/approve cancel` in a comment
</details>
<!-- META={"approvers":[]} -->

Patch:
diff --git a/hash/bucketer.go b/hash/bucketer.go
index d0c1460a8..41d26d97c 100644
--- a/hash/bucketer.go
+++ b/hash/bucketer.go
@@ -111,6 +111,11 @@ func (bs *BucketSet) Owner(key string) string {
 	return ret
 }
 
+// HasBucket returns true if this BucketSet has the given bucket name.
+func (bs *BucketSet) HasBucket(bkt string) bool {
+	return bs.buckets.Has(bkt)
+}
+
 // BucketList returns the bucket names of this BucketSet in random order.
 func (bs *BucketSet) BucketList() []string {
 	bs.mu.RLock()


Test patch:
diff --git a/hash/bucketer_test.go b/hash/bucketer_test.go
index 18497a087..8181175f2 100644
--- a/hash/bucketer_test.go
+++ b/hash/bucketer_test.go
@@ -118,6 +118,16 @@ func TestBucketSetBuckets(t *testing.T) {
 	}
 }
 
+func TestBucketSetHasBucket(t *testing.T) {
+	bs := NewBucketSet(sets.NewString(thisBucket, "aguacero", "chaparrón"))
+	if !bs.HasBucket(thisBucket) {
+		t.Errorf("HasBucket(%v) = false", thisBucket)
+	}
+	if bs.HasBucket(otherBucket) {
+		t.Errorf("HasBucket(%v) = true", otherBucket)
+	}
+}
+
 func TestBucketHas(t *testing.T) {
 	bs := NewBucketSet(buckets)
 	b := Bucket{