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

CRD 생성 오류 수정 #12

Closed
wants to merge 2 commits into from
Closed
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
120 changes: 57 additions & 63 deletions helm2yaml/applib/helm.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,69 +97,55 @@ def toSeperatedResources(self, targetdir='/output', verbose=False, local_reposit
target = '{}/{}/'.format(targetdir, self.name)
os.system('mkdir -p {}'.format(target))

# For crd-only argoCD app, just copy crd files into output directory
if self.name.endswith('-crds'):
print('[Copy CRD yamls for {} from {}/{}]'.
format(self.name, self.repo.repository(), self.repo.chart()))

# Pull helm chart from chart repo
if verbose > 0:
print('(DEBUG) Pull helm chart: helm pull --repo {} {} --version {}'.format(self.repo.repository(), self.repo.chart(), self.repo.version()))
os.system('helm pull --repo {} {} --version {} | grep -i error'.format(self.repo.repository(), self.repo.chart(), self.repo.version()))

# Untar chart tarball file
if verbose > 0:
print('(DEBUG) Extract helm chart: tar xf {}-{}.tgz'.format(self.repo.chart(), self.repo.version()))
os.system('tar xf {}-{}.tgz'.format(self.repo.chart(), self.repo.version()))

# Copy crd files into output directory
os.system('cp -r {}/crds/* {}/{}/'.format(self.repo.chart(), targetdir, self.name))
# Cleanup
os.system('rm -rf ./{}'.format(self.repo.chart()))

# For general argoCD app, render helm chart into single manifest yaml
else:
genfile=self.genTemplateFile(verbose)
genfile=self.genTemplateFile(verbose)

if verbose > 0:
print('(DEBUG) seperate all-in-one yaml into each resource yaml')
os.system('mv {} {}'.format(genfile, target))
if verbose > 0:
print('(DEBUG) seperate all-in-one yaml into each resource yaml')
os.system('mv {} {}'.format(genfile, target))

# Split into each k8s resource yaml
splitcmd = "awk '{f=\""+target+"/_\" NR; print $0 > f}' RS='\n---\n' "+target+genfile
os.system(splitcmd)
os.system('rm {0}{1}'.format(target,genfile))
# Split into each k8s resource yaml
splitcmd = "awk '{f=\""+target+"/_\" NR; print $0 > f}' RS='\n---\n' "+target+genfile
os.system(splitcmd)
os.system('rm {0}{1}'.format(target,genfile))

# Rename yaml to "KIND_RESOURCENAME.yaml"
if verbose > 0:
print('(DEBUG) rename resource yaml files')
isCrd=self.name.endswith('-crds')
if isCrd:
for entry in os.scandir(target):
refinedname =''
with open(entry, 'r') as stream:
try:
parsed = yaml.safe_load(stream)
refinedname = '{}_{}.yaml'.format(parsed['kind'],parsed['metadata']['name'])

if(local_repository!=None):
self.__replaceImages(parsed, local_repository)

except yaml.YAMLError as exc:
if entry.is_file():
splitcmd = "awk '{f=\""+target+"/"+entry.name+"-\" NR; print $0 > f}' RS='' "+target+"/"+entry.name
os.system(splitcmd)
os.system('rm {0}{1}'.format(target,entry.name))

# Rename yaml to "KIND_RESOURCENAME.yaml"
if verbose > 0:
print('(DEBUG) rename resource yaml files')
for entry in os.scandir(target):
refinedname =''
with open(entry, 'r') as stream:
try:
parsed = yaml.safe_load(stream)
refinedname = '{}_{}.yaml'.format(parsed['kind'],parsed['metadata']['name'])

if(local_repository!=None):
self.__replaceImages(parsed, local_repository)

except yaml.YAMLError as exc:
print('(WARN)',exc,":::", parsed)
except TypeError as exc:
if os.path.getsize(entry)>80:
print('(WARN)',exc,":::", parsed)
except TypeError as exc:
if os.path.getsize(entry)>80:
print('(WARN)',exc,":::", parsed)
if verbose > 0:
print("(DEBUG) Contents in the file :", entry.name)
print(stream.readlines())
if (refinedname!=''):
with open(target+refinedname, 'w') as file:
documents = yaml.dump(parsed, file)
if os.path.exists(target+refinedname):
os.remove(entry)
else:
os.rename(entry, target+'/'+refinedname)
else:
if verbose > 0:
print("(DEBUG) Contents in the file :", entry.name)
print(stream.readlines())
if (refinedname!=''):
with open(target+refinedname, 'w') as file:
documents = yaml.dump(parsed, file)
if os.path.exists(target+refinedname):
os.remove(entry)
else:
os.rename(entry, target+'/'+refinedname)
else:
os.remove(entry)

def get_image_list(self, verbose=False):

Expand Down Expand Up @@ -201,20 +187,25 @@ def get_image_list(self, verbose=False):
return image_list

def genTemplateFile(self, verbose=0):
# For crd-only argoCD app, using other cmd 'helm show crds'
isCrd=self.name.endswith('-crds')

yaml.dump(self.override, open('vo', 'w') , default_flow_style=False)
print('[Generate resource yamls for {} from {}/{} in {}]'.
format(self.name, self.repo.repository(), self.repo.chart(), self.namespace))

if self.repo.repotype == RepoType.HELMREPO:
if isCrd:
helmcmd='helm show crds --repo {2} {3} --version {4} > {1}.plain.yaml'.format(self.namespace, self.name, self.repo.repository(), self.repo.chart(), self.repo.version())
else:
helmcmd='helm template -n {0} {1} --repo {2} {3} --version {4} -f vo > {1}.plain.yaml'.format(self.namespace, self.name, self.repo.repository(), self.repo.chart(), self.repo.version())
# Generate template file
if verbose > 0:
print('(DEBUG) gernerate template file')
print(self.toString())
print('helm template -n {0} {1} --repo {2} {3} --version {4} -f vo > {1}.plain.yaml'
.format(self.namespace, self.name, self.repo.repository(), self.repo.chart(), self.repo.version()))
print(helmcmd)

os.system('helm template -n {0} {1} --repo {2} {3} --version {4} -f vo > {1}.plain.yaml'
.format(self.namespace, self.name, self.repo.repository(), self.repo.chart(), self.repo.version()))
os.system(helmcmd)

elif self.repo.repotype == RepoType.GIT:
# prepare repository
Expand All @@ -234,8 +225,11 @@ def genTemplateFile(self, verbose=0):
if verbose > 0:
print('(DEBUG) gernerat a template file')

os.system('helm template -n {0} {1} .temporary-clone/{2} -f vo > {1}.plain.yaml'
.format(self.namespace, self.name, self.repo.path()))
if isCrd:
helmcmd='helm show crds .temporary-clone/{2} > {1}.plain.yaml'.format(self.namespace, self.name, self.repo.path())
else:
helmcmd='helm template -n {0} {1} .temporary-clone/{2} -f vo > {1}.plain.yaml'.format(self.namespace, self.name, self.repo.path())
os.system(helmcmd)

# clean reposiotry
os.system('rm -rf .temporary-clone')
Expand Down