github.com/apache/incubator-kie-tools/packages/kn-plugin-workflow@v0.28.1-0.20240311201729-34c6856b157f/pkg/command/undeploy.go (about) 1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one 3 * or more contributor license agreements. See the NOTICE file 4 * distributed with this work for additional information 5 * regarding copyright ownership. The ASF licenses this file 6 * to you under the Apache License, Version 2.0 (the 7 * "License"); you may not use this file except in compliance 8 * with the License. You may obtain a copy of the License at 9 * 10 * http://www.apache.org/licenses/LICENSE-2.0 11 * 12 * Unless required by applicable law or agreed to in writing, 13 * software distributed under the License is distributed on an 14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 * KIND, either express or implied. See the License for the 16 * specific language governing permissions and limitations 17 * under the License. 18 */ 19 20 package command 21 22 import ( 23 "fmt" 24 "github.com/apache/incubator-kie-tools/packages/kn-plugin-workflow/pkg/metadata" 25 26 "github.com/apache/incubator-kie-tools/packages/kn-plugin-workflow/pkg/common" 27 "github.com/ory/viper" 28 "github.com/spf13/cobra" 29 "os" 30 "path" 31 ) 32 33 func NewUndeployCommand() *cobra.Command { 34 var cmd = &cobra.Command{ 35 Use: "undeploy", 36 Short: "Undeploy a SonataFlow project on Kubernetes via SonataFlow Operator", 37 Long: ` 38 Undeploy a SonataFlow project in Kubernetes via the SonataFlow Operator. 39 `, 40 Example: ` 41 # Undeploy the workflow project from the current directory's project. 42 # You must provide target namespace. 43 {{.Name}} undeploy --namespace <your_namespace> 44 45 # Persist the generated Kubernetes manifests on a given path and deploy the 46 # workflow from the current directory's project. 47 {{.Name}} undeploy --custom-generated-manifests-dir=<full_directory_path> 48 49 # Specify a custom manifest files directory. 50 # This option *will not* automatically generate the manifest files, but will use the existing ones. 51 {{.Name}} deploy --custom-manifests-dir=<full_directory_path> 52 53 # Specify a custom subflows files directory. (default: ./subflows) 54 {{.Name}} deploy --subflows-dir=<full_directory_path> 55 56 # Specify a custom support specs directory. (default: ./specs) 57 {{.Name}} deploy --specs-dir=<full_directory_path> 58 59 # Specify a custom support schemas directory. (default: ./schemas) 60 {{.Name}} deploy --schemas-dir=<full_directory_path> 61 62 `, 63 64 PreRunE: common.BindEnv("namespace", "custom-manifests-dir", "custom-generated-manifests-dir", "specs-dir", "schemas-dir", "subflows-dir"), 65 SuggestFor: []string{"undelpoy", "undeplyo"}, 66 } 67 68 cmd.RunE = func(cmd *cobra.Command, args []string) error { 69 return runUndeploy(cmd, args) 70 } 71 72 cmd.Flags().StringP("namespace", "n", "", "Target namespace of your deployment.") 73 cmd.Flags().StringP("custom-manifests-dir", "m", "", "Specify a custom manifest files directory. This option will not automatically generate the manifest files, but will use the existing ones.") 74 cmd.Flags().StringP("custom-generated-manifests-dir", "c", "", "Target directory of your generated Kubernetes manifests.") 75 cmd.Flags().StringP("specs-dir", "p", "", "Specify a custom specs files directory") 76 cmd.Flags().StringP("subflows-dir", "s", "", "Specify a custom subflows files directory") 77 cmd.Flags().StringP("schemas-dir", "t", "", "Specify a custom schemas files directory") 78 79 cmd.SetHelpFunc(common.DefaultTemplatedHelp) 80 81 return cmd 82 } 83 84 func runUndeploy(cmd *cobra.Command, args []string) error { 85 86 cfg, err := runUndeployCmdConfig(cmd) 87 //temp dir cleanup 88 defer func(cfg *DeployUndeployCmdConfig) { 89 if cfg.TempDir != "" { 90 if err := os.RemoveAll(cfg.TempDir); err != nil { 91 fmt.Errorf("ā ERROR: failed to remove temp dir: %v", err) 92 } 93 } 94 }(&cfg) 95 96 if err != nil { 97 return fmt.Errorf("ā ERROR: initializing undeploy config: %w", err) 98 } 99 100 fmt.Println("š ļøļø Undeploy a SonataFlow project on Kubernetes via the SonataFlow Operator...") 101 102 if err := checkEnvironment(&cfg); err != nil { 103 return fmt.Errorf("ā ERROR: checking undeploy environment: %w", err) 104 } 105 106 if len(cfg.CustomManifestsFileDir) == 0 { 107 if err := generateManifests(&cfg); err != nil { 108 return fmt.Errorf("ā ERROR: generating deploy environment: %w", err) 109 } 110 } else { 111 fmt.Printf("š Using manifests located at %s\n", cfg.CustomManifestsFileDir) 112 } 113 114 if err = undeploy(&cfg); err != nil { 115 return fmt.Errorf("ā ERROR: undeploying: %w", err) 116 } 117 118 fmt.Println("\nš SonataFlow project successfully undeployed.") 119 120 return nil 121 } 122 123 func undeploy(cfg *DeployUndeployCmdConfig) error { 124 fmt.Printf("šØ Undeploying your SonataFlow project in namespace %s\n", cfg.NameSpace) 125 126 manifestExtension := []string{metadata.YAMLExtension} 127 128 manifestPath := cfg.CustomGeneratedManifestDir 129 if len(cfg.CustomManifestsFileDir) != 0 { 130 manifestPath = cfg.CustomManifestsFileDir 131 } 132 133 files, err := common.FindFilesWithExtensions(manifestPath, manifestExtension) 134 if err != nil { 135 return fmt.Errorf("ā ERROR: failed to get manifest directory and files: %w", err) 136 } 137 138 for _, file := range files { 139 if err = common.ExecuteKubectlDelete(file, cfg.NameSpace); err != nil { 140 return fmt.Errorf("ā ERROR: failed to undeploy manifest %s, %w", file, err) 141 } 142 fmt.Printf(" - ā Manifest %s successfully undeployed in namespace %s\n", path.Base(file), cfg.NameSpace) 143 144 } 145 return nil 146 } 147 148 func runUndeployCmdConfig(cmd *cobra.Command) (cfg DeployUndeployCmdConfig, err error) { 149 150 cfg = DeployUndeployCmdConfig{ 151 NameSpace: viper.GetString("namespace"), 152 CustomManifestsFileDir: viper.GetString("custom-manifests-dir"), 153 CustomGeneratedManifestDir: viper.GetString("custom-generated-manifests-dir"), 154 SpecsDir: viper.GetString("specs-dir"), 155 SchemasDir: viper.GetString("schemas-dir"), 156 SubflowsDir: viper.GetString("subflows-dir"), 157 } 158 159 if len(cfg.SubflowsDir) == 0 { 160 dir, err := os.Getwd() 161 cfg.SubflowsDir = dir + "/subflows" 162 if err != nil { 163 return cfg, fmt.Errorf("ā ERROR: failed to get default subflows workflow files folder: %w", err) 164 } 165 } 166 167 if len(cfg.SpecsDir) == 0 { 168 dir, err := os.Getwd() 169 cfg.SpecsDir = dir + "/specs" 170 if err != nil { 171 return cfg, fmt.Errorf("ā ERROR: failed to get default support specs files folder: %w", err) 172 } 173 } 174 175 if len(cfg.SchemasDir) == 0 { 176 dir, err := os.Getwd() 177 cfg.SchemasDir = dir + "/schemas" 178 if err != nil { 179 return cfg, fmt.Errorf("ā ERROR: failed to get default support schemas files folder: %w", err) 180 } 181 } 182 183 dir, err := os.Getwd() 184 cfg.DefaultDashboardsFolder = dir + "/" + metadata.DashboardsDefaultDirName 185 if err != nil { 186 return cfg, fmt.Errorf("ā ERROR: failed to get default dashboards files folder: %w", err) 187 } 188 189 //setup manifest path 190 if err := setupConfigManifestPath(&cfg); err != nil { 191 return cfg, err 192 } 193 194 return cfg, nil 195 }