k8s.io/test-infra@v0.0.0-20240520184403-27c6b4c223d8/experiment/maintenance/recreate_configmaps.py (about)

     1  #!/usr/bin/env python3
     2  
     3  # Copyright 2018 The Kubernetes Authors.
     4  #
     5  # Licensed under the Apache License, Version 2.0 (the "License");
     6  # you may not use this file except in compliance with the License.
     7  # You may obtain a copy of the License at
     8  #
     9  #     http://www.apache.org/licenses/LICENSE-2.0
    10  #
    11  # Unless required by applicable law or agreed to in writing, software
    12  # distributed under the License is distributed on an "AS IS" BASIS,
    13  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14  # See the License for the specific language governing permissions and
    15  # limitations under the License.
    16  
    17  # This script deletes and recreates the prow configmaps
    18  # USE AT YOUR OWN RISK! This is a break-glass tool.
    19  # See September 25th, 2018 in docs/post-mortems.md
    20  
    21  #
    22  # USAGE: have KUBECONFIG pointed at your prow cluster then from test-infra root:
    23  #
    24  # hack/recreate_prow_configmaps.py [--wet]
    25  #
    26  
    27  
    28  
    29  from argparse import ArgumentParser
    30  import os
    31  import sys
    32  import subprocess
    33  
    34  
    35  
    36  def recreate_prow_config(wet, configmap_name, path):
    37      print('recreating prow config:')
    38      real_cmd = ['/bin/sh', '-c', 'gzip -k '+path]
    39      print(real_cmd)
    40      if wet:
    41          subprocess.check_call(real_cmd)
    42      cmd = (
    43          'kubectl create configmap %s'
    44          ' --from-file=config.yaml=%s'
    45          ' --dry-run -o yaml | kubectl replace configmap config -f -'
    46      ) % (configmap_name, path)
    47      real_cmd = ['/bin/sh', '-c', cmd]
    48      print(real_cmd)
    49      if wet:
    50          subprocess.check_call(real_cmd)
    51      real_cmd = ['/bin/sh', '-c', 'rm '+path+'.gz']
    52      print(real_cmd)
    53      if wet:
    54          subprocess.check_call(real_cmd)
    55  
    56  
    57  def recreate_plugins_config(wet, configmap_name, path):
    58      print('recreating plugins config:')
    59      cmd = (
    60          'kubectl create configmap %s'
    61          ' --from-file=plugins.yaml=%s'
    62          ' --dry-run -o yaml | kubectl replace configmap config -f -'
    63      ) % (configmap_name, path)
    64      real_cmd = ['/bin/sh', '-c', cmd]
    65      print(real_cmd)
    66      if wet:
    67          subprocess.check_call(real_cmd)
    68  
    69  def recreate_job_config(wet, job_configmap, job_config_dir):
    70      print('recreating jobs config:')
    71       # delete configmap (apply has size limit)
    72      cmd = ["kubectl", "delete", "configmap", job_configmap]
    73      print(cmd)
    74      if wet:
    75          subprocess.check_call(cmd)
    76  
    77      # regenerate
    78      cmd = ["kubectl", "create", "configmap", job_configmap]
    79      for root, _, files in os.walk(job_config_dir):
    80          for name in files:
    81              if name.endswith(".yaml"):
    82                  cmd.append("--from-file=%s=%s" % (name, os.path.join(root, name)))
    83      print(cmd)
    84      if wet:
    85          subprocess.check_call(cmd)
    86  
    87  def main():
    88      parser = ArgumentParser()
    89      # jobs config
    90      parser.add_argument("--job-configmap", default="job-config", help="name of prow jobs configmap")
    91      parser.add_argument(
    92          "--job-config-dir", default="config/jobs",
    93          help="root dir of prow jobs configmap")
    94      # prow config
    95      parser.add_argument("--prow-configmap", default="config",
    96                          help="name of prow primary configmap")
    97      parser.add_argument(
    98          "--prow-config-path", default="config/prow/config.yaml",
    99          help="path to the primary prow config")
   100      # plugins config
   101      parser.add_argument("--plugins-configmap", default="plugins",
   102                          help="name of prow plugins configmap")
   103      parser.add_argument(
   104          "--plugins-config-path", default="config/prow/plugins.yaml",
   105          help="path to the prow plugins config")
   106      # wet or dry?
   107      parser.add_argument("--wet", action="store_true")
   108      args = parser.parse_args()
   109  
   110      # debug the current context
   111      out = subprocess.check_output(['kubectl', 'config', 'current-context'], encoding='utf-8')
   112      print('Current KUBECONFIG context: '+out)
   113  
   114      # require additional confirmation in --wet mode
   115      prompt = '!'*65 + (
   116          "\n!!     WARNING THIS WILL RECREATE **ALL** PROW CONFIGMAPS.     !!"
   117          "\n!!    ARE YOU SURE YOU WANT TO DO THIS? IF SO, ENTER 'YES'.    !! "
   118      ) + '\n' + '!'*65 + '\n\n: '
   119      if args.wet:
   120          if input(prompt) != "YES":
   121              print("you did not enter 'YES'")
   122              sys.exit(-1)
   123  
   124      # first prow config
   125      recreate_prow_config(args.wet, args.prow_configmap, args.prow_config_path)
   126      print('')
   127      # then plugins config
   128      recreate_plugins_config(args.wet, args.plugins_configmap, args.plugins_config_path)
   129      print('')
   130      # finally jobs config
   131      recreate_job_config(args.wet, args.job_configmap, args.job_config_dir)
   132  
   133  
   134  
   135  if __name__ == '__main__':
   136      main()