github.com/jmrodri/operator-sdk@v0.5.0/pkg/scaffold/types.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  	"path/filepath"
    19  	"strings"
    20  
    21  	"github.com/operator-framework/operator-sdk/pkg/scaffold/input"
    22  )
    23  
    24  // Types is the input needed to generate a pkg/apis/<group>/<version>/<kind>_types.go file
    25  type Types struct {
    26  	input.Input
    27  
    28  	// Resource defines the inputs for the new types file
    29  	Resource *Resource
    30  }
    31  
    32  func (s *Types) GetInput() (input.Input, error) {
    33  	if s.Path == "" {
    34  		s.Path = filepath.Join(ApisDir,
    35  			strings.ToLower(s.Resource.Group),
    36  			strings.ToLower(s.Resource.Version),
    37  			s.Resource.LowerKind+"_types.go")
    38  	}
    39  	// Error if this file exists.
    40  	s.IfExistsAction = input.Error
    41  	s.TemplateBody = typesTemplate
    42  	return s.Input, nil
    43  }
    44  
    45  const typesTemplate = `package {{ .Resource.Version }}
    46  
    47  import (
    48  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    49  )
    50  
    51  // EDIT THIS FILE!  THIS IS SCAFFOLDING FOR YOU TO OWN!
    52  // NOTE: json tags are required.  Any new fields you add must have json tags for the fields to be serialized.
    53  
    54  // {{.Resource.Kind}}Spec defines the desired state of {{.Resource.Kind}}
    55  // +k8s:openapi-gen=true
    56  type {{.Resource.Kind}}Spec struct {
    57  	// INSERT ADDITIONAL SPEC FIELDS - desired state of cluster
    58  	// Important: Run "operator-sdk generate k8s" to regenerate code after modifying this file
    59  	// Add custom validation using kubebuilder tags: https://book.kubebuilder.io/beyond_basics/generating_crd.html
    60  }
    61  
    62  // {{.Resource.Kind}}Status defines the observed state of {{.Resource.Kind}}
    63  // +k8s:openapi-gen=true
    64  type {{.Resource.Kind}}Status struct {
    65  	// INSERT ADDITIONAL STATUS FIELD - define observed state of cluster
    66  	// Important: Run "operator-sdk generate k8s" to regenerate code after modifying this file
    67  	// Add custom validation using kubebuilder tags: https://book.kubebuilder.io/beyond_basics/generating_crd.html
    68  }
    69  
    70  // +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
    71  
    72  // {{.Resource.Kind}} is the Schema for the {{ .Resource.Resource }} API
    73  // +k8s:openapi-gen=true
    74  type {{.Resource.Kind}} struct {
    75  	metav1.TypeMeta   ` + "`" + `json:",inline"` + "`" + `
    76  	metav1.ObjectMeta ` + "`" + `json:"metadata,omitempty"` + "`" + `
    77  
    78  	Spec   {{.Resource.Kind}}Spec   ` + "`" + `json:"spec,omitempty"` + "`" + `
    79  	Status {{.Resource.Kind}}Status ` + "`" + `json:"status,omitempty"` + "`" + `
    80  }
    81  
    82  // +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
    83  
    84  // {{.Resource.Kind}}List contains a list of {{.Resource.Kind}}
    85  type {{.Resource.Kind}}List struct {
    86  	metav1.TypeMeta ` + "`" + `json:",inline"` + "`" + `
    87  	metav1.ListMeta ` + "`" + `json:"metadata,omitempty"` + "`" + `
    88  	Items           []{{ .Resource.Kind }} ` + "`" + `json:"items"` + "`" + `
    89  }
    90  
    91  func init() {
    92  	SchemeBuilder.Register(&{{.Resource.Kind}}{}, &{{.Resource.Kind}}List{})
    93  }
    94  `