Skip to content
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
16 changes: 8 additions & 8 deletions cmd/atc/flight_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func TestFlightInstance(t *testing.T) {
client, err := k8s.NewClientFromKubeConfig(home.Kubeconfig)
require.NoError(t, err)

flightIntf := k8s.TypedInterface[v1alpha1.Flight](client.Dynamic, v1alpha1.FlightGVR()).Namespace("default")
flightIntf := k8s.TypedInterface[v1alpha1.Flight](client, v1alpha1.FlightGVR()).Namespace("default")

toJSONString := func(t *testing.T, value any) string {
var buffer bytes.Buffer
Expand Down Expand Up @@ -132,7 +132,7 @@ func TestFlightCrossNamespace(t *testing.T) {
client, err := k8s.NewClientFromKubeConfig(home.Kubeconfig)
require.NoError(t, err)

flightIntf := k8s.TypedInterface[v1alpha1.Flight](client.Dynamic, v1alpha1.FlightGVR()).Namespace("default")
flightIntf := k8s.TypedInterface[v1alpha1.Flight](client, v1alpha1.FlightGVR()).Namespace("default")

_, err = flightIntf.Create(
context.Background(),
Expand All @@ -144,7 +144,7 @@ func TestFlightCrossNamespace(t *testing.T) {
)
require.ErrorContains(t, err, "Multiple namespaces detected (if desired enable multinamespace releases)")

clusterFlightIntf := k8s.TypedInterface[v1alpha1.ClusterFlight](client.Dynamic, v1alpha1.ClusterFlightGVR())
clusterFlightIntf := k8s.TypedInterface[v1alpha1.ClusterFlight](client, v1alpha1.ClusterFlightGVR())

// Crossnamespace depends on "foo" and "bar"
for _, ns := range []string{"foo", "bar"} {
Expand Down Expand Up @@ -205,7 +205,7 @@ func TestFlightInputObject(t *testing.T) {
client, err := k8s.NewClientFromKubeConfig(home.Kubeconfig)
require.NoError(t, err)

flightIntf := k8s.TypedInterface[v1alpha1.Flight](client.Dynamic, v1alpha1.FlightGVR()).Namespace("default")
flightIntf := k8s.TypedInterface[v1alpha1.Flight](client, v1alpha1.FlightGVR()).Namespace("default")

flight, err := flightIntf.Create(
context.Background(),
Expand Down Expand Up @@ -261,7 +261,7 @@ func TestFlightValidationWebhook(t *testing.T) {
client, err := k8s.NewClientFromKubeConfig(home.Kubeconfig)
require.NoError(t, err)

flightIntf := k8s.TypedInterface[v1alpha1.Flight](client.Dynamic, v1alpha1.FlightGVR()).Namespace("default")
flightIntf := k8s.TypedInterface[v1alpha1.Flight](client, v1alpha1.FlightGVR()).Namespace("default")

_, err = flightIntf.Create(
context.Background(),
Expand All @@ -282,7 +282,7 @@ func TestNotAllowedFlightWasmURL(t *testing.T) {
client, err := k8s.NewClientFromKubeConfig(home.Kubeconfig)
require.NoError(t, err)

flightIntf := k8s.TypedInterface[v1alpha1.Flight](client.Dynamic, v1alpha1.FlightGVR()).Namespace("default")
flightIntf := k8s.TypedInterface[v1alpha1.Flight](client, v1alpha1.FlightGVR()).Namespace("default")

_, err = flightIntf.Create(
context.Background(),
Expand All @@ -299,7 +299,7 @@ func TestFlightInvalidChecksum(t *testing.T) {
client, err := k8s.NewClientFromKubeConfig(home.Kubeconfig)
require.NoError(t, err)

flightIntf := k8s.TypedInterface[v1alpha1.Flight](client.Dynamic, v1alpha1.FlightGVR()).Namespace("default")
flightIntf := k8s.TypedInterface[v1alpha1.Flight](client, v1alpha1.FlightGVR()).Namespace("default")

// Create the flight initially so that we can warm the cache.
// Otherwise it'll behave different when we run this test alone versus with the rest of the test suite.
Expand Down Expand Up @@ -405,7 +405,7 @@ func TestFlightCodeSigning(t *testing.T) {
Insecure: true,
}))

flightIntf := k8s.TypedInterface[v1alpha1.Flight](client.Dynamic, v1alpha1.FlightGVR()).Namespace("default")
flightIntf := k8s.TypedInterface[v1alpha1.Flight](client, v1alpha1.FlightGVR()).Namespace("default")

_, err = flightIntf.Create(
context.Background(),
Expand Down
22 changes: 11 additions & 11 deletions cmd/atc/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1056,7 +1056,7 @@ func TestClusterScopeDynamicAirway(t *testing.T) {
require.NoError(t, client.EnsureNamespace(context.Background(), ns))
}

testIntf := k8s.TypedInterface[EmptyCRD](client.Dynamic, schema.GroupVersionResource{
testIntf := k8s.TypedInterface[EmptyCRD](client, schema.GroupVersionResource{
Group: "examples.com",
Version: "v1",
Resource: "tests",
Expand Down Expand Up @@ -2219,7 +2219,7 @@ func TestDynamicWithExternalResource(t *testing.T) {
configMapIntf := client.Clientset.CoreV1().ConfigMaps("default")

backendIntf := k8s.
TypedInterface[EmptyCRD](client.Dynamic, schema.GroupVersionResource{
TypedInterface[EmptyCRD](client, schema.GroupVersionResource{
Group: "examples.com",
Version: "v1",
Resource: "backends",
Expand Down Expand Up @@ -2383,7 +2383,7 @@ func TestExternalDynamicCreateEvent(t *testing.T) {
configmapIntf := client.Clientset.CoreV1().ConfigMaps("default")

cloneIntf := k8s.
TypedInterface[CopyJob](client.Dynamic, schema.GroupVersionResource{
TypedInterface[CopyJob](client, schema.GroupVersionResource{
Group: "examples.com",
Version: "v1",
Resource: "clones",
Expand Down Expand Up @@ -2544,7 +2544,7 @@ func TestStatusUpdates(t *testing.T) {
Resource: "backends",
}

backendIntf := k8s.TypedInterface[CR](client.Dynamic, backendGVR).Namespace("default")
backendIntf := k8s.TypedInterface[CR](client, backendGVR).Namespace("default")

cases := []struct {
Name string
Expand Down Expand Up @@ -2783,7 +2783,7 @@ func TestDeploymentStatus(t *testing.T) {
Resource: "backends",
}

backendIntf := k8s.TypedInterface[CR](client.Dynamic, backendGVR).Namespace("default")
backendIntf := k8s.TypedInterface[CR](client, backendGVR).Namespace("default")

be := &CR{
TypeMeta: metav1.TypeMeta{
Expand Down Expand Up @@ -3311,7 +3311,7 @@ func TestTimeout(t *testing.T) {
}()

timeoutIntf := k8s.
TypedInterface[EmptyCRD](client.Dynamic, schema.GroupVersionResource{
TypedInterface[EmptyCRD](client, schema.GroupVersionResource{
Group: "examples.com",
Version: "v1",
Resource: "timeouts",
Expand Down Expand Up @@ -3403,7 +3403,7 @@ func TestSubscriptionMode(t *testing.T) {
)
}()

subIntf := k8s.TypedInterface[EmptyCRD](client.Dynamic, schema.GroupVersionResource{
subIntf := k8s.TypedInterface[EmptyCRD](client, schema.GroupVersionResource{
Group: "example.com",
Version: "v1",
Resource: "subscriptions",
Expand Down Expand Up @@ -3568,7 +3568,7 @@ func TestValidationCycle(t *testing.T) {

require.NoError(t, client.EnsureNamespace(context.Background(), "foo"))

backendIntf := k8s.TypedInterface[backendv1.Backend](client.Dynamic, schema.GroupVersionResource{
backendIntf := k8s.TypedInterface[backendv1.Backend](client, schema.GroupVersionResource{
Resource: "backends",
Group: "examples.com",
Version: "v1",
Expand Down Expand Up @@ -3682,7 +3682,7 @@ func TestIdentityWithError(t *testing.T) {
Version: "v1",
}

testIntf := k8s.TypedInterface[CR](client.Dynamic, testGVR).Namespace("default")
testIntf := k8s.TypedInterface[CR](client, testGVR).Namespace("default")

test, err := testIntf.Create(
context.Background(),
Expand Down Expand Up @@ -3798,7 +3798,7 @@ func TestInvalidFlightURL(t *testing.T) {
Resource: "backends",
}

backendIntf := k8s.TypedInterface[backendv1.Backend](client.Dynamic, backendGVR).Namespace("default")
backendIntf := k8s.TypedInterface[backendv1.Backend](client, backendGVR).Namespace("default")

be, err := backendIntf.Create(
context.Background(),
Expand Down Expand Up @@ -3890,7 +3890,7 @@ func TestInvalidChecksum(t *testing.T) {
Resource: "backends",
}

backendIntf := k8s.TypedInterface[backendv1.Backend](client.Dynamic, backendGVR).Namespace("default")
backendIntf := k8s.TypedInterface[backendv1.Backend](client, backendGVR).Namespace("default")

be, err := backendIntf.Create(
context.Background(),
Expand Down
2 changes: 1 addition & 1 deletion internal/atc/reconciler_flight.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func flightReconciler(modules *cache.ModuleCache, clusterScope bool) ctrl.Funcs
var (
client = (*k8s.Client)(ctrl.Client(ctx))
commander = yoke.FromK8Client(client)
flightIntf = k8s.TypedInterface[AltFlight](client.Dynamic, gvr).Namespace(evt.Namespace)
flightIntf = k8s.TypedInterface[AltFlight](client, gvr).Namespace(evt.Namespace)
flightCache = ctrl.CacheFromEvent[AltFlight](ctx, evt)
)

Expand Down
18 changes: 15 additions & 3 deletions internal/k8s/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func (c TypedIntf[T]) Get(ctx context.Context, name string, options metav1.GetOp
}
var result T
if err := runtime.DefaultUnstructuredConverter.FromUnstructured(obj.Object, &result); err != nil {
return nil, fmt.Errorf("failed to convert unstructerd value to typed api: %w", err)
return nil, fmt.Errorf("failed to convert unstructured value to typed api: %w", err)
}
return &result, nil
}
Expand All @@ -46,6 +46,9 @@ func (c TypedIntf[T]) Create(ctx context.Context, api *T, options metav1.CreateO
if err != nil {
return nil, fmt.Errorf("failed to convert typed api to unstructured object: %w", err)
}
if ns := obj.GetNamespace(); ns != "" {
c = c.Namespace(ns)
}
obj, err = c.getIntf().Create(ctx, obj, options)
if err != nil {
return nil, err
Expand All @@ -62,6 +65,9 @@ func (c TypedIntf[T]) Update(ctx context.Context, api *T, options metav1.UpdateO
if err != nil {
return nil, fmt.Errorf("failed to convert typed api to unstructured object: %w", err)
}
if ns := obj.GetNamespace(); ns != "" {
c = c.Namespace(ns)
}
obj, err = c.getIntf().Update(ctx, obj, options)
if err != nil {
return nil, err
Expand All @@ -78,6 +84,9 @@ func (c TypedIntf[T]) Apply(ctx context.Context, api *T, options metav1.ApplyOpt
if err != nil {
return nil, fmt.Errorf("failed to convert typed api to unstructured object: %w", err)
}
if ns := obj.GetNamespace(); ns != "" {
c = c.Namespace(ns)
}
obj, err = c.getIntf().Apply(ctx, obj.GetName(), obj, options)
if err != nil {
return nil, err
Expand All @@ -94,6 +103,9 @@ func (c TypedIntf[T]) UpdateStatus(ctx context.Context, api *T, options metav1.U
if err != nil {
return nil, fmt.Errorf("failed to convert typed api to unstructured object: %w", err)
}
if ns := obj.GetNamespace(); ns != "" {
c = c.Namespace(ns)
}
obj, err = c.getIntf().UpdateStatus(ctx, obj, options)
if err != nil {
return nil, err
Expand Down Expand Up @@ -130,9 +142,9 @@ type MetaObject[T any] interface {
metav1.Object
}

func TypedInterface[T any, obj MetaObject[T]](client *dynamic.DynamicClient, resource schema.GroupVersionResource) TypedIntf[T] {
func TypedInterface[T any, obj MetaObject[T]](client *Client, resource schema.GroupVersionResource) TypedIntf[T] {
return TypedIntf[T]{
intf: client.Resource(resource),
intf: client.Dynamic.Resource(resource),
ns: "",
}
}
4 changes: 2 additions & 2 deletions internal/k8s/k8s.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ type Client struct {
DefaultNamespace string
}

func (client Client) AirwayIntf() TypedIntf[v1alpha1.Airway] {
return TypedInterface[v1alpha1.Airway](client.Dynamic, v1alpha1.AirwayGVR())
func (client *Client) AirwayIntf() TypedIntf[v1alpha1.Airway] {
return TypedInterface[v1alpha1.Airway](client, v1alpha1.AirwayGVR())
}

func NewClientFromConfigFlags(cfgFlags *genericclioptions.ConfigFlags) (*Client, error) {
Expand Down
5 changes: 2 additions & 3 deletions pkg/k8s/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/client-go/dynamic"
"k8s.io/client-go/rest"

"github.com/yokecd/yoke/internal/k8s"
Expand All @@ -30,8 +29,8 @@ type TypedIntf[T any] = k8s.TypedIntf[T]
// TypedInterface returns a typed wrapper over the client-go dynamic client.
//
// TODO: once go1.27 is out and generic functions are added this should become a method of the standard client.
func TypedInterface[T any, obj k8s.MetaObject[T]](client *dynamic.DynamicClient, resource schema.GroupVersionResource) TypedIntf[T] {
return k8s.TypedInterface[T, obj](client, resource)
func TypedInterface[T any, obj k8s.MetaObject[T]](client *Client, resource schema.GroupVersionResource) TypedIntf[T] {
return k8s.TypedInterface[T, obj]((*k8s.Client)(client), resource)
}

type WaitOptions = k8s.WaitOptions
Expand Down
Loading