|
| 1 | +/* |
| 2 | +Copyright 2021 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 | +package scaffolds |
| 18 | + |
| 19 | +import ( |
| 20 | + "fmt" |
| 21 | + |
| 22 | + "sigs.k8s.io/kubebuilder/v3/pkg/config" |
| 23 | + "sigs.k8s.io/kubebuilder/v3/pkg/machinery" |
| 24 | + "sigs.k8s.io/kubebuilder/v3/pkg/model/resource" |
| 25 | + "sigs.k8s.io/kubebuilder/v3/pkg/plugins" |
| 26 | + "sigs.k8s.io/kubebuilder/v3/pkg/plugins/common/kustomize/v1/scaffolds/internal/templates/config/crd" |
| 27 | + "sigs.k8s.io/kubebuilder/v3/pkg/plugins/common/kustomize/v1/scaffolds/internal/templates/config/crd/patches" |
| 28 | + "sigs.k8s.io/kubebuilder/v3/pkg/plugins/common/kustomize/v1/scaffolds/internal/templates/config/rbac" |
| 29 | + "sigs.k8s.io/kubebuilder/v3/pkg/plugins/common/kustomize/v1/scaffolds/internal/templates/config/samples" |
| 30 | +) |
| 31 | + |
| 32 | +var _ plugins.Scaffolder = &apiScaffolder{} |
| 33 | + |
| 34 | +// apiScaffolder contains configuration for generating scaffolding for Go type |
| 35 | +// representing the API and controller that implements the behavior for the API. |
| 36 | +type apiScaffolder struct { |
| 37 | + config config.Config |
| 38 | + resource resource.Resource |
| 39 | + |
| 40 | + // fs is the filesystem that will be used by the scaffolder |
| 41 | + fs machinery.Filesystem |
| 42 | + |
| 43 | + // force indicates whether to scaffold files even if they exist. |
| 44 | + force bool |
| 45 | +} |
| 46 | + |
| 47 | +// NewAPIScaffolder returns a new Scaffolder for API/controller creation operations |
| 48 | +func NewAPIScaffolder(config config.Config, res resource.Resource, force bool) plugins.Scaffolder { |
| 49 | + return &apiScaffolder{ |
| 50 | + config: config, |
| 51 | + resource: res, |
| 52 | + force: force, |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +// InjectFS implements cmdutil.Scaffolder |
| 57 | +func (s *apiScaffolder) InjectFS(fs machinery.Filesystem) { |
| 58 | + s.fs = fs |
| 59 | +} |
| 60 | + |
| 61 | +// Scaffold implements cmdutil.Scaffolder |
| 62 | +func (s *apiScaffolder) Scaffold() error { |
| 63 | + fmt.Println("Writing kustomize manifests for you to edit...") |
| 64 | + |
| 65 | + // Initialize the machinery.Scaffold that will write the files to disk |
| 66 | + scaffold := machinery.NewScaffold(s.fs, |
| 67 | + machinery.WithConfig(s.config), |
| 68 | + machinery.WithResource(&s.resource), |
| 69 | + ) |
| 70 | + |
| 71 | + // Keep track of these values before the update |
| 72 | + if s.resource.HasAPI() { |
| 73 | + if err := scaffold.Execute( |
| 74 | + &samples.CRDSample{Force: s.force}, |
| 75 | + &rbac.CRDEditorRole{}, |
| 76 | + &rbac.CRDViewerRole{}, |
| 77 | + &patches.EnableWebhookPatch{}, |
| 78 | + &patches.EnableCAInjectionPatch{}, |
| 79 | + &crd.Kustomization{}, |
| 80 | + &crd.KustomizeConfig{}, |
| 81 | + ); err != nil { |
| 82 | + return fmt.Errorf("error scaffolding kustomize API manifests: %v", err) |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + return nil |
| 87 | +} |
0 commit comments