forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
strategyref.go
165 lines (144 loc) · 4.73 KB
/
strategyref.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package app
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"github.com/openshift/origin/pkg/generate/dockerfile"
"github.com/openshift/origin/pkg/generate/errors"
"github.com/openshift/origin/pkg/generate/git"
"github.com/openshift/origin/pkg/generate/source"
)
// BuildStrategyRefGenerator generates BuildStrategyRef
//
// Flows for BuildStrategyRef
// SourceRef -> BuildStrategyRef
// SourceRef + Docker Context -> BuildStrategyRef
// Docker Context + Parent Image -> BuildStrategyRef
// STI Builder Image -> BuildStrategyRef
type BuildStrategyRefGenerator struct {
gitRepository git.Repository
dockerfileFinder dockerfile.Finder
dockerfileParser dockerfile.Parser
sourceDetectors source.Detectors
imageRefGenerator ImageRefGenerator
}
// NewBuildStrategyRefGenerator creates a BuildStrategyRefGenerator
func NewBuildStrategyRefGenerator(sourceDetectors source.Detectors) *BuildStrategyRefGenerator {
return &BuildStrategyRefGenerator{
gitRepository: git.NewRepository(),
dockerfileFinder: dockerfile.NewFinder(),
dockerfileParser: dockerfile.NewParser(),
sourceDetectors: sourceDetectors,
imageRefGenerator: NewImageRefGenerator(),
}
}
// FromSourceRef creates a build strategy from a source reference
func (g *BuildStrategyRefGenerator) FromSourceRef(srcRef *SourceRef) (*BuildStrategyRef, error) {
// Download source locally first if not available
if len(srcRef.Dir) == 0 {
if err := g.getSource(srcRef); err != nil {
return nil, err
}
}
// Detect a Dockerfile
context, found, err := g.detectDockerFile(srcRef.Dir)
if err != nil {
return nil, err
}
if found {
return g.FromSourceRefAndDockerContext(srcRef, context)
}
// Detect a STI repository
sourceInfo, ok := g.sourceDetectors.DetectSource(srcRef.Dir)
if !ok {
return nil, errors.CouldNotDetect
}
builderImage, err := g.imageForSourceInfo(sourceInfo)
if err != nil {
return nil, err
}
return g.FromSTIBuilderImage(builderImage)
}
// FromSourceRefAndDockerContext generates a BuildStrategyRef from a source ref and context path
func (g *BuildStrategyRefGenerator) FromSourceRefAndDockerContext(srcRef *SourceRef, context string) (*BuildStrategyRef, error) {
// Download source locally first if not available
if len(srcRef.Dir) == 0 {
if err := g.getSource(srcRef); err != nil {
return nil, err
}
}
if len(context) > 0 {
srcRef.ContextDir = context
}
// Look for Dockerfile in repository
file, err := os.Open(filepath.Join(srcRef.Dir, context, "Dockerfile"))
if err != nil {
return nil, err
}
dockerFile, err := g.dockerfileParser.Parse(file)
if err != nil {
return nil, err
}
parentImageName, ok := dockerFile.GetDirective("FROM")
if !ok {
return nil, errors.InvalidDockerfile
}
ports, _ := dockerFile.GetDirective("EXPOSE")
parentRef, err := g.imageRefGenerator.FromNameAndPorts(parentImageName[0], ports)
if err != nil {
return nil, err
}
return g.FromDockerContextAndParent(parentRef)
}
// FromDockerContextAndParent generates a build strategy ref from a context path and parent image name
func (g *BuildStrategyRefGenerator) FromDockerContextAndParent(parentRef *ImageRef) (*BuildStrategyRef, error) {
return &BuildStrategyRef{
IsDockerBuild: true,
Base: parentRef,
}, nil
}
// FromSTIBuilderImage generates a build strategy from a builder image ref
func (g *BuildStrategyRefGenerator) FromSTIBuilderImage(image *ImageRef) (*BuildStrategyRef, error) {
return &BuildStrategyRef{
IsDockerBuild: false,
Base: image,
}, nil
}
func (g *BuildStrategyRefGenerator) imageForSourceInfo(s *source.Info) (*ImageRef, error) {
// TODO: More sophisticated matching
imageName := BuilderForPlatform(s.Platform)
if len(imageName) == 0 {
return nil, errors.NoBuilderFound
}
return g.imageRefGenerator.FromName(imageName)
}
func (g *BuildStrategyRefGenerator) detectDockerFile(dir string) (contextDir string, found bool, err error) {
dockerFiles, err := g.dockerfileFinder.Find(dir)
if err != nil {
return "", false, err
}
if len(dockerFiles) > 1 {
return "", true, errors.NewMultipleDockerfilesErr(dockerFiles)
}
if len(dockerFiles) == 1 {
return filepath.Dir(dockerFiles[0]), true, nil
}
return "", false, nil
}
func (g *BuildStrategyRefGenerator) getSource(srcRef *SourceRef) error {
var err error
// Clone git repository into a local directory
if srcRef.Dir, err = ioutil.TempDir("", "gen"); err != nil {
return err
}
if err = g.gitRepository.Clone(srcRef.Dir, srcRef.URL.String()); err != nil {
return fmt.Errorf("unable to clone repository at %s", srcRef.URL.String())
}
if len(srcRef.Ref) != 0 {
if err = g.gitRepository.Checkout(srcRef.Dir, srcRef.Ref); err != nil {
return fmt.Errorf("unable to checkout reference %s from repository at %s", srcRef.Ref, srcRef.URL.String())
}
}
return nil
}