sigs.k8s.io/kubebuilder/v3@v3.14.0/pkg/machinery/injector.go (about)

     1  /*
     2  Copyright 2022 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 machinery
    18  
    19  import (
    20  	"sigs.k8s.io/kubebuilder/v3/pkg/config"
    21  	"sigs.k8s.io/kubebuilder/v3/pkg/model/resource"
    22  )
    23  
    24  // injector is used to inject certain fields to file templates.
    25  type injector struct {
    26  	// config stores the project configuration.
    27  	config config.Config
    28  
    29  	// boilerplate is the copyright comment added at the top of scaffolded files.
    30  	boilerplate string
    31  
    32  	// resource contains the information of the API that is being scaffolded.
    33  	resource *resource.Resource
    34  }
    35  
    36  // injectInto injects fields from the universe into the builder
    37  func (i injector) injectInto(builder Builder) {
    38  	// Inject project configuration
    39  	if i.config != nil {
    40  		if builderWithDomain, hasDomain := builder.(HasDomain); hasDomain {
    41  			builderWithDomain.InjectDomain(i.config.GetDomain())
    42  		}
    43  		if builderWithRepository, hasRepository := builder.(HasRepository); hasRepository {
    44  			builderWithRepository.InjectRepository(i.config.GetRepository())
    45  		}
    46  		if builderWithProjectName, hasProjectName := builder.(HasProjectName); hasProjectName {
    47  			builderWithProjectName.InjectProjectName(i.config.GetProjectName())
    48  		}
    49  		if builderWithMultiGroup, hasMultiGroup := builder.(HasMultiGroup); hasMultiGroup {
    50  			builderWithMultiGroup.InjectMultiGroup(i.config.IsMultiGroup())
    51  		}
    52  		if builderWithComponentConfig, hasComponentConfig := builder.(HasComponentConfig); hasComponentConfig {
    53  			builderWithComponentConfig.InjectComponentConfig(i.config.IsComponentConfig())
    54  		}
    55  	}
    56  	// Inject boilerplate
    57  	if builderWithBoilerplate, hasBoilerplate := builder.(HasBoilerplate); hasBoilerplate {
    58  		builderWithBoilerplate.InjectBoilerplate(i.boilerplate)
    59  	}
    60  	// Inject resource
    61  	if i.resource != nil {
    62  		if builderWithResource, hasResource := builder.(HasResource); hasResource {
    63  			builderWithResource.InjectResource(i.resource)
    64  		}
    65  	}
    66  }