-
Notifications
You must be signed in to change notification settings - Fork 4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add support to install and uninstall kwok
- add option to disable installation - add option to manually specify kwok release tag - add future scope in readme Signed-off-by: vadasambar <[email protected]>
- Loading branch information
1 parent
33a9a35
commit b6bda34
Showing
9 changed files
with
301 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,147 @@ | ||
/* | ||
Copyright 2023 The Kubernetes Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package kwok | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"fmt" | ||
"io/ioutil" | ||
"net/http" | ||
"os" | ||
"path/filepath" | ||
"strings" | ||
"text/template" | ||
"time" | ||
|
||
log "github.com/sirupsen/logrus" | ||
|
||
"github.com/google/go-github/v53/github" | ||
"k8s.io/cli-runtime/pkg/genericclioptions" | ||
kubectlcmd "k8s.io/kubectl/pkg/cmd" | ||
) | ||
|
||
type action string | ||
|
||
const ( | ||
owner = "kubernetes-sigs" | ||
repo = "kwok" | ||
apply action = "apply" | ||
delete action = "delete" | ||
) | ||
|
||
// UninstallKwok uninstalls kwok from the cluster | ||
func UninstallKwok(release string) error { | ||
return kwokKubectl(release, delete) | ||
} | ||
|
||
// InstallKwok installs kwok in the cluster | ||
func InstallKwok(release string) error { | ||
return kwokKubectl(release, apply) | ||
} | ||
|
||
func GetLatestKwokRelease() (string, error) { | ||
// find latest release of `kwok` | ||
client := github.NewClient(nil) | ||
rel, resp, err := client.Repositories.GetLatestRelease(context.Background(), owner, repo) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
if resp.Response.StatusCode != http.StatusOK { | ||
log.Fatal("expected 200 response but received", resp.Response.StatusCode) | ||
} | ||
|
||
return rel.GetTagName(), nil | ||
} | ||
|
||
// kwokKubectl builds kustomize for kwok and runs `kubectl` on it | ||
func kwokKubectl(release string, action action) error { | ||
|
||
if release == "" { | ||
return fmt.Errorf("release is empty: '%s'", release) | ||
} | ||
|
||
// create tmp working directory for kwok | ||
tmpDir, err := ioutil.TempDir("", "install-kwok") | ||
if err != nil { | ||
return err | ||
} | ||
defer os.RemoveAll(tmpDir) | ||
|
||
// create kustomization file | ||
kustomizeTemplate := ` | ||
apiVersion: kustomize.config.k8s.io/v1beta1 | ||
kind: Kustomization | ||
images: | ||
- name: registry.k8s.io/kwok/kwok | ||
newTag: "{{.latestRelease}}" | ||
resources: | ||
- "https://github.com/{{.repo}}/kustomize/kwok?ref={{.latestRelease}}" | ||
` | ||
tmpl, err := template.New("kwok-kustomize").Parse(kustomizeTemplate) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
b := &bytes.Buffer{} | ||
err = tmpl.Execute(b, map[string]interface{}{ | ||
"latestRelease": release, | ||
"repo": owner + "/" + repo, | ||
}) | ||
|
||
if err != nil { | ||
return err | ||
} | ||
|
||
log.Infof("latest kwok release is %s", release) | ||
|
||
kustomizationFilePath := filepath.Join(tmpDir, "kustomization.yaml") | ||
if err := os.WriteFile(kustomizationFilePath, | ||
b.Bytes(), | ||
os.FileMode(0600)); err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
// `kubectl apply` using kustomize | ||
o, err := runKubectl("kubectl", string(action), "-k", tmpDir) | ||
if err != nil { | ||
return err | ||
} | ||
log.Infof("kubectl output: \n%s", string(o)) | ||
|
||
return nil | ||
} | ||
|
||
// based on argo-workflows executor | ||
// code ref: https://github.com/argoproj/argo-workflows/blob/545bf3803d6f0c59a4c0a93db23d18001462bf3c/workflow/executor/resource.go#L366 | ||
func runKubectl(args ...string) ([]byte, error) { | ||
log.Info(strings.Join(args, " ")) | ||
os.Args = args | ||
var buf bytes.Buffer | ||
if err := kubectlcmd.NewKubectlCommand(kubectlcmd.KubectlOptions{ | ||
Arguments: args, | ||
ConfigFlags: genericclioptions.NewConfigFlags(true). | ||
WithDeprecatedPasswordFlag(). | ||
WithDiscoveryBurst(300). | ||
WithDiscoveryQPS(50.0), | ||
IOStreams: genericclioptions.IOStreams{Out: &buf, ErrOut: os.Stderr}, | ||
}).Execute(); err != nil { | ||
return nil, err | ||
} | ||
return buf.Bytes(), nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.