diff --git a/README.md b/README.md index c6f0c29..3ce5c13 100644 --- a/README.md +++ b/README.md @@ -6,4 +6,37 @@ $ go run main.go ``` ### Misc -`main.go` is basically a go implementation of `alternative-install.sh` \ No newline at end of file +`main.go` is basically a go implementation of `alternative-install.sh` + +### Output +``` +$ go run main.go +deploymentAndCRDs URL https://github.com/kubernetes-sigs/kwok/releases/download/v0.4.0/kwok.yaml +INFO[0000] kubectl apply -f https://github.com/kubernetes-sigs/kwok/releases/download/v0.4.0/kwok.yaml +INFO[0004] kubectl output: +customresourcedefinition.apiextensions.k8s.io/attaches.kwok.x-k8s.io created +customresourcedefinition.apiextensions.k8s.io/clusterattaches.kwok.x-k8s.io created +customresourcedefinition.apiextensions.k8s.io/clusterexecs.kwok.x-k8s.io created +customresourcedefinition.apiextensions.k8s.io/clusterlogs.kwok.x-k8s.io created +customresourcedefinition.apiextensions.k8s.io/clusterportforwards.kwok.x-k8s.io created +customresourcedefinition.apiextensions.k8s.io/execs.kwok.x-k8s.io created +customresourcedefinition.apiextensions.k8s.io/logs.kwok.x-k8s.io created +customresourcedefinition.apiextensions.k8s.io/metrics.kwok.x-k8s.io created +customresourcedefinition.apiextensions.k8s.io/portforwards.kwok.x-k8s.io created +customresourcedefinition.apiextensions.k8s.io/stages.kwok.x-k8s.io created +serviceaccount/kwok-controller created +clusterrole.rbac.authorization.k8s.io/kwok-controller created +clusterrolebinding.rbac.authorization.k8s.io/kwok-controller created +service/kwok-controller created +deployment.apps/kwok-controller created +stagesCRs URL https://github.com/kubernetes-sigs/kwok/releases/download/v0.4.0/stage-fast.yaml +INFO[0004] kubectl apply -f https://github.com/kubernetes-sigs/kwok/releases/download/v0.4.0/stage-fast.yaml +INFO[0006] kubectl output: +stage.kwok.x-k8s.io/node-heartbeat-with-lease created +stage.kwok.x-k8s.io/node-initialize created +stage.kwok.x-k8s.io/pod-complete created +stage.kwok.x-k8s.io/pod-delete created +stage.kwok.x-k8s.io/pod-ready created +``` + +![](./images/2023-09-06-11-28-54.png) \ No newline at end of file diff --git a/images/2023-09-06-11-28-54.png b/images/2023-09-06-11-28-54.png new file mode 100644 index 0000000..244d210 Binary files /dev/null and b/images/2023-09-06-11-28-54.png differ diff --git a/alternative-install.sh b/legacy-alternative-install.sh similarity index 100% rename from alternative-install.sh rename to legacy-alternative-install.sh diff --git a/main.go b/main.go index 237d745..f543357 100644 --- a/main.go +++ b/main.go @@ -28,8 +28,11 @@ const ( apply action = "apply" delete action = "delete" minVersion = "v0.4.0" + kwokRepo = "kubernetes-sigs/kwok" ) +var installRelease string + func main() { rel, err := GetLatestKwokRelease() if err != nil { @@ -41,8 +44,68 @@ func main() { log.Fatalf("latest release %s is a lower version than min required version %s\n", rel, minVersion) } - LegacyInstallAndUninstall(rel) + installRelease = rel + + // clean existing release if any + if err := UninstallKwok(); err != nil { + panic(err) + } + + if err := InstallKwok(); err != nil { + panic(err) + } + + time.Sleep(time.Second * 20) + if err := UninstallKwok(); err != nil { + panic(err) + } + +} + +// InstallKwok installs kwok >= v0.4.0 +// Based on https://kwok.sigs.k8s.io/docs/user/kwok-in-cluster/#deploy-kwok-in-a-cluster +func InstallKwok() error { + return kwokKubectl(apply, nil) +} + +// UninstallKwok uninstalls kwok >= v0.4.0 +// Based on https://kwok.sigs.k8s.io/docs/user/kwok-in-cluster/#deploy-kwok-in-a-cluster +func UninstallKwok() error { + return kwokKubectl(delete, []string{"--ignore-not-found=true"}) +} +func kwokKubectl(action action, extraArgs []string) error { + deploymentAndCRDs := fmt.Sprintf("https://github.com/%s/releases/download/%s/kwok.yaml", kwokRepo, installRelease) + // fmt.Println("deploymentAndCRDs URL", deploymentAndCRDs) // for debugging + // `kubectl apply` deployment and CRDs + cmd := []string{ + "kubectl", string(action), "-f", deploymentAndCRDs, + } + if len(extraArgs) > 0 { + cmd = append(cmd, extraArgs...) + } + o, err := runKubectl(cmd...) + if err != nil { + return err + } + log.Infof("kubectl output: \n%s", string(o)) + + stagesCRs := fmt.Sprintf("https://github.com/%s/releases/download/%s/stage-fast.yaml", kwokRepo, installRelease) + // fmt.Println("stagesCRs URL", stagesCRs) // for debugging + // `kubectl apply` stages + cmd = []string{ + "kubectl", string(action), "-f", stagesCRs, + } + if len(extraArgs) > 0 { + cmd = append(cmd, extraArgs...) + } + o, err = runKubectl(cmd...) + if err != nil { + return err + } + log.Infof("kubectl output: \n%s", string(o)) + + return nil } // LegacyInstallAndUninstall installs and uninstalls kwok the legacy way