github.com/mkimuram/operator-sdk@v0.7.1-0.20190410172100-52ad33a4bda0/cmd/operator-sdk/add/controller.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 add
    16  
    17  import (
    18  	"fmt"
    19  
    20  	"github.com/operator-framework/operator-sdk/internal/pkg/scaffold"
    21  	"github.com/operator-framework/operator-sdk/internal/pkg/scaffold/input"
    22  	"github.com/operator-framework/operator-sdk/internal/util/projutil"
    23  
    24  	log "github.com/sirupsen/logrus"
    25  	"github.com/spf13/cobra"
    26  )
    27  
    28  func newAddControllerCmd() *cobra.Command {
    29  	controllerCmd := &cobra.Command{
    30  		Use:   "controller",
    31  		Short: "Adds a new controller pkg",
    32  		Long: `operator-sdk add controller --kind=<kind> --api-version=<group/version> creates a new
    33  controller pkg under pkg/controller/<kind> that, by default, reconciles on a custom resource for the specified apiversion and kind.
    34  The controller will expect to use the custom resource type that should already be defined under pkg/apis/<group>/<version>
    35  via the "operator-sdk add api --kind=<kind> --api-version=<group/version>" command.
    36  This command must be run from the project root directory.
    37  If the controller pkg for that Kind already exists at pkg/controller/<kind> then the command will not overwrite and return an error.
    38  
    39  Example:
    40  	$ operator-sdk add controller --api-version=app.example.com/v1alpha1 --kind=AppService
    41  	$ tree pkg/controller
    42  	pkg/controller/
    43  	├── add_appservice.go
    44  	├── appservice
    45  	│   └── appservice_controller.go
    46  	└── controller.go
    47  
    48  `,
    49  		RunE: controllerRun,
    50  	}
    51  
    52  	controllerCmd.Flags().StringVar(&apiVersion, "api-version", "", "Kubernetes APIVersion that has a format of $GROUP_NAME/$VERSION (e.g app.example.com/v1alpha1)")
    53  	if err := controllerCmd.MarkFlagRequired("api-version"); err != nil {
    54  		log.Fatalf("Failed to mark `api-version` flag for `add controller` subcommand as required")
    55  	}
    56  	controllerCmd.Flags().StringVar(&kind, "kind", "", "Kubernetes resource Kind name. (e.g AppService)")
    57  	if err := controllerCmd.MarkFlagRequired("kind"); err != nil {
    58  		log.Fatalf("Failed to mark `kind` flag for `add controller` subcommand as required")
    59  	}
    60  
    61  	return controllerCmd
    62  }
    63  
    64  func controllerRun(cmd *cobra.Command, args []string) error {
    65  	projutil.MustInProjectRoot()
    66  
    67  	// Only Go projects can add controllers.
    68  	if err := projutil.CheckGoProjectCmd(cmd); err != nil {
    69  		return err
    70  	}
    71  
    72  	log.Infof("Generating controller version %s for kind %s.", apiVersion, kind)
    73  
    74  	// Create and validate new resource
    75  	r, err := scaffold.NewResource(apiVersion, kind)
    76  	if err != nil {
    77  		return err
    78  	}
    79  
    80  	cfg := &input.Config{
    81  		Repo:           projutil.CheckAndGetProjectGoPkg(),
    82  		AbsProjectPath: projutil.MustGetwd(),
    83  	}
    84  
    85  	s := &scaffold.Scaffold{}
    86  	err = s.Execute(cfg,
    87  		&scaffold.ControllerKind{Resource: r},
    88  		&scaffold.AddController{Resource: r},
    89  	)
    90  	if err != nil {
    91  		return fmt.Errorf("controller scaffold failed: (%v)", err)
    92  	}
    93  
    94  	log.Info("Controller generation complete.")
    95  	return nil
    96  }