Skip to content

Commit 40121c6

Browse files
committed
kubernetes: store config files for k8s
Signed-off-by: Tonis Tiigi <[email protected]>
1 parent 4c1621c commit 40121c6

File tree

3 files changed

+56
-28
lines changed

3 files changed

+56
-28
lines changed

driver/kubernetes/driver.go

+9-9
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ type Driver struct {
4141
factory driver.Factory
4242
minReplicas int
4343
deployment *appsv1.Deployment
44-
configMap *corev1.ConfigMap
44+
configMaps []*corev1.ConfigMap
4545
clientset *kubernetes.Clientset
4646
deploymentClient clientappsv1.DeploymentInterface
4747
podClient clientcorev1.PodInterface
@@ -65,16 +65,16 @@ func (d *Driver) Bootstrap(ctx context.Context, l progress.Logger) error {
6565
return errors.Wrapf(err, "error for bootstrap %q", d.deployment.Name)
6666
}
6767

68-
if d.configMap != nil {
68+
for _, cfg := range d.configMaps {
6969
// create ConfigMap first if exists
70-
_, err = d.configMapClient.Create(ctx, d.configMap, metav1.CreateOptions{})
70+
_, err = d.configMapClient.Create(ctx, cfg, metav1.CreateOptions{})
7171
if err != nil {
7272
if !apierrors.IsAlreadyExists(err) {
73-
return errors.Wrapf(err, "error while calling configMapClient.Create for %q", d.configMap.Name)
73+
return errors.Wrapf(err, "error while calling configMapClient.Create for %q", cfg.Name)
7474
}
75-
_, err = d.configMapClient.Update(ctx, d.configMap, metav1.UpdateOptions{})
75+
_, err = d.configMapClient.Update(ctx, cfg, metav1.UpdateOptions{})
7676
if err != nil {
77-
return errors.Wrapf(err, "error while calling configMapClient.Update for %q", d.configMap.Name)
77+
return errors.Wrapf(err, "error while calling configMapClient.Update for %q", cfg.Name)
7878
}
7979
}
8080
}
@@ -171,10 +171,10 @@ func (d *Driver) Rm(ctx context.Context, force bool, rmVolume bool) error {
171171
return errors.Wrapf(err, "error while calling deploymentClient.Delete for %q", d.deployment.Name)
172172
}
173173
}
174-
if d.configMap != nil {
175-
if err := d.configMapClient.Delete(ctx, d.configMap.Name, metav1.DeleteOptions{}); err != nil {
174+
for _, cfg := range d.configMaps {
175+
if err := d.configMapClient.Delete(ctx, cfg.Name, metav1.DeleteOptions{}); err != nil {
176176
if !apierrors.IsNotFound(err) {
177-
return errors.Wrapf(err, "error while calling configMapClient.Delete for %q", d.configMap.Name)
177+
return errors.Wrapf(err, "error while calling configMapClient.Delete for %q", cfg.Name)
178178
}
179179
}
180180
}

driver/kubernetes/factory.go

+2-5
Original file line numberDiff line numberDiff line change
@@ -73,14 +73,11 @@ func (f *factory) New(ctx context.Context, cfg driver.InitConfig) (driver.Driver
7373
BuildkitFlags: cfg.BuildkitFlags,
7474
Rootless: false,
7575
Platforms: cfg.Platforms,
76+
ConfigFiles: cfg.Files,
7677
}
7778

7879
deploymentOpt.Qemu.Image = bkimage.QemuImage
7980

80-
if cfg, ok := cfg.Files["buildkitd.toml"]; ok {
81-
deploymentOpt.BuildkitConfig = cfg
82-
}
83-
8481
loadbalance := LoadbalanceSticky
8582

8683
for k, v := range cfg.DriverOpts {
@@ -142,7 +139,7 @@ func (f *factory) New(ctx context.Context, cfg driver.InitConfig) (driver.Driver
142139
}
143140
}
144141

145-
d.deployment, d.configMap, err = manifest.NewDeployment(deploymentOpt)
142+
d.deployment, d.configMaps, err = manifest.NewDeployment(deploymentOpt)
146143
if err != nil {
147144
return nil, err
148145
}

driver/kubernetes/manifest/manifest.go

+45-14
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package manifest
22

33
import (
4+
"fmt"
5+
"path"
46
"strings"
57

68
"github.com/docker/buildx/util/platformutil"
@@ -25,9 +27,8 @@ type DeploymentOpt struct {
2527
}
2628

2729
BuildkitFlags []string
28-
// BuildkitConfig
29-
// when not empty, will create configmap with buildkit.toml and mounted
30-
BuildkitConfig []byte
30+
// files mounted at /etc/buildkitd
31+
ConfigFiles map[string][]byte
3132

3233
Rootless bool
3334
NodeSelector map[string]string
@@ -43,7 +44,7 @@ const (
4344
AnnotationPlatform = "buildx.docker.com/platform"
4445
)
4546

46-
func NewDeployment(opt *DeploymentOpt) (d *appsv1.Deployment, c *corev1.ConfigMap, err error) {
47+
func NewDeployment(opt *DeploymentOpt) (d *appsv1.Deployment, c []*corev1.ConfigMap, err error) {
4748
labels := map[string]string{
4849
"app": opt.Name,
4950
}
@@ -103,38 +104,36 @@ func NewDeployment(opt *DeploymentOpt) (d *appsv1.Deployment, c *corev1.ConfigMa
103104
},
104105
},
105106
}
106-
107-
if len(opt.BuildkitConfig) > 0 {
108-
c = &corev1.ConfigMap{
107+
for _, cfg := range splitConfigFiles(opt.ConfigFiles) {
108+
cc := &corev1.ConfigMap{
109109
TypeMeta: metav1.TypeMeta{
110110
APIVersion: corev1.SchemeGroupVersion.String(),
111111
Kind: "ConfigMap",
112112
},
113113
ObjectMeta: metav1.ObjectMeta{
114114
Namespace: opt.Namespace,
115-
Name: opt.Name + "-config",
115+
Name: opt.Name + "-" + cfg.name,
116116
Annotations: annotations,
117117
},
118-
Data: map[string]string{
119-
"buildkitd.toml": string(opt.BuildkitConfig),
120-
},
118+
Data: cfg.files,
121119
}
122120

123121
d.Spec.Template.Spec.Containers[0].VolumeMounts = []corev1.VolumeMount{{
124-
Name: "config",
125-
MountPath: "/etc/buildkit",
122+
Name: cfg.name,
123+
MountPath: path.Join("/etc/buildkit", cfg.path),
126124
}}
127125

128126
d.Spec.Template.Spec.Volumes = []corev1.Volume{{
129127
Name: "config",
130128
VolumeSource: corev1.VolumeSource{
131129
ConfigMap: &corev1.ConfigMapVolumeSource{
132130
LocalObjectReference: corev1.LocalObjectReference{
133-
Name: c.Name,
131+
Name: cc.Name,
134132
},
135133
},
136134
},
137135
}}
136+
c = append(c, cc)
138137
}
139138

140139
if opt.Qemu.Install {
@@ -208,3 +207,35 @@ func toRootless(d *appsv1.Deployment) error {
208207
d.Spec.Template.ObjectMeta.Annotations["container.seccomp.security.alpha.kubernetes.io/"+containerName] = "unconfined"
209208
return nil
210209
}
210+
211+
type config struct {
212+
name string
213+
path string
214+
files map[string]string
215+
}
216+
217+
func splitConfigFiles(m map[string][]byte) []config {
218+
var c []config
219+
idx := map[string]int{}
220+
nameIdx := 0
221+
for k, v := range m {
222+
dir := path.Dir(k)
223+
i, ok := idx[dir]
224+
if !ok {
225+
idx[dir] = len(c)
226+
i = len(c)
227+
name := "config"
228+
if dir != "." {
229+
nameIdx++
230+
name = fmt.Sprintf("%s-%d", name, nameIdx)
231+
}
232+
c = append(c, config{
233+
path: dir,
234+
name: name,
235+
files: map[string]string{},
236+
})
237+
}
238+
c[i].files[path.Base(k)] = string(v)
239+
}
240+
return c
241+
}

0 commit comments

Comments
 (0)