1 /* 2 Copyright 2015 The Kubernetes Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 // deepcopy-gen is a tool for auto-generating DeepCopy functions. 18 // 19 // Given a list of input directories, it will generate functions that 20 // efficiently perform a full deep-copy of each type. For any type that 21 // offers a `.DeepCopy()` method, it will simply call that. Otherwise it will 22 // use standard value assignment whenever possible. If that is not possible it 23 // will try to call its own generated copy function for the type, if the type is 24 // within the allowed root packages. Failing that, it will fall back on 25 // `conversion.Cloner.DeepCopy(val)` to make the copy. The resulting file will 26 // be stored in the same directory as the processed source package. 27 // 28 // Generation is governed by comment tags in the source. Any package may 29 // request DeepCopy generation by including a comment in the file-comments of 30 // one file, of the form: 31 // // +k8s:deepcopy-gen=package 32 // 33 // DeepCopy functions can be generated for individual types, rather than the 34 // entire package by specifying a comment on the type definion of the form: 35 // // +k8s:deepcopy-gen=true 36 // 37 // When generating for a whole package, individual types may opt out of 38 // DeepCopy generation by specifying a comment on the of the form: 39 // // +k8s:deepcopy-gen=false 40 // 41 // Note that registration is a whole-package option, and is not available for 42 // individual types. 43 44 // This code is duplicated from https://github.com/kubernetes/code-generator/blob/master/cmd/deepcopy-gen/main.go 45 // so that this repository can utilize the deepcopy-gen functionality. 46 package main 47 48 import ( 49 "flag" 50 "path/filepath" 51 52 "github.com/spf13/pflag" 53 "k8s.io/gengo/args" 54 "k8s.io/gengo/examples/deepcopy-gen/generators" 55 "k8s.io/klog/v2" 56 57 generatorargs "k8s.io/code-generator/cmd/deepcopy-gen/args" 58 "k8s.io/code-generator/pkg/util" 59 ) 60 61 func main() { 62 klog.InitFlags(nil) 63 genericArgs, customArgs := generatorargs.NewDefaults() 64 65 // Override defaults. 66 // TODO: move this out of deepcopy-gen 67 genericArgs.GoHeaderFilePath = filepath.Join(args.DefaultSourceTree(), util.BoilerplatePath()) 68 69 genericArgs.AddFlags(pflag.CommandLine) 70 customArgs.AddFlags(pflag.CommandLine) 71 flag.Set("logtostderr", "true") 72 pflag.CommandLine.AddGoFlagSet(flag.CommandLine) 73 pflag.Parse() 74 75 if err := generatorargs.Validate(genericArgs); err != nil { 76 klog.Fatalf("Error: %v", err) 77 } 78 79 // Run it. 80 if err := genericArgs.Execute( 81 generators.NameSystems(), 82 generators.DefaultNameSystem(), 83 generators.Packages, 84 ); err != nil { 85 klog.Fatalf("Error: %v", err) 86 } 87 klog.V(2).Info("Completed successfully.") 88 } 89