redhat-cop / namespace-configuration-operator

The namespace-configuration-operator helps keeping configurations related to Users, Groups and Namespaces aligned with one of more policies specified as a CRs
Apache License 2.0
204 stars 55 forks source link

Refactor createorupdate #10

Closed etsauer closed 5 years ago

etsauer commented 5 years ago

There was a bug in the createOrUpdate method where the api would complain about the presence of a resource version when attempting to Update a previously created resource.

Here's an example:

{"level":"error","ts":1568781734.1283247,"logger":"controller_namespaceconfig","msg":"unable to create object","object":{"apiVersion":"v1","kind":"ResourceQuota","metadata":{"labels":{"namespace-config-operator.redhat-cop.io_owner":"namespace-configuration-operator-large-size"},"name":"large-size","resourceVersion":"53652"},"spec":{"hard":{"requests.cpu":"8","requests.memory":"4Gi"}}},"error":"resourceVersion should not be set on objects to be created","stacktrace":"github.com/redhat-cop/namespace-configuration-operator/vendor/github.com/go-logr/zapr.(*zapLogger).Error\n\t/home/travis/gopath/src/github.com/redhat-cop/namespace-configuration-operator/vendor/github.com/go-logr/zapr/zapr.go:128\ngithub.com/redhat-cop/namespace-configuration-operator/pkg/controller/namespaceconfig.createOrUpdate\n\t/home/travis/gopath/src/github.com/redhat-cop/namespace-configuration-operator/pkg/controller/namespaceconfig/namespaceconfig_controller.go:291\ngithub.com/redhat-cop/namespace-configuration-operator/pkg/controller/namespaceconfig.(*ReconcileNamespaceConfig).applyConfigToNamespace\n\t/home/travis/gopath/src/github.com/redhat-cop/namespace-configuration-operator/pkg/controller/namespaceconfig/namespaceconfig_controller.go:276\ngithub.com/redhat-cop/namespace-configuration-operator/pkg/controller/namespaceconfig.(*ReconcileNamespaceConfig).Reconcile\n\t/home/travis/gopath/src/github.com/redhat-cop/namespace-configuration-operator/pkg/controller/namespaceconfig/namespaceconfig_controller.go:216\ngithub.com/redhat-cop/namespace-configuration-operator/vendor/sigs.k8s.io/controller-runtime/pkg/internal/controller.(*Controller).processNextWorkItem\n\t/home/travis/gopath/src/github.com/redhat-cop/namespace-configuration-operator/vendor/sigs.k8s.io/controller-runtime/pkg/internal/controller/controller.go:215\ngithub.com/redhat-cop/namespace-configuration-operator/vendor/sigs.k8s.io/controller-runtime/pkg/internal/controller.(*Controller).Start.func1\n\t/home/travis/gopath/src/github.com/redhat-cop/namespace-configuration-operator/vendor/sigs.k8s.io/controller-runtime/pkg/internal/controller/controller.go:158\ngithub.com/redhat-cop/namespace-configuration-operator/vendor/k8s.io/apimachinery/pkg/util/wait.JitterUntil.func1\n\t/home/travis/gopath/src/github.com/redhat-cop/namespace-configuration-operator/vendor/k8s.io/apimachinery/pkg/util/wait/wait.go:133\ngithub.com/redhat-cop/namespace-configuration-operator/vendor/k8s.io/apimachinery/pkg/util/wait.JitterUntil\n\t/home/travis/gopath/src/github.com/redhat-cop/namespace-configuration-operator/vendor/k8s.io/apimachinery/pkg/util/wait/wait.go:134\ngithub.com/redhat-cop/namespace-configuration-operator/vendor/k8s.io/apimachinery/pkg/util/wait.Until\n\t/home/travis/gopath/src/github.com/redhat-cop/namespace-configuration-operator/vendor/k8s.io/apimachinery/pkg/util/wait/wait.go:88"}

This PR fixes that bug, and heavily refactors the createOrUpdate method.

raffaelespazzoli commented 5 years ago

besides the comment I left, I am good with the changes. coincidentally I am working on this operator and this method will disappear, I found a better way to do it. Anyway for now like I said I am good with the proposed change.

etsauer commented 5 years ago

@raffaelespazzoli @sabre1041 updated.