@@ -11,6 +11,7 @@ import (
11
11
"github.com/moby/buildkit/client/llb"
12
12
gwclient "github.com/moby/buildkit/frontend/gateway/client"
13
13
ocispecs "github.com/opencontainers/image-spec/specs-go/v1"
14
+ "github.com/pkg/errors"
14
15
)
15
16
16
17
func handleRPM (w worker ) gwclient.BuildFunc {
@@ -26,7 +27,7 @@ func handleRPM(w worker) gwclient.BuildFunc {
26
27
return nil , nil , err
27
28
}
28
29
29
- st , err := specToRpmLLB (ctx , w , client , spec , sOpt , targetKey , pg , dalec . WithPlatform ( platform ) )
30
+ st , err := buildOutputRPM (ctx , w , client , spec , sOpt , targetKey , platform , pg )
30
31
if err != nil {
31
32
return nil , nil , err
32
33
}
@@ -53,7 +54,7 @@ func handleRPM(w worker) gwclient.BuildFunc {
53
54
}
54
55
55
56
// Creates and installs an rpm meta-package that requires the passed in deps as runtime-dependencies
56
- func installBuildDepsPackage (target string , packageName string , w worker , deps map [string ]dalec.PackageConstraints , installOpts ... installOpt ) installFunc {
57
+ func installBuildDepsPackage (target string , packageName string , w worker , sOpt dalec. SourceOpts , deps map [string ]dalec.PackageConstraints , platform * ocispecs. Platform , installOpts ... installOpt ) installFunc {
57
58
// depsOnly is a simple dalec spec that only includes build dependencies and their constraints
58
59
depsOnly := dalec.Spec {
59
60
Name : fmt .Sprintf ("%s-build-dependencies" , packageName ),
@@ -66,11 +67,11 @@ func installBuildDepsPackage(target string, packageName string, w worker, deps m
66
67
},
67
68
}
68
69
69
- return func (ctx context.Context , client gwclient. Client , sOpt dalec.SourceOpts ) (llb.RunOption , error ) {
70
+ return func (ctx context.Context , Opt dalec.SourceOpts ) (llb.RunOption , error ) {
70
71
pg := dalec .ProgressGroup ("Building container for build dependencies" )
71
72
72
73
// create an RPM with just the build dependencies, using our same base worker
73
- rpmDir , err := specToRpmLLB (ctx , w , client , & depsOnly , sOpt , target , pg )
74
+ rpmDir , err := createRPM (ctx , w , sOpt , & depsOnly , target , platform , pg )
74
75
if err != nil {
75
76
return nil , err
76
77
}
@@ -91,20 +92,15 @@ func installBuildDepsPackage(target string, packageName string, w worker, deps m
91
92
}
92
93
}
93
94
94
- func installBuildDeps (ctx context.Context , w worker , client gwclient. Client , spec * dalec.Spec , targetKey string , opts ... llb.ConstraintsOpt ) (llb.StateOption , error ) {
95
+ func installBuildDeps (ctx context.Context , w worker , sOpt dalec. SourceOpts , spec * dalec.Spec , targetKey string , platform * ocispecs. Platform , opts ... llb.ConstraintsOpt ) (llb.StateOption , error ) {
95
96
deps := spec .GetBuildDeps (targetKey )
96
97
if len (deps ) == 0 {
97
98
return func (in llb.State ) llb.State { return in }, nil
98
99
}
99
100
100
- sOpt , err := frontend .SourceOptFromClient (ctx , client )
101
- if err != nil {
102
- return nil , err
103
- }
104
-
105
101
opts = append (opts , dalec .ProgressGroup ("Install build deps" ))
106
102
107
- installOpt , err := installBuildDepsPackage (targetKey , spec .Name , w , deps , installWithConstraints (opts ))(ctx , client , sOpt )
103
+ installOpt , err := installBuildDepsPackage (targetKey , spec .Name , w , sOpt , deps , platform , installWithConstraints (opts ))(ctx , sOpt )
108
104
if err != nil {
109
105
return nil , err
110
106
}
@@ -114,24 +110,58 @@ func installBuildDeps(ctx context.Context, w worker, client gwclient.Client, spe
114
110
}, nil
115
111
}
116
112
117
- func specToRpmLLB (ctx context.Context , w worker , client gwclient.Client , spec * dalec.Spec , sOpt dalec.SourceOpts , targetKey string , opts ... llb.ConstraintsOpt ) (llb.State , error ) {
118
- base , err := w .Base (sOpt , opts ... )
113
+ func createBuildroot (ctx context.Context , w worker , sOpt dalec.SourceOpts , spec * dalec.Spec , targetKey string , opts ... llb.ConstraintsOpt ) (llb.State , error ) {
114
+ opts = append (opts , dalec .ProgressGroup ("Prepare rpm build root" ))
115
+
116
+ // Always generate the build root usign the native platform
117
+ // There is nothing it does that should require the requested target platform
118
+ native , err := w .Base (sOpt , opts ... )
119
119
if err != nil {
120
120
return llb .Scratch (), err
121
121
}
122
122
123
- installOpt , err := installBuildDeps (ctx , w , client , spec , targetKey , opts ... )
123
+ if spec .HasGomods () {
124
+ // Since the spec has go mods in it, we need to make sure we have go
125
+ // installed in the image.
126
+ nativeInstallDeps , err := installBuildDeps (ctx , w , sOpt , spec , targetKey , nil , opts ... )
127
+ if err != nil {
128
+ return llb .Scratch (), err
129
+ }
130
+
131
+ native = native .With (nativeInstallDeps )
132
+ }
133
+
134
+ return rpm .SpecToBuildrootLLB (native , spec , sOpt , targetKey , opts ... )
135
+ }
136
+
137
+ func createRPM (ctx context.Context , w worker , sOpt dalec.SourceOpts , spec * dalec.Spec , targetKey string , platform * ocispecs.Platform , opts ... llb.ConstraintsOpt ) (llb.State , error ) {
138
+ br , err := createBuildroot (ctx , w , sOpt , spec , targetKey , opts ... )
139
+ if err != nil {
140
+ return llb .Scratch (), errors .Wrap (err , "error creating rpm build root" )
141
+ }
142
+
143
+ specPath := filepath .Join ("SPECS" , spec .Name , spec .Name + ".spec" )
144
+
145
+ // Build the RPM with the target platform
146
+ opts = append (opts , dalec .WithPlatform (platform ))
147
+ base , err := w .Base (sOpt , opts ... )
124
148
if err != nil {
125
149
return llb .Scratch (), err
126
150
}
127
- base = base .With (installOpt )
128
151
129
- br , err := rpm . SpecToBuildrootLLB ( base , spec , sOpt , targetKey , opts ... )
152
+ installDeps , err := installBuildDeps ( ctx , w , sOpt , spec , targetKey , platform , opts ... )
130
153
if err != nil {
131
154
return llb .Scratch (), err
132
155
}
133
- specPath := filepath .Join ("SPECS" , spec .Name , spec .Name + ".spec" )
134
- st := rpm .Build (br , base , specPath , opts ... )
135
156
157
+ base = base .With (installDeps )
158
+ return rpm .Build (br , base , specPath , opts ... ), nil
159
+ }
160
+
161
+ func buildOutputRPM (ctx context.Context , w worker , client gwclient.Client , spec * dalec.Spec , sOpt dalec.SourceOpts , targetKey string , platform * ocispecs.Platform , opts ... llb.ConstraintsOpt ) (llb.State , error ) {
162
+ st , err := createRPM (ctx , w , sOpt , spec , targetKey , platform , opts ... )
163
+ if err != nil {
164
+ return llb .Scratch (), err
165
+ }
136
166
return frontend .MaybeSign (ctx , client , st , spec , targetKey , sOpt )
137
167
}
0 commit comments