Skip to content

Commit

Permalink
add force-conflicts as a option
Browse files Browse the repository at this point in the history
  • Loading branch information
kalinon committed Dec 14, 2023
1 parent f491415 commit 5287ed7
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 28 deletions.
2 changes: 1 addition & 1 deletion shard.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: kube-helper
version: 1.0.2
version: 1.0.3

authors:
- Holden Omans <[email protected]>
Expand Down
54 changes: 30 additions & 24 deletions src/kube/helper.cr
Original file line number Diff line number Diff line change
Expand Up @@ -6,30 +6,31 @@ require "./types"
require "./helper/*"

OPTIONS = Hash(Symbol, Bool | String | Nil | Array(String)){
:quiet => false,
:verbose => false,
:debug => false,
:all => false,
:manifests => false,
:namespaces => false,
:names => Array(String).new,
:name_filter => false,
:secrets => false,
:config_maps => false,
:apps => false,
:delete => false,
:groups => false,
:kustomize => false,
:group_names => Array(String).new,
:list => false,
:workdir => "./",
:config_file => "deployment.yml",
:kube_config => ENV.fetch("KUBECONFIG", File.join(Path.home, ".kube/config")),
:kube_bin => "kubectl",
:helm_bin => "helm",
:context => nil,
:dry_run => false,
:server_side => false,
:quiet => false,
:verbose => false,
:debug => false,
:all => false,
:manifests => false,
:namespaces => false,
:names => Array(String).new,
:name_filter => false,
:secrets => false,
:config_maps => false,
:apps => false,
:delete => false,
:groups => false,
:kustomize => false,
:group_names => Array(String).new,
:list => false,
:workdir => "./",
:config_file => "deployment.yml",
:kube_config => ENV.fetch("KUBECONFIG", File.join(Path.home, ".kube/config")),
:kube_bin => "kubectl",
:helm_bin => "helm",
:context => nil,
:dry_run => false,
:server_side => false,
:force_conflicts => false,
}

class Kube::Helper
Expand All @@ -53,6 +54,11 @@ class Kube::Helper

config_file = File.join(OPTIONS[:workdir].as(String), OPTIONS[:config_file].as(String))

# Try to find `deployment.yaml` if `deployment.yml` is not found
if OPTIONS[:config_file].as(String) == "deployment.yml" && !File.exists?(config_file)
config_file = File.join(OPTIONS[:workdir].as(String), "deployment.yaml")
end

unless File.exists?(config_file)
puts "ERROR: Config file #{config_file} does not exist"
exit 1
Expand Down
2 changes: 2 additions & 0 deletions src/kube/helper/args.cr
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ module Kube::Helper::Args
"NOTE: this will override context in config file") { |file| OPTIONS[:context] = file }
parser.on("--dry-run", "Do not apply changes") { OPTIONS[:dry_run] = true }
parser.on("--server-side", "Apply changes server side") { OPTIONS[:server_side] = true }
parser.on("--force-conflicts", "If true, server-side apply will force the changes against conflicts.") { OPTIONS[:force_conflicts] = true }

parser.separator
parser.separator("Logging Flags:")
parser.on("-q", "--quiet", "Log errors only") { OPTIONS[:quiet] = true }
Expand Down
10 changes: 7 additions & 3 deletions src/kube/helper/kubectl.cr
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,17 @@ module Kube::Helper::Kubectl
if File.exists?(path)
logger.debug { "applying #{file}" }
FileUtils.cp(path, File.join(ks_path, "all.yaml"))
kubectl "apply", apply_server_side(path), "-k", ks_path
kubectl "apply", apply_server_side(path), apply_force_conflicts, "-k", ks_path
else
logger.error { "cannot find #{file}" }
end
end

private def apply_force_conflicts
return "--force-conflicts=false" if !opt(:force_conflicts) || !opt(:server_side)
"--force-conflicts=true"
end

private def apply_server_side(path)
return "--server-side=false" unless opt(:server_side)
server_side = File.size(path) > 262144
Expand All @@ -55,15 +60,14 @@ module Kube::Helper::Kubectl

def apply_manifest(path : String, namespace : String, ks_path : String)
return apply_manifest(path, ks_path) if /^http/ === path
# server_side = File.size(path) > 262144
create_ns(namespace)
FileUtils.cp(path, File.join(ks_path, "all.yaml"))
if opt(:delete)
logger.warn { "deleting #{path}" }
kubectl "delete", "-n", namespace, "-k", ks_path
else
logger.debug { "applying #{path}" }
kubectl "apply", "-n", namespace, apply_server_side(path), "-k", ks_path
kubectl "apply", "-n", namespace, apply_server_side(path), apply_force_conflicts, "-k", ks_path
end
end

Expand Down

0 comments on commit 5287ed7

Please sign in to comment.