Skip to content

Commit

Permalink
engine: write ImageMaps to the apiserver (#4677)
Browse files Browse the repository at this point in the history
  • Loading branch information
nicks committed Jun 23, 2021
1 parent 13dfbd1 commit 638da06
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 1 deletion.
45 changes: 45 additions & 0 deletions internal/engine/configs/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (

"github.com/tilt-dev/tilt/internal/controllers/apicmp"
"github.com/tilt-dev/tilt/internal/tiltfile"
"github.com/tilt-dev/tilt/pkg/apis"
"github.com/tilt-dev/tilt/pkg/apis/core/v1alpha1"
)

Expand Down Expand Up @@ -67,6 +68,7 @@ func updateOwnedObjects(ctx context.Context, client ctrlclient.Client, tlr tiltf
func toAPIObjects(tlr tiltfile.TiltfileLoadResult) objectSet {
result := objectSet{}
result[(&v1alpha1.KubernetesApply{}).GetGroupVersionResource()] = toKubernetesApplyObjects(tlr)
result[(&v1alpha1.ImageMap{}).GetGroupVersionResource()] = toImageMapObjects(tlr)
return result
}

Expand Down Expand Up @@ -98,9 +100,35 @@ func toKubernetesApplyObjects(tlr tiltfile.TiltfileLoadResult) typedObjectSet {
return result
}

// Pulls out all the ImageMap objects generated by the Tiltfile.
func toImageMapObjects(tlr tiltfile.TiltfileLoadResult) typedObjectSet {
result := typedObjectSet{}

for _, m := range tlr.Manifests {
for _, iTarget := range m.ImageTargets {
name := apis.SanitizeName(iTarget.ID().Name.String())
// Note that an ImageMap might be in more than one Manifest, so we
// can't annotate them to a particular manifest.
im := &v1alpha1.ImageMap{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Labels: map[string]string{
LabelOwnerKind: LabelOwnerKindTiltfile,
},
},
Spec: iTarget.ImageMapSpec,
}
result[name] = im
}
}
return result
}

// Fetch all the existing API objects that were generated from the Tiltfile.
func getExistingAPIObjects(ctx context.Context, client ctrlclient.Client) (objectSet, error) {
result := objectSet{}

// TODO(nick): There's got to be a more generic way to do this.
var kaList v1alpha1.KubernetesApplyList
err := client.List(ctx, &kaList, &ctrlclient.ListOptions{LabelSelector: ownerSelector})
if err != nil {
Expand All @@ -113,6 +141,20 @@ func getExistingAPIObjects(ctx context.Context, client ctrlclient.Client) (objec
kaMap[item.Name] = &item
}
result[(&v1alpha1.KubernetesApply{}).GetGroupVersionResource()] = kaMap

var imList v1alpha1.ImageMapList
err = client.List(ctx, &imList, &ctrlclient.ListOptions{LabelSelector: ownerSelector})
if err != nil {
return nil, err
}

imMap := typedObjectSet{}
for _, item := range imList.Items {
item := item
imMap[item.Name] = &item
}
result[(&v1alpha1.ImageMap{}).GetGroupVersionResource()] = imMap

return result, nil
}

Expand All @@ -138,6 +180,9 @@ func updateObjects(ctx context.Context, client ctrlclient.Client, newObjects, ol
continue
}

// Are there other fields here we should check?
// e.g., once labels are generated from the Tiltfile, it seems
// we should also update the labels when they change.
if !apicmp.DeepEqual(old.GetSpec(), obj.GetSpec()) {
obj.SetResourceVersion(old.GetResourceVersion())
err := client.Update(ctx, obj)
Expand Down
21 changes: 21 additions & 0 deletions internal/engine/configs/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/tilt-dev/tilt/internal/testutils/manifestbuilder"
"github.com/tilt-dev/tilt/internal/testutils/tempdir"
"github.com/tilt-dev/tilt/internal/tiltfile"
"github.com/tilt-dev/tilt/pkg/apis"
"github.com/tilt-dev/tilt/pkg/apis/core/v1alpha1"
"github.com/tilt-dev/tilt/pkg/model"
)
Expand Down Expand Up @@ -78,3 +79,23 @@ func TestAPIUpdate(t *testing.T) {
assert.NoError(t, err)
assert.Contains(t, ka.Spec.YAML, "sidecar")
}

func TestImageMapCreate(t *testing.T) {
f := tempdir.NewTempDirFixture(t)
defer f.TearDown()

ctx := context.Background()
c := fake.NewTiltClient()
fe := manifestbuilder.New(f, "fe").
WithImageTarget(NewSanchoDockerBuildImageTarget(f)).
WithK8sYAML(testyaml.SanchoYAML).
Build()
err := updateOwnedObjects(ctx, c, tiltfile.TiltfileLoadResult{Manifests: []model.Manifest{fe}})
assert.NoError(t, err)

name := apis.SanitizeName(SanchoRef.String())

var im v1alpha1.ImageMap
assert.NoError(t, c.Get(ctx, types.NamespacedName{Name: name}, &im))
assert.Contains(t, im.Spec.Selector, SanchoRef.String())
}
6 changes: 5 additions & 1 deletion internal/engine/configs/configs_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,11 @@ ENTRYPOINT /go/bin/sancho

var SanchoRef = container.MustParseSelector(testyaml.SanchoImage)

func NewSanchoDockerBuildImageTarget(f *ccFixture) model.ImageTarget {
type pathFixture interface {
Path() string
}

func NewSanchoDockerBuildImageTarget(f pathFixture) model.ImageTarget {
return model.MustNewImageTarget(SanchoRef).WithBuildDetails(model.DockerBuild{
Dockerfile: SanchoDockerfile,
BuildPath: f.Path(),
Expand Down
4 changes: 4 additions & 0 deletions pkg/apis/core/v1alpha1/imagemap_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,10 @@ type ImageMapOverrideArgs struct {
var _ resource.Object = &ImageMap{}
var _ resourcestrategy.Validater = &ImageMap{}

func (in *ImageMap) GetSpec() interface{} {
return &in.Spec
}

func (in *ImageMap) GetObjectMeta() *metav1.ObjectMeta {
return &in.ObjectMeta
}
Expand Down

0 comments on commit 638da06

Please sign in to comment.