Skip to content

Commit

Permalink
Refactor tolerations arguments for the install command (#82)
Browse files Browse the repository at this point in the history
* feat: ignore idea files

* chore: run go fmt

* refactor: use k8s api for tolerations

* refactor: tolerations arguments

* chore: update install options in README

* chore: bump version to 0.30.0
  • Loading branch information
fernandocarletti authored Feb 12, 2020
1 parent f93ccd4 commit 89a7990
Show file tree
Hide file tree
Showing 15 changed files with 528 additions and 502 deletions.
24 changes: 14 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# VENONA
[![Go Report Card](https://goreportcard.com/badge/github.com/codefresh-io/venona)](https://goreportcard.com/report/github.com/codefresh-io/venona)
[![Go Report Card](https://goreportcard.com/badge/github.com/codefresh-io/venona)](https://goreportcard.com/report/github.com/codefresh-io/venona)
[![Codefresh build status]( https://g.codefresh.io/api/badges/pipeline/codefresh-inc/codefresh-io%2Fvenona%2Fvenona?type=cf-1)]( https://g.codefresh.io/public/accounts/codefresh-inc/pipelines/codefresh-io/venona/venona)

## Installation
Expand All @@ -16,33 +16,37 @@
### Install venona

* Download [venona's](https://github.com/codefresh-io/venona/releases) binary
* With homebrew:
* With homebrew:
* `brew tap codefresh-io/venona`
* `brew install venona`
* Create namespace where venona should run<br />
> `kubectl create namespace codefresh-runtime`
* Create *new* runtime-environment with Venona's agents installed <br />
> `venona install --kube-namespace codefresh-runtime`
* Get the status <br />
> `venona status`
> `venona status`
> `kubectl get pods -n codefresh-runtime`
#### Install Options

| Option Argument | Type | Description |
| -------------------- | -------- | --------------------------------------------------- |
| --build-annotations | stringArray | The kubernetes metadata.annotations as "key=value" to be used by venona build resources (default is no node selector) |
| --build-node-selector | string | The kubernetes node selector "key=value" to be used by venona build resources (default is no node selector) |
| --cluster-name | string | cluster name (if not passed runtime-environment will be created cluster-less); this is a friendly name used for metadata does not need to match the literal cluster name. Limited to 20 Characters. |
| --dry-run | boolean | Set to true to simulate installation |
| -h, --help | | help for install |
| -h, --help | boolean | help for install |
| --in-cluster | boolean | Set flag if venona is been installed from inside a cluster |
| --kube-context-name | string | Name of the kubernetes context on which venona should be installed (default is current-context) [$KUBE_CONTEXT] |
| --kube-namespace | string | Name of the namespace on which venona should be installed [$KUBE_NAMESPACE] |
| --kubernetes-runner-type | string | Set the runner type to kubernetes (alpha feature) |
| --only-runtime-environment | boolean | Set to true to only configure namespace as runtime-environment for Codefresh |
| --kube-namespace | string |Name of the namespace on which venona should be installed [$KUBE_NAMESPACE] |
| --kube-node-selector | string | The kubernetes node selector "key=value" to be used by venona resources (default is no node selector) |
| --kubernetes-runner-type | boolean | Set the runner type to kubernetes (alpha feature) |
| --only-runtime-environment | boolean | Set to true to onlky configure namespace as runtime-environment for Codefresh |
| --runtime-environment | string | if --skip-runtime-installation set, will try to configure venona on current runtime-environment |
| --set-default | boolean | Mark the install runtime-environment as default one after installation |
| --skip-runtime-installation | boolean | Set flag if you already have a configured runtime-environment, add --runtime-environment flag with name |
| --storage-class | string | Set a name of your custom storage class, note: this will not install volume provisioning components |
| --tolerations | string | The kubernetes tolerations as JSON string to be used by venona resources (default is no tolerations). If prefixed with "@", loads from a file: @/tmp/tolerations.json |
| --venona-version | string | Version of venona to install (default is the latest) |

#### Install on cluster version < 1.10
Expand Down Expand Up @@ -84,8 +88,8 @@ List of the resources that will be created
* `cluster-role-binding.dind-volume-provisioner.yaml` - Binds the ClusterRole to `service-account.dind-volume-provisioner.re.yaml`

### Access the cluster from executed pipeline
After a successfull installation of Venona, you'll be able to run a Codefresh pipeline on the configured cluster.
However, the pipeline itself dosent have any permission to connect to the hosted cluster.
After a successfull installation of Venona, you'll be able to run a Codefresh pipeline on the configured cluster.
However, the pipeline itself dosent have any permission to connect to the hosted cluster.
To make it work you need to add the cluster to Codefresh (make sure the service acount has all the permissions you need)
> codefresh create cluster --kube-context CONTEXT_NAME --namespace NAMESPACE --serviceaccount SERVICE_ACCOUNT --behind-firewall

Expand All @@ -96,5 +100,5 @@ Example: `codefresh get cluster`
* Install <br />
Example: `venona install --cluster-name CLUSTER`
* Get the status <br />
Example: `venona status RUNTIME-ENVIRONMENT`
Example: `venona status RUNTIME-ENVIRONMENT`
Example: `kubectl get pods -n NAMESPACE`
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "venona",
"version": "0.29.1",
"version": "0.30.0",
"description": "Codefresh agent to run on Codefresh's runtime environment and execute pipeline",
"main": "index.js",
"scripts": {
Expand Down
1 change: 1 addition & 0 deletions venonactl/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.idea/
5 changes: 4 additions & 1 deletion venonactl/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,7 @@ build-local:
@sh ./hack/build.sh

build-linux:
@sh ./hack/build-linux.sh
@sh ./hack/build-linux.sh

fmt:
go fmt ./...
2 changes: 1 addition & 1 deletion venonactl/VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.29.1
0.30.0
61 changes: 34 additions & 27 deletions venonactl/cmd/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ limitations under the License.
*/

import (
"encoding/json"
"errors"
"fmt"
"strings"
"encoding/json"
"gopkg.in/yaml.v2"
"io/ioutil"
"strings"

"k8s.io/client-go/tools/clientcmd"

Expand All @@ -31,19 +31,14 @@ import (
"github.com/codefresh-io/venona/venonactl/pkg/plugins"
"github.com/spf13/cobra"
"github.com/spf13/viper"
k8sApi "k8s.io/api/core/v1"
)

const (
clusterNameMaxLength = 20
namespaceMaxLength = 20
namespaceMaxLength = 20
)

type toleration struct {
Key string `json:key`
Operator string `json:operator`
Effect string `json:effect`
}

var installCmdOptions struct {
dryRun bool
clusterNameInCodefresh string
Expand All @@ -64,7 +59,7 @@ var installCmdOptions struct {
kubernetesRunnerType bool
buildNodeSelector string
buildAnnotations []string
tolerationJsonString string
tolerations string
}

// installCmd represents the install command
Expand Down Expand Up @@ -118,19 +113,22 @@ var installCmd = &cobra.Command{
}
s.KubernetesAPI.NodeSelector = kns.String()

if installCmdOptions.tolerationJsonString != "" {

data, err := ioutil.ReadFile(installCmdOptions.tolerationJsonString)
if err != nil {
dieOnError(err)
if installCmdOptions.tolerations != "" {
var tolerationsString string

if installCmdOptions.tolerations[0] == '@' {
tolerationsString = loadTolerationsFromFile(installCmdOptions.tolerations[1:])
} else {
tolerationsString = installCmdOptions.tolerations
}
tolerations, err := parseToleration(string(data))

tolerations, err := parseTolerations(tolerationsString)
if err != nil {
dieOnError(err)
}

s.KubernetesAPI.Tolerations = tolerations
}


if installCmdOptions.dryRun {
s.DryRun = installCmdOptions.dryRun
Expand Down Expand Up @@ -224,7 +222,7 @@ func init() {
installCmd.Flags().StringVar(&installCmdOptions.kube.nodeSelector, "kube-node-selector", "", "The kubernetes node selector \"key=value\" to be used by venona resources (default is no node selector)")
installCmd.Flags().StringVar(&installCmdOptions.buildNodeSelector, "build-node-selector", "", "The kubernetes node selector \"key=value\" to be used by venona build resources (default is no node selector)")
installCmd.Flags().StringArrayVar(&installCmdOptions.buildAnnotations, "build-annotations", []string{}, "The kubernetes metadata.annotations as \"key=value\" to be used by venona build resources (default is no node selector)")
installCmd.Flags().StringVar(&installCmdOptions.tolerationJsonString, "tolerations", "", "The kubernetes tolerations as JSON string to be used by venona resources (default is no tolerations)")
installCmd.Flags().StringVar(&installCmdOptions.tolerations, "tolerations", "", `The kubernetes tolerations as JSON string to be used by venona resources (default is no tolerations). If prefixed with "@", loads from a file: @/tmp/tolerations.json`)

installCmd.Flags().BoolVar(&installCmdOptions.skipRuntimeInstallation, "skip-runtime-installation", false, "Set flag if you already have a configured runtime-environment, add --runtime-environment flag with name")
installCmd.Flags().BoolVar(&installCmdOptions.kube.inCluster, "in-cluster", false, "Set flag if venona is been installed from inside a cluster")
Expand All @@ -248,30 +246,39 @@ func parseNodeSelector(s string) (nodeSelector, error) {
return nodeSelector{v[0]: v[1]}, nil
}

func parseToleration(s string) (string, error) {
func loadTolerationsFromFile(filename string) string {
data, err := ioutil.ReadFile(filename)
if err != nil {
dieOnError(err)
}

return string(data)
}

func parseTolerations(s string) (string, error) {
if s == "" {
return "", nil
}
data := []toleration{}
err := json.Unmarshal([]byte(s), &data);
if (err != nil) {
return "", errors.New("can not parse tolerations")
var data []k8sApi.Toleration
err := json.Unmarshal([]byte(s), &data)
if err != nil {
return "", fmt.Errorf("can not parse tolerations: %s", err)
}
y, err := yaml.Marshal(&data)
if (err != nil) {
return "", errors.New("can not marshel tolerations to yaml")
if err != nil {
return "", fmt.Errorf("can not marshel tolerations to yaml: %s", err)
}
d := fmt.Sprintf("\n%s", string(y))
return d, nil
}

func validateInstallOptions(opts* plugins.InstallOptions) (error) {
func validateInstallOptions(opts *plugins.InstallOptions) error {
if len(opts.ClusterName) > clusterNameMaxLength {
return errors.New(fmt.Sprintf("cluster name length is limited to %d", clusterNameMaxLength))
}
if len(opts.ClusterNamespace) > namespaceMaxLength {
return errors.New(fmt.Sprintf("cluster namespace length is limited to %d", namespaceMaxLength))
}
}
return nil
}

Expand Down
4 changes: 3 additions & 1 deletion venonactl/hack/build-linux.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
set -e
OUTFILE=$PWD/venonactl-linux
go generate ${PWD}/hack/generate.go
go fmt ${PWD}/pkg/obj/kubeobj/kubeobj.go
go fmt ${PWD}/pkg/templates/kubernetes/templates.go
GOOS=linux GOARCH=386 go build -ldflags '-X github.com/codefresh-io/venona/venonactl/cmd.localDevFlow=true' -o $OUTFILE main.go

chmod +x $OUTFILE
chmod +x $OUTFILE
4 changes: 3 additions & 1 deletion venonactl/hack/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
set -e
OUTFILE=/usr/local/bin/venonactl
go generate ${PWD}/hack/generate.go
go fmt ${PWD}/pkg/obj/kubeobj/kubeobj.go
go fmt ${PWD}/pkg/templates/kubernetes/templates.go
go build -ldflags '-X github.com/codefresh-io/venona/venonactl/cmd.localDevFlow=true' -o $OUTFILE main.go

chmod +x $OUTFILE
chmod +x $OUTFILE
Loading

0 comments on commit 89a7990

Please sign in to comment.