Skip to content

pkg/predicate: move predicates elsewhere and remove #3532

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

Merged
merged 1 commit into from
Jul 24, 2020
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
11 changes: 11 additions & 0 deletions changelog/fragments/remove-pkg-predicate.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
entries:
- description: >
Remove `pkg/predicate`: `ResourceFilterPredicate` is now available via
`pkg/ansible/predicate.NewResourceFilterPredicate()`; `GenerationChangedPredicate` has been renamed,
modified, and moved (see this [operator-lib PR](https://github.com/operator-framework/operator-lib/pull/11)
for details).
kind: removal
breaking: true
migration:
header: Update `pkg/predicate` imports
body: TBD
6 changes: 5 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ require (
github.com/onsi/ginkgo v1.12.1
github.com/onsi/gomega v1.10.1
github.com/operator-framework/api v0.3.8
github.com/operator-framework/operator-lib v0.0.0-20200724152139-f4e8074e89d3
github.com/operator-framework/operator-lib v0.0.0-20200724203809-f6728cc91ac6
github.com/operator-framework/operator-registry v1.12.6-0.20200611222234-275301b779f8
github.com/prometheus/client_golang v1.5.1
github.com/rogpeppe/go-internal v1.5.0
Expand Down Expand Up @@ -52,3 +52,7 @@ replace (
github.com/mattn/go-sqlite3 => github.com/mattn/go-sqlite3 v1.10.0
golang.org/x/text => golang.org/x/text v0.3.3 // Required to fix CVE-2020-14040
)

// This replaced version includes controller-runtime predicate utilities necessary for v1.0.0 that are still in master.
// Remove this and require the next minor/patch version of controller-runtime (>v0.6.1) when released.
replace sigs.k8s.io/controller-runtime => sigs.k8s.io/controller-runtime v0.6.1-0.20200724132623-e50c7b819263
11 changes: 4 additions & 7 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -424,7 +424,6 @@ github.com/huandu/xstrings v1.3.1/go.mod h1:y5/lhBue+AyNmUVz9RLU9xbLR0o4KIIExikq
github.com/iancoleman/strcase v0.0.0-20191112232945-16388991a334 h1:VHgatEHNcBFEB7inlalqfNqw65aNkM1lGX2yt3NmbS8=
github.com/iancoleman/strcase v0.0.0-20191112232945-16388991a334/go.mod h1:SK73tn/9oHe+/Y0h39VT4UCxmurVJkR5NA7kMEAOgSE=
github.com/imdario/mergo v0.3.5/go.mod h1:2EnlNZ0deacrJVfApfmtdGgDfMuh/nq6Ok1EcJh5FfA=
github.com/imdario/mergo v0.3.6/go.mod h1:2EnlNZ0deacrJVfApfmtdGgDfMuh/nq6Ok1EcJh5FfA=
github.com/imdario/mergo v0.3.8 h1:CGgOkSJeqMRmt0D9XLWExdT4m4F1vd3FV3VPt+0VxkQ=
github.com/imdario/mergo v0.3.8/go.mod h1:2EnlNZ0deacrJVfApfmtdGgDfMuh/nq6Ok1EcJh5FfA=
github.com/imdario/mergo v0.3.9 h1:UauaLniWCFHWd+Jp9oCEkTBj8VO/9DKg3PV3VCNMDIg=
Expand Down Expand Up @@ -586,8 +585,8 @@ github.com/operator-framework/api v0.3.7-0.20200602203552-431198de9fc2 h1:2KtDe3
github.com/operator-framework/api v0.3.7-0.20200602203552-431198de9fc2/go.mod h1:Xbje9x0SHmh0nihE21kpesB38vk3cyxnE6JdDS8Jo1Q=
github.com/operator-framework/api v0.3.8 h1:tJykTCmwGKZBsPVTCfxbwz6nTF6dzmKydWJtC40erc8=
github.com/operator-framework/api v0.3.8/go.mod h1:Xbje9x0SHmh0nihE21kpesB38vk3cyxnE6JdDS8Jo1Q=
github.com/operator-framework/operator-lib v0.0.0-20200724152139-f4e8074e89d3 h1:of4TVUV5Eg/xTDkoDKsERDKYrzY5nCy/e9QknvGyiF8=
github.com/operator-framework/operator-lib v0.0.0-20200724152139-f4e8074e89d3/go.mod h1:3xYWtjZXmIi9LikMmxqwLWjROJVVoil47sDJ1HD0pAI=
github.com/operator-framework/operator-lib v0.0.0-20200724203809-f6728cc91ac6 h1:xlaNuaiCeFie6HHkDHC4/8kqFU77sTF7zy0JAPM01c8=
github.com/operator-framework/operator-lib v0.0.0-20200724203809-f6728cc91ac6/go.mod h1:3xYWtjZXmIi9LikMmxqwLWjROJVVoil47sDJ1HD0pAI=
github.com/operator-framework/operator-registry v1.12.6-0.20200611222234-275301b779f8 h1:F3zzxoBJJANdKMxmSOi5z/HWiVT+gwOdhROkEwDWD2M=
github.com/operator-framework/operator-registry v1.12.6-0.20200611222234-275301b779f8/go.mod h1:loVINznYhgBIkmv83kU4yee88RS0BBk+hqOw9r4bhJk=
github.com/otiai10/copy v1.2.0 h1:HvG945u96iNadPoG2/Ja2+AUJeW5YuFQMixq9yirC+k=
Expand Down Expand Up @@ -1078,10 +1077,8 @@ rsc.io/letsencrypt v0.0.3 h1:H7xDfhkaFFSYEJlKeq38RwX2jYcnTeHuDQyT+mMNMwM=
rsc.io/letsencrypt v0.0.3/go.mod h1:buyQKZ6IXrRnB7TdkHP0RyEybLx18HHyOSoTyoOLqNY=
sigs.k8s.io/apiserver-network-proxy/konnectivity-client v0.0.7 h1:uuHDyjllyzRyCIvvn0OBjiRB0SgBZGqHNYAmjR7fO50=
sigs.k8s.io/apiserver-network-proxy/konnectivity-client v0.0.7/go.mod h1:PHgbrJT7lCHcxMU+mDHEm+nx46H4zuuHZkDP6icnhu0=
sigs.k8s.io/controller-runtime v0.6.0 h1:Fzna3DY7c4BIP6KwfSlrfnj20DJ+SeMBK8HSFvOk9NM=
sigs.k8s.io/controller-runtime v0.6.0/go.mod h1:CpYf5pdNY/B352A1TFLAS2JVSlnGQ5O2cftPHndTroo=
sigs.k8s.io/controller-runtime v0.6.1 h1:LcK2+nk0kmaOnKGN+vBcWHqY5WDJNJNB/c5pW+sU8fc=
sigs.k8s.io/controller-runtime v0.6.1/go.mod h1:XRYBPdbf5XJu9kpS84VJiZ7h/u1hF3gEORz0efEja7A=
sigs.k8s.io/controller-runtime v0.6.1-0.20200724132623-e50c7b819263 h1:NXplhERc848Vj70RKGIPT7M/Qh/KzhEjY0JURY0NHG8=
sigs.k8s.io/controller-runtime v0.6.1-0.20200724132623-e50c7b819263/go.mod h1:XRYBPdbf5XJu9kpS84VJiZ7h/u1hF3gEORz0efEja7A=
sigs.k8s.io/controller-tools v0.3.0 h1:y3YD99XOyWaXkiF1kd41uRvfp/64teWcrEZFuHxPhJ4=
sigs.k8s.io/controller-tools v0.3.0/go.mod h1:enhtKGfxZD1GFEoMgP8Fdbu+uKQ/cq1/WGJhdVChfvI=
sigs.k8s.io/kubebuilder v1.0.9-0.20200723213622-353f7a6ba73b h1:FhUDioJh37CVomwCLftxP4AbW+gy/lw0QObz8QYe2VY=
Expand Down
20 changes: 13 additions & 7 deletions pkg/ansible/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,20 @@ import (
"time"

"github.com/operator-framework/operator-lib/handler"
libpredicate "github.com/operator-framework/operator-lib/predicate"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"sigs.k8s.io/controller-runtime/pkg/controller"
logf "sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/manager"
ctrlpredicate "sigs.k8s.io/controller-runtime/pkg/predicate"
"sigs.k8s.io/controller-runtime/pkg/source"

"github.com/operator-framework/operator-sdk/pkg/ansible/events"
"github.com/operator-framework/operator-sdk/pkg/ansible/predicate"
"github.com/operator-framework/operator-sdk/pkg/ansible/runner"
"github.com/operator-framework/operator-sdk/pkg/predicate"
)

var log = logf.Log.WithName("ansible-controller")
Expand Down Expand Up @@ -97,17 +99,21 @@ func Add(mgr manager.Manager, options Options) *controller.Controller {
os.Exit(1)
}

u := &unstructured.Unstructured{}
u.SetGroupVersionKind(options.GVK)
// Set up predicates.
predicates := []ctrlpredicate.Predicate{
ctrlpredicate.Or(ctrlpredicate.GenerationChangedPredicate{}, libpredicate.NoGenerationPredicate{}),
}
filterPredicate, err := predicate.NewResourceFilterPredicate(options.Selector)

if err != nil {
log.Error(err, "Error in parsing selector")
log.Error(err, "Error creating resource filter predicate")
os.Exit(1)
}
predicates = append(predicates, filterPredicate)

if err := c.Watch(&source.Kind{Type: u}, &handler.InstrumentedEnqueueRequestForObject{},
predicate.GenerationChangedPredicate{}, filterPredicate); err != nil {
u := &unstructured.Unstructured{}
u.SetGroupVersionKind(options.GVK)
err = c.Watch(&source.Kind{Type: u}, &handler.InstrumentedEnqueueRequestForObject{}, predicates...)
if err != nil {
log.Error(err, "")
os.Exit(1)
}
Expand Down
56 changes: 56 additions & 0 deletions pkg/ansible/predicate/predicate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// Copyright 2018 The Operator-SDK Authors
//
// 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 predicate

import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"sigs.k8s.io/controller-runtime/pkg/event"
"sigs.k8s.io/controller-runtime/pkg/predicate"
)

type resourceFilterPredicate struct {
predicate.Funcs
Selector labels.Selector
}

// Skips events that have labels matching selectors defined in watches.yaml
func (r resourceFilterPredicate) eventFilter(eventLabels map[string]string) bool {
return r.Selector.Matches(labels.Set(eventLabels))
}

func NewResourceFilterPredicate(s metav1.LabelSelector) (predicate.Predicate, error) {
selectorSpecs, err := metav1.LabelSelectorAsSelector(&s)
requirements := resourceFilterPredicate{Selector: selectorSpecs}
return requirements, err

}

// Predicate functions that call the EventFilter Function
func (r resourceFilterPredicate) Update(e event.UpdateEvent) bool {
return r.eventFilter(e.MetaNew.GetLabels())
}

func (r resourceFilterPredicate) Create(e event.CreateEvent) bool {
return r.eventFilter(e.Meta.GetLabels())
}

func (r resourceFilterPredicate) Delete(e event.DeleteEvent) bool {
return r.eventFilter(e.Meta.GetLabels())
}

func (r resourceFilterPredicate) Generic(e event.GenericEvent) bool {
return r.eventFilter(e.Meta.GetLabels())
}
89 changes: 0 additions & 89 deletions pkg/predicate/predicate.go

This file was deleted.