Skip to content

Commit

Permalink
Fallback to in-reconcile admission if webhooks are not in use
Browse files Browse the repository at this point in the history
Signed-off-by: Lucas Caparelli <[email protected]>
  • Loading branch information
LCaparelli committed Feb 2, 2021
1 parent 28bd7f3 commit cb1732a
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 13 deletions.
17 changes: 12 additions & 5 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@

## Have found a bug or have a feature request?

Please, open an issue for us. That will really help us improving the Operator and it would benefit other users such as yourself. There are templates ready for bug reporting and feature request to make things easier for you.
Please, open an issue for us. That will really help us improving the Operator and it would benefit other users such as
yourself. There are templates ready for bug reporting and feature request to make things easier for you.

## Have any questions?

We're happy to answer! Either [open an issue](https://github.com/m88i/nexus-operator/issues) or send an email to our mailing list: [[email protected]](mailto:[email protected]).
We're happy to answer! Either [open an issue](https://github.com/m88i/nexus-operator/issues) or send an email to our
mailing list: [[email protected]](mailto:[email protected]).

## Are you willing to send a PR?

Expand Down Expand Up @@ -43,9 +45,11 @@ Pushing to origin/pr-prep
# (output omitted)
```

If you don't inform remote name and branch, it will use "origin" as the remote and your current branch (the defaults, which appear between "[]"). Double check if the information is correct.
If you don't inform remote name and branch, it will use "origin" as the remote and your current branch (the defaults,
which appear between "[]"). Double check if the information is correct.

If you don't want to go over the interactive prompt every time, you can push with the defaults using the `PUSH_WITH_DEFAULTS` environment variable:
If you don't want to go over the interactive prompt every time, you can push with the defaults using the
`PUSH_WITH_DEFAULTS` environment variable:

```shell
$ PUSH_WITH_DEFAULTS=TRUE make pr-prep
Expand All @@ -62,4 +66,7 @@ case to `controllers/nexus_controller_test.go`.

All mutating (including default values) and validating logic lives in our admission webhooks, which are not in place
when the e2e suite runs. Because of that, no changes or checks are performed against Nexus CRs being reconciled. Be sure
to always write tests which use valid Nexus resources.
to always write tests which use valid Nexus resources.

In the future, after we migrate to operator-sdk v1.3.0 and kubebuilder plugin v3, scaffolding will create a separate
suite for testing the webhooks.
4 changes: 2 additions & 2 deletions config/manager/kustomization.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
images:
- name: controller
newName: controller
newTag: latest
newName: quay.io/m88i/nexus-operator
newTag: 0.5.0
11 changes: 11 additions & 0 deletions controllers/nexus_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ import (
"github.com/m88i/nexus-operator/pkg/cluster/kubernetes"
"github.com/m88i/nexus-operator/pkg/cluster/openshift"
"github.com/m88i/nexus-operator/pkg/framework"
"github.com/m88i/nexus-operator/pkg/util"
)

const (
Expand Down Expand Up @@ -89,12 +90,22 @@ func (r *NexusReconciler) Reconcile(req ctrl.Request) (ctrl.Result, error) {
return result, nil
}
// Error reading the object - requeue the request.
result.Requeue = true
return result, err
}

// In case of any errors from here, we should update the Nexus CR and its status
defer r.updateNexus(nexus, &err)

// if we're not using webhooks let's validate during reconcile as a fallback strategy
if !util.ShouldUseWebhooks() {
nexus.Default()
// validation is the same for create and update, any would be ok
if err := nexus.ValidateCreate(); err != nil {
return result, err
}
}

// Initialize the resource managers
err = r.Supervisor.InitManagers(nexus)
if err != nil {
Expand Down
13 changes: 9 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import (
"github.com/m88i/nexus-operator/controllers"
"github.com/m88i/nexus-operator/controllers/nexus/resource"
"github.com/m88i/nexus-operator/pkg/cluster/discovery"
"github.com/m88i/nexus-operator/pkg/util"
// +kubebuilder:scaffold:imports
)

Expand Down Expand Up @@ -97,9 +98,13 @@ func main() {
setupLog.Error(err, "unable to create controller", "controller", "Nexus")
os.Exit(1)
}
if err = (&appsm88iiov1alpha1.Nexus{}).SetupWebhookWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create webhook", "webhook", "Nexus")
os.Exit(1)

if util.ShouldUseWebhooks() {
setupLog.Info("Setting up admission webhooks")
if err = (&appsm88iiov1alpha1.Nexus{}).SetupWebhookWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create webhook", "webhook", "Nexus")
os.Exit(1)
}
}
// +kubebuilder:scaffold:builder

Expand All @@ -119,7 +124,7 @@ func getWatchNamespace() string {

ns, found := os.LookupEnv(watchNamespaceEnvVar)
if !found {
setupLog.Info("unable to get WatchNamespace, "+
setupLog.Info("Unable to get WatchNamespace, "+
"the manager will watch and manage resources in all namespaces",
"Env Var lookup", watchNamespaceEnvVar)
return ""
Expand Down
9 changes: 7 additions & 2 deletions pkg/util/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,14 @@ import (
"strconv"
)

// ShouldUseWebhooks returns true if "USE_WEBHOOKS" value evaluates to true or if it isn't set
func ShouldUseWebhooks() bool {
return GetBoolOSEnv("USE_WEBHOOKS", "true")
}

// GetBoolOSEnv gets a env variable as a boolean
func GetBoolOSEnv(key string) bool {
val := GetOSEnv(key, "false")
func GetBoolOSEnv(key, fallback string) bool {
val := GetOSEnv(key, fallback)
ret, err := strconv.ParseBool(val)
if err != nil {
return false
Expand Down

0 comments on commit cb1732a

Please sign in to comment.