Skip to content

Commit

Permalink
Implement extractConfig binary in Go (bazelbuild#967)
Browse files Browse the repository at this point in the history
* Implement extractConfig binary in Go

* builidifier error resolution

* moved extract config binary to a package under container/go

* update layer_tools.bzl to utilize new binary
  • Loading branch information
xwinxu authored and k8s-ci-robot committed Jul 9, 2019
1 parent c2f7f81 commit f937ad5
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 57 deletions.
18 changes: 17 additions & 1 deletion container/go/cmd/extract_config/BUILD
Original file line number Diff line number Diff line change
@@ -1,8 +1,24 @@
# Copyright 2017 The Bazel Authors. 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.
# ###
# Build file for extractConfig binary based on go-containerregistry backend.

load("@io_bazel_rules_go//go:def.bzl", "go_binary", "go_library")

go_library(
name = "go_default_library",
srcs = ["main.go"],
srcs = ["extractConfig.go"],
importpath = "github.com/bazelbuild/rules_docker/container/go/cmd/extract_config",
visibility = ["//visibility:private"],
deps = ["@com_github_google_go_containerregistry//pkg/v1/tarball:go_default_library"],
Expand Down
74 changes: 74 additions & 0 deletions container/go/cmd/extract_config/extractConfig.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
// Copyright 2017 The Bazel Authors. 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/
////////////////////////////////////
//This binary implements the ability to load a docker image tarball and
// extract its config & manifest json to paths specified via command line
// arguments.
// It expects to be run with:
// extract_config -tarball=image.tar -output=output.confi
package main

import (
"flag"
"io/ioutil"
"log"

"github.com/google/go-containerregistry/pkg/v1/tarball"
)

var (
imageTar = flag.String("imageTar", "", "The path to the Docker image tarball to extract the config & manifest for.")
outputConfig = flag.String("outputConfig", "", "The path to the output file where the image config will be written to.")
outputManifest = flag.String("outputManifest", "", "The path to the output file where the image manifest will be written to.")
)

func main() {
flag.Parse()
log.Println("Running the Image Config & Manifest Extractor.")

if *imageTar == "" {
log.Fatalln("Required option -imageTar was not specified.")
}
if *outputConfig == "" {
log.Fatalln("Required option -outputConfig was not specified.")
}
if *outputManifest == "" {
log.Fatalln("Required option -outputManifest was not specified.")
}

img, err := tarball.ImageFromPath(*imageTar, nil)
if err != nil {
log.Fatalf("Unable to load docker image from %s: %v", *imageTar, err)
}

// Write the config file contents to the ouput file specified withh permissions 0644.
configContent, err := img.RawConfigFile()
if err != nil {
log.Fatalf("Failed to read config file: %v", err)
}
if err := ioutil.WriteFile(*outputConfig, configContent, 0644); err != nil {
log.Fatalf("Failed to write config file contents to %s: %v", *outputConfig, err)
}

// Write the manifest file contents to the manifestoutput file specified with permissions 0644.
manifestContent, err := img.RawManifest()
if err != nil {
log.Fatalf("Failed to read manifest file: %v", err)
}
if err := ioutil.WriteFile(*outputManifest, manifestContent, 0644); err != nil {
log.Fatalf("Failed to write manifest file contents to %s: %v", *outputManifest, err)
}

log.Println("Image Config & Manifest Extractor was successful.")
}
52 changes: 0 additions & 52 deletions container/go/cmd/extract_config/main.go

This file was deleted.

8 changes: 4 additions & 4 deletions container/layer_tools.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@ def _extract_layers(ctx, name, artifact):
ctx.actions.run(
executable = ctx.executable.extract_config,
arguments = [
"--tarball",
"-imageTar",
artifact.path,
"--output",
"-outputConfig",
config_file.path,
"--manifestoutput",
"-outputManifest",
manifest_file.path,
],
tools = [artifact],
Expand Down Expand Up @@ -235,7 +235,7 @@ def incremental_load(

tools = {
"extract_config": attr.label(
default = Label("//container:extract_config"),
default = Label("//container/go/cmd/extract_config:extract_config"),
cfg = "host",
executable = True,
allow_files = True,
Expand Down

0 comments on commit f937ad5

Please sign in to comment.