Skip to content

Commit

Permalink
Use rancher/k3s images for k3s preset
Browse files Browse the repository at this point in the history
* Use upstream docker.io/rancher/k3s images for k3s preset

* JSON encode kubeconfig-httpd manifest in init and panic on error

* Add WithPort back and new WithDynamicPort

* Complete backwards compatibility with support and test against k3s v1.16.x-v1.26.x

* only formal support for Kubernetes actively maintained versions
  • Loading branch information
VJftw authored Apr 15, 2023
1 parent 8182852 commit 947251f
Show file tree
Hide file tree
Showing 9 changed files with 338 additions and 40 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,7 @@ jobs:
test-k3s:
name: "[preset] k3s"
runs-on: ubuntu-20.04
runs-on: ubuntu-latest
env:
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}
steps:
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ MongoDB | [Go package](https://github.com/orlangure/gnomock/tree/master/preset/m
RabbitMQ | [Go package](https://github.com/orlangure/gnomock/tree/master/preset/rabbitmq) | [Reference](https://pkg.go.dev/github.com/orlangure/gnomock/preset/rabbitmq?tab=doc) | `3.8.9-alpine`, `3.8.9-management-alpine` | ✅
Kafka | [Go package](https://github.com/orlangure/gnomock/tree/master/preset/kafka) | [Reference](https://pkg.go.dev/github.com/orlangure/gnomock/preset/kafka?tab=doc) | `2.5.1-L0` | ❌
Elasticsearch | [Go package](https://github.com/orlangure/gnomock/tree/master/preset/elastic) | [Reference](https://pkg.go.dev/github.com/orlangure/gnomock/preset/elastic?tab=doc) | `7.9.3` | ✅
Kubernetes | [Go package](https://github.com/orlangure/gnomock/tree/master/preset/k3s) | [Reference](https://pkg.go.dev/github.com/orlangure/gnomock/preset/k3s?tab=doc) | `v1.19.12` |
Kubernetes | [Go package](https://github.com/orlangure/gnomock/tree/master/preset/k3s) | [Reference](https://pkg.go.dev/github.com/orlangure/gnomock/preset/k3s?tab=doc) | `v1.26.3-k3s1` |
CockroachDB | [Go package](https://github.com/orlangure/gnomock/tree/master/preset/cockroachdb) | [Reference](https://pkg.go.dev/github.com/orlangure/gnomock/preset/cockroachdb?tab=doc) | `v19.2.11`, `v20.1.10` | ✅
InfluxDB | [Go package](https://github.com/orlangure/gnomock/tree/master/preset/influxdb) | [Reference](https://pkg.go.dev/github.com/orlangure/gnomock/preset/influxdb?tab=doc) | `2.0.4-alpine` | ✅
Cassandra | [Go package](https://github.com/orlangure/gnomock/tree/master/preset/cassandra) | [Reference](https://pkg.go.dev/github.com/orlangure/gnomock/preset/cassandra?tab=doc) | `4.0`, `3` | ✅
Expand Down
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ require (
k8s.io/client-go v0.27.0
)

require golang.org/x/mod v0.8.0

require (
github.com/cenkalti/backoff/v3 v3.0.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -471,6 +471,8 @@ golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/mod v0.4.2/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/mod v0.6.0-dev.0.20220106191415-9b9b3d81d5e3/go.mod h1:3p9vT2HGsQu2K1YbXdKPJLVgG5VJdoTa1poYQBtP1AY=
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
golang.org/x/mod v0.8.0 h1:LUYupSeNrTNCGzR/hVBk2NHZO4hXcVaW1k4Qx7rjPx8=
golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20180906233101-161cd47e91fd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
Expand Down
40 changes: 38 additions & 2 deletions preset/k3s/options.go
Original file line number Diff line number Diff line change
@@ -1,24 +1,60 @@
package k3s

import (
"strings"

"golang.org/x/mod/semver"
)

// Option is an optional configuration of this Gnomock preset. Use available
// Options to configure the container.
type Option func(*P)

// WithVersion sets image version.
func WithVersion(version string) Option {
return func(o *P) {
// support original orlangure/k3s versions with rancher/k3s by
// appending '-k3s1' to them.
if version != "latest" {
if !strings.Contains(version, "k3s") {
version += "-k3s1"
}

if semver.Compare(version, "v1.17.0") > 0 {
// --disable flag only exists in k3s versions above v1.17.0.
o.K3sServerFlags = append(o.K3sServerFlags, "--disable=traefik")
}
}

o.Version = version
}
}

// WithPort allows to use a custom port for k3s API access instead of the
// default one. If no custom port is provided, port 48443 is used instead.
// Deprecated: WithPort allows to use a custom port for k3s API access instead
// of the default one. If no custom port is provided, port 48443 is used
// instead.
//
// Please make sure that whichever port you choose to use (including the
// default) is available on the host system. Otherwise this container won't
// start.
//
// This option and its affects has been kept as is for backward compatibility.
// We recommend using `WithDynamicPort()` instead as it does not require a
// static port to be available on the host.
func WithPort(port int) Option {
return func(o *P) {
o.Port = port
}
}

// WithDynamicPort configures the preset to find and use a dynamic (free) port
// for k3s API access from the host. The kubeconfig is replaced with the
// container host port so local Kubernetes clients will still work.
//
// This is preferred to `WithPort` as it does not require a specific host port
// to be available.
func WithDynamicPort() Option {
return func(o *P) {
o.UseDynamicPort = true
}
}
67 changes: 67 additions & 0 deletions preset/k3s/options_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package k3s_test

import (
"testing"

"github.com/orlangure/gnomock/preset/k3s"
"github.com/stretchr/testify/assert"
)

func TestWithVersion(t *testing.T) {
tests := []struct {
inVersion string
expectedSetVersion string
}{
{"v1.17.11", "v1.17.11-k3s1"},
{"v1.17.12", "v1.17.12-k3s1"},
{"v1.17.13", "v1.17.13-k3s1"},
{"v1.17.2", "v1.17.2-k3s1"},
{"v1.17.3", "v1.17.3-k3s1"},
{"v1.17.4", "v1.17.4-k3s1"},
{"v1.17.5", "v1.17.5-k3s1"},
{"v1.17.6", "v1.17.6-k3s1"},
{"v1.17.7", "v1.17.7-k3s1"},
{"v1.17.9", "v1.17.9-k3s1"},
{"v1.18.10", "v1.18.10-k3s1"},
{"v1.18.2", "v1.18.2-k3s1"},
{"v1.18.3", "v1.18.3-k3s1"},
{"v1.18.4", "v1.18.4-k3s1"},
{"v1.18.6", "v1.18.6-k3s1"},
{"v1.18.8", "v1.18.8-k3s1"},
{"v1.18.9", "v1.18.9-k3s1"},
{"v1.19.1", "v1.19.1-k3s1"},
{"v1.19.10", "v1.19.10-k3s1"},
{"v1.19.11", "v1.19.11-k3s1"},
{"v1.19.12", "v1.19.12-k3s1"},
{"v1.19.2", "v1.19.2-k3s1"},
{"v1.19.3", "v1.19.3-k3s1"},
{"v1.19.4", "v1.19.4-k3s1"},
{"v1.19.5", "v1.19.5-k3s1"},
{"v1.19.7", "v1.19.7-k3s1"},
{"v1.19.8", "v1.19.8-k3s1"},
{"v1.19.9", "v1.19.9-k3s1"},
{"v1.20.2", "v1.20.2-k3s1"},
{"v1.20.4", "v1.20.4-k3s1"},
{"v1.20.5", "v1.20.5-k3s1"},
{"v1.20.6", "v1.20.6-k3s1"},
{"v1.20.7", "v1.20.7-k3s1"},
{"v1.20.8", "v1.20.8-k3s1"},
{"v1.21.0", "v1.21.0-k3s1"},
{"v1.21.1", "v1.21.1-k3s1"},
{"v1.21.2", "v1.21.2-k3s1"},
{"latest", "latest"},
{"v1.25.8-k3s1-amd64", "v1.25.8-k3s1-amd64"},
{"v1.26.3-k3s1-arm64", "v1.26.3-k3s1-arm64"},
}

for _, tt := range tests {
t.Run(tt.inVersion, func(t *testing.T) {
p := k3s.Preset(k3s.WithVersion(tt.inVersion))

assert.IsType(t, &k3s.P{}, p)
if k3sp, ok := p.(*k3s.P); ok {
assert.Equal(t, tt.expectedSetVersion, k3sp.Version)
}
})
}
}
Loading

0 comments on commit 947251f

Please sign in to comment.