Skip to content

Commit

Permalink
Merge pull request #413 from rmweir/only-update-change
Browse files Browse the repository at this point in the history
[Forwardport] v2.6: Only update change
  • Loading branch information
rmweir authored May 20, 2022
2 parents 5a324b6 + c0d3c00 commit 4cc2f5a
Show file tree
Hide file tree
Showing 5 changed files with 475 additions and 58 deletions.
23 changes: 23 additions & 0 deletions generator/funcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,18 @@ package generator

import (
"net/http"
"os"
"strings"
"text/template"

"github.com/rancher/norman/types"
"github.com/rancher/norman/types/convert"
)

const (
managementContextType = "mgmt"
)

func funcs() template.FuncMap {
return template.FuncMap{
"capitalize": convert.Capitalize,
Expand Down Expand Up @@ -53,3 +58,21 @@ func getCollectionOutput(output, codeName string) string {
}
return convert.Capitalize(output)
}

// SyncOnlyChangedObjects check whether the CATTLE_SKIP_NO_CHANGE_UPDATE env var is
// configured to skip the update handler for events on the management context
// that do not contain a change to the object.
func SyncOnlyChangedObjects() bool {
skipNoChangeUpdate := os.Getenv("CATTLE_SYNC_ONLY_CHANGED_OBJECTS")
if skipNoChangeUpdate == "" {
return false
}
parts := strings.Split(skipNoChangeUpdate, ",")

for _, part := range parts {
if part == managementContextType {
return true
}
}
return false
}
4 changes: 4 additions & 0 deletions generator/generator.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
/*
Package generator contains templates and functions to generate clients, controllers, and lifecycles. The Generate functions
are exported and intended to be used by other libraries.
*/
package generator

import (
Expand Down
6 changes: 5 additions & 1 deletion generator/k8s_client_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/rancher/lasso/pkg/client"
"github.com/rancher/lasso/pkg/controller"
"github.com/rancher/norman/objectclient"
"github.com/rancher/norman/generator"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/rest"
{{.importPackage}}
Expand All @@ -26,7 +27,10 @@ func NewForConfig(cfg rest.Config) (Interface, error) {
if err := {{.prefix}}AddToScheme(scheme); err != nil {
return nil, err
}
controllerFactory, err := controller.NewSharedControllerFactoryFromConfig(&cfg, scheme)
sharedOpts := &controller.SharedControllerFactoryOptions{
SyncOnlyChangedObjects: generator.SyncOnlyChangedObjects(),
}
controllerFactory, err := controller.NewSharedControllerFactoryFromConfigWithOptions(&cfg, scheme, sharedOpts)
if err != nil {
return nil, err
}
Expand Down
18 changes: 8 additions & 10 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,15 @@ require (
github.com/maruel/ut v1.0.0 // indirect
github.com/matryer/moq v0.0.0-20200607124540-4638a53893e6
github.com/pkg/errors v0.9.1
github.com/prometheus/client_golang v1.11.1
github.com/rancher/lasso v0.0.0-20200820172840-0e4cc0ef5cb0
github.com/rancher/lasso v0.0.0-20220519004610-700f167d8324
github.com/rancher/wrangler v0.6.2-0.20200820173016-2068de651106
github.com/sirupsen/logrus v1.6.0
github.com/stretchr/testify v1.4.0
golang.org/x/sync v0.0.0-20201207232520-09787c993a3a
golang.org/x/tools v0.0.0-20210106214847-113979e3529a
google.golang.org/appengine v1.6.1 // indirect
k8s.io/api v0.18.19
github.com/stretchr/testify v1.7.0
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c
golang.org/x/tools v0.1.5
k8s.io/api v0.23.3
k8s.io/apiextensions-apiserver v0.18.0
k8s.io/apimachinery v0.18.19
k8s.io/client-go v0.18.8
k8s.io/gengo v0.0.0-20200114144118-36b2048a9120
k8s.io/apimachinery v0.23.3
k8s.io/client-go v0.23.3
k8s.io/gengo v0.0.0-20210813121822-485abfe95c7c
)
Loading

0 comments on commit 4cc2f5a

Please sign in to comment.