Skip to content

Commit

Permalink
Improve syncPolicy options for applications
Browse files Browse the repository at this point in the history
  • Loading branch information
arnarg committed Jul 27, 2024
1 parent 620dd7b commit bcdea62
Show file tree
Hide file tree
Showing 2 changed files with 117 additions and 9 deletions.
104 changes: 99 additions & 5 deletions modules/applications/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,11 @@
lib.mkIf (lib.length filtered > 0) {
"argocd.argoproj.io/compare-options" = lib.mkDefault (lib.concatStringsSep "," filtered);
};

convertSyncOptionsList = opts: let
filtered = lib.filter (val: val != null) (lib.mapAttrsToList (_: val: val) opts);
in
filtered;
in {
imports = [
./helm.nix
Expand Down Expand Up @@ -130,25 +135,112 @@ in {
};
};
syncPolicy = {
automated = {
autoSync = {
enabled = mkOption {
type = types.bool;
default = nixidyDefaults.syncPolicy.autoSync.enabled;
defaultText = literalExpression "config.nixidy.defaults.syncPolicy.autoSync.enabled";
description = ''
Specifies if application should automatically sync.
'';
};
prune = mkOption {
type = types.bool;
default = nixidyDefaults.syncPolicy.automated.prune;
defaultText = literalExpression "config.nixidy.defaults.syncPolicy.automated.prune";
default = nixidyDefaults.syncPolicy.autoSync.prune;
defaultText = literalExpression "config.nixidy.defaults.syncPolicy.autoSync.prune";
description = ''
Specifies if resources should be pruned during auto-syncing.
'';
};
selfHeal = mkOption {
type = types.bool;
default = nixidyDefaults.syncPolicy.automated.selfHeal;
defaultText = literalExpression "config.nixidy.defaults.syncPolicy.automated.selfHeal";
default = nixidyDefaults.syncPolicy.autoSync.selfHeal;
defaultText = literalExpression "config.nixidy.defaults.syncPolicy.autoSync.selfHeal";
description = ''
Specifies if partial app sync should be executed when resources are changed only in
target Kubernetes cluster and no git change detected.
'';
};
};
syncOptions = {
applyOutOfSyncOnly = mkOption {
type = types.bool;
default = false;
apply = val:
if val
then "ApplyOutOfSyncOnly=true"
else null;
description = ''
Currently when syncing using auto sync Argo CD applies every object in the application.
For applications containing thousands of objects this takes quite a long time and puts undue pressure on the api server.
Turning on selective sync option which will sync only out-of-sync resources.
'';
};
pruneLast = mkOption {
type = types.bool;
default = false;
apply = val:
if val
then "PruneLast=true"
else null;
description = ''
This feature is to allow the ability for resource pruning to happen as a final, implicit wave of a sync operation,
after the other resources have been deployed and become healthy, and after all other waves completed successfully.
'';
};
replace = mkOption {
type = types.bool;
default = false;
apply = val:
if val
then "Replace=true"
else null;
description = ''
By default, Argo CD executes `kubectl apply` operation to apply the configuration stored in Git.
In some cases `kubectl apply` is not suitable. For example, resource spec might be too big and won't fit into
`kubectl.kubernetes.io/last-applied-configuration` annotation that is added by kubectl apply.
If the `replace = true;`` sync option is set the Argo CD will use `kubectl replace` or `kubectl create` command
to apply changes.
'';
};
serverSideApply = mkOption {
type = types.bool;
default = false;
apply = val:
if val
then "ServerSideApply=true"
else null;
description = ''
By default, Argo CD executes `kubectl apply` operation to apply the configuration stored in Git.
This is a client side operation that relies on `kubectl.kubernetes.io/last-applied-configuration` annotation to
store the previous resource state.
If `serverSideApply = true;` sync option is set, Argo CD will use `kubectl apply --server-side` command to apply changes.
More info [here](https://argo-cd.readthedocs.io/en/stable/user-guide/sync-options/#server-side-apply).
'';
};
failOnSharedResource = mkOption {
type = types.bool;
default = false;
apply = val:
if val
then "FailOnSharedResource=true"
else null;
description = ''
By default, Argo CD will apply all manifests found in the git path configured in the Application regardless if the
resources defined in the yamls are already applied by another Application. If the `failOnSharedResource` sync option
is set, Argo CD will fail the sync whenever it finds a resource in the current Application that is already applied in
the cluster by another Application.
'';
};
};
finalSyncOpts = mkOption {
type = types.listOf types.str;
default = [];
internal = true;
};
};
output = {
path = mkOption {
Expand Down Expand Up @@ -274,5 +366,7 @@ in {
config.types);

annotations = convertCmpOptionsAnnotation config.compareOptions;

syncPolicy.finalSyncOpts = convertSyncOptionsList config.syncPolicy.syncOptions;
};
}
22 changes: 18 additions & 4 deletions modules/nixidy.nix
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,15 @@ in {
};

syncPolicy = {
automated = {
autoSync = {
enabled = mkOption {
type = types.bool;
default = false;
description = ''
Specifies if the application should automatically sync.
This is the default value for all applications if not explicitly set.
'';
};
prune = mkOption {
type = types.bool;
default = false;
Expand Down Expand Up @@ -187,9 +195,15 @@ in {
inherit (app) namespace;
server = "https://kubernetes.default.svc";
};
syncPolicy.automated = {
inherit (app.syncPolicy.automated) prune selfHeal;
};
syncPolicy =
(lib.optionalAttrs (app.syncPolicy.autoSync.enabled) {

Check warning on line 199 in modules/nixidy.nix

View workflow job for this annotation

GitHub Actions / static-lint

These parentheses can be omitted

Useless parentheses around primitive expression
automated = {
inherit (app.syncPolicy.autoSync) prune selfHeal;
};
})
// (lib.optionalAttrs (lib.length app.syncPolicy.finalSyncOpts > 0) {
syncOptions = app.syncPolicy.finalSyncOpts;
});
};
}
)
Expand Down

0 comments on commit bcdea62

Please sign in to comment.