github.com/alex123012/deckhouse-controller-tools@v0.0.0-20230510090815-d594daf1af8c/pkg/typescaffold/scaffold.go (about)

     1  /*
     2  Copyright 2018 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 typescaffold
    18  
    19  import (
    20  	"io"
    21  	"strings"
    22  	"text/template"
    23  )
    24  
    25  var (
    26  	typesTemplateRaw = `// {{.Resource.Kind}}Spec defines the desired state of {{.Resource.Kind}}
    27  type {{.Resource.Kind}}Spec struct {
    28  	// INSERT ADDITIONAL SPEC FIELDS -- desired state of cluster
    29  {{- if .AdditionalHelp }}
    30  {{- range .AdditionalHelp | SplitLines }}
    31  	// {{.}}
    32  {{- end }}
    33  {{- end }}
    34  }
    35  
    36  // {{.Resource.Kind}}Status defines the observed state of {{.Resource.Kind}}.
    37  // It should always be reconstructable from the state of the cluster and/or outside world.
    38  type {{.Resource.Kind}}Status struct {
    39  	// INSERT ADDITIONAL STATUS FIELDS -- observed state of cluster
    40  {{- if .AdditionalHelp }}
    41  {{- range .AdditionalHelp | SplitLines }}
    42  	// {{.}}
    43  {{- end }}
    44  {{- end }}
    45  }
    46  
    47  // +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
    48  {{- if .GenerateClients }}
    49  // +genclient
    50  {{- if not .Resource.Namespaced }}
    51  // +genclient:nonNamespaced
    52  {{- end }}
    53  {{- end }}
    54  
    55  // {{.Resource.Kind}} is the Schema for the {{ .Resource.Resource }} API
    56  // +k8s:openapi-gen=true
    57  type {{.Resource.Kind}} struct {
    58  	metav1.TypeMeta   ` + "`" + `json:",inline"` + "`" + `
    59  	metav1.ObjectMeta ` + "`" + `json:"metadata,omitempty"` + "`" + `
    60  
    61  	Spec   {{.Resource.Kind}}Spec   ` + "`" + `json:"spec,omitempty"` + "`" + `
    62  	Status {{.Resource.Kind}}Status ` + "`" + `json:"status,omitempty"` + "`" + `
    63  }
    64  
    65  // +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
    66  {{- if and (.GenerateClients) (not .Resource.Namespaced) }}
    67  // +genclient:nonNamespaced
    68  {{- end }}
    69  
    70  // {{.Resource.Kind}}List contains a list of {{.Resource.Kind}}
    71  type {{.Resource.Kind}}List struct {
    72  	metav1.TypeMeta ` + "`" + `json:",inline"` + "`" + `
    73  	metav1.ListMeta ` + "`" + `json:"metadata,omitempty"` + "`" + `
    74  	Items           []{{ .Resource.Kind }} ` + "`" + `json:"items"` + "`" + `
    75  }
    76  `
    77  	typesTemplateHelpers = template.FuncMap{
    78  		"SplitLines": func(raw string) []string { return strings.Split(raw, "\n") },
    79  	}
    80  
    81  	typesTemplate = template.Must(template.New("object-scaffolding").Funcs(typesTemplateHelpers).Parse(typesTemplateRaw))
    82  )
    83  
    84  // ScaffoldOptions describes how to scaffold out a Kubernetes object
    85  // with the basic metadata and comment annotations required to generate code
    86  // for and conform to runtime.Object and metav1.Object.
    87  type ScaffoldOptions struct {
    88  	Resource        Resource
    89  	AdditionalHelp  string
    90  	GenerateClients bool
    91  }
    92  
    93  // Validate validates the options, returning an error if anything is invalid.
    94  func (o *ScaffoldOptions) Validate() error {
    95  	err := o.Resource.Validate()
    96  	return err
    97  }
    98  
    99  // Scaffold prints the Kubernetes object scaffolding to the given output.
   100  func (o *ScaffoldOptions) Scaffold(out io.Writer) error {
   101  	return typesTemplate.Execute(out, o)
   102  }