-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ add hack/tooling and controller setup to use binaries from the proj…
…ect bin
- Loading branch information
Camila Macedo
committed
Nov 23, 2020
1 parent
2c3ae8f
commit a434f1b
Showing
71 changed files
with
3,062 additions
and
95 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
191 changes: 191 additions & 0 deletions
191
pkg/plugin/v2/scaffolds/internal/templates/hack/tooling.go
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,191 @@ | ||
/* | ||
Copyright 2020 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 hack | ||
|
||
import ( | ||
"path/filepath" | ||
|
||
"sigs.k8s.io/kubebuilder/v2/pkg/model/file" | ||
) | ||
|
||
var _ file.Template = &Tooling{} | ||
|
||
// Tooling scaffolds the tooling file in the root dir. | ||
type Tooling struct { | ||
file.TemplateMixin | ||
file.BoilerplateMixin | ||
|
||
// KubernetesVersion defines the version used to download the binaries | ||
KubernetesVersion string | ||
} | ||
|
||
// SetTemplateDefaults implements input.Template | ||
func (f *Tooling) SetTemplateDefaults() error { | ||
if f.Path == "" { | ||
f.Path = filepath.Join("hack", "tooling.go") | ||
} | ||
|
||
f.TemplateBody = toolingTemplate | ||
|
||
f.IfExistsAction = file.Error | ||
|
||
return nil | ||
} | ||
|
||
// nolint: lll | ||
const toolingTemplate = `{{ .Boilerplate }} | ||
package main | ||
import ( | ||
"fmt" | ||
"io" | ||
"net/http" | ||
"os" | ||
"os/exec" | ||
"path/filepath" | ||
"strings" | ||
"k8s.io/klog" | ||
) | ||
const ( | ||
// defaultKubebuilderPath is the fixed value defined in the controller-runtime to looking for the | ||
// binaries. | ||
defaultKubebuilderPath = "/usr/local/kubebuilder/bin" | ||
// kubernetesVersion defines the version used to download the binaries | ||
kubernetesVersion = "{{ .KubernetesVersion }}" | ||
) | ||
func main() { | ||
klog.Info("Checking EnvTest binaries") | ||
assetsEnv := os.Getenv("KUBEBUILDER_ASSETS") | ||
if strings.TrimSpace(assetsEnv) != "" { | ||
klog.Infof("EnvTest binaries configured via KUBEBUILDER_ASSETS: %s", assetsEnv) | ||
os.Exit(0) | ||
} | ||
if HasBinaries(defaultKubebuilderPath) { | ||
klog.Infof("EnvTest binaries found in : %s ", defaultKubebuilderPath) | ||
os.Exit(0) | ||
} | ||
currentPath, err := os.Getwd() | ||
if err != nil { | ||
klog.Fatal(err) | ||
} | ||
binPath := filepath.Join(currentPath, "bin") | ||
if HasBinaries(binPath) { | ||
klog.Info("EnvTest binaries found in bin/") | ||
os.Exit(0) | ||
} | ||
klog.Infof("Downloading EnvTest tools") | ||
envtest_tools_archive_name := getEnvToolsArchiveName() | ||
envtest_tools_download_url := "https://storage.googleapis.com/kubebuilder-tools/" + envtest_tools_archive_name | ||
err = downloadFile(envtest_tools_archive_name, envtest_tools_download_url) | ||
if err != nil { | ||
klog.Fatalf("unable to download the file (%v) from (%v): %v", envtest_tools_archive_name, envtest_tools_download_url, err) | ||
} | ||
if _, err := os.Stat(binPath); os.IsNotExist(err) { | ||
klog.Infof("Creating bin/ directory") | ||
err = os.Mkdir(binPath, 0755) | ||
if err != nil { | ||
klog.Fatalf("error to create the bin/ directory : %s", err) | ||
} | ||
} | ||
cmd := exec.Command("tar", "-C", binPath, "--strip-components=2", "-zvxf", envtest_tools_archive_name) | ||
if err := cmd.Run(); err != nil { | ||
klog.Fatalf("error to untar bin %v: %v", filepath.Join(binPath, envtest_tools_archive_name ) ,err) | ||
} | ||
cmd = exec.Command("rm", "-rf", filepath.Join(currentPath,envtest_tools_archive_name)) | ||
if err := cmd.Run(); err != nil { | ||
klog.Fatal(err) | ||
} | ||
klog.Infof("EnvTest binaries are in the directory %v", binPath) | ||
} | ||
// getEnvToolsArchiveName will return the name of the env tools archive based or the arch and so | ||
func getEnvToolsArchiveName() string { | ||
cmd := exec.Command("go", "env", "GOARCH") | ||
goarch, err := cmd.CombinedOutput() | ||
if err != nil { | ||
klog.Fatal(err) | ||
} | ||
cmd = exec.Command("go", "env", "GOOS") | ||
goos, err := cmd.CombinedOutput() | ||
if err != nil { | ||
klog.Fatal(err) | ||
} | ||
return fmt.Sprintf("kubebuilder-tools-%s-%s-%s.tar.gz", kubernetesVersion, strings.TrimSpace(string(goos)), strings.TrimSpace(string(goarch))) | ||
} | ||
// HasBinaries will return true when the testbin exist and has the binaries | ||
func HasBinaries(path string) bool { | ||
if _, err := os.Stat(path); os.IsNotExist(err) { | ||
return false | ||
} | ||
if _, err := os.Stat(filepath.Join(path, "etcd")); os.IsNotExist(err) { | ||
return false | ||
} | ||
if _, err := os.Stat(filepath.Join(path, "kube-apiserver")); os.IsNotExist(err) { | ||
return false | ||
} | ||
if _, err := os.Stat(filepath.Join(path, "kubectl")); os.IsNotExist(err) { | ||
return false | ||
} | ||
return true | ||
} | ||
// downloadFile will download a url to a local file. | ||
func downloadFile(filepath string, url string) error { | ||
// Get the data | ||
resp, err := http.Get(url) | ||
if err != nil { | ||
return err | ||
} | ||
defer resp.Body.Close() | ||
// Create the file | ||
out, err := os.Create(filepath) | ||
if err != nil { | ||
return err | ||
} | ||
defer out.Close() | ||
// Write the body to file | ||
_, err = io.Copy(out, resp.Body) | ||
return err | ||
} | ||
` |
Oops, something went wrong.