gokuberneteskubebuilder

Why does kubebuilder include // +build !ignore_autogenerated in zz_generated.deepcopy.go files?


Every deepcopy generated file that is produced by make with kubebuilder produces a file with a // +build !ignore_autogenerated build tag directive at the top.

//go:build !ignore_autogenerated
// +build !ignore_autogenerated

/*
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.
*/

// Code generated by controller-gen. DO NOT EDIT.

Why is this specific build tag directive added to these generated files? What's its purpose?


Solution

  • It's used by controller-gen to identify files it generated, it will only overwrite those.

    E.g. edit a generated zz_generated.deepcopy.go and run make generate => the file is overwritten.

    Now edit the file again, also remove the two lines with the build constraints (the go:build line is for go >= 1.17, the +buildline for older versions IIRC) and run make generate again => your changes to the file have not been overwritten this time.