This repository has been archived by the owner on Oct 24, 2023. It is now read-only.
/
kubediff
executable file
·99 lines (73 loc) · 3 KB
/
kubediff
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import (division, absolute_import, print_function,
unicode_literals)
import argparse
import sys
from kubedifflib import (
check_files,
JSONPrinter,
QuietTextPrinter,
)
class ParseArgs():
def __init__(self):
# Parse arguments passed at cli
self.parse_arguments()
def parse_arguments(self):
description = '''
_ _ _ _ __ __
| |__ _ _ | |__ ___ __| |(_) / _| / _|
| / /| || || '_ \\/ -_)/ _` || || _|| _|
|_\\_\\ \\_,_||_.__/\\___|\\__,_||_||_| |_|
Compare yaml files in path(s) to running state in kubernetes and print the
differences. This is useful to ensure you have applied all your changes
to the appropriate environment. This tools runs kubectl, so unless your
~/.kube/config is configured for the correct environment, you will need
to supply the kubeconfig for the appropriate environment.'''
parser = argparse.ArgumentParser(description=description,
formatter_class=argparse.RawTextHelpFormatter)
parser.add_argument('--kubeconfig',
'-k',
help='path to kubeconfig')
parser.add_argument('--context',
'-c',
help='name of kubeconfig context to use')
parser.add_argument('--namespace',
'-n',
help=('Namespace to assume for objects where it is not '
'specified (default = Kubernetes default for '
'current context)'),
default='default')
parser.add_argument('--json',
'-j',
help='output in json format',
action='store_true',
dest='json')
parser.add_argument('--no-error-on-diff',
'-e',
help='don\'t exit with 2 if diff exists',
action='store_false',
dest='exit_on_diff',
default=True)
parser.add_argument('paths', nargs='*', help='path(s) from which '
'kubediff will look for configuration files')
self.args = parser.parse_args()
if len(self.args.paths) == 0:
parser.print_help()
sys.exit(1)
def main():
options = ParseArgs()
print(options.args)
printer = QuietTextPrinter()
if options.args.json:
printer = JSONPrinter()
config = {
"kubeconfig": options.args.kubeconfig,
"namespace": options.args.namespace,
"context": options.args.context
}
failed = check_files(options.args.paths, printer, config)
if failed and options.args.exit_on_diff:
sys.exit(2)
if __name__ == '__main__':
main()