Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option to skip namespaces based on a regex #13

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ Docker image by default runs `kube_backup backup && kube_backup push`
* `BACKUP_VERBOSE` use 1 to enable verbose logging
* `TARGET_PATH` - local git repository folder, default `./kube_state`
* `SKIP_NAMESPACES` - namespaces to exclude, separated by coma (,)
* `SKIP_NAMESPACES_REGEX` - namespaces to exclude, supports a single regex like `/^gitlab-runner-\d+$/` to match `gitlab-runner-1234`
* `ONLY_NAMESPACES` - whitelist namespaces
* `GLOBAL_RESOURCES` - override global resources list, default is `node, apiservice, clusterrole, clusterrolebinding, podsecuritypolicy, storageclass, persistentvolume, customresourcedefinition, mutatingwebhookconfiguration, validatingwebhookconfiguration, priorityclass`
* `EXTRA_GLOBAL_RESOURCES` - use it to add resources to `GLOBAL_RESOURCES` list
Expand Down
7 changes: 7 additions & 0 deletions lib/kube_backup.rb
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ def self.perform_backup!(options = {})
end

skip_namespaces = options[:skip_namespaces] ? options[:skip_namespaces].split(",") : []
skip_namespaces_regex = options[:skip_namespaces_regex] ? options[:skip_namespaces_regex] : nil
only_namespaces = options[:only_namespaces] ? options[:only_namespaces].split(",") : nil

writter = Writter.new(options)
Expand Down Expand Up @@ -173,6 +174,12 @@ def self.perform_backup!(options = {})

namespace = item.dig("metadata", "namespace")

if skip_namespaces_regex && namespace.match(skip_namespaces_regex)
name = item.dig("metadata", "name")
logger.info "skip resource #{namespace}/#{item["kind"]}/#{name} by skip_namespaces_regex filter"
next
end

if skip_namespaces.include?(namespace)
name = item.dig("metadata", "name")
logger.info "skip resource #{namespace}/#{item["kind"]}/#{name} by namespace filter"
Expand Down
2 changes: 1 addition & 1 deletion lib/kube_backup/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def default_args_from_env(defaults = {})
end

vars = [
:target_path, :skip_namespaces, :only_namespaces,
:target_path, :skip_namespaces, :skip_namespaces_regex, :only_namespaces,
:global_resources, :extra_global_resources, :skip_global_resources,
:resources, :extra_resources, :skip_resources, :skip_objects,
:git_user, :git_email, :git_branch, :git_prefix
Expand Down