-
Notifications
You must be signed in to change notification settings - Fork 401
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
support building from a scratch image
- Loading branch information
Showing
3 changed files
with
138 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
/* | ||
Copyright 2023 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 build | ||
|
||
import ( | ||
"fmt" | ||
|
||
v1 "github.com/google/go-containerregistry/pkg/v1" | ||
"github.com/google/go-containerregistry/pkg/v1/empty" | ||
"github.com/google/go-containerregistry/pkg/v1/mutate" | ||
"github.com/google/go-containerregistry/pkg/v1/types" | ||
) | ||
|
||
// ScratchImage returns a scratch image manifest with scratch images for each of the specified platforms | ||
func ScratchImage(platforms []string) (Result, error) { | ||
idx := mutate.IndexMediaType(empty.Index, types.OCIImageIndex) | ||
for _, pf := range platforms { | ||
if pf == "all" { | ||
return nil, fmt.Errorf("'all' is not supported for building a scratch image, the platform list must be provided") | ||
} | ||
p, err := v1.ParsePlatform(pf) | ||
if err != nil { | ||
return nil, err | ||
} | ||
img, err := mutate.ConfigFile(empty.Image, &v1.ConfigFile{ | ||
RootFS: v1.RootFS{ | ||
// Some clients check this. | ||
Type: "layers", | ||
}, | ||
Architecture: p.Architecture, | ||
OS: p.OS, | ||
Variant: p.Variant, | ||
OSVersion: p.OSVersion, | ||
OSFeatures: p.OSFeatures, | ||
}, | ||
) | ||
if err != nil { | ||
return nil, fmt.Errorf("setting config file on empty image, %w", err) | ||
} | ||
idx = mutate.AppendManifests(idx, mutate.IndexAddendum{ | ||
Add: img, | ||
Descriptor: v1.Descriptor{ | ||
Platform: p, | ||
}, | ||
}) | ||
} | ||
return idx, 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,66 @@ | ||
/* | ||
Copyright 2023 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 build | ||
|
||
import ( | ||
"testing" | ||
|
||
v1 "github.com/google/go-containerregistry/pkg/v1" | ||
"github.com/google/go-containerregistry/pkg/v1/types" | ||
) | ||
|
||
func TestScratchImage(t *testing.T) { | ||
img, err := ScratchImage([]string{"linux/s390x", "plan9/386"}) | ||
if err != nil { | ||
t.Fatalf("expected to create the image, %s", err) | ||
} | ||
mt, err := img.MediaType() | ||
if err != nil { | ||
t.Fatalf("expected to get a mediatype, %s", err) | ||
} | ||
expMT := types.OCIImageIndex | ||
if mt != expMT { | ||
t.Errorf("expected media type = %s, got %s", expMT, mt) | ||
} | ||
|
||
imgIdx, ok := img.(v1.ImageIndex) | ||
if !ok { | ||
t.Fatalf("expected to have an image index") | ||
} | ||
mf, err := imgIdx.IndexManifest() | ||
if mt != expMT { | ||
t.Errorf("expected a manifest, got %s", err) | ||
} | ||
if len(mf.Manifests) != 2 { | ||
t.Fatalf("expected two manifests, got %d", len(mf.Manifests)) | ||
} | ||
for _, m := range mf.Manifests { | ||
switch m.Platform.OS { | ||
case "linux": | ||
if m.Platform.Architecture != "s390x" { | ||
t.Errorf("expected arch = s390x, got %s", m.Platform.Architecture) | ||
} | ||
case "plan9": | ||
if m.Platform.Architecture != "386" { | ||
t.Errorf("expected arch = 386, got %s", m.Platform.Architecture) | ||
} | ||
default: | ||
t.Errorf("unexpected OS %s", m.Platform.OS) | ||
} | ||
} | ||
|
||
} |
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