0

I am working on a bash script to dynamically change the key within my Kubernetes secrets. I am able to use sed to change the values, but the output file is changed to a new line format. How do you have sed just replace word in file without breaking the file format?

Here is an example of the original chart:

apiVersion: v1
kind: Secret
metadata:
  name: configs
  namespace: {{Namespace}}
stringData:
  DOMAIN: {{Domain}}
  DB_USER: postgres
  DB_PASS: {{DBpass}}

This is my current script:

   original_yaml=$(cat chart.yam)
 # create new yaml file. 
   modified_yaml=$(echo "$original_yaml" |
    sed  "s/{{Namespace}}/$name/" |
    sed  "s/{{NODE_COOKIE}}/$node_cookiee/" |
    sed  "s/{{HubDomain}}/$hub_domain/" |
    sed  "s/{{DBpass}}/$db_pass/" |
    sed  "s/{{SmtpServer}}/$mail_server/" |
    sed  "s/{{SmtpPort}}/$mail_port/" |
    sed  "s/{{SmtpUser}}/$mail_user/" |
    sed  "s/{{SmtpPass}}/$mail_pass/" |
    sed  "s/{{UserEmail}}/$mail_email/"|
  )
    echo $modified_yaml > render.yam

Here is an example of the resulting output in the render.yaml file:

apiVersion: v1 kind: Secret metadata: name: configs namespace: x stringData:  DB_USER: postgres DB_PASS: x 
Benjamin W.
  • 46,058
  • 19
  • 106
  • 116
  • 1
    This has nothing to do with sed, change `echo $modified_yaml` to `echo "$modified_yaml"`. Read https://mywiki.wooledge.org/Quotes and as the [tag:bash] tag you used instructs - "For shell scripts with syntax or other errors, please check them at https://shellcheck.net before posting them here." – Ed Morton Jun 16 '23 at 13:56
  • Don't edit the file. See, I think, https://kubernetes.io/docs/concepts/configuration/configmap/ for how to define a second configuration file that contains the values for `Namespace`, `Domain`, etc. – chepner Jun 16 '23 at 14:05
  • Ed is probably leading you the same direction but using `$( )` to make a variable, then make another edited variable, and then writing that to a file is a sed antipattern when you're working with entire files. Sed means "stream editor" you could `cat chart.yam | sed ... > render.yam` but it's better yet to `sed ... chart.yam > render.yam`. And both would preserve linebreaks. – stevesliva Jun 16 '23 at 15:20
  • @EdMorton Suggestion has put me on the right path I was looking – user22084183 Jun 16 '23 at 15:34

0 Answers0