Skip to content

Commit

Permalink
kie-issues#1362: Migrate kn-workflow-plugin to kubernetes/client-go (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
treblereel authored Sep 5, 2024
1 parent e4cd28e commit 6b01127
Show file tree
Hide file tree
Showing 23 changed files with 1,038 additions and 199 deletions.
7 changes: 4 additions & 3 deletions packages/kn-plugin-workflow/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ require (
github.com/spf13/cobra v1.7.0
github.com/stretchr/testify v1.8.4
gopkg.in/yaml.v2 v2.4.0
k8s.io/apiextensions-apiserver v0.28.1
k8s.io/apimachinery v0.28.1
k8s.io/client-go v0.28.1
)

require (
Expand All @@ -33,6 +36,7 @@ require (
github.com/dprotaso/go-yit v0.0.0-20220510233725-9ba8df137936 // indirect
github.com/dustin/go-humanize v1.0.1 // indirect
github.com/emicklei/go-restful/v3 v3.11.0 // indirect
github.com/evanphx/json-patch v5.6.0+incompatible // indirect
github.com/evanphx/json-patch/v5 v5.7.0 // indirect
github.com/fsnotify/fsnotify v1.6.0 // indirect
github.com/gabriel-vasile/mimetype v1.4.2 // indirect
Expand Down Expand Up @@ -105,9 +109,6 @@ require (
gopkg.in/yaml.v3 v3.0.1 // indirect
gotest.tools/v3 v3.3.0 // indirect
k8s.io/api v0.28.1 // indirect
k8s.io/apiextensions-apiserver v0.28.1 // indirect
k8s.io/apimachinery v0.28.1 // indirect
k8s.io/client-go v0.28.1 // indirect
k8s.io/component-base v0.28.1 // indirect
k8s.io/klog/v2 v2.100.1 // indirect
k8s.io/kube-openapi v0.0.0-20230905202853-d090da108d2f // indirect
Expand Down
28 changes: 22 additions & 6 deletions packages/kn-plugin-workflow/pkg/command/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@
package command

import (
"errors"
"fmt"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"os"
"path"

Expand Down Expand Up @@ -138,7 +141,7 @@ func deploy(cfg *DeployUndeployCmdConfig) error {
return fmt.Errorf("❌ ERROR: failed to get manifest directory and files: %w", err)
}
for _, file := range files {
if err = common.ExecuteKubectlApply(file, cfg.NameSpace); err != nil {
if err = common.ExecuteApply(file, cfg.NameSpace); err != nil {
return fmt.Errorf("❌ ERROR: failed to deploy manifest %s, %w", file, err)
}
fmt.Printf(" - ✅ Manifest %s successfully deployed in namespace %s\n", path.Base(file), cfg.NameSpace)
Expand Down Expand Up @@ -188,11 +191,9 @@ func runDeployCmdConfig(cmd *cobra.Command) (cfg DeployUndeployCmdConfig, err er
return cfg, fmt.Errorf("❌ ERROR: failed to get default dashboards files folder: %w", err)
}

// check if sonataflow operator CRDs are installed
for _, crd := range metadata.SonataflowCRDs {
if !common.CheckKubectlCrdExists(crd) {
return cfg, fmt.Errorf("❌ ERROR: the required CRDs are not installed.. Install the SonataFlow Operator CRD first")
}
// check if sonataflow operator and knative CRDs are installed
if err := CheckCRDs(metadata.SonataflowCRDs, "SonataFlow Operator"); err != nil {
return cfg, err
}

//setup manifest path
Expand All @@ -202,3 +203,18 @@ func runDeployCmdConfig(cmd *cobra.Command) (cfg DeployUndeployCmdConfig, err er

return cfg, nil
}

func CheckCRDs(crds []string, typeName string) error {
for _, crd := range crds {
err := common.CheckCrdExists(crd)
if err != nil {
var statusErr *apierrors.StatusError
if errors.As(err, &statusErr) && statusErr.ErrStatus.Reason == metav1.StatusReasonNotFound {
return fmt.Errorf("❌ ERROR: the required CRDs are not installed.. Install the %s CRD first", typeName)
} else {
return fmt.Errorf("❌ ERROR: failed to check if CRD %s exists: %w", crd, err)
}
}
}
return nil
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ import (

"github.com/apache/incubator-kie-tools/packages/kn-plugin-workflow/pkg/common"
"github.com/apache/incubator-kie-tools/packages/kn-plugin-workflow/pkg/metadata"
"github.com/apache/incubator-kie-tools/packages/sonataflow-operator/workflowproj"
apimetadata "github.com/apache/incubator-kie-tools/packages/sonataflow-operator/api/metadata"
"github.com/apache/incubator-kie-tools/packages/sonataflow-operator/workflowproj"
)

type DeployUndeployCmdConfig struct {
Expand All @@ -54,19 +54,15 @@ type DeployUndeployCmdConfig struct {
func checkEnvironment(cfg *DeployUndeployCmdConfig) error {
fmt.Println("\n🔎 Checking your environment...")

if err := common.CheckKubectl(); err != nil {
return err
}

if ctx, err := common.CheckKubectlContext(); err != nil {
if ctx, err := common.CheckContext(); err != nil {
return err
} else {
cfg.KubectlContext = ctx
}

//setup namespace
if len(cfg.NameSpace) == 0 {
if defaultNamespace, err := common.GetKubectlNamespace(); err == nil {
if defaultNamespace, err := common.GetNamespace(); err == nil {
cfg.NameSpace = defaultNamespace
} else {
return err
Expand Down Expand Up @@ -216,7 +212,7 @@ func generateManifests(cfg *DeployUndeployCmdConfig) error {
}

if cfg.Image != "" {
handler.Image(cfg.Image)
handler.Image(cfg.Image)
}

err = handler.SaveAsKubernetesManifests(cfg.CustomGeneratedManifestDir)
Expand Down
4 changes: 2 additions & 2 deletions packages/kn-plugin-workflow/pkg/command/gen_manifest.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ func runGenManifestCmdConfig(cmd *cobra.Command) (cfg DeployUndeployCmdConfig, e

if cmd.Flags().Changed("profile") && len(cfg.Profile) == 0 {
profile, _ := cmd.Flags().GetString("profile")
if err := isValidProfile(profile); err != nil{
if err := isValidProfile(profile); err != nil {
return cfg, err
}
cfg.Profile = profile
Expand Down Expand Up @@ -198,7 +198,7 @@ func setupEnvironment(cfg *DeployUndeployCmdConfig) error {

//setup namespace
if len(cfg.NameSpace) == 0 && !cfg.EmptyNameSpace {
if defaultNamespace, err := common.GetKubectlNamespace(); err == nil {
if defaultNamespace, err := common.GetNamespace(); err == nil {
cfg.NameSpace = defaultNamespace
fmt.Printf(" - ✅ resolved namespace: %s\n", cfg.NameSpace)
} else {
Expand Down
45 changes: 20 additions & 25 deletions packages/kn-plugin-workflow/pkg/command/quarkus/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,12 @@ package quarkus

import (
"fmt"
"github.com/apache/incubator-kie-tools/packages/kn-plugin-workflow/pkg/command"
"github.com/apache/incubator-kie-tools/packages/kn-plugin-workflow/pkg/common"
"github.com/apache/incubator-kie-tools/packages/kn-plugin-workflow/pkg/metadata"
"github.com/ory/viper"
"github.com/spf13/cobra"
"path/filepath"
)

type DeployCmdConfig struct {
Expand Down Expand Up @@ -71,6 +74,11 @@ func NewDeployCommand() *cobra.Command {
return cmd
}

var crds = map[string][]string{
"SonataFlow Operator": metadata.SonataflowCRDs,
"Knative Serving and Knative Eventing": metadata.KnativeCoreServingCRDs,
}

func runDeploy(cmd *cobra.Command, args []string) error {
fmt.Println("🛠️ Deploying your Quarkus SonataFlow project...")

Expand All @@ -79,11 +87,15 @@ func runDeploy(cmd *cobra.Command, args []string) error {
return fmt.Errorf("initializing deploy config: %w", err)
}

if err = common.CheckKubectl(); err != nil {
return err
// check necessary CRDs are installed
for name, crds := range crds {
if err := command.CheckCRDs(crds, name); err != nil {
return err
}
}

if _, err = deployKnativeServiceAndEventingBindings(cfg); err != nil {
fmt.Println("❌ ERROR:Deploy failed, Knative Eventing binding was not created.")
return err
}

Expand All @@ -94,36 +106,19 @@ func runDeploy(cmd *cobra.Command, args []string) error {

func deployKnativeServiceAndEventingBindings(cfg DeployCmdConfig) (bool, error) {
isKnativeEventingBindingsCreated := false
createService := common.ExecCommand("kubectl", "apply", "-f", fmt.Sprintf("%s/knative.yml", cfg.Path), fmt.Sprintf("--namespace=%s", cfg.Namespace))
if err := common.RunCommand(
createService,
"deploy",
); err != nil {
fmt.Println("❌ ERROR: Deploy failed, Knative service was not created.")

err := common.ExecuteApply(filepath.Join(cfg.Path, "knative.yml"), cfg.Namespace)
if err != nil {
return isKnativeEventingBindingsCreated, err
}
fmt.Println("🎉 Knative service successfully created")

// Check if kogito.yml file exists
if exists, err := checkIfKogitoFileExists(cfg); exists && err == nil {
if cfg.Namespace == "" {
if namespace, err := common.GetKubectlNamespace(); err == nil {
cfg.Namespace = namespace
} else {
fmt.Println("❌ ERROR: Failed to get current kubectl namespace")
return isKnativeEventingBindingsCreated, err
}
}
deploy := common.ExecCommand("kubectl", "apply", "-f", fmt.Sprintf("%s/kogito.yml", cfg.Path), fmt.Sprintf("--namespace=%s", cfg.Namespace))
if err := common.RunCommand(
deploy,
"deploy",
); err != nil {
fmt.Println("❌ ERROR:Deploy failed, Knative Eventing binding was not created.")
if err := common.ExecuteApply(filepath.Join(cfg.Path, "kogito.yml"), cfg.Namespace); err != nil {
return isKnativeEventingBindingsCreated, err
}
isKnativeEventingBindingsCreated = true
fmt.Println(" Knative Eventing bindings successfully created")
fmt.Println("🎉 Knative Eventing bindings successfully created")
}
return isKnativeEventingBindingsCreated, nil
}
Expand All @@ -137,7 +132,7 @@ func runDeployCmdConfig(cmd *cobra.Command) (cfg DeployCmdConfig, err error) {
}

func checkIfKogitoFileExists(cfg DeployCmdConfig) (bool, error) {
if _, err := common.FS.Stat(fmt.Sprintf("%s/kogito.yml", cfg.Path)); err == nil {
if _, err := common.FS.Stat(filepath.Join(cfg.Path, "kogito.yml")); err == nil {
return true, nil
} else {
return false, err
Expand Down
Loading

0 comments on commit 6b01127

Please sign in to comment.