Skip to content

Commit

Permalink
refactor: isolate legacy kwok install code
Browse files Browse the repository at this point in the history
  • Loading branch information
vadasambar committed Sep 6, 2023
1 parent 6bcb96c commit 27d1913
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 14 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ go 1.20
require (
github.com/google/go-github/v53 v53.2.0
github.com/sirupsen/logrus v1.9.3
golang.org/x/mod v0.9.0
k8s.io/cli-runtime v0.27.4
k8s.io/kubectl v0.27.4
)
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,8 @@ golang.org/x/lint v0.0.0-20190227174305-5b3e6a55c961/go.mod h1:wehouNa3lNwaWXcvx
golang.org/x/lint v0.0.0-20190313153728-d0100b6bd8b3/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc=
golang.org/x/mod v0.2.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/mod v0.9.0 h1:KENHtAZL2y3NLMYZeHY9DW8HW8V+kQyJsY/V9JlKvCs=
golang.org/x/mod v0.9.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-20190213061140-3a22650c66bd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
Expand Down
47 changes: 33 additions & 14 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"time"

log "github.com/sirupsen/logrus"
"golang.org/x/mod/semver"

"github.com/google/go-github/v53/github"
"k8s.io/cli-runtime/pkg/genericclioptions"
Expand All @@ -22,35 +23,53 @@ import (
type action string

const (
owner = "kubernetes-sigs"
repo = "kwok"
apply action = "apply"
delete action = "delete"
owner = "kubernetes-sigs"
repo = "kwok"
apply action = "apply"
delete action = "delete"
minVersion = "v0.4.0"
)

func main() {
rel, err := GetLatestKwokRelease()
if err != nil {
panic(err)
}
if err := InstallKwok(rel); err != nil {

c := semver.Compare(rel, minVersion)
if c < 0 {
log.Fatalf("latest release %s is a lower version than min required version %s\n", rel, minVersion)
}

LegacyInstallAndUninstall(rel)

}

// LegacyInstallAndUninstall installs and uninstalls kwok the legacy way
func LegacyInstallAndUninstall(release string) {
c := semver.Compare(release, minVersion)
if c > 0 {
log.Fatalf("release %s is a version higher than version %s\n", release, minVersion)
}

if err := InstallKwokLegacy(release); err != nil {
panic(err)
}

time.Sleep(20 * time.Second)
if err := UninstallKwok(rel); err != nil {
if err := UninstallKwokLegacy(release); err != nil {
panic(err)
}
}

// UninstallKwok uninstalls kwok from the cluster
func UninstallKwok(release string) error {
return kwokKubectl(release, delete)
// UninstallKwokLegacy uninstalls kwok < v0.4.0 from the cluster
func UninstallKwokLegacy(release string) error {
return kwokKubectlLegacy(release, delete)
}

// InstallKwok installs kwok in the cluster
func InstallKwok(release string) error {
return kwokKubectl(release, apply)
// InstallKwokLegacy installs kwok < v0.4.0 in the cluster
func InstallKwokLegacy(release string) error {
return kwokKubectlLegacy(release, apply)
}

func GetLatestKwokRelease() (string, error) {
Expand All @@ -68,8 +87,8 @@ func GetLatestKwokRelease() (string, error) {
return rel.GetTagName(), nil
}

// kwokKubectl builds kustomize for kwok and runs `kubectl` on it
func kwokKubectl(release string, action action) error {
// kwokKubectlLegacy builds kustomize for kwok < v0.4.0 and runs `kubectl` on it
func kwokKubectlLegacy(release string, action action) error {

if release == "" {
return fmt.Errorf("release is empty: '%s'", release)
Expand Down

0 comments on commit 27d1913

Please sign in to comment.