Skip to content
This repository has been archived by the owner on Mar 14, 2024. It is now read-only.

Commit

Permalink
[KOGITO-148] - Implementation of deploy command to CLI (#35)
Browse files Browse the repository at this point in the history
  • Loading branch information
ricardozanini committed Aug 21, 2019
1 parent 0d93829 commit 1b00925
Show file tree
Hide file tree
Showing 26 changed files with 670 additions and 139 deletions.
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ build:
build-cli:
./hack/go-build-cli.sh

.PHONY: install-cli
install-cli:
./hack/go-install-cli.sh

.PHONY: clean
clean:
rm -rf build/_output
71 changes: 65 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,15 +88,21 @@ This function will create a new `RoleBinding` that depends on the `ServiceAccoun
make
```

## Upload to a container registry
## Installation

Kogito Operator is not available in the OperatorHub [yet](https://issues.jboss.org/browse/KOGITO-67), hence have to be installed manually on [OpenShift 4.x](#deploy-to-openshift-4.x-using-olm) or [OpenShift 3.11](#deploy-to-openshift-311-manually).

You can also [run the operator locally](#running-locally) if you have the [requirements](#requirements) configured in your local machine.

### Upload to a container registry

e.g.

```bash
docker push quay.io/kiegroup/kogito-cloud-operator:<version>
```

## Deploy to OpenShift 4 using OLM
### Deploy to OpenShift 4.x using OLM

To install this operator on OpenShift 4 for end-to-end testing, make sure you have access to a quay.io account to create an application repository. Follow the [authentication](https://github.com/operator-framework/operator-courier/#authentication) instructions for Operator Courier to obtain an account token. This token is in the form of "basic XXXXXXXXX" and both words are required for the command.

Expand All @@ -120,14 +126,13 @@ Remember to replace _registryNamespace_ with your quay namespace. The name, disp

It will take a few minutes for the operator to become visible under the _OperatorHub_ section of the OpenShift console _Catalog_. The Operator can be easily found by filtering the provider type to _Custom_.


It's possible to verify the operator status by running:

```bash
oc describe operatorsource.operators.coreos.com/kiecloud-operators -n openshift-marketplace
```

## Deploy to OpenShift 3.11 manually
### Deploy to OpenShift 3.11 manually

```bash
## kogito imagestreams should already be installed/available ... e.g.
Expand All @@ -151,7 +156,7 @@ kogitoapp.app.kiegroup.org/example-quarkus created
oc delete kogitoapp example-quarkus
```

## Running Locally
### Running Locally

Change log level at runtime with the `DEBUG` environment variable. e.g. -

Expand All @@ -169,6 +174,60 @@ It's always worth noting that you should generate, vet, format, lint, and test y
make test
```

## CLI

A CLI tool is available to make it easy to deploy new Kogito Services from source instead of relying on CRs yaml files.

### CLI Requirements

1. [`oc` client](https://docs.okd.io/latest/cli_reference/get_started_cli.html) installed
2. An authenticated OpenShift user with permissions to create resources in a given namespace
3. [Go installed](https://golang.org/doc/install) and available in your `PATH`
4. Kogito Operator [installed](#installation) in the cluster

### Build CLI from source

Build the CLI by running:

```bash
$ git clone https://github.com/kiegroup/kogito-cloud-operator
$ cd kogito-cloud-operator
$ make install-cli
```

The `kogito` CLI should be available in your path:

```bash
$ kogito
Kogito CLI deploys your Kogito Services into an OpenShift cluster

Usage:
kogito [command]

Available Commands:
app Sets the Kogito application where your Kogito Service will be deployed
deploy Deploys a new Kogito Service into the application context
help Help about any command
new-app Creates a new Kogito Application for your Kogito Services

Flags:
--config string config file (default is $HOME/.kogito.json)
-h, --help help for kogito
-v, --verbose verbose output

Use "kogito [command] --help" for more information about a command.
```

After [installing](#installation) the Kogito Operator, it's possible to deploy a new Kogito Service by using the CLI:

```bash
# creates a new namespace/kogito app in your cluster
$ kogito new-app kogito-cli

# deploys a new kogito service from source
$ kogito deploy example-drools https://github.com/kiegroup/kogito-examples --context drools-quarkus-example
```

## Contributing

Please take a look at the [Contributing to Kogito Operator](CONTRIBUTING.MD) guide.
Please take a look at the [Contributing to Kogito Operator](CONTRIBUTING.MD) guide.
21 changes: 11 additions & 10 deletions cmd/kogito/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,24 @@ import (
)

var appCmd *cobra.Command
var appName string
var appCmdName string

var _ = RegisterCommandVar(func() {
appCmd = &cobra.Command{
Use: "app NAME",
Short: "Sets the Kogito application where your application will be deployed",
Short: "Sets the Kogito application where your Kogito Service will be deployed",
Long: `app will set the context where the Kogito services will be deployed. It's the namespace/project on Kubernetes/OpenShift world.`,
RunE: func(cmd *cobra.Command, args []string) error {
return appExec(cmd, args)
},
PostRun: saveConfiguration,
PreRun: preRunF,
PostRun: posRunF,
}
})

var _ = RegisterCommandInit(func() {
rootCmd.AddCommand(appCmd)
appCmd.Flags().StringVarP(&appName, "name", "n", "", "The app name")
appCmd.Flags().StringVarP(&appCmdName, "name", "n", "", "The app name")
})

func appExec(cmd *cobra.Command, args []string) error {
Expand All @@ -33,19 +34,19 @@ func appExec(cmd *cobra.Command, args []string) error {
if len(config.Namespace) == 0 {
return fmt.Errorf("No application set in the context. Use 'kogito new-app NAME' to create a new application")
}
log.Infof("Application in the context is '%s'. Use 'kogito deploy SOURCE' to deploy a new application.", config.Namespace)
log.Infof("Application in the context is '%s'. Use 'kogito deploy NAME SOURCE' to deploy a new application.", config.Namespace)
return nil
}
appName = args[0]
appCmdName = args[0]
}

if ns, err := kubernetes.NamespaceC(kubeCli).Fetch(appName); err != nil {
if ns, err := kubernetes.NamespaceC(kubeCli).Fetch(appCmdName); err != nil {
return fmt.Errorf("Error while trying to look for the namespace. Are you logged in? %s", err)
} else if ns != nil {
config.Namespace = appName
log.Infof("Application set to '%s'", appName)
config.Namespace = appCmdName
log.Infof("Application set to '%s'", appCmdName)
return nil
}

return fmt.Errorf("Application '%s' not found. Try running 'kogito new-app %s' to create your application first", appName, appName)
return fmt.Errorf("Application '%s' not found. Try running 'kogito new-app %s' to create your application first", appCmdName, appCmdName)
}
10 changes: 6 additions & 4 deletions cmd/kogito/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,20 @@ import (
"bytes"
"strings"

"github.com/kiegroup/kogito-cloud-operator/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/client/fake"
"github.com/kiegroup/kogito-cloud-operator/pkg/client/meta"

"github.com/kiegroup/kogito-cloud-operator/pkg/client"
"k8s.io/apimachinery/pkg/runtime"
"sigs.k8s.io/controller-runtime/pkg/client/fake"
)

func runKogito() error {
return Main()
}

func setupFakeKubeCli(initObjs ...runtime.Object) {
kubeCli = &client.Client{ControlCli: fake.NewFakeClient(initObjs...)}
s := meta.GetRegisteredSchema()
kubeCli = &client.Client{ControlCli: fake.NewFakeClientWithScheme(s, initObjs...)}
}

func kogitoCliTestSetup(arg string) (*bytes.Buffer, *bytes.Buffer) {
Expand All @@ -25,7 +27,7 @@ func kogitoCliTestSetup(arg string) (*bytes.Buffer, *bytes.Buffer) {
rootCmd.SetArgs(strings.Split(arg, " "))
rootCmd.SetOut(testOut)
rootCmd.SetErr(testErr)
setDefaultLog("kogito_cli_test", testOut)
commandOutput = testOut

return testOut, testErr
}
Expand Down
6 changes: 5 additions & 1 deletion cmd/kogito/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"os"
"path/filepath"

"github.com/kiegroup/kogito-cloud-operator/pkg/apis/app/v1alpha1"

"gopkg.in/yaml.v2"

"github.com/mitchellh/go-homedir"
Expand All @@ -24,6 +26,8 @@ const (
type configuration struct {
// Namespace is the projet/namespace context where the application will be deployed
Namespace string
// LastKogitoAppCreated is the last kogitoapp created by the CLI
LastKogitoAppCreated *v1alpha1.KogitoApp
}

func (c *configuration) initConfig(configFile string) {
Expand Down Expand Up @@ -61,7 +65,7 @@ func (c *configuration) initConfig(configFile string) {
// If a config file is found, read it in.
if err := viper.ReadInConfig(); err == nil {
viper.Unmarshal(&config)
log.Info("Using config file:", viper.ConfigFileUsed())
log.Debug("Using config file:", viper.ConfigFileUsed())
}
}

Expand Down
31 changes: 31 additions & 0 deletions cmd/kogito/converters.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package main

import (
"github.com/kiegroup/kogito-cloud-operator/pkg/apis/app/v1alpha1"
"github.com/kiegroup/kogito-cloud-operator/pkg/util"
)

// fromStringArrayToControllerEnvs converts a string array in the format of key=value pairs to the kogitoapp controller required type
func fromStringArrayToControllerEnvs(strings []string) []v1alpha1.Env {
if strings == nil {
return nil
}
envs := []v1alpha1.Env{}
mapstr := util.FromStringsKeyPairToMap(strings)
for k, v := range mapstr {
envs = append(envs, v1alpha1.Env{Name: k, Value: v})
}
return envs
}

func fromStringArrayToControllerResourceMap(strings []string) []v1alpha1.ResourceMap {
if strings == nil {
return nil
}
res := []v1alpha1.ResourceMap{}
mapstr := util.FromStringsKeyPairToMap(strings)
for k, v := range mapstr {
res = append(res, v1alpha1.ResourceMap{Resource: v1alpha1.ResourceKind(k), Value: v})
}
return res
}
Loading

0 comments on commit 1b00925

Please sign in to comment.