Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

api: indexing for DockerImage/CmdImage #5665

Merged
merged 4 commits into from
Apr 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions internal/cli/wire_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

46 changes: 37 additions & 9 deletions internal/controllers/core/cmdimage/reconciler.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,37 +3,45 @@ package cmdimage
import (
"context"

"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/controller-runtime/pkg/builder"
"sigs.k8s.io/controller-runtime/pkg/handler"
"sigs.k8s.io/controller-runtime/pkg/source"

apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/types"
ctrl "sigs.k8s.io/controller-runtime"
ctrlclient "sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/reconcile"

"github.com/tilt-dev/tilt/internal/build"
"github.com/tilt-dev/tilt/internal/controllers/core/dockerimage"
"github.com/tilt-dev/tilt/internal/controllers/indexer"
"github.com/tilt-dev/tilt/internal/docker"
"github.com/tilt-dev/tilt/internal/store"
"github.com/tilt-dev/tilt/pkg/apis"
"github.com/tilt-dev/tilt/pkg/apis/core/v1alpha1"
"github.com/tilt-dev/tilt/pkg/model"
)

var clusterGVK = v1alpha1.SchemeGroupVersion.WithKind("Cluster")

// Manages the CmdImage API object.
type Reconciler struct {
client ctrlclient.Client
docker docker.Client
ib *build.ImageBuilder
client ctrlclient.Client
indexer *indexer.Indexer
docker docker.Client
ib *build.ImageBuilder
}

var _ reconcile.Reconciler = &Reconciler{}

func NewReconciler(client ctrlclient.Client, docker docker.Client, ib *build.ImageBuilder) *Reconciler {
func NewReconciler(client ctrlclient.Client, scheme *runtime.Scheme, docker docker.Client, ib *build.ImageBuilder) *Reconciler {
return &Reconciler{
client: client,
docker: docker,
ib: ib,
client: client,
indexer: indexer.NewIndexer(scheme, indexCmdImage),
docker: docker,
ib: ib,
}
}

Expand All @@ -43,6 +51,7 @@ func (r *Reconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Resu
if err != nil && !apierrors.IsNotFound(err) {
return ctrl.Result{}, err
}
r.indexer.OnReconcile(req.NamespacedName, obj)

if apierrors.IsNotFound(err) || obj.ObjectMeta.DeletionTimestamp != nil {
return ctrl.Result{}, nil
Expand Down Expand Up @@ -84,7 +93,26 @@ func (r *Reconciler) ForceApply(

func (r *Reconciler) CreateBuilder(mgr ctrl.Manager) (*builder.Builder, error) {
b := ctrl.NewControllerManagedBy(mgr).
For(&v1alpha1.CmdImage{})
For(&v1alpha1.CmdImage{}).
Watches(&source.Kind{Type: &v1alpha1.Cluster{}},
handler.EnqueueRequestsFromMapFunc(r.indexer.Enqueue))

return b, nil
}

func indexCmdImage(obj ctrlclient.Object) []indexer.Key {
var keys []indexer.Key

ci := obj.(*v1alpha1.CmdImage)
if ci != nil && ci.Spec.Cluster != "" {
keys = append(keys, indexer.Key{
Name: types.NamespacedName{
Namespace: obj.GetNamespace(),
Name: ci.Spec.Cluster,
},
GVK: clusterGVK,
})
}

return keys
}
59 changes: 59 additions & 0 deletions internal/controllers/core/cmdimage/reconciler_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package cmdimage

import (
"testing"

"github.com/jonboulle/clockwork"
"github.com/stretchr/testify/require"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/controller-runtime/pkg/reconcile"

"github.com/tilt-dev/tilt/internal/build"
"github.com/tilt-dev/tilt/internal/controllers/fake"
"github.com/tilt-dev/tilt/internal/docker"
"github.com/tilt-dev/tilt/pkg/apis/core/v1alpha1"
)

func TestIndexCluster(t *testing.T) {
f := newFixture(t)

f.Create(&v1alpha1.CmdImage{
ObjectMeta: metav1.ObjectMeta{
Name: "my-image",
},
Spec: v1alpha1.CmdImageSpec{
Cluster: "cluster",
},
})

reqs := f.r.indexer.Enqueue(&v1alpha1.Cluster{ObjectMeta: metav1.ObjectMeta{Name: "cluster"}})
require.ElementsMatch(t, []reconcile.Request{
{NamespacedName: types.NamespacedName{Name: "my-image"}},
}, reqs, "Index result for known cluster")

reqs = f.r.indexer.Enqueue(&v1alpha1.Cluster{ObjectMeta: metav1.ObjectMeta{Name: "other"}})
require.Empty(t, reqs, "Index result for unknown cluster")
}

type fixture struct {
*fake.ControllerFixture
r *Reconciler
}

func newFixture(t testing.TB) *fixture {
cfb := fake.NewControllerFixtureBuilder(t)

clock := clockwork.NewFakeClock()
dockerCli := docker.NewFakeClient()
ib := build.NewImageBuilder(
build.NewDockerBuilder(dockerCli, nil),
build.NewCustomBuilder(dockerCli, clock),
build.NewKINDLoader())

r := NewReconciler(cfb.Client, cfb.Scheme(), docker.NewFakeClient(), ib)
return &fixture{
ControllerFixture: cfb.Build(r),
r: r,
}
}
46 changes: 37 additions & 9 deletions internal/controllers/core/dockerimage/reconciler.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,36 +3,44 @@ package dockerimage
import (
"context"

"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/controller-runtime/pkg/builder"
"sigs.k8s.io/controller-runtime/pkg/handler"
"sigs.k8s.io/controller-runtime/pkg/source"

apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/types"
ctrl "sigs.k8s.io/controller-runtime"
ctrlclient "sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/reconcile"

"github.com/tilt-dev/tilt/internal/build"
"github.com/tilt-dev/tilt/internal/controllers/indexer"
"github.com/tilt-dev/tilt/internal/docker"
"github.com/tilt-dev/tilt/internal/store"
"github.com/tilt-dev/tilt/pkg/apis"
"github.com/tilt-dev/tilt/pkg/apis/core/v1alpha1"
"github.com/tilt-dev/tilt/pkg/model"
)

var clusterGVK = v1alpha1.SchemeGroupVersion.WithKind("Cluster")

// Manages the DockerImage API object.
type Reconciler struct {
client ctrlclient.Client
docker docker.Client
ib *build.ImageBuilder
client ctrlclient.Client
indexer *indexer.Indexer
docker docker.Client
ib *build.ImageBuilder
}

var _ reconcile.Reconciler = &Reconciler{}

func NewReconciler(client ctrlclient.Client, docker docker.Client, ib *build.ImageBuilder) *Reconciler {
func NewReconciler(client ctrlclient.Client, scheme *runtime.Scheme, docker docker.Client, ib *build.ImageBuilder) *Reconciler {
return &Reconciler{
client: client,
docker: docker,
ib: ib,
client: client,
indexer: indexer.NewIndexer(scheme, indexDockerImage),
docker: docker,
ib: ib,
}
}

Expand All @@ -42,6 +50,7 @@ func (r *Reconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Resu
if err != nil && !apierrors.IsNotFound(err) {
return ctrl.Result{}, err
}
r.indexer.OnReconcile(req.NamespacedName, obj)

if apierrors.IsNotFound(err) || obj.ObjectMeta.DeletionTimestamp != nil {
return ctrl.Result{}, nil
Expand Down Expand Up @@ -83,7 +92,26 @@ func (r *Reconciler) ForceApply(

func (r *Reconciler) CreateBuilder(mgr ctrl.Manager) (*builder.Builder, error) {
b := ctrl.NewControllerManagedBy(mgr).
For(&v1alpha1.DockerImage{})
For(&v1alpha1.DockerImage{}).
Watches(&source.Kind{Type: &v1alpha1.Cluster{}},
handler.EnqueueRequestsFromMapFunc(r.indexer.Enqueue))

return b, nil
}

func indexDockerImage(obj ctrlclient.Object) []indexer.Key {
var keys []indexer.Key

di := obj.(*v1alpha1.DockerImage)
if di != nil && di.Spec.Cluster != "" {
keys = append(keys, indexer.Key{
Name: types.NamespacedName{
Namespace: obj.GetNamespace(),
Name: di.Spec.Cluster,
},
GVK: clusterGVK,
})
}

return keys
}
59 changes: 59 additions & 0 deletions internal/controllers/core/dockerimage/reconciler_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package dockerimage

import (
"testing"

"github.com/jonboulle/clockwork"
"github.com/stretchr/testify/require"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/controller-runtime/pkg/reconcile"

"github.com/tilt-dev/tilt/internal/build"
"github.com/tilt-dev/tilt/internal/controllers/fake"
"github.com/tilt-dev/tilt/internal/docker"
"github.com/tilt-dev/tilt/pkg/apis/core/v1alpha1"
)

func TestIndexCluster(t *testing.T) {
f := newFixture(t)

f.Create(&v1alpha1.DockerImage{
ObjectMeta: metav1.ObjectMeta{
Name: "my-image",
},
Spec: v1alpha1.DockerImageSpec{
Cluster: "cluster",
},
})

reqs := f.r.indexer.Enqueue(&v1alpha1.Cluster{ObjectMeta: metav1.ObjectMeta{Name: "cluster"}})
require.ElementsMatch(t, []reconcile.Request{
{NamespacedName: types.NamespacedName{Name: "my-image"}},
}, reqs, "Index result for known cluster")

reqs = f.r.indexer.Enqueue(&v1alpha1.Cluster{ObjectMeta: metav1.ObjectMeta{Name: "other"}})
require.Empty(t, reqs, "Index result for unknown cluster")
}

type fixture struct {
*fake.ControllerFixture
r *Reconciler
}

func newFixture(t testing.TB) *fixture {
cfb := fake.NewControllerFixtureBuilder(t)

clock := clockwork.NewFakeClock()
dockerCli := docker.NewFakeClient()
ib := build.NewImageBuilder(
build.NewDockerBuilder(dockerCli, nil),
build.NewCustomBuilder(dockerCli, clock),
build.NewKINDLoader())

r := NewReconciler(cfb.Client, cfb.Scheme(), dockerCli, ib)
return &fixture{
ControllerFixture: cfb.Build(r),
r: r,
}
}
12 changes: 6 additions & 6 deletions internal/engine/buildcontrol/wire_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions internal/engine/upper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3357,8 +3357,8 @@ func newTestFixture(t *testing.T, options ...fixtureOptions) *testFixture {
customBuilder := build.NewCustomBuilder(dockerClient, clock)
kp := build.NewKINDLoader()
ib := build.NewImageBuilder(dockerBuilder, customBuilder, kp)
dir := dockerimage.NewReconciler(cdc, dockerClient, ib)
cir := cmdimage.NewReconciler(cdc, dockerClient, ib)
dir := dockerimage.NewReconciler(cdc, sch, dockerClient, ib)
cir := cmdimage.NewReconciler(cdc, sch, dockerClient, ib)
clr := cluster.NewReconciler(ctx, cdc, st, clock, clusterClients, docker.LocalEnv{},
cluster.FakeDockerClientOrError(dockerClient, nil),
cluster.FakeKubernetesClientOrError(kClient, nil))
Expand Down
6 changes: 3 additions & 3 deletions internal/engine/wire_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.