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