Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Script Runner addon #229

Open
wants to merge 18 commits into
base: master
Choose a base branch
from

Conversation

gadelkareem
Copy link
Contributor

Script Runner makes it easy to run a bash script on all your k8s nodes with one command.

hetzner-kube cluster addon install --name demo script-runner /path/to/script.sh 

@xetys
Copy link
Owner

xetys commented Nov 19, 2018

wow, that's I cool. I'll review asap

@mavimo
Copy link
Collaborator

mavimo commented Nov 20, 2018

Yeah! I suggest to allow to specify the server type, actually we have 3 main "type" of server, etcd, master and worker.. I think most of time we need to run specific script only on a specific type of server, so maybe have a flag to define what kind of server should be affected from script execution should be a big improvement.

@gadelkareem
Copy link
Contributor Author

@mavimo we can use the power of bash and keep the addon simple :)

#!/bin/bash

if [[ $(hostname -s) = *master* ]]; then
    exit 0
fi

@mavimo
Copy link
Collaborator

mavimo commented Nov 20, 2018

@gadelkareem it's a possibility, so the addon is simple but bash script should be "complex" ;)

I think is also a bit fragile because we can't guarantee that the name of server match the master / worker / etcd name, we generate new server with the specified name but a user can add the server to the pool with a different name or rename it.. so IMHO we should move server type definition on addon instead of into the script that we deploy

@gadelkareem gadelkareem mentioned this pull request Nov 20, 2018
@gadelkareem
Copy link
Contributor Author

@mavimo I see that the names are not constant, especially if it is an external server being added. Maybe we should have a file on the server to hint its type?

I think the bash script will probably be much more complex than the hostname check example above. And since the user already knows his own cluster then it should be trivial to manage the script based on the hostname or any other properties. Because if we start adding a new argument for each use case then it will not be as simple as now.

For example, with the current use case of choosing the instance group to run on, we will need to think if the user also wants to run the script on 2 groups (master and worker) or 3 of them. And what if there is a new group created then we need to modify this addon. That's why I always prefer to keep it simple and if we have feature request then it could be added to it.

@mavimo
Copy link
Collaborator

mavimo commented Nov 20, 2018

@gadelkareem WDYT to send as args to script invocation some information about current server / cluster topology?

@gadelkareem
Copy link
Contributor Author

@mavimo good idea! which info do you think would be nice to pass to the script?

@mavimo
Copy link
Collaborator

mavimo commented Nov 21, 2018

@gadelkareem NB: I'm not sure but what we should pass, I'll try to write some proposal for discussion 😄

We should start with something like:

  • Server type (master, worker, etcd) or a combination of them (some server should have more than one role at time) in order to allow script to have specific config (eg port exposed by firewall? ;) )
  • Master IP: because some time script should need to dialog with the K8s API for specific operation
  • Cluster type (without HA, with HA level 3, with HA level 4, ..) or List of server configs in order to make easy to add action that dialogue with other servers in pool

Maybe point 2 and 3 should be mixed if we build a JSON that contains all cluster information as second argument like:

 /tmp/script-to-run.sh master {"masters":[{"name":"master-1","ip":"111.222.0.1"}]...}

where script should process the second information if needed (eg: echo $2 | jq .masters[].ip)

@gadelkareem
Copy link
Contributor Author

Ok so now the script will have the node group as first param then the cluster info which is the same as the JSON from the config file.

Signed-off-by: Waleed Gadelkareem <[email protected]>
@gadelkareem
Copy link
Contributor Author

I also allowed the display of output for each node since it will be different.

@xetys
Copy link
Owner

xetys commented Nov 26, 2018

The LGTM already, but I would love to see some docs for the users on how to use that. With some examples, so users will know how to deal with the params. Maybe a distinct page like the HA-guide and wireguard info page?

pkg/addons/addon_script_runner.go Outdated Show resolved Hide resolved
pkg/addons/addon_script_runner.go Outdated Show resolved Hide resolved
pkg/addons/addon_script_runner.go Outdated Show resolved Hide resolved
pkg/addons/addon_script_runner.go Outdated Show resolved Hide resolved
pkg/addons/addon_script_runner.go Outdated Show resolved Hide resolved
pkg/addons/addon_script_runner.go Outdated Show resolved Hide resolved
pkg/addons/addon_script_runner.go Outdated Show resolved Hide resolved
pkg/addons/addon_script_runner.go Outdated Show resolved Hide resolved
cmd/cluster_addon.go Show resolved Hide resolved
pkg/addons/addon_script_runner.go Outdated Show resolved Hide resolved
package addons

import (
"fmt"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please re-sort import definition, typically:

  • stdlib packages (alphabetic)
  • EMPTY LINE
  • other packages (alphabetic)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm.. did you mean remove empty line?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I mean that we should change it to:

import (
	"encoding/json"
	"fmt"
	"io/ioutil"
	"log"
	"strings"
	"time"

	"github.com/xetys/hetzner-kube/pkg/clustermanager"
)

pkg/addons/addon_script_runner.go Outdated Show resolved Hide resolved
pkg/addons/addon_script_runner.go Outdated Show resolved Hide resolved
FatalOnError(err)

replacer := strings.NewReplacer("\n", "", "'", "\\'")
clusterInfo := replacer.Replace(string(clusterInfoBin))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure to understand why we are going to use replacer here, this is needed since there are some info in JSON encoded that can generate invalid string argument? Is possibile to use some different approach in order to do not manipulate result value, like:

echo $(cat <<EOF
<JSON_MARSHALLED_VALUE>
EOF)

I'm not sure is doable, but will allow us to do not manipulate the value and be "safe" on changes we do in config..

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think the run command will allow multi line. If it works for you, please let me know.

@mavimo
Copy link
Collaborator

mavimo commented Nov 27, 2018

@gadelkareem do you have time to write some doc? If you need some help please ping me :)

@gadelkareem
Copy link
Contributor Author

@mavimo Since you are asking. Do you have an example config.json that i can use?

@gadelkareem
Copy link
Contributor Author

added some docs!

# Script Runner Addon Guide

Script Runner lets you run a bash script on all your cluster nodes with one command. It uploads the bash script to each node then execute. The script will receive two arguments upon execution:
1) Node group, for example (master, worker, etcd)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Markdown suggest to use:

Suggested change
1) Node group, for example (master, worker, etcd)
1. Node group, allowed values are `master`, `worker` and `etcd`


Script Runner lets you run a bash script on all your cluster nodes with one command. It uploads the bash script to each node then execute. The script will receive two arguments upon execution:
1) Node group, for example (master, worker, etcd)
2) Cluster info JSON config, which is similar to the hetzner-kube config found in `~/.hetzner-kube/config.json`
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Markdown suggest to use

Suggested change
2) Cluster info JSON config, which is similar to the hetzner-kube config found in `~/.hetzner-kube/config.json`
1. Cluster info JSON config, which is similar to the `hetzner-kube` configuration that you can found in `~/.hetzner-kube/config.json`

```
Example script.sh:
```bash

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bash script should start with:

#!/bin/bash

this should be omitted (than specify on the doc that should not be included) or is missing here (than add to the sample)?

2) Cluster info JSON config, which is similar to the hetzner-kube config found in `~/.hetzner-kube/config.json`

### Example
hetzner-kube Command:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add a space between title and text and between text and code sample

@mavimo
Copy link
Collaborator

mavimo commented Nov 28, 2018

@gadelkareem I know I should sound like a "nuisance" (sorry), added some comments to the doc :)

@gadelkareem
Copy link
Contributor Author

cool done ;)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants