Skip to content

Commit f97c97e

Browse files
committed
add --dry-run option to kubectl.apply
1 parent b87f504 commit f97c97e

File tree

1 file changed

+12
-2
lines changed

1 file changed

+12
-2
lines changed

ckan_cloud_operator/kubectl.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import traceback
55
import datetime
66
import json
7+
import sys
78

89

910
### disable yaml load warnings
@@ -220,10 +221,19 @@ def create(resource, is_yaml=False):
220221
subprocess.run('kubectl create -f -', input=yaml.dump(resource).encode(), shell=True, check=True)
221222

222223

223-
def apply(resource, is_yaml=False, reconcile=False):
224+
def apply(resource, is_yaml=False, reconcile=False, dry_run=False):
224225
if is_yaml: resource = yaml.load(resource)
225226
cmd = 'auth reconcile' if reconcile else 'apply'
226-
subprocess.run(f'kubectl {cmd} -f -', input=yaml.dump(resource).encode(), shell=True, check=True)
227+
args = []
228+
if dry_run:
229+
args.append('--dry-run')
230+
args = " ".join(args)
231+
subprocess.run(
232+
f'kubectl {cmd} {args} -f -',
233+
input=yaml.dump(resource).encode(), shell=True, check=True
234+
)
235+
if dry_run:
236+
print(yaml.dump(resource, default_flow_style=False))
227237

228238

229239
def install_crd(plural, singular, kind):

0 commit comments

Comments
 (0)