forked from flyteorg/flyte
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Kubeclient has a filter to prevent unnecessary calls to KubeAPI #minor (
flyteorg#339) * [wip] Kubeclient has a filter to prevent unnecessary calls to KubeAPI - Filter is existence check and needs to provide zero false positives, false negatives are ok - Every object that was created or deleted is recorded, so that in subsequent rounds, if needed to be created or deleted can be skipped Signed-off-by: Ketan Umare <[email protected]> * updated Signed-off-by: Ketan Umare <[email protected]> * Added a test Signed-off-by: Ketan Umare <[email protected]> * fixed Signed-off-by: Ketan Umare <[email protected]> * lint fix Signed-off-by: Ketan Umare <[email protected]> * updated Signed-off-by: Ketan Umare <[email protected]> * updated Signed-off-by: Ketan Umare <[email protected]> * comment updated Signed-off-by: Ketan Umare <[email protected]> * comment updatd Signed-off-by: Ketan Umare <[email protected]> * updated size Signed-off-by: Ketan Umare <[email protected]> * removed erroneous print Signed-off-by: Ketan Umare <[email protected]> * Improved error message Signed-off-by: Ketan Umare <[email protected]>
- Loading branch information
Showing
9 changed files
with
218 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
package executors | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"reflect" | ||
"testing" | ||
|
||
"github.com/flyteorg/flytestdlib/contextutils" | ||
"github.com/flyteorg/flytestdlib/promutils" | ||
"github.com/flyteorg/flytestdlib/promutils/labeled" | ||
"github.com/stretchr/testify/assert" | ||
v1 "k8s.io/api/core/v1" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
) | ||
|
||
func TestIdFromObject(t *testing.T) { | ||
type args struct { | ||
ns string | ||
name string | ||
cluster string | ||
kind string | ||
op string | ||
} | ||
tests := []struct { | ||
name string | ||
args args | ||
want string | ||
}{ | ||
{"default", args{"default", "name", "", "pod", "c"}, ":/v1, Kind=pod:default:name:c"}, | ||
{"no-cluster", args{"my-ns", "name", "", "pod", "c"}, ":/v1, Kind=pod:my-ns:name:c"}, | ||
{"differ-oper", args{"default", "name", "", "pod", "d"}, ":/v1, Kind=pod:default:name:d"}, | ||
{"withcluster", args{"default", "name", "cluster", "pod", "d"}, "cluster:/v1, Kind=pod:default:name:d"}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
p := &v1.Pod{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Namespace: tt.args.ns, | ||
Name: tt.args.name, | ||
ClusterName: tt.args.cluster, | ||
}, | ||
TypeMeta: metav1.TypeMeta{ | ||
Kind: tt.args.kind, | ||
APIVersion: "v1", | ||
}, | ||
} | ||
if got := IDFromObject(p, tt.args.op); !reflect.DeepEqual(got, []byte(tt.want)) { | ||
t.Errorf("IDFromObject() = %s, want %s", string(got), tt.want) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
type singleInvokeClient struct { | ||
client.Client | ||
createCalled bool | ||
deleteCalled bool | ||
} | ||
|
||
func (f *singleInvokeClient) Create(ctx context.Context, obj client.Object, opts ...client.CreateOption) error { | ||
if f.createCalled { | ||
return fmt.Errorf("create called more than once") | ||
} | ||
f.createCalled = true | ||
return nil | ||
} | ||
|
||
func (f *singleInvokeClient) Delete(ctx context.Context, obj client.Object, opts ...client.DeleteOption) error { | ||
if f.deleteCalled { | ||
return fmt.Errorf("delete called more than once") | ||
} | ||
f.deleteCalled = true | ||
return nil | ||
} | ||
|
||
func TestWriteThroughCachingWriter_Create(t *testing.T) { | ||
ctx := context.TODO() | ||
c := &singleInvokeClient{} | ||
w, err := newWriteThroughCachingWriter(c, 1000, promutils.NewTestScope()) | ||
assert.NoError(t, err) | ||
|
||
p := &v1.Pod{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Namespace: "ns", | ||
Name: "name", | ||
ClusterName: "cluster", | ||
}, | ||
TypeMeta: metav1.TypeMeta{ | ||
Kind: "pod", | ||
APIVersion: "v1", | ||
}, | ||
} | ||
|
||
err = w.Create(ctx, p) | ||
assert.NoError(t, err) | ||
|
||
assert.True(t, c.createCalled) | ||
|
||
err = w.Create(ctx, p) | ||
assert.NoError(t, err) | ||
} | ||
|
||
func TestWriteThroughCachingWriter_Delete(t *testing.T) { | ||
ctx := context.TODO() | ||
c := &singleInvokeClient{} | ||
w, err := newWriteThroughCachingWriter(c, 1000, promutils.NewTestScope()) | ||
assert.NoError(t, err) | ||
|
||
p := &v1.Pod{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Namespace: "ns", | ||
Name: "name", | ||
ClusterName: "cluster", | ||
}, | ||
TypeMeta: metav1.TypeMeta{ | ||
Kind: "pod", | ||
APIVersion: "v1", | ||
}, | ||
} | ||
|
||
err = w.Delete(ctx, p) | ||
assert.NoError(t, err) | ||
|
||
assert.True(t, c.deleteCalled) | ||
|
||
err = w.Delete(ctx, p) | ||
assert.NoError(t, err) | ||
} | ||
|
||
func init() { | ||
labeled.SetMetricKeys(contextutils.ExecIDKey) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters