1
0
mirror of https://github.com/ko-build/ko.git synced 2025-02-01 19:14:40 +02:00

Implement ko deps (#403)

* Implement ko deps

* actually add deps.go

* specify auth, useragent, platform

* stop reading tar if the context is cancelled

* chmod to the file's perms

* remove support for --platform, modules don't care about build tags

* fix copyright boilerplate

* drop fs dependency

* udpate module integration test to newer Go versions

* use entrypoint to identify the binary

* fix gosec finding, some style comments

* revert modules integration test change
This commit is contained in:
Jason Hall 2021-07-30 13:19:40 -04:00 committed by GitHub
parent 305f086c4e
commit 8c7b9cbb8b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 134 additions and 15 deletions

View File

@ -1,15 +1,13 @@
/*
Copyright 2020 Google LLC All Rights Reserved.
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.
*/
// Copyright 2021 Google LLC All Rights Reserved.
//
// 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.

View File

@ -31,6 +31,7 @@ func AddKubeCommands(topLevel *cobra.Command) {
addResolve(topLevel)
addPublish(topLevel)
addRun(topLevel)
addDeps(topLevel)
addCompletion(topLevel)
}

120
pkg/commands/deps.go Normal file
View File

@ -0,0 +1,120 @@
// Copyright 2021 Google LLC All Rights Reserved.
//
// 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 commands
import (
"archive/tar"
"fmt"
"io"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"github.com/google/go-containerregistry/pkg/authn"
"github.com/google/go-containerregistry/pkg/name"
"github.com/google/go-containerregistry/pkg/v1/mutate"
"github.com/google/go-containerregistry/pkg/v1/remote"
"github.com/spf13/cobra"
)
// addDeps augments our CLI surface with deps.
func addDeps(topLevel *cobra.Command) {
deps := &cobra.Command{
Use: "deps IMAGE",
Short: "Print Go module dependency information about the ko-built binary in the image",
Long: `This sub-command finds and extracts the executable binary in the image, assuming it was built by ko, and prints information about the Go module dependencies of that executable, as reported by "go version -m".
If the image was not built using ko, or if it was built without embedding dependency information, this command will fail.`,
Example: `
# Fetch and extract Go dependency information from an image:
ko deps docker.io/my-user/my-image:v3`,
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
ctx := createCancellableContext()
ref, err := name.ParseReference(args[0])
if err != nil {
return err
}
img, err := remote.Image(ref,
remote.WithContext(ctx),
remote.WithAuthFromKeychain(authn.DefaultKeychain),
remote.WithUserAgent(ua()))
if err != nil {
return err
}
cfg, err := img.ConfigFile()
if err != nil {
return err
}
ep := cfg.Config.Entrypoint
if len(ep) != 1 {
return fmt.Errorf("unexpected entrypoint: %s", ep)
}
bin := ep[0]
rc := mutate.Extract(img)
defer rc.Close()
tr := tar.NewReader(rc)
for {
// Stop reading if the context is cancelled.
select {
case <-ctx.Done():
return ctx.Err()
default:
// keep reading.
}
h, err := tr.Next()
if err == io.EOF {
return fmt.Errorf("no ko-built executable named %q found", bin)
}
if err != nil {
return err
}
if h.Typeflag != tar.TypeReg {
continue
}
if h.Name != bin {
continue
}
tmp, err := ioutil.TempFile("", filepath.Base(h.Name))
if err != nil {
return err
}
n := tmp.Name()
defer os.RemoveAll(n) // best effort: remove tmp file afterwards.
defer tmp.Close() // close it first.
// io.LimitReader to appease gosec...
if _, err := io.Copy(tmp, io.LimitReader(tr, h.Size)); err != nil {
return err
}
if err := os.Chmod(n, os.FileMode(h.Mode)); err != nil {
return err
}
cmd := exec.CommandContext(ctx, "go", "version", "-m", n)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
return cmd.Run()
}
// unreachable
},
}
topLevel.AddCommand(deps)
}