Skip to content

Commit

Permalink
Add a new directory/remote builder
Browse files Browse the repository at this point in the history
Signed-off-by: Davanum Srinivas <davanum@gmail.com>
  • Loading branch information
dims committed May 14, 2024
1 parent 5d17676 commit fbef009
Show file tree
Hide file tree
Showing 6 changed files with 319 additions and 19 deletions.
45 changes: 34 additions & 11 deletions pkg/build/nodeimage/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ limitations under the License.
package nodeimage

import (
"fmt"
"runtime"

"sigs.k8s.io/kind/pkg/build/nodeimage/internal/kube"
Expand Down Expand Up @@ -46,21 +47,43 @@ func Build(options ...Option) error {
ctx.logger.Warnf("unsupported architecture %q", ctx.arch)
}

// locate sources if no kubernetes source was specified
if ctx.kubeRoot == "" {
kubeRoot, err := kube.FindSource()
if ctx.buildType == "directory" {
if ctx.kubeParam == "" {
return fmt.Errorf("error missing root directory for artifacts")
}
// initialize bits
builder, err := kube.NewDirectoryBuilder(ctx.logger, ctx.kubeParam)
if err != nil {
return errors.Wrap(err, "error finding kuberoot")
return err
}
ctx.builder = builder
} else if ctx.buildType == "release" {
if ctx.kubeParam == "" {
return fmt.Errorf("error missing release version for artifacts")
}
// initialize bits
builder, err := kube.NewRemoteBuilder(ctx.logger, ctx.kubeParam)
if err != nil {
return err
}
ctx.builder = builder
} else {
// locate sources if no kubernetes source was specified
if ctx.kubeParam == "" {
kubeRoot, err := kube.FindSource()
if err != nil {
return errors.Wrap(err, "error finding kuberoot")
}
ctx.kubeParam = kubeRoot
}
ctx.kubeRoot = kubeRoot
}

// initialize bits
builder, err := kube.NewDockerBuilder(ctx.logger, ctx.kubeRoot, ctx.arch)
if err != nil {
return err
// initialize bits
builder, err := kube.NewDockerBuilder(ctx.logger, ctx.kubeParam, ctx.arch)
if err != nil {
return err
}
ctx.builder = builder
}
ctx.builder = builder

// do the actual build
return ctx.Build()
Expand Down
3 changes: 2 additions & 1 deletion pkg/build/nodeimage/buildcontext.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ type buildContext struct {
baseImage string
logger log.Logger
arch string
kubeRoot string
kubeParam string
buildType string
// non-option fields
builder kube.Builder
}
Expand Down
67 changes: 67 additions & 0 deletions pkg/build/nodeimage/internal/kube/builder_directory.go
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
}
201 changes: 201 additions & 0 deletions pkg/build/nodeimage/internal/kube/builder_remote.go
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
}
16 changes: 13 additions & 3 deletions pkg/build/nodeimage/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,10 @@ func WithBaseImage(image string) Option {
})
}

// WithKuberoot sets the path to the Kubernetes source directory (if empty, the path will be autodetected)
func WithKuberoot(root string) Option {
// WithKubeParam sets the path to the Kubernetes source directory (if empty, the path will be autodetected)
func WithKubeParam(root string) Option {
return optionAdapter(func(b *buildContext) error {
b.kubeRoot = root
b.kubeParam = root
return nil
})
}
Expand All @@ -72,3 +72,13 @@ func WithArch(arch string) Option {
return nil
})
}

// WithType sets the type of build
func WithType(buildType string) Option {
return optionAdapter(func(b *buildContext) error {
if buildType != "" {
b.buildType = buildType
}
return nil
})
}
6 changes: 2 additions & 4 deletions pkg/cmd/kind/build/nodeimage/nodeimage.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,6 @@ func NewCommand(logger log.Logger, streams cmd.IOStreams) *cobra.Command {
}
logger.Warn("--kube-root is deprecated, please switch to passing this as an argument")
}
if cmd.Flags().Lookup("type").Changed {
return errors.New("--type is no longer supported, please remove this flag")
}
return runE(logger, flags, args)
},
}
Expand Down Expand Up @@ -97,9 +94,10 @@ func runE(logger log.Logger, flags *flagpole, args []string) error {
if err := nodeimage.Build(
nodeimage.WithImage(flags.Image),
nodeimage.WithBaseImage(flags.BaseImage),
nodeimage.WithKuberoot(kubeRoot),
nodeimage.WithKubeParam(kubeRoot),
nodeimage.WithLogger(logger),
nodeimage.WithArch(flags.Arch),
nodeimage.WithType(flags.BuildType),
); err != nil {
return errors.Wrap(err, "error building node image")
}
Expand Down

0 comments on commit fbef009

Please sign in to comment.