-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
Copy pathdebug.go
140 lines (123 loc) · 4.8 KB
/
debug.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
131
132
133
134
135
136
137
138
139
140
/*
Copyright 2019 The Skaffold Authors
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 debug
import (
"bufio"
"bytes"
"context"
"fmt"
"strings"
"github.com/sirupsen/logrus"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/serializer/json"
"k8s.io/client-go/kubernetes/scheme"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/docker"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/graph"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/kubernetes/manifest"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/runner/runcontext"
)
var (
decodeFromYaml = scheme.Codecs.UniversalDeserializer().Decode
encodeAsYaml = func(o runtime.Object) ([]byte, error) {
s := json.NewYAMLSerializer(json.DefaultMetaFactory, scheme.Scheme, scheme.Scheme)
var b bytes.Buffer
w := bufio.NewWriter(&b)
if err := s.Encode(o, w); err != nil {
return nil, err
}
w.Flush()
return b.Bytes(), nil
}
)
// ApplyDebuggingTransforms applies language-platform-specific transforms to a list of manifests.
func ApplyDebuggingTransforms(l manifest.ManifestList, builds []graph.Artifact, registries manifest.Registries) (manifest.ManifestList, error) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
retriever := func(image string) (imageConfiguration, error) {
if artifact := findArtifact(image, builds); artifact != nil {
return retrieveImageConfiguration(ctx, artifact, registries.InsecureRegistries)
}
return imageConfiguration{}, fmt.Errorf("no build artifact for %q", image)
}
return applyDebuggingTransforms(l, retriever, registries.DebugHelpersRegistry)
}
func applyDebuggingTransforms(l manifest.ManifestList, retriever configurationRetriever, debugHelpersRegistry string) (manifest.ManifestList, error) {
var updated manifest.ManifestList
for _, manifest := range l {
obj, _, err := decodeFromYaml(manifest, nil, nil)
if err != nil {
logrus.Debugf("Unable to interpret manifest for debugging: %v\n", err)
} else if transformManifest(obj, retriever, debugHelpersRegistry) {
manifest, err = encodeAsYaml(obj)
if err != nil {
return nil, fmt.Errorf("marshalling yaml: %w", err)
}
if logrus.IsLevelEnabled(logrus.DebugLevel) {
logrus.Debugln("Applied debugging transform:\n", string(manifest))
}
}
updated = append(updated, manifest)
}
return updated, nil
}
// findArtifact finds the corresponding artifact for the given image.
// If `builds` is empty, then treat all `image` images as a build artifact.
func findArtifact(image string, builds []graph.Artifact) *graph.Artifact {
if len(builds) == 0 {
logrus.Debugf("No build artifacts specified: using image as-is %q", image)
return &graph.Artifact{ImageName: image, Tag: image}
}
for _, artifact := range builds {
if image == artifact.ImageName || image == artifact.Tag {
logrus.Debugf("Found artifact for image %q", image)
return &artifact
}
}
return nil
}
// retrieveImageConfiguration retrieves the image container configuration for
// the given build artifact
func retrieveImageConfiguration(ctx context.Context, artifact *graph.Artifact, insecureRegistries map[string]bool) (imageConfiguration, error) {
// TODO: use the proper RunContext
apiClient, err := docker.NewAPIClient(&runcontext.RunContext{
InsecureRegistries: insecureRegistries,
})
if err != nil {
return imageConfiguration{}, fmt.Errorf("could not connect to local docker daemon: %w", err)
}
// the apiClient will go to the remote registry if local docker daemon is not available
manifest, err := apiClient.ConfigFile(ctx, artifact.Tag)
if err != nil {
logrus.Debugf("Error retrieving image manifest for %v: %v", artifact.Tag, err)
return imageConfiguration{}, fmt.Errorf("retrieving image config for %q: %w", artifact.Tag, err)
}
config := manifest.Config
logrus.Debugf("Retrieved local image configuration for %v: %v", artifact.Tag, config)
return imageConfiguration{
artifact: artifact.ImageName,
env: envAsMap(config.Env),
entrypoint: config.Entrypoint,
arguments: config.Cmd,
labels: config.Labels,
workingDir: config.WorkingDir,
}, nil
}
// envAsMap turns an array of environment "NAME=value" strings into a map
func envAsMap(env []string) map[string]string {
result := make(map[string]string)
for _, pair := range env {
s := strings.SplitN(pair, "=", 2)
result[s[0]] = s[1]
}
return result
}