github.com/apache/incubator-kie-tools/packages/kn-plugin-workflow@v0.28.1-0.20240311201729-34c6856b157f/pkg/command/quarkus/deploy.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 quarkus
    21  
    22  import (
    23  	"fmt"
    24  	"github.com/apache/incubator-kie-tools/packages/kn-plugin-workflow/pkg/common"
    25  	"github.com/ory/viper"
    26  	"github.com/spf13/cobra"
    27  )
    28  
    29  type DeployCmdConfig struct {
    30  	Path string // service name
    31  }
    32  
    33  func NewDeployCommand() *cobra.Command {
    34  	var cmd = &cobra.Command{
    35  		Use:   "deploy",
    36  		Short: "Deploy a Quarkus SonataFlow project",
    37  		Long: `
    38  	Deploys a Quarkus SonataFlow project in the current directory. 
    39  	By default, this command uses the ./target/kubernetes folder to find
    40  	the deployment files generated in the build process. The build step
    41  	is required before using the deploy command.
    42  
    43  	Before you use the deploy command, ensure that your cluster have 
    44  	access to the build output image.
    45  		`,
    46  		Example: `
    47  	# Deploy the workflow from the current directory's project. 
    48  	# Deploy as Knative service.
    49  	{{.Name}} deploy
    50  	
    51  	# Specify the path of the directory containing the "knative.yml" 
    52  	{{.Name}} deploy --path ./kubernetes
    53  		`,
    54  		SuggestFor: []string{"delpoy", "deplyo"},
    55  		PreRunE:    common.BindEnv("path"),
    56  	}
    57  
    58  	cmd.RunE = func(cmd *cobra.Command, args []string) error {
    59  		return runDeploy(cmd, args)
    60  	}
    61  
    62  	cmd.Flags().StringP("path", "p", "./target/kubernetes", fmt.Sprintf("%s path to knative deployment files", cmd.Name()))
    63  
    64  	cmd.SetHelpFunc(common.DefaultTemplatedHelp)
    65  
    66  	return cmd
    67  }
    68  
    69  func runDeploy(cmd *cobra.Command, args []string) error {
    70  	fmt.Println("🛠️  Deploying your Quarkus SonataFlow project...")
    71  
    72  	cfg, err := runDeployCmdConfig(cmd)
    73  	if err != nil {
    74  		return fmt.Errorf("initializing deploy config: %w", err)
    75  	}
    76  
    77  	if err = common.CheckKubectl(); err != nil {
    78  		return err
    79  	}
    80  
    81  	if _, err = deployKnativeServiceAndEventingBindings(cfg); err != nil {
    82  		return err
    83  	}
    84  
    85  	fmt.Println("🎉 Quarkus SonataFlow project successfully deployed")
    86  
    87  	return nil
    88  }
    89  
    90  func deployKnativeServiceAndEventingBindings(cfg DeployCmdConfig) (bool, error) {
    91  	isKnativeEventingBindingsCreated := false
    92  	createService := common.ExecCommand("kubectl", "apply", "-f", fmt.Sprintf("%s/knative.yml", cfg.Path))
    93  	if err := common.RunCommand(
    94  		createService,
    95  		"deploy",
    96  	); err != nil {
    97  		fmt.Println("❌ ERROR: Deploy failed, Knative service was not created.")
    98  		return isKnativeEventingBindingsCreated, err
    99  	}
   100  	fmt.Println("🎉 Knative service successfully created")
   101  
   102  	// Check if kogito.yml file exists
   103  	if exists, err := checkIfKogitoFileExists(cfg); exists && err == nil {
   104  		deploy := common.ExecCommand("kubectl", "apply", "-f", fmt.Sprintf("%s/kogito.yml", cfg.Path))
   105  		if err := common.RunCommand(
   106  			deploy,
   107  			"deploy",
   108  		); err != nil {
   109  			fmt.Println("❌ ERROR:Deploy failed, Knative Eventing binding was not created.")
   110  			return isKnativeEventingBindingsCreated, err
   111  		}
   112  		isKnativeEventingBindingsCreated = true
   113  		fmt.Println("✅ Knative Eventing bindings successfully created")
   114  	}
   115  	return isKnativeEventingBindingsCreated, nil
   116  }
   117  
   118  func runDeployCmdConfig(cmd *cobra.Command) (cfg DeployCmdConfig, err error) {
   119  	cfg = DeployCmdConfig{
   120  		Path: viper.GetString("path"),
   121  	}
   122  	return
   123  }
   124  
   125  func checkIfKogitoFileExists(cfg DeployCmdConfig) (bool, error) {
   126  	if _, err := common.FS.Stat(fmt.Sprintf("%s/kogito.yml", cfg.Path)); err == nil {
   127  		return true, nil
   128  	} else {
   129  		return false, err
   130  	}
   131  }