github.com/mkimuram/operator-sdk@v0.7.1-0.20190410172100-52ad33a4bda0/cmd/operator-sdk/generate/k8s.go (about)

     1  // Copyright 2019 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 generate
    16  
    17  import (
    18  	"fmt"
    19  
    20  	"github.com/operator-framework/operator-sdk/cmd/operator-sdk/internal/genutil"
    21  	"github.com/operator-framework/operator-sdk/internal/util/projutil"
    22  
    23  	"github.com/spf13/cobra"
    24  )
    25  
    26  func newGenerateK8SCmd() *cobra.Command {
    27  	k8sCmd := &cobra.Command{
    28  		Use:   "k8s",
    29  		Short: "Generates Kubernetes code for custom resource",
    30  		Long: `k8s generator generates code for custom resources given the API
    31  specs in pkg/apis/<group>/<version> directories to comply with kube-API
    32  requirements. Go code is generated under
    33  pkg/apis/<group>/<version>/zz_generated.deepcopy.go.
    34  Example:
    35  	$ operator-sdk generate k8s
    36  	$ tree pkg/apis
    37  	pkg/apis/
    38  	└── app
    39  		└── v1alpha1
    40  			├── zz_generated.deepcopy.go
    41  `,
    42  		RunE: k8sFunc,
    43  	}
    44  
    45  	k8sCmd.Flags().StringVar(&headerFile, "header-file", "", "Path to file containing headers for generated files.")
    46  
    47  	return k8sCmd
    48  }
    49  
    50  func k8sFunc(cmd *cobra.Command, args []string) error {
    51  	if len(args) != 0 {
    52  		return fmt.Errorf("command %s doesn't accept any arguments", cmd.CommandPath())
    53  	}
    54  
    55  	// Only Go projects can generate k8s deepcopy code.
    56  	if err := projutil.CheckGoProjectCmd(cmd); err != nil {
    57  		return err
    58  	}
    59  
    60  	return genutil.K8sCodegen(headerFile)
    61  }