-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Davanum Srinivas <davanum@gmail.com>
- Loading branch information
Showing
6 changed files
with
319 additions
and
19 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,67 @@ | ||
/* | ||
Copyright 2018 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 kube | ||
|
||
import ( | ||
"os" | ||
"path/filepath" | ||
"sigs.k8s.io/kind/pkg/log" | ||
"strings" | ||
) | ||
|
||
// TODO(bentheelder): plumb through arch | ||
|
||
// directoryBuilder implements Bits for a local docker-ized make / bash build | ||
type directoryBuilder struct { | ||
rootDirectory string | ||
logger log.Logger | ||
} | ||
|
||
var _ Builder = &directoryBuilder{} | ||
|
||
// NewDirectoryBuilder returns a new Bits backed by the docker-ized build, | ||
// given kubeRoot, the path to the kubernetes source directory | ||
func NewDirectoryBuilder(logger log.Logger, rootDirectory string) (Builder, error) { | ||
return &directoryBuilder{ | ||
rootDirectory: rootDirectory, | ||
logger: logger, | ||
}, nil | ||
} | ||
|
||
// Build implements Bits.Build | ||
func (b *directoryBuilder) Build() (Bits, error) { | ||
binDir := filepath.Join(b.rootDirectory, "kubernetes/server/bin") | ||
contents, err := os.ReadFile(filepath.Join(binDir, "kube-apiserver.docker_tag")) | ||
if err != nil { | ||
return nil, err | ||
} | ||
sourceVersionRaw := strings.TrimSpace(string(contents)) | ||
return &bits{ | ||
binaryPaths: []string{ | ||
filepath.Join(binDir, "kubeadm"), | ||
filepath.Join(binDir, "kubelet"), | ||
filepath.Join(binDir, "kubectl"), | ||
}, | ||
imagePaths: []string{ | ||
filepath.Join(binDir, "kube-apiserver.tar"), | ||
filepath.Join(binDir, "kube-controller-manager.tar"), | ||
filepath.Join(binDir, "kube-scheduler.tar"), | ||
filepath.Join(binDir, "kube-proxy.tar"), | ||
}, | ||
version: sourceVersionRaw, | ||
}, 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,201 @@ | ||
/* | ||
Copyright 2018 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 kube | ||
|
||
import ( | ||
"archive/tar" | ||
"compress/gzip" | ||
"context" | ||
"fmt" | ||
"io" | ||
"net" | ||
"net/http" | ||
"os" | ||
"path/filepath" | ||
"strings" | ||
"time" | ||
|
||
"sigs.k8s.io/kind/pkg/log" | ||
) | ||
|
||
type remoteBuilder struct { | ||
version string | ||
logger log.Logger | ||
} | ||
|
||
var _ Builder = &remoteBuilder{} | ||
|
||
func NewRemoteBuilder(logger log.Logger, version string) (Builder, error) { | ||
return &remoteBuilder{ | ||
version: version, | ||
logger: logger, | ||
}, nil | ||
} | ||
|
||
// Build implements Bits.Build | ||
func (b *remoteBuilder) Build() (Bits, error) { | ||
url := "https://dl.k8s.io/" + b.version + "/kubernetes-server-linux-amd64.tar.gz" | ||
|
||
tmpDir, err := os.MkdirTemp(os.TempDir(), "k8s-tar-extract-") | ||
if err != nil { | ||
return nil, fmt.Errorf("error creating temporary directory for tar extraction: %w", err) | ||
} | ||
|
||
tgzFile := filepath.Join(tmpDir, "kubernetes-"+b.version+"-server-linux-amd64.tar.gz") | ||
err = b.downloadURL(url, tgzFile) | ||
if err != nil { | ||
return nil, fmt.Errorf("error downloading file: %w", err) | ||
} | ||
|
||
err = b.extractTarball(tgzFile, tmpDir) | ||
if err != nil { | ||
return nil, fmt.Errorf("error extracting tgz file: %w", err) | ||
} | ||
|
||
binDir := filepath.Join(tmpDir, "kubernetes/server/bin") | ||
contents, err := os.ReadFile(filepath.Join(binDir, "kube-apiserver.docker_tag")) | ||
if err != nil { | ||
return nil, err | ||
} | ||
sourceVersionRaw := strings.TrimSpace(string(contents)) | ||
return &bits{ | ||
binaryPaths: []string{ | ||
filepath.Join(binDir, "kubeadm"), | ||
filepath.Join(binDir, "kubelet"), | ||
filepath.Join(binDir, "kubectl"), | ||
}, | ||
imagePaths: []string{ | ||
filepath.Join(binDir, "kube-apiserver.tar"), | ||
filepath.Join(binDir, "kube-controller-manager.tar"), | ||
filepath.Join(binDir, "kube-scheduler.tar"), | ||
filepath.Join(binDir, "kube-proxy.tar"), | ||
}, | ||
version: sourceVersionRaw, | ||
}, nil | ||
} | ||
|
||
func (b *remoteBuilder) downloadURL(url string, destPath string) error { | ||
output, err := os.Create(destPath) | ||
if err != nil { | ||
return fmt.Errorf("error creating file for download %q: %v", destPath, err) | ||
} | ||
defer output.Close() | ||
|
||
b.logger.V(0).Infof("Downloading %q", url) | ||
|
||
// Create a client with custom timeouts | ||
// to avoid idle downloads to hang the program | ||
httpClient := &http.Client{ | ||
Transport: &http.Transport{ | ||
Proxy: http.ProxyFromEnvironment, | ||
DialContext: (&net.Dialer{ | ||
Timeout: 30 * time.Second, | ||
KeepAlive: 30 * time.Second, | ||
}).DialContext, | ||
TLSHandshakeTimeout: 10 * time.Second, | ||
ResponseHeaderTimeout: 30 * time.Second, | ||
IdleConnTimeout: 30 * time.Second, | ||
}, | ||
} | ||
|
||
// this will stop slow downloads after 3 minutes | ||
// and interrupt reading of the Response.Body | ||
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Minute) | ||
defer cancel() | ||
|
||
req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil) | ||
if err != nil { | ||
return fmt.Errorf("cannot create request: %v", err) | ||
} | ||
|
||
response, err := httpClient.Do(req) | ||
if err != nil { | ||
return fmt.Errorf("error doing HTTP fetch of %q: %v", url, err) | ||
} | ||
defer response.Body.Close() | ||
|
||
if response.StatusCode >= 400 { | ||
return fmt.Errorf("error response from %q: HTTP %v", url, response.StatusCode) | ||
} | ||
|
||
start := time.Now() | ||
defer func() { | ||
b.logger.V(2).Infof("Copying %q to %q took %q", url, destPath, time.Since(start)) | ||
}() | ||
|
||
_, err = io.Copy(output, response.Body) | ||
if err != nil { | ||
return fmt.Errorf("error downloading HTTP content from %q: %v", url, err) | ||
} | ||
return nil | ||
} | ||
|
||
func (b *remoteBuilder) extractTarball(tarPath, destDirectory string) (err error) { | ||
// Open the tar file | ||
f, err := os.Open(tarPath) | ||
if err != nil { | ||
return fmt.Errorf("opening tarball: %w", err) | ||
} | ||
defer f.Close() | ||
|
||
gzipReader, err := gzip.NewReader(f) | ||
if err != nil { | ||
return err | ||
} | ||
tr := tar.NewReader(gzipReader) | ||
|
||
numFiles := 0 | ||
for { | ||
hdr, err := tr.Next() | ||
if err == io.EOF { | ||
break | ||
} | ||
if err != nil { | ||
return fmt.Errorf("reading tarfile %s: %w", tarPath, err) | ||
} | ||
|
||
if hdr.FileInfo().IsDir() { | ||
continue | ||
} | ||
|
||
if err := os.MkdirAll( | ||
filepath.Join(destDirectory, filepath.Dir(hdr.Name)), os.FileMode(0o755), | ||
); err != nil { | ||
return fmt.Errorf("creating image directory structure: %w", err) | ||
} | ||
|
||
f, err := os.Create(filepath.Join(destDirectory, hdr.Name)) | ||
if err != nil { | ||
return fmt.Errorf("creating image layer file: %w", err) | ||
} | ||
|
||
if _, err := io.CopyN(f, tr, hdr.Size); err != nil { | ||
f.Close() | ||
if err == io.EOF { | ||
break | ||
} | ||
|
||
return fmt.Errorf("extracting image data: %w", err) | ||
} | ||
f.Close() | ||
|
||
numFiles++ | ||
} | ||
|
||
b.logger.V(2).Infof("Successfully extracted %d files from image tarball %s", numFiles, tarPath) | ||
return err | ||
} |
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