mirror of https://github.com/kubernetes/kops.git
Kubetest2 to only download the kubectl binary instead of entire tar
Also readds checksum tests
This commit is contained in:
parent
c307d3fc7b
commit
5f1e9b174e
|
|
@ -17,15 +17,11 @@ limitations under the License.
|
||||||
package tester
|
package tester
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"archive/tar"
|
|
||||||
"compress/gzip"
|
|
||||||
"crypto/sha256"
|
"crypto/sha256"
|
||||||
"encoding/hex"
|
"encoding/hex"
|
||||||
"errors"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
|
||||||
"runtime"
|
"runtime"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
|
@ -37,9 +33,6 @@ import (
|
||||||
// AcquireKubectl obtains kubectl and places it in rundir
|
// AcquireKubectl obtains kubectl and places it in rundir
|
||||||
// If a kubectl already exists in rundir, it will be reused.
|
// If a kubectl already exists in rundir, it will be reused.
|
||||||
func (t *Tester) AcquireKubectl() error {
|
func (t *Tester) AcquireKubectl() error {
|
||||||
if _, err := os.Stat(KubectlPath()); errors.Is(err, os.ErrNotExist) {
|
|
||||||
klog.Infof("found kubectl in %s. Will reuse it.", KubectlPath())
|
|
||||||
}
|
|
||||||
|
|
||||||
// first, get the name of the latest release (e.g. v1.20.0-alpha.0)
|
// first, get the name of the latest release (e.g. v1.20.0-alpha.0)
|
||||||
if t.TestPackageVersion == "" {
|
if t.TestPackageVersion == "" {
|
||||||
|
|
@ -60,75 +53,19 @@ func (t *Tester) AcquireKubectl() error {
|
||||||
klog.Infof("Kubectl package version was not specified. Defaulting to version from %s: %s", t.TestPackageMarker, t.TestPackageVersion)
|
klog.Infof("Kubectl package version was not specified. Defaulting to version from %s: %s", t.TestPackageMarker, t.TestPackageVersion)
|
||||||
}
|
}
|
||||||
|
|
||||||
clientTar := fmt.Sprintf("kubernetes-client-%s-%s.tar.gz", runtime.GOOS, runtime.GOARCH)
|
if err := t.ensureKubectl(); err != nil {
|
||||||
|
|
||||||
downloadDir, err := os.UserCacheDir()
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("failed to get user cache directory: %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
downloadPath := filepath.Join(downloadDir, clientTar)
|
|
||||||
|
|
||||||
if err := t.ensureClientTar(downloadPath, clientTar); err != nil {
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
return t.extractBinaries(downloadPath)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (t *Tester) extractBinaries(downloadPath string) error {
|
|
||||||
// finally, search for the client package and extract it
|
|
||||||
f, err := os.Open(downloadPath)
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("failed to open downloaded tar at %s: %w", downloadPath, err)
|
|
||||||
}
|
|
||||||
defer f.Close()
|
|
||||||
gzf, err := gzip.NewReader(f)
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("failed to create gzip reader: %w", err)
|
|
||||||
}
|
|
||||||
tarReader := tar.NewReader(gzf)
|
|
||||||
|
|
||||||
// this is the expected path of the package inside the tar
|
|
||||||
// it will be extracted to kubectlDir in the loop
|
|
||||||
kubectlPackagePath := "kubernetes/client/bin/kubectl"
|
|
||||||
for {
|
|
||||||
header, err := tarReader.Next()
|
|
||||||
if err == io.EOF {
|
|
||||||
break
|
|
||||||
}
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("error during tar read: %w", err)
|
|
||||||
}
|
|
||||||
if header.Name == kubectlPackagePath {
|
|
||||||
kubectlPath := KubectlPath()
|
|
||||||
outFile, err := os.Create(kubectlPath)
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("error creating file at %s: %w", kubectlPath, err)
|
|
||||||
}
|
|
||||||
defer outFile.Close()
|
|
||||||
|
|
||||||
if err := outFile.Chmod(0o700); err != nil {
|
|
||||||
return fmt.Errorf("failed to make %s executable: %w", kubectlPath, err)
|
|
||||||
}
|
|
||||||
|
|
||||||
if _, err := io.Copy(outFile, tarReader); err != nil {
|
|
||||||
return fmt.Errorf("error reading data from tar with header name %s: %w", header.Name, err)
|
|
||||||
}
|
|
||||||
return nil
|
return nil
|
||||||
}
|
|
||||||
}
|
|
||||||
return fmt.Errorf("failed to find %s in %s", kubectlPackagePath, downloadPath)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// ensureClientTar checks if the kubernetes client tarball already exists
|
// ensureKubectl checks if the kubectl binary already exists
|
||||||
// and verifies the hashes
|
// and verifies the hashes else downloads it from GCS
|
||||||
// else downloads it from GCS
|
func (t *Tester) ensureKubectl() error {
|
||||||
func (t *Tester) ensureClientTar(downloadPath, clientTar string) error {
|
if _, err := os.Stat(KubectlPath()); err == nil {
|
||||||
if _, err := os.Stat(downloadPath); err == nil {
|
klog.V(0).Infof("Found existing kubectl at %s", KubectlPath())
|
||||||
klog.V(0).Infof("Found existing tar at %v", downloadPath)
|
if err := t.compareSHA(); err == nil {
|
||||||
if err := t.compareSHA(downloadPath, clientTar); err == nil {
|
klog.V(0).Infof("Validated hash for existing kubectl binary at %v", KubectlPath())
|
||||||
klog.V(0).Infof("Validated hash for existing tar at %v", downloadPath)
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
klog.Warning(err)
|
klog.Warning(err)
|
||||||
|
|
@ -136,43 +73,30 @@ func (t *Tester) ensureClientTar(downloadPath, clientTar string) error {
|
||||||
|
|
||||||
args := []string{
|
args := []string{
|
||||||
"gsutil", "cp",
|
"gsutil", "cp",
|
||||||
fmt.Sprintf(
|
t.kubectlGSLocation(),
|
||||||
"gs://%s/%s/%s/%s",
|
KubectlPath(),
|
||||||
t.TestPackageBucket,
|
|
||||||
t.TestPackageDir,
|
|
||||||
t.TestPackageVersion,
|
|
||||||
clientTar,
|
|
||||||
),
|
|
||||||
downloadPath,
|
|
||||||
}
|
}
|
||||||
klog.Info(strings.Join(args, " "))
|
klog.Info(strings.Join(args, " "))
|
||||||
|
|
||||||
cmd := exec.Command(args[0], args[1:]...)
|
cmd := exec.Command(args[0], args[1:]...)
|
||||||
exec.InheritOutput(cmd)
|
exec.InheritOutput(cmd)
|
||||||
if err := cmd.Run(); err != nil {
|
if err := cmd.Run(); err != nil {
|
||||||
return fmt.Errorf("failed to download release tar %s for release %s: %s", clientTar, t.TestPackageVersion, err)
|
return fmt.Errorf("failed to download kubectl binary for release %s: %s", t.TestPackageVersion, err)
|
||||||
}
|
}
|
||||||
|
os.Chmod(KubectlPath(), os.FileMode(0o700))
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *Tester) compareSHA(downloadPath string, clientTar string) error {
|
func (t *Tester) compareSHA() error {
|
||||||
cmd := exec.Command("gsutil", "cat",
|
cmd := exec.Command("gsutil", "cat", t.kubectlGSLocation()+".sha256")
|
||||||
fmt.Sprintf(
|
|
||||||
"gs://%s/%s/%s/%s",
|
|
||||||
t.TestPackageBucket,
|
|
||||||
t.TestPackageDir,
|
|
||||||
t.TestPackageVersion,
|
|
||||||
clientTar+".sha256",
|
|
||||||
),
|
|
||||||
)
|
|
||||||
expectedSHABytes, err := exec.Output(cmd)
|
expectedSHABytes, err := exec.Output(cmd)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("failed to get sha256 for release tar %s for release %s: %s", clientTar, t.TestPackageVersion, err)
|
return fmt.Errorf("failed to get sha256 for kubectl binary for release %s: %s", t.TestPackageVersion, err)
|
||||||
}
|
}
|
||||||
expectedSHA := strings.TrimSuffix(string(expectedSHABytes), "\n")
|
expectedSHA := strings.TrimSuffix(string(expectedSHABytes), "\n")
|
||||||
actualSHA, err := sha256sum(downloadPath)
|
actualSHA, err := sha256sum(KubectlPath())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("failed to compute sha256 for %q: %v", downloadPath, err)
|
return fmt.Errorf("failed to compute sha256 for %q: %v", KubectlPath(), err)
|
||||||
}
|
}
|
||||||
if actualSHA != expectedSHA {
|
if actualSHA != expectedSHA {
|
||||||
return fmt.Errorf("sha256 does not match")
|
return fmt.Errorf("sha256 does not match")
|
||||||
|
|
@ -180,6 +104,17 @@ func (t *Tester) compareSHA(downloadPath string, clientTar string) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (t *Tester) kubectlGSLocation() string {
|
||||||
|
return fmt.Sprintf(
|
||||||
|
"gs://%s/%s/%s/bin/%s/%s/kubectl",
|
||||||
|
t.TestPackageBucket,
|
||||||
|
t.TestPackageDir,
|
||||||
|
t.TestPackageVersion,
|
||||||
|
runtime.GOOS,
|
||||||
|
runtime.GOARCH,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
func sha256sum(path string) (string, error) {
|
func sha256sum(path string) (string, error) {
|
||||||
f, err := os.Open(path)
|
f, err := os.Open(path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue