k8c.io/api/v3@v3.0.0-20230904060738-b0a93889c0b6/hack/update-codegen.sh (about)

     1  #!/usr/bin/env bash
     2  
     3  # Copyright 2023 The Kubermatic Kubernetes Platform contributors.
     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  set -euo pipefail
    18  
    19  cd $(dirname $0)/..
    20  source hack/lib.sh
    21  
    22  CRD_DIR=crd
    23  CODEGEN_DIR=pkg/generated
    24  
    25  echodate "Removing old generated clients"
    26  rm -rf "$CODEGEN_DIR"
    27  
    28  echodate "Creating vendor directory"
    29  go mod vendor
    30  
    31  echodate "Generating Kubernetes clientset"
    32  
    33  echo "" > /tmp/headerfile
    34  
    35  # no deepcopy here, as controller-gen takes care of that
    36  bash vendor/k8s.io/code-generator/generate-groups.sh client,lister,informer \
    37    k8c.io/api/v3/$CODEGEN_DIR \
    38    k8c.io/api/v3/pkg/apis \
    39    "kubermatic:v1 ee.kubermatic:v1 apps.kubermatic:v1 ee.apps.kubermatic:v1" \
    40    --go-header-file /tmp/headerfile
    41  
    42  # move generated code to the correct location; this should work regardless where
    43  # this repository has been cloned to
    44  mv $GOPATH/src/k8c.io/api/v3/pkg/generated pkg/
    45  
    46  # in case the repository was cloned to the module path in $GOPATH, make sure to
    47  # remove the leftover v3 directory
    48  rm -rf v3
    49  
    50  # cleanup
    51  rm -rf vendor
    52  
    53  # generate CRDs from the Go types
    54  echodate "Generating CRDs"
    55  
    56  go run sigs.k8s.io/controller-tools/cmd/controller-gen \
    57    crd \
    58    "object:headerFile=./hack/boilerplate/ce/boilerplate.go.txt" \
    59    "paths=./pkg/apis/..." \
    60    "output:crd:dir=$CRD_DIR"
    61  
    62  # beautify CRDs just because we can
    63  for f in $CRD_DIR/*.yaml; do
    64    yq --inplace --no-doc 'del(.metadata.creationTimestamp)' "$f"
    65    mv "$f" "$f.bak"
    66    echo -e "# This file has been generated by hack/update-codegen.sh, DO NOT EDIT.\n" > "$f"
    67    cat "$f.bak" >> "$f"
    68    rm "$f.bak"
    69  done
    70  
    71  is_enterprise_extension() {
    72    # yq's `contains` function is weird, this is safer
    73    # https://mikefarah.gitbook.io/yq/operators/contains
    74    yq --exit-status '[.spec.names.categories[] | select(. == "kkpee")] | length == 1' "$1" 2>/dev/null
    75  }
    76  
    77  # create pre-configured collections of CRDs to make installation easier
    78  prepare_crd_set() {
    79    setName="$1"
    80    echodate "Preparing $setName CRD set"
    81    target="$CRD_DIR/$setName"
    82  
    83    rm -rf "$target"
    84    mkdir -p "$target"
    85  }
    86  
    87  (
    88    prepare_crd_set enterprise/seed
    89    target="$CRD_DIR/$setName"
    90  
    91    cp $CRD_DIR/kubermatic.k8c.io_*.yaml "$target/"
    92    cp $CRD_DIR/apps.kubermatic.k8c.io_*.yaml "$target/"
    93  )
    94  
    95  (
    96    prepare_crd_set enterprise/kcp
    97    target="$CRD_DIR/$setName"
    98  
    99    cp $CRD_DIR/ee.kubermatic.k8c.io_*.yaml "$target/"
   100    cp $CRD_DIR/ee.apps.kubermatic.k8c.io_*.yaml "$target/"
   101  )
   102  
   103  (
   104    prepare_crd_set community
   105    target="$CRD_DIR/$setName"
   106  
   107    cp $CRD_DIR/enterprise/seed/*.yaml "$target/"
   108  
   109    # remove unused, misleading CRDs
   110    for f in $target/*.yaml; do
   111      if $(is_enterprise_extension "$f"); then
   112        rm "$f"
   113      fi
   114    done
   115  )
   116  
   117  rm $CRD_DIR/*.yaml