sample-controller: The update-codegen script does not automatically generate files & directories

To quote the README:

The update-codegen script will automatically generate the following files & directories:

  • pkg/apis/samplecontroller/v1alpha1/zz_generated.deepcopy.go
  • pkg/generated/

But:

$ git clone https://github.com/kubernetes/sample-controller.git
$ cd sample-controller
$ rm -f pkg/apis/samplecontroller/v1alpha1/zz_generated.deepcopy.go
$ ./hack/update-codegen.sh
Generating deepcopy funcs
Generating clientset for samplecontroller:v1alpha1 at k8s.io/sample-controller/pkg/generated/clientset
Generating listers for samplecontroller:v1alpha1 at k8s.io/sample-controller/pkg/generated/listers
Generating informers for samplecontroller:v1alpha1 at k8s.io/sample-controller/pkg/generated/informers
$ ls pkg/apis/samplecontroller/v1alpha1/zz_generated.deepcopy.go
ls: pkg/apis/samplecontroller/v1alpha1/zz_generated.deepcopy.go: No such file or directory

Am I misunderstanding how this is supposed to work?

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Comments: 24 (10 by maintainers)

Most upvoted comments

Here’s what I’ve figured out from a couple of hours of faffing around with this:

The first problem is “how do you run generate-groups.sh”? There are two answers. One was pointed out by @farah : populate the vendor directory. The other is to clone the code-generator project so that it’s a sibling of sample-controller (which is where it lives in the kubernetes/kubernetes repo). You can look in hack/update-codegen.sh and you’ll see that the script looks in the vendor directory and then falls back to …/code-generator.

The second problem is “where does generate-groups.sh put the files that it generates”? Remember how this repo is a mirror of a small part of kubernetes/kubernetes? Well, generate-groups.sh assumes and requires that everything is laid out exactly like it is in the kubernetes project. In that project, sample-controller is in k8s.io/sample-controller, i.e. {parent}/{project}. You’ll see in generate-groups.sh that it goes up three directories, then uses the module path as a directory path which means that it doesn’t work properly if your project’s module isn’t {parent}/{project}. You can hack update-codegen.sh to use a different output-base but you might still end up having to move the files after they’re generated. I did.

This smells like it was written before Go modules and never updated to work with modules since it just so happens to work with the directory structure in the kubernetes/kubernetes project.

HTH

This works for me:

#!/usr/bin/env bash

# Copyright 2017 The Kubernetes Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

set -o errexit
set -o nounset
set -o pipefail

SCRIPT_ROOT=$(dirname "${BASH_SOURCE[0]}")/..
CODEGEN_PKG=${CODEGEN_PKG:-$(cd "${SCRIPT_ROOT}"; ls -d -1 ./vendor/k8s.io/code-generator 2>/dev/null || echo ../code-generator)}

# Pre-requisites
# 1. this script needs ./pkg/apis/samplecontroller/v1alpha1 to generate everything else (generated files will be empty otherwise)
# 2. changing --output-base to something else doesn't work
# 
# Only the following things can be auto-generated:
# 1. ./pkg/apis/samplecontroller/v1alpha1/zz_generated.deepcopy.go
# 2. everything under ./pkg/apis/generated
bash "${CODEGEN_PKG}"/generate-groups.sh "deepcopy,client,informer,lister" \
  k8s.io/sample-controller/pkg/generated k8s.io/sample-controller/pkg/apis \
  samplecontroller:v1alpha1 \
  --output-base "$(dirname "${BASH_SOURCE[0]}")/../../.." \
  --go-header-file "${SCRIPT_ROOT}"/hack/boilerplate.go.txt

# This block:
# output_base: the directory outside the repo where auto-generated files are created
# 1. removes existing auto-generated files
# 2. copies over the auto-generated files from the output_base to pkg/apis and pkg/generated
# 3. removes output_base
rm -R ./pkg/apis/samplecontroller/v1alpha1/zz_generated.deepcopy.go ./pkg/generated -f
mv "$(dirname "${BASH_SOURCE[0]}")/../../../k8s.io/sample-controller/pkg/apis/samplecontroller/v1alpha1/zz_generated.deepcopy.go" $SCRIPT_ROOT/pkg/apis/samplecontroller/v1alpha1 -f
mv "$(dirname "${BASH_SOURCE[0]}")/../../../k8s.io/sample-controller/pkg/generated" $SCRIPT_ROOT/pkg
rm -R "$(dirname "${BASH_SOURCE[0]}")/../../../k8s.io"

# To use your own boilerplate text append:
#   --go-header-file "${SCRIPT_ROOT}"/hack/custom-boilerplate.go.txt

hey @max-rocket-internet, just realised that the fix is actually in the read me. Run go mod vendor and you should be good.

Reopened for @vadasambar but I moved on from this topic a long time ago 🙂

TBH I think this code generation thing adds more headache/complexity than it solves, especially when you’re just beginning to write a controller using this repo as a start.