Skip to content

Commit

Permalink
DeploymentController: Do not overwrite manifest
Browse files Browse the repository at this point in the history
When applying hooks, make sure not to overwrite the original manifest. Some
hooks may change the content later.

Example: a hook that replaces ${LOG_LEVEL} with operator.spec.logLevel. As
user modifies the logLevel, the controller + its hooks should replace
${LOG_LEVEL} with the current value, not with the value that was available
on the first sync.
  • Loading branch information
jsafrane committed Jun 22, 2021
1 parent ef142b5 commit cf213b1
Showing 1 changed file with 4 additions and 3 deletions.
7 changes: 4 additions & 3 deletions pkg/operator/deploymentcontroller/deployment_controller.go
Expand Up @@ -24,6 +24,7 @@ import (
type DeploymentHookFunc func(*opv1.OperatorSpec, *appsv1.Deployment) error

// ManifestHookFunc is a hook function to modify the manifest in raw format.
// The hook must not modify the original manifest!
type ManifestHookFunc func(*opv1.OperatorSpec, []byte) ([]byte, error)

// DeploymentController is a generic controller that manages a deployment
Expand Down Expand Up @@ -109,15 +110,15 @@ func (c *DeploymentController) sync(ctx context.Context, syncContext factory.Syn
return nil
}

manifest := c.manifest
for i := range c.optionalManifestHooks {
manifest, err := c.optionalManifestHooks[i](opSpec, c.manifest)
manifest, err = c.optionalManifestHooks[i](opSpec, manifest)
if err != nil {
return fmt.Errorf("error running hook function (index=%d): %w", i, err)
}
c.manifest = manifest
}

required := resourceread.ReadDeploymentV1OrDie(c.manifest)
required := resourceread.ReadDeploymentV1OrDie(manifest)

for i := range c.optionalDeploymentHooks {
err := c.optionalDeploymentHooks[i](opSpec, required)
Expand Down

0 comments on commit cf213b1

Please sign in to comment.