Commit Graph

110 Commits

Author SHA1 Message Date
Sunny efafedd5a7
Introduce Generic error in reconcile
Add Generic error in RuntimeResultBuilder and ComputeReconcileResult
implementation with consideration to the error configurations.

Safeguards are added in the runtime result builder to ensure default
requeue after interval is set when is's set to zero or unset.

Signed-off-by: Sunny <darkowlzz@protonmail.com>
2022-05-13 18:29:09 +05:30
Sunny 9241218484
Introduce ErrorActionHandler ResultProcessor
ErrorActionHandler processes the reconciliation error results based on
their configurations. It performs actions like logging and event
recording based on the error configuration. More actions can be
accommodated in the future with more error configurations.

It can be a replacement for RecordContextualError() which does the same
operations but can't be configured much.

Signed-off-by: Sunny <darkowlzz@protonmail.com>
2022-05-13 16:10:29 +05:30
Sunny 0a29e3e44e
Introduce Generic error and error Config
Generic error is an attempt to avoid creating new error type for every
new unique scenario. It can be used to configure and build custom error
handling behavior, logging and event recording at present.
Contextual errors, Stalling and Waiting error, have special meaning for
the reconciliation results. But the Event error type can be replaced
with Generic error with some specific configurations. The Event error
is kept for a gradual migation to Generic error. Similarly, the Generic
error can be used to easily create new error handling behaviors.

The error Config can be used to configure any of the errors, including
contextual errors, without altering their contextual meaning, to modify
how they are handled.

The error constructors configure the errors with common default
configurations. These configurations can be modified to alter the
behavior.

Signed-off-by: Sunny <darkowlzz@protonmail.com>
2022-05-13 16:10:17 +05:30
Paulo Gomes 262efc08bc
Add git-optimized-clones feature gate
OptimizedGitClones decreases resource utilization for GitRepository
reconciliations. It supports both go-git and libgit2 implementations
when cloning repositories using branches or tags.

This is an opt-out feature, which can be disabled by starting the
controller with the argument '--feature-gates=OptimizedGitClones=false'.

Signed-off-by: Paulo Gomes <paulo.gomes@weave.works>
2022-05-11 14:39:21 +01:00
Sunny 2240106c8e
summarize: Consider obj status condition in result
SummarizeAndPatch() should also consider the object's status conditions
when computing and returning the runtime results to avoid any
inconsistency in the runtime result and status condition of the object.
When an object's Ready condition is False, the reconciler should retry
unless it's in stalled condition.

Signed-off-by: Sunny <darkowlzz@protonmail.com>
2022-04-30 10:12:21 +05:30
Eng Zer Jun 10b92da433
test: use `T.TempDir` to create temporary test directory
This commit replaces `os.MkdirTemp` with `t.TempDir` in tests. The
directory created by `t.TempDir` is automatically removed when the test
and all its subtests complete.

Prior to this commit, temporary directory created using `os.MkdirTemp`
needs to be removed manually by calling `os.RemoveAll`, which is omitted
in some tests. The error handling boilerplate e.g.
	defer func() {
		if err := os.RemoveAll(dir); err != nil {
			t.Fatal(err)
		}
	}
is also tedious, but `t.TempDir` handles this for us nicely.

Reference: https://pkg.go.dev/testing#T.TempDir
Signed-off-by: Eng Zer Jun <engzerjun@gmail.com>
2022-04-29 23:01:39 +08:00
Paulo Gomes 009504b294 helm: optimise repository index loading
Avoid validating (and thus loading) indexes if the checksum already exists in storage.
In other words, if the YAML is identical to the Artifact in storage, the reconciliation should
be a no-op, and therefore can short-circuit long/heavy operations.

Co-authored-by: Hidde Beydals <hello@hidde.co>
Signed-off-by: Paulo Gomes <paulo.gomes@weave.works>
2022-04-25 17:00:27 +02:00
Soule BA 0df8dcccec
Enable dependency manager to use in memory cache
If implemented this will:
- enable the helmCharts dependency manager to use the helm in memry
  cache to retrieve reconciled HelmRepositories indexes.
- record cache events.

Signed-off-by: Soule BA <soule@weave.works>
2022-04-19 11:46:27 +02:00
Hidde Beydals 18b59351c1 Update dependencies
Azure SDK dependencies cannot be updated, as this requires us to move to
Go 1.18.

- cloud.google.com/go/storage to v1.22.0
- github.com/ProtonMail/go-crypto to v0.0.0-20220407094043-a94812496cf5
- github.com/darkowlzz/controller-check to v0.0.0-20220325122359-11f5827b7981
- github.com/elazarl/goproxy to v0.0.0-20220403042543-a53172b9392e
- github.com/fluxcd/pkg/gittestserver to v0.5.2
- github.com/go-logr/logr to v1.2.3
- github.com/minio/minio-go/v7 to v7.0.24
- github.com/onsi/gomega to v1.19.0
- golang.org/x/crypto to v0.0.0-20220411220226-7b82a4e95df4
- google.golang.org/api to v0.74.0

Signed-off-by: Hidde Beydals <hello@hidde.co>
2022-04-15 16:41:42 +02:00
Peter Gundel 37551f1964
Reduce file permission
As suggested by @pjbgf

Co-authored-by: Paulo Gomes <paulo.gomes.uk@gmail.com>

Co-authored-by: Paulo Gomes <paulo.gomes.uk@gmail.com>
Signed-off-by: Peter Gundel <mail@petergundel.de>
2022-04-13 17:14:45 +02:00
Peter Gundel 0949d0adb9
Change all file permissions to octal format
This better represent permissions as Linux handles such information in
octal format, meaning that the left-most 0 has an important meaning
and is not to be ignored as normally integers would.

See https://github.com/fluxcd/source-controller/issues/603

Signed-off-by: Peter Gundel <mail@petergundel.de>
2022-04-12 21:24:34 +02:00
Hidde Beydals 9a17fd53e7 helm: attach loader to helm.MaxChartFileSize
Signed-off-by: Hidde Beydals <hello@hidde.co>
2022-04-11 11:47:28 +02:00
Hidde Beydals e85ea781e2 helm: switch to our own chart loader package
This includes some rewiring of tests, and slight changes in how we work
with the local chart reference. `Path` is expected to be relative to
`WorkDir`, and both fields are now mandatory.

Signed-off-by: Hidde Beydals <hello@hidde.co>
2022-04-11 11:47:28 +02:00
Hidde Beydals b9063d7362 helm: add more test coverage for secureloader
Signed-off-by: Hidde Beydals <hello@hidde.co>
2022-04-11 11:47:28 +02:00
Hidde Beydals 6fc066b1b6 helm: introduce customized chart loaders
This introduces our own `secureloader` package, with a directory
loader that's capable of following symlinks while validating they stay
within a certain root boundary.

Signed-off-by: Hidde Beydals <hello@hidde.co>
2022-04-11 09:36:14 +02:00
Hidde Beydals 5ae30cb4aa helm: drop github.com/pkg/errors
Signed-off-by: Hidde Beydals <hello@hidde.co>
2022-04-11 09:36:14 +02:00
Hidde Beydals 25f54ee80e sympath: provide abs path after eval symlink
This can be used to detect traversion outside of a certain path scope
while walking.

Signed-off-by: Hidde Beydals <hello@hidde.co>
2022-04-11 09:36:14 +02:00
Hidde Beydals ad597b352c helm: copy internal ignore and sympath modules
We require these to be able to mimic Helm's own directory loader, and
surprisingly (for `ignore` at least), these are not public.

Signed-off-by: Hidde Beydals <hello@hidde.co>
2022-04-11 09:36:14 +02:00
Sunny 5da74ca5a9
Add notify() in all the reconcilers
notify() is used to emit events for new artifact and failure recovery
scenarios. It's implemented in all the reconcilers.
Previously, when there used to be a failure due to any reason, on a
subsequent successful reconciliation, no notification was sent to
indicate that the failure has been resolved.
With notify(), the old version of the object is compared with the new
version of the object to determine if all, if any, of the failures have
been resolved and a notification is sent. The notification message is
the same that's sent in usual successful source reconciliation message
about stored artifact.

Signed-off-by: Sunny <darkowlzz@protonmail.com>
2022-04-07 21:39:16 +05:30
Soule BA 7ff96a8b0c
Update the docs with a section regarding the cache usage
Signed-off-by: Soule BA <soule@weave.works>
2022-04-02 18:13:59 +02:00
Soule BA 0f9302827c
Add MIT Licence copyright notice
Signed-off-by: Soule BA <soule@weave.works>
2022-04-01 12:41:53 +02:00
Soule BA 366f5cfde8
Cache HelmRepository index files
If implemented, will provide users with a way to cache index files.

This addresses issues where the index file is loaded and unmarshalled in
concurrent reconciliation resulting in a heavy memory footprint.

The caching strategy used is cache aside, and the cache is a k/v store
with expiration.

The cache number of entries and ttl for entries are configurable.

The cache is optional and is disabled by default

Signed-off-by: Soule BA <soule@weave.works>
2022-04-01 12:41:52 +02:00
Paulo Gomes 3819ac37bc
Move TransportPool to its own package
The experimental managed transport can also leverage TransportPool,
moving it to its own package to accommodate that use case.

Signed-off-by: Paulo Gomes <paulo.gomes@weave.works>
2022-03-25 19:08:50 +00:00
Hidde Beydals aa40a3a313 mock/gcs: strip /storage/v1 URI element
This is required after the update of `cloud.google.com/go/storage` from
`v1.16.0` to `v1.21.0`, due to changed `STORAGE_EMULATOR_HOST`
behavior.

Signed-off-by: Hidde Beydals <hello@hidde.co>
2022-03-16 11:16:47 +01:00
Paulo Gomes 7d61553463
Remove dependency to helm v2
Signed-off-by: Paulo Gomes <paulo.gomes@weave.works>
2022-03-02 17:58:15 +00:00
Paulo Gomes f63681f372
Improve TransportPool documentation
Signed-off-by: Paulo Gomes <paulo.gomes@weave.works>
2022-03-02 13:53:31 +00:00
Paulo Gomes d9d789fdb1
Reuse transport for helm chart download
Reuses the same transport across different helm chart downloads,
whilst resetting the tlsconfig to avoid cross-contamination.

Crypto material is now only processed in-memory and does not
touch the disk.

Signed-off-by: Paulo Gomes <paulo.gomes@weave.works>
2022-03-02 13:02:11 +00:00
pa250194 ed6c6ebc3c Introduce BucketProvider interface
This commit introduces a BucketProvider interface for fetch operations
against object storage provider buckets. Allowing for easier
introduction of new provider implementations.

The algorithm for conditionally downloading object files is the same,
whether you are using GCP storage or an S3/Minio-compatible
bucket. The only thing that differs is how the respective clients
handle enumerating through the objects in the bucket; by implementing
just that in each provider, I can have the select-and-fetch code in
once place.

The client implementations do now include safe-guards to ensure the
fetched object is the same as metadata has been collected for. In
addition, minor changes have been made to the object fetch operation
to take into account that:

- Etags can change between composition of index and actual fetch, in
  which case the etag is now updated.
- Objects can disappear between composition of index and actual fetch,
  in which case the item is removed from the index.

Lastly, the requirement for authentication has been removed (and not
referring to a Secret at all is thus allowed), to provide support
for e.g. public buckets.

Co-authored-by: Hidde Beydals <hello@hidde.co>
Co-authored by: Michael Bridgen <michael@weave.works>
Signed-off-by: pa250194 <pa250194@ncr.com>
2022-03-01 10:15:10 +01:00
Sunny 84bf8c8572 fuzz: Update to use v1beta2 APIs
Signed-off-by: Sunny <darkowlzz@protonmail.com>
2022-02-23 12:37:48 +01:00
Sunny f72a28a193 Use field owner in the patch helper
- Update summarize helper to have patch field owner.
- Updated the controllers to set the patch field owner.

Signed-off-by: Sunny <darkowlzz@protonmail.com>
2022-02-23 12:35:30 +01:00
Sunny d997876b07 Make generic SummarizeAndPatch()
summarizeAndPatch() was used by all the reconcilers with their own
object type. This creates a generic SummarizeAndPatch helper that takes
a conditions.Setter object and performs the same operations. All the
reconcilers are updated to use SummarizeAndPatch(). The process of
summarize and patch can be configured using the HelperOptions.

Introduce ResultProcessor to allow injecting middlewares in the
SummarizeAndPatch process.

Introduce RuntimeResultBuilder to allow defining how the reconciliation
result is computed for specific reconciler. This enabled different
reconcilers to have different meanings of the reconciliation results.

Introduce Conditions in summary package to store all the status
conditions related information of a reconciler. This is passed to
SummarizeAndPatch() to be used for summary and patch calculation.

Remove all the redundant summarizeAndPatch() tests per reconciler.

Add package internal/object containing helpers for interacting with
runtime.Object needed by the generic SummarizeAndPatch().

Add tests for ComputeReconcileResult().

Signed-off-by: Sunny <darkowlzz@protonmail.com>
2022-02-23 12:35:30 +01:00
Hidde Beydals eb0a554561 internal/helm: ErrChartReference on local load err
If a local reference does not contain a path to a valid file, returning
`ErrChartReference` is more correct to signal the reference is invalid.

This also indirectly causes the reconciler to signal a Suspend, as the
source or resource requires a change before a reattempt might be
successful.

Signed-off-by: Hidde Beydals <hello@hidde.co>
2022-02-23 12:35:30 +01:00
Sunny 78882b3b36 Consolidate result conversion and computation
Consolidate BuildRuntimeResult() into summarizeAndPatch() to simplify
where the results are computed, summarized and patched.

Move the event recording and logging of context specific errors into
RecordContextualError() and call it in summarizeAndPatch().

Introduce Waiting error for wait and requeue scenarios. Update
ComputeReconcileResult() and RecordContextualError() to consider Waiting
error.

Signed-off-by: Sunny <darkowlzz@protonmail.com>
2022-02-23 12:35:30 +01:00
Hidde Beydals 032ffb4d27 controllers: tweak events and logging
- Ensure all logged messages start with a lowercase.
- Make some pushed (and logged) events of type `EventTypeTrace` to
  prevent them from being sinked to the external event recorder, to
  prevent spam.
- Only log if artifact is up-to-date with upstream (instead of pushing
  an event).

Signed-off-by: Hidde Beydals <hello@hidde.co>
2022-02-23 12:35:30 +01:00
Hidde Beydals 527fce05df Rewrite HelmChartReconciler tests
Signed-off-by: Hidde Beydals <hello@hidde.co>
2022-02-23 12:35:30 +01:00
Hidde Beydals 8e107ea60e HelmChartReconciler refactor
Signed-off-by: Hidde Beydals <hello@hidde.co>
2022-02-23 12:35:30 +01:00
Hidde Beydals 5ab2f6219b internal/util: introduce temp dir/path helpers
In most of the reconcilers we have a repetative pattern of using part of
the object metadata to construct a temporary file path.

This commit introduces helpers as an abstraction, for both the creation
of a temporary directory based on `client.Object` type and object
metadata, and the generation of an arbitrary random temporary path
string.

Signed-off-by: Hidde Beydals <hello@hidde.co>
2022-02-23 12:35:30 +01:00
Sunny 5df4acb710 Add internal packages error and reconcile
- internal/error - Contains internal error type used across the
  source-controller reconcilers.
- internal/reconcile - Contains helper abstractions for the
  controller-runtime reconcile Result type and functions to
  interact with the abstractions.

Signed-off-by: Sunny <darkowlzz@protonmail.com>
2022-02-23 12:35:30 +01:00
Hidde Beydals 8aad5520b6 helm: remove temp file name from index cache err
Due to the temporary file having a random suffix, it would result in
the notification-controller not rate limiting the messages as they
are "unique".

For the close error (which also makes use of the name), we keep the
information as it might be of importance to figure out why the close
failed.

Signed-off-by: Hidde Beydals <hello@hidde.co>
2022-01-13 10:54:37 +01:00
Hidde Beydals fb0d7f24c8 internal/helm: validate loaded chart metadata obj
Signed-off-by: Hidde Beydals <hello@hidde.co>
2021-11-23 10:14:00 +01:00
Hidde Beydals 16d4a4899b internal/helm: ensure err is checked before defer
Signed-off-by: Hidde Beydals <hello@hidde.co>
2021-11-23 09:51:44 +01:00
Hidde Beydals ee1cb49b0c internal/helm: check size of meta files in package
Signed-off-by: Hidde Beydals <hello@hidde.co>
2021-11-23 09:35:27 +01:00
Hidde Beydals 87c7c80e0a internal/helm: validate package while loading meta
There was an unfinished code path that should have continued validating
the paths within the package. This commit completes it.

Signed-off-by: Hidde Beydals <hello@hidde.co>
2021-11-23 09:35:27 +01:00
Hidde Beydals 2392326ba9 internal/helm: doc block nitpicks
Signed-off-by: Hidde Beydals <hello@hidde.co>
2021-11-19 17:16:20 +01:00
Hidde Beydals 88ff049ab0 internal/helm: ensure cached chart name matches
This helps detect e.g. path or chart name reference changes.

Signed-off-by: Hidde Beydals <hello@hidde.co>
2021-11-19 17:04:00 +01:00
Hidde Beydals c202ad59aa helm/internal: add `ErrChartReference`
This makes it possible to signal reference (validation) errors
happening before the build process actually starts dealing with
the chart.

At present, this does not have a more specific counterpart in the API,
but this is expected to change when the conditions logic is revised.

Signed-off-by: Hidde Beydals <hello@hidde.co>
2021-11-19 17:04:00 +01:00
Hidde Beydals dcd5dd3db1 internal/helm: various nitpicks
- Add some more documentation around chart builders
- Ensure correct indentation in some doc comments
- Provide example of using `errors.Is` for typed `BuildError`
- Mention "bytes" in file size limit errors
- Add missing copyright header

Signed-off-by: Hidde Beydals <hello@hidde.co>
2021-11-19 17:04:00 +01:00
Hidde Beydals a1e9302b7d internal/helm: "value files" -> "values files"
Previous usage while consistent, was incorrect, and inconsitent with
the field in the API spec.

Signed-off-by: Hidde Beydals <hello@hidde.co>
2021-11-19 17:04:00 +01:00
Sunny 37ac5a9679 internal/helm: test load funcs for max size cases
This includes a change of the defaults to more acceptible (higher)
values.

Signed-off-by: Sunny <darkowlzz@protonmail.com>
2021-11-19 17:04:00 +01:00
Hidde Beydals 2b8134ce20 internal/helm: introduce typed BuildError
This commit introduces a typed `BuildError` to be returned by
`Builder.Build` in case of a failure.

The `Reason` field in combination with `BuildErrorReason` can be used
to signal (or determine) the reason of a returned error within the
context of the build process.

At present this is used to determine the correct Condition Reason, but
in a future iteration this can be used to determine the negative
polarity condition that should be set to indicate a precise failure to
the user.

Signed-off-by: Hidde Beydals <hello@hidde.co>
2021-11-19 17:04:00 +01:00