-
Notifications
You must be signed in to change notification settings - Fork 404
/
config.go
91 lines (78 loc) · 2.62 KB
/
config.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
// Copyright 2018 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 (
"fmt"
"log"
"os"
"strconv"
"time"
"github.com/google/go-containerregistry/pkg/authn"
"github.com/google/go-containerregistry/pkg/name"
v1 "github.com/google/go-containerregistry/pkg/v1"
"github.com/google/go-containerregistry/pkg/v1/remote"
"github.com/spf13/viper"
)
var (
defaultBaseImage name.Reference
baseImageOverrides map[string]name.Reference
)
func getBaseImage(s string) (v1.Image, error) {
ref, ok := baseImageOverrides[s]
if !ok {
ref = defaultBaseImage
}
log.Printf("Using base %s for %s", ref, s)
return remote.Image(ref, remote.WithAuthFromKeychain(authn.DefaultKeychain))
}
func getCreationTime() (*v1.Time, error) {
epoch := os.Getenv("SOURCE_DATE_EPOCH")
if epoch == "" {
return nil, nil
}
seconds, err := strconv.ParseInt(epoch, 10, 64)
if err != nil {
return nil, fmt.Errorf("the environment variable SOURCE_DATE_EPOCH should be the number of seconds since January 1st 1970, 00:00 UTC, got: %v", err)
}
return &v1.Time{time.Unix(seconds, 0)}, nil
}
func init() {
// If omitted, use this base image.
viper.SetDefault("defaultBaseImage", "gcr.io/distroless/static:latest")
viper.SetConfigName(".ko") // .yaml is implicit
if override := os.Getenv("KO_CONFIG_PATH"); override != "" {
viper.AddConfigPath(override)
}
viper.AddConfigPath("./")
if err := viper.ReadInConfig(); err != nil {
if _, ok := err.(viper.ConfigFileNotFoundError); !ok {
log.Fatalf("error reading config file: %v", err)
}
}
ref := viper.GetString("defaultBaseImage")
dbi, err := name.ParseReference(ref, name.WeakValidation)
if err != nil {
log.Fatalf("'defaultBaseImage': error parsing %q as image reference: %v", ref, err)
}
defaultBaseImage = dbi
baseImageOverrides = make(map[string]name.Reference)
overrides := viper.GetStringMapString("baseImageOverrides")
for k, v := range overrides {
bi, err := name.ParseReference(v, name.WeakValidation)
if err != nil {
log.Fatalf("'baseImageOverrides': error parsing %q as image reference: %v", v, err)
}
baseImageOverrides[k] = bi
}
}