Commit Graph

177 Commits

Author SHA1 Message Date
iwilltry42
487418974e remove --all flag for ls 2019-09-06 11:47:25 +02:00
iwilltry42
ee1eb58466 fix passing args to add-node node 2019-09-06 11:37:13 +02:00
iwilltry42
e4e7a32e0d operate on clusterspec for reusability 2019-09-06 11:21:04 +02:00
iwilltry42
3e80e37faf allow connections to non-dockerized k3s clusters 2019-09-04 18:10:08 +02:00
iwilltry42
c8f8fdb139 add addNode function with basic functionality 2019-09-04 18:10:08 +02:00
iwilltry42
89ba0f67b7 use --port/-p for publishing ports now instead of api-port 2019-09-04 18:09:36 +02:00
iwilltry42
53eba1828d remove 'create --version' flag 2019-09-04 18:09:35 +02:00
iwilltry42
d6364af32c add add-node command 2019-09-04 18:09:35 +02:00
misakwa
29204d18a5 Address review comment 2019-08-05 21:14:43 -05:00
misakwa
4e52afd578 Add agent-arg and pass through to container 2019-07-30 23:08:40 -05:00
iwilltry42
205a521c81 go mod tidy 2019-07-30 10:36:49 +02:00
wxdao
058bb1d9e4 Use client.WithAPIVersionNegotiation() 2019-07-25 06:42:31 +00:00
wxdao
c1263487a5 Upgrade docker sdk 2019-07-25 03:15:28 +00:00
Thorsten Klein
c5e5adb0e2
Merge pull request #91 from rancher/feature/import-images-helper
[Feature] import images from docker daemon into k3d using a helper container
2019-07-23 19:26:01 +02:00
iwilltry42
96c883a10a cleanup tools container using defer 2019-07-17 09:32:46 +02:00
iwilltry42
081ec611bc --keep flag to not remove the tarball after import 2019-07-11 15:43:49 +02:00
iwilltry42
a4c75c6568 use a tools container 2019-07-10 18:23:47 +02:00
iwilltry42
07e458d6f1 test 2019-07-08 09:59:01 +02:00
iwilltry42
036a8a9fd0 Merge branch 'feature/load-images' of https://github.com/rancher/k3d into feature/load-images 2019-07-08 09:58:33 +02:00
iwilltry42
f670dde640 test 2019-07-08 09:58:26 +02:00
Ubuntu
4bde59702d error should occur when current time is after start+timout 2019-07-04 02:12:40 +00:00
iwilltry42
93fa5630ac create named volume 2019-07-03 16:51:41 +02:00
iwilltry42
cbdeea3bfa create named volume 2019-07-03 16:50:02 +02:00
iwilltry42
deccb0122a enable importing multiple images at once 2019-07-01 12:17:03 +02:00
iwilltry42
ab2c54ca1d add import-image command for importing single image 2019-07-01 11:31:35 +02:00
Andy Zhou
7d42a4ebd1 Ignore docker-machine command line error
It turns out that docker client only pays attention to the following
environment variables:

DOCKER_HOST to set the url to the docker server.
DOCKER_API_VERSION to set the version of the API to reach, leave empty for latest.
DOCKER_CERT_PATH to load the TLS certificates from.
DOCKER_TLS_VERIFY to enable or disable TLS verification, off by default.

A miss configured DOCKER_MACHINE_NAME won't affect docker client, so k3d
should just ignore the error.
2019-06-14 12:51:32 -07:00
Andy Zhou
aa6e142b81 Log more information when the docker-machine command fails
Currently, when the 'docker-machine' command fails, we only logs

   "exit status 1'

Which is not very helpful in root cause analysis.

With this patch, the 'docker-machine' command's stderr output is also
logged.

  # To cause docker-machine command fail
  $ export DOCKER_MACHINE_NAME=xx
  $ k3d create
2019/06/13 16:45:31 Created cluster network with ID 6fc91e0e5e912443e6b847f113a6a0bb85ccd610e5232592296d4b199f0347cf
2019/06/13 16:45:31 Error executing 'docker-machine ip'
2019/06/13 16:45:31 Docker machine "xx" does not exist. Use "docker-machine ls" to list machines. Use "docker-machine create" to add a new one.
exit status 1
2019-06-13 16:47:27 -07:00
Andy Zhou
f037e1b225 Deprecate --timeout, shift shot hand options around
We have been giving user warnings about --timeout going away for a
while. Now fully deprecate it.

Use -t for --wait option

Move -w from --wait to --workers. Many users of K3d generate multi-node
clusters, -w will make their life easier.
2019-06-06 12:26:26 -07:00
Andy Zhou
fa18c37155 Simplify Dockerm machine ip handling
Based on review comments from @iwilltry42
2019-06-05 09:18:57 -07:00
Andy Zhou
fc6a01d55a Allow --api-port to take host with either name or IP address
To allow some flexibilities in how user specifies the --api-port
argument.  In case the 'host' is an string, We will try to convert
it into an IP address for port mapping. IP address is also allowed.
2019-06-03 17:51:55 -07:00
Andy Zhou
1080c0b157 Fix up kubeconfig.yaml with --api-port host 2019-06-03 17:51:55 -07:00
Andy Zhou
a72945af71 Enhance docekr port mapping for --api-port 2019-06-03 17:51:55 -07:00
Andy Zhou
58849e4a01 Make use of the parseApiPort() function when creating cluster 2019-06-03 17:51:55 -07:00
Andy Zhou
1e6ac3cce8 Enhance ClusterSpec
Pass the apiPort sturct directly via ClusterSpec.
2019-06-03 17:22:23 -07:00
Andy Zhou
f0c1b24e0b Add parseApiPort()
Now that --api-port takes a string argument, add a parser function to
handle error checking for this argument.
2019-06-03 17:21:44 -07:00
Andy Zhou
b97ef230bd Refactor
The APIs of createServer() and createWorker() takes too many arguments.
It is getting harder to maintain.

Use a new struct ClusterSpec to make API simpler. This also reduces
some code duplications.
2019-06-03 17:18:51 -07:00
Andy Zhou
bd58e9b874
Merge pull request #71 from andyz-dev/docker-machine
[Feature] Docker machine
2019-06-01 11:03:42 -07:00
Andy Zhou
f3ec169414 Add docker machine IP to SAN
When running on a docker machine, the default X598 certificate does not
allow access via docker machine's IP. This patch fixes this by adding
"--tls-san <docker machine IP>" to the K3S server argument list.
2019-06-01 10:57:42 -07:00
Andy Zhou
92b5d90216
Merge pull request #72 from andyz-dev/network-name
[Enhancement] Add k3d- prefix to docker network name
2019-05-31 10:39:54 -07:00
iwilltry42
6a46ef9d94 fix prompt from env 2019-05-31 12:00:00 +02:00
Andy Zhou
1f7d234c80 Add k3d- prefix to docker network name
Should make sense since all container names are with the same prefix.
2019-05-31 02:04:07 -07:00
iwilltry42
f220f0121a get prompt from env depdending on selected shell 2019-05-31 10:52:48 +02:00
iwilltry42
93115400fc add shell struct with prompt differentiation 2019-05-31 09:43:45 +02:00
Andy Zhou
de6e845abd Support docker machine for kubeconfig.yaml
The kubeconfig.yaml generated by K3S uses local host as the host name by
default. It won't work when running with docker machine.

This patch detects if the docker environment is set up with docker
machine. If it is, then replace the host name to the IP address of
the docker machine.
2019-05-29 09:09:13 -07:00
iwilltry42
eb5b88052e add auto mode for shell based on SHELL env var and support zsh 2019-05-29 09:14:36 +02:00
Thorsten Klein
df5cc1da84
Merge pull request #66 from andyz-dev/bash
[Feature] Add Bash shell support
2019-05-29 07:56:49 +02:00
Lars Mogren
f2cedfd6cd Now passes the environment variables to the workers. 2019-05-28 22:59:27 +02:00
Andy Zhou
abd9a984eb Add --shell argument
Add subshell argument --shell. Currently, support only bash, which is
also the default value.
2019-05-27 17:16:04 -07:00
Andy Zhou
d63f7d4bf2 Rename bash command to shell
@iwilltry42 suggested to group all shell support under and single
subcommand -- shell.
2019-05-27 17:14:43 -07:00
Andy Zhou
8aaf70f4bf Block recursive bash invocation
In theory, we can execute 'k3d bash' again within an cluster shell. I
can't think of any practical value for allowing this capability.
On the contrary, this can lead to confusion to the user.

This patch adds a simple mechanism to detect and block recursive bash
invocation.
2019-05-24 17:16:12 -07:00
Andy Zhou
2971dd6845 Add the ability to execute commands directly with the bash subcommand
In addition to provide an interactive shell, this patch adds the
'--command' and '-c' options to allow user to issue a command in the
context of a cluster.

For example:

$ k3d bash -c 'kubectl cluster-info'
2019-05-24 17:16:12 -07:00
Andy Zhou
5c6f2d7dc5 Avoid hard coding the bash path
OS distribution and user may choose to install bash in different path.
This patch uses bash found by "$PATH" environment, rather than hard code
the bash path.

This patch also handle the case 'bash' are not found. Mostly likely due
to bash not being supported by the platform, or it is not installed.
2019-05-24 17:16:12 -07:00
Andy Zhou
4ef6710e22 Add basic bashShell() function
Add the basic frame work for supporting spawning a bash shell by cli
command.

With this change, we can spawn a bash shell in the context of a cluster

$ k3d create -n my-cluster
$ k3d bash -n my-cluster
[my-cluster] $>

// execute commands with KUBECONFIG already set up
[my-cluster] $> kubectl get pods
2019-05-24 17:16:12 -07:00
Andy Zhou
9f276a68f3 Add getKubeConfig()
Move the logic of retrive per cluster kube config file into
cli/cluster.go. Simplify the CLI handling code.
2019-05-24 17:16:12 -07:00
Andy Zhou
cdcc5e1de6 Add createKubeconfigFile()
Refactoring. Make createKubeconfigFile() a stand along function, so we
don't have to recreate the file every time we look up the file name.
2019-05-24 17:16:12 -07:00
Andy Zhou
1eff974064 Add getClusterKubeConfigPath()
Minor refactor. Add getClusterKubeConfigOath() to make logic reusable
for later changes.
2019-05-24 17:16:12 -07:00
Andy Zhou
175ccbe3cd Improve error handling for CreateCluster()
Co-authored-by: Thorsten Klein <iwilltry42@gmail.com>

@iwilltry42 pointed out that currnet error path calls os.Exit(1), which
does follow the normal CLI framework. This patch implements this
suggestion.

Add a closure fucntion deleteCluster() to reduce code duplication.
2019-05-21 14:10:11 -07:00
Andy Zhou
6dfb1e054b
Merge pull request #61 from andyz-dev/always-on
[Feature] Implement --always-on flag for the create command
2019-05-21 10:46:00 -07:00
Andy Zhou
b9713c4ac4 Add --auto-restart flag for the create command
When creating clusters with the --auto-restart flag, any running cluster
will remain "running" up on docker daemon restart.

By default, without this flag, a "running" cluster becomes "stopped"
after docker daemon restart.

Clusters stopped with 'k3d stop' command will remain stopped after
docker daemon restart regardless the settings of this flag.
2019-05-21 09:31:45 -07:00
Andy Zhou
5028f2882f
Merge pull request #60 from andyz-dev/atomic-create
[Enhancement] Atomic create
2019-05-20 23:21:19 -07:00
Andy Zhou
490b7cd864 Atomic create
Before this commit, when creating a cluster was not successful, some
resource may be lingering behind. This commit fixes those cases.

Now the cluster creation model is easier to understand. Either a cluster
is successfully created, or all resources created will be cleaned up.

The intention is to keep the "atomic create" model going forward for
k3d.
2019-05-20 19:03:32 -07:00
Andy Zhou
3683eeb181 Deprecate the --timeout option
Use --wait <timeout> instead
2019-05-20 14:07:12 -07:00
Andy Zhou
01931b5894 Make list command always show all clusters. Remove --all option
It feels more natural to have the list command show all clusters by
default.
2019-05-18 22:25:24 -07:00
Andy Zhou
cb1daebe41
Merge pull request #52 from andyz-dev/rename
[Enhancement] Rename cli/config.go to cli/cluster.go
2019-05-16 23:52:07 -07:00
Andy Zhou
abfd6bd18a Makefile: Add lint target and factor the check target 2019-05-16 23:37:46 -07:00
Andy Zhou
10c6eeeea5 Rename cli/config.go to cli/cluster.go
Config.go mainly contains functions around the cluster data structure. Rename
the file to cluster.go
2019-05-16 20:07:09 -07:00
Andy Zhou
89d51a0abc
Merge pull request #49 from andyz-dev/makefile
[Enhancement] Makefile
2019-05-16 00:21:50 -07:00
Andy Zhou
c3c8f0d6f8 Golang format clean up
Changes made by 'make fmt'.
2019-05-16 00:02:10 -07:00
Andy Zhou
62f7071ee5
Merge pull request #40 from andyz-dev/reclaim-volumes
[Enhancement] Automatically reclaim k3s container volumes after a cluster is deleted
2019-05-15 15:42:16 -07:00
Andy Zhou
cd2292ba3a Automatically reclaim k3s container volumes after a cluster is deleted
Thanks @zeerorg for the suggestion on possible container volume leak.

With out this fix the k3s container volumes are left in the reclaimable
state. This experiment confirms it:

$ docker system df
TYPE                TOTAL               ACTIVE              SIZE                RECLAIMABLE
Images              14                  0                   2.131GB             2.131GB (100%)
Containers          0                   0                   0B                  0B
Local Volumes       0                   0                   0B                  0B
Build Cache         0                   0                   0B                  0B

$ bin/k3d create; sleep 5; bin/k3d delete

$ docker system df
TYPE                TOTAL               ACTIVE              SIZE                RECLAIMABLE
Images              14                  0                   2.131GB             2.131GB (100%)
Containers          0                   0                   0B                  0B
Local Volumes       3                   0                   2.366MB             2.366MB (100%)
Build Cache         0                   0                   0B                  0B

In this case, 2.36MB are left in the reclaimable state. This number can be
larger with a larger cluster.

With this fix, output of "docker system df" does not contain the
claimable volume
TYPE                TOTAL               ACTIVE              SIZE                RECLAIMABLE
Images              14                  0                   2.131GB             2.131GB (100%)
Containers          0                   0                   0B                  0B
Local Volumes       0                   0                   0B                  0B
Build Cache         0                   0                   0B                  0B
2019-05-15 15:27:47 -07:00
Andy Zhou
3c3c72b4af Check for cluster existence before using a name to create a new cluster
This change improve the error message. It also avoids the tricky situation of
potentially destroying an existing clusters when the same name cluster
creation fails. For more details see issue #42.
2019-05-15 09:56:48 -07:00
Andy Zhou
b48debb176 Refactor getClusters() to make the API easier to use
Simplify the caller of this function in the code base. Also remove
getCluster() since there is no caller of this function any more.
2019-05-15 09:56:48 -07:00
Andy Zhou
a0eb69b73e Allow the volume argument to be specified multiple times
Most k3d arguments are using in "stringSlice" style, allowing the
argument to supplied multiple times. Currently "volume" is an exception
to this style, require multiple arguments to be supplied in a single
argument, separated by comma.

This commit improve the k3d usability by improve the consistency of its
argument style.
2019-05-15 09:35:07 -07:00
iwilltry42
6823470ef5 don't allow empty node-specifiers 2019-05-15 07:11:06 +02:00
iwilltry42
8512ac0c8a make server the default node for portmaps 2019-05-14 20:48:48 +02:00
iwilltry42
2640fc13ce make server the default node for portmaps 2019-05-14 16:44:47 +02:00
iwilltry42
c5f410d5ef Merge branch 'master' of https://github.com/rancher/k3d into feature/enhanced-port-mapping 2019-05-14 14:40:16 +02:00
iwilltry42
6d0fc76b22 improve portspec and ndoespec validation 2019-05-14 14:27:30 +02:00
iwilltry42
464d2e23c5 Offset multiplication 2019-05-14 13:13:56 +02:00
iwilltry42
107700dc98 update log messages 2019-05-14 13:12:10 +02:00
iwilltry42
a68d9e7042 generate container names in a func and use it to determine if node-specifier is valid 2019-05-14 11:33:10 +02:00
Andy Zhou
4674a5d6c8 Improve cluster status display
Before this change, k3d list stopped cluster as "exited", This patch
change it to "stopped".

$ k3d create -n test
$ k3d stop -n test
$ k3d list --all

+------+------------------------------+--------+---------+
| NAME |            IMAGE             | STATUS | WORKERS |
+------+------------------------------+--------+---------+
| test | docker.io/rancher/k3s:v0.5.0 | exited |   0/0   |
+------+------------------------------+--------+---------+

Before this change, k3d report the server status as the cluster status.
This commit output the server status if the server and all worker
containers status agree. Otherwise, the cluster status is reported as "unhealthy".

$ k3d create --workers 2
$ docker ps -a

d14135f5929c        rancher/k3s:v0.5.0   "/bin/k3s agent"         6 seconds ago       Up 5 seconds                                        k3d-k3s-default-worker-1
612d71f3ec23        rancher/k3s:v0.5.0   "/bin/k3s agent"         6 seconds ago       Up 5 seconds                                        k3d-k3s-default-worker-0
7f201731bf45        rancher/k3s:v0.5.0   "/bin/k3s server --h…"   7 seconds ago       Up 6 seconds               0.0.0.0:6443->6443/tcp   k3d-k3s-default-server

$ docker stop d14135f5929c

$ bin/k3d list --all

+-------------+------------------------------+-----------+---------+
|    NAME     |            IMAGE             |  STATUS   | WORKERS |
+-------------+------------------------------+-----------+---------+
| k3s-default | docker.io/rancher/k3s:v0.5.0 | unhealthy |   1/2   |
+-------------+------------------------------+-----------+---------+
2019-05-13 21:16:19 -07:00
Andy Zhou
6292c89ec4 Minor simplification of getCluster()
This make the code easier to read.
2019-05-13 21:12:24 -07:00
Andy Zhou
953578ed11 Avoid output an empty table when no cluster to list
Before this patch:

$ k3d create
$ k3d stop
$ k3d list

+------+-------+--------+---------+
| NAME | IMAGE | STATUS | WORKERS |
+------+-------+--------+---------+
+------+-------+--------+---------+

This commit omits such empty table.
2019-05-13 21:10:38 -07:00
iwilltry42
f77d67aaf0 Merge branch 'master' of https://github.com/rancher/k3d into new-publish 2019-05-13 09:10:11 +02:00
Thorsten Klein
3a2eeba9b1
Merge pull request #41 from andyz-dev/center-align
[Enhancement] Center align list output
2019-05-13 09:10:04 +02:00
iwilltry42
a0c0c3ff9d add --api-port and --port-auto-offset 2019-05-13 09:08:32 +02:00
iwilltry42
7d6ae201e9 make node-specifier mandatory 2019-05-13 08:12:09 +02:00
iwilltry42
58b1682e32 Merge branch 'master' of https://github.com/rancher/k3d into new-publish 2019-05-13 07:54:07 +02:00
iwilltry42
0670a0e11b resovle conflicts 2019-05-13 07:53:23 +02:00
iwilltry42
a881a87ef6 go fmt 2019-05-13 07:50:05 +02:00
Andy Zhou
2f07bdb78a Center align list output
Make list output center aligne. It is slightly eaier to read.

Before the commit:

$ bin/k3d list --all

+-------------+------------------------------+---------+---------+
|    NAME     |            IMAGE             | STATUS  | WORKERS |
+-------------+------------------------------+---------+---------+
| k3s-default | docker.io/rancher/k3s:v0.5.0 | running | 2/2     |
+-------------+------------------------------+---------+---------+

After this commit:

$ bin/k3d list --all

+-------------+------------------------------+---------+---------+
|    NAME     |            IMAGE             | STATUS  | WORKERS |
+-------------+------------------------------+---------+---------+
| k3s-default | docker.io/rancher/k3s:v0.5.0 | running |   2/2   |
+-------------+------------------------------+---------+---------+

In this output, only the last cell shows up differently.
2019-05-12 16:01:49 -07:00
Rishabh Gupta
787d6608ba Delete a cluster if it is not started due to port conflicts or any other unforseen error
Signed-off-by: Rishabh Gupta <r.g.gupta@outlook.com>
2019-05-10 13:10:55 +05:30
iwilltry42
0c8cdc004a Merge branch 'master' of https://github.com/rancher/k3d into new-publish 2019-05-09 15:36:27 +02:00
Andy Zhou
32cc70b599
Merge pull request #32 from andyz-dev/publish
[Feature] Add --publish/--add-port flag for port mappings
2019-05-09 01:09:40 -07:00
iwilltry42
fd720b0887 move hostname check to util 2019-05-09 08:05:39 +02:00
Andy Zhou
93f72e672a Make sure the culster name is valid before creating it.
MAke sure the cluster name is a RFC 1123 compliant host name, since the
cluster name is used as base for expanding into a auto generated
host names for k3s nodes.
2019-05-08 15:57:32 -07:00
iwilltry42
124a45688f working version 2019-05-08 15:29:57 +02:00
iwilltry42
d35f12a77e init 2019-05-08 13:37:24 +02:00
Andy Zhou
65f2820b3f Support publishing ports for the worker nodes
All ports exposed by --publish will also be exported for all worker
nodes. The host port will be auto indexed based worker id.

For example: with the following command option:

k3d create --publish  80:80  --publish 90:90/udp --workers 1

The exposed ports will be:

host TCP port 80  -> k3s server TCP 80
host TCP port 90  -> k3s server TCP 90
host UDP port 81 -> k3s worker 0 UDP 80
host UDP port 91 -> k3s worker 0 UDP 90
2019-05-08 01:45:16 -07:00