github.skymusic.top/operator-framework/operator-sdk@v0.8.2/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  	return &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  
    46  func k8sFunc(cmd *cobra.Command, args []string) error {
    47  	if len(args) != 0 {
    48  		return fmt.Errorf("command %s doesn't accept any arguments", cmd.CommandPath())
    49  	}
    50  
    51  	// Only Go projects can generate k8s deepcopy code.
    52  	if err := projutil.CheckGoProjectCmd(cmd); err != nil {
    53  		return err
    54  	}
    55  
    56  	return genutil.K8sCodegen()
    57  }