/
kind.go
130 lines (115 loc) · 3.15 KB
/
kind.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
// Copyright 2020 ko Build 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.
package publish
import (
"context"
"fmt"
"log"
"os"
"strings"
"github.com/google/go-containerregistry/pkg/name"
v1 "github.com/google/go-containerregistry/pkg/v1"
"github.com/google/ko/pkg/build"
"github.com/google/ko/pkg/publish/kind"
)
const (
// KindDomain is a sentinel "registry" that represents side-loading images into kind nodes.
KindDomain = "kind.local"
)
type kindPublisher struct {
base string
namer Namer
tags []string
}
// NewKindPublisher returns a new publish.Interface that loads images into kind nodes.
func NewKindPublisher(base string, namer Namer, tags []string) Interface {
return &kindPublisher{
base: base,
namer: namer,
tags: tags,
}
}
// Publish implements publish.Interface.
func (t *kindPublisher) Publish(ctx context.Context, br build.Result, s string) (name.Reference, error) {
s = strings.TrimPrefix(s, build.StrictScheme)
// https://github.com/google/go-containerregistry/issues/212
s = strings.ToLower(s)
// There's no way to write an index to a kind, so attempt to downcast it to an image.
var img v1.Image
switch i := br.(type) {
case v1.Image:
img = i
case v1.ImageIndex:
im, err := i.IndexManifest()
if err != nil {
return nil, err
}
goos, goarch := os.Getenv("GOOS"), os.Getenv("GOARCH")
if goos == "" {
goos = "linux"
}
if goarch == "" {
goarch = "amd64"
}
for _, manifest := range im.Manifests {
if manifest.Platform == nil {
continue
}
if manifest.Platform.OS != goos {
continue
}
if manifest.Platform.Architecture != goarch {
continue
}
img, err = i.Image(manifest.Digest)
if err != nil {
return nil, err
}
break
}
if img == nil {
return nil, fmt.Errorf("failed to find %s/%s image in index for image: %v", goos, goarch, s)
}
default:
return nil, fmt.Errorf("failed to interpret %s result as image: %v", s, br)
}
h, err := img.Digest()
if err != nil {
return nil, err
}
digestTag, err := name.NewTag(fmt.Sprintf("%s:%s", t.namer(t.base, s), h.Hex))
if err != nil {
return nil, err
}
log.Printf("Loading %v", digestTag)
if err := kind.Write(ctx, digestTag, img); err != nil {
return nil, err
}
log.Printf("Loaded %v", digestTag)
for _, tagName := range t.tags {
log.Printf("Adding tag %v", tagName)
tag, err := name.NewTag(fmt.Sprintf("%s:%s", t.namer(t.base, s), tagName))
if err != nil {
return nil, err
}
if err := kind.Tag(ctx, digestTag, tag); err != nil {
return nil, err
}
log.Printf("Added tag %v", tagName)
}
return &digestTag, nil
}
func (t *kindPublisher) Close() error {
return nil
}