github.com/jmrodri/operator-sdk@v0.5.0/pkg/scaffold/addtoscheme.go (about)

     1  // Copyright 2018 The Operator-SDK Authors
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package scaffold
    16  
    17  import (
    18  	"fmt"
    19  	"path/filepath"
    20  	"strings"
    21  
    22  	"github.com/operator-framework/operator-sdk/pkg/scaffold/input"
    23  )
    24  
    25  // AddToScheme is the input needed to generate an addtoscheme_<group>_<version>.go file
    26  type AddToScheme struct {
    27  	input.Input
    28  
    29  	// Resource defines the inputs for the new api
    30  	Resource *Resource
    31  }
    32  
    33  func (s *AddToScheme) GetInput() (input.Input, error) {
    34  	if s.Path == "" {
    35  		fileName := fmt.Sprintf("addtoscheme_%s_%s.go",
    36  			strings.ToLower(s.Resource.Group),
    37  			strings.ToLower(s.Resource.Version))
    38  		s.Path = filepath.Join(ApisDir, fileName)
    39  	}
    40  	s.TemplateBody = addToSchemeTemplate
    41  	return s.Input, nil
    42  }
    43  
    44  const addToSchemeTemplate = `package apis
    45  
    46  import (
    47  	"{{ .Repo }}/pkg/apis/{{ .Resource.Group }}/{{ .Resource.Version }}"
    48  )
    49  
    50  func init() {
    51  	// Register the types with the Scheme so the components can map objects to GroupVersionKinds and back
    52  	AddToSchemes = append(AddToSchemes, {{ .Resource.Version }}.SchemeBuilder.AddToScheme)
    53  }
    54  `