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

Updates TransformXXX Functions in k8s pkg #26244

Merged
merged 1 commit into from
Jun 27, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
51 changes: 43 additions & 8 deletions pkg/k8s/factory_functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -472,11 +472,13 @@ func ConvertToSlimIngressLoadBalancerStatus(slimLBStatus *slim_corev1.LoadBalanc
}
}

// TransformToK8sService transforms a *v1.Service into a
// *slim_corev1.Service or a cache.DeletedFinalStateUnknown into
// a cache.DeletedFinalStateUnknown with a *slim_corev1.Service in its Obj.
// If the given obj can't be cast into either *slim_corev1.Service
// nor cache.DeletedFinalStateUnknown, an error is returned.
// TransformToK8sService transforms a *v1.Service into a *slim_corev1.Service
// or a cache.DeletedFinalStateUnknown into a cache.DeletedFinalStateUnknown
// with a *slim_corev1.Service in its Obj. If obj is a *slim_corev1.Service
// or a cache.DeletedFinalStateUnknown with a *slim_corev1.Service in its Obj,
// obj is returned without any transformations. If the given obj can't be cast
// into either *slim_corev1.Service nor cache.DeletedFinalStateUnknown, an error
// is returned.
func TransformToK8sService(obj interface{}) (interface{}, error) {
switch concreteObj := obj.(type) {
case *v1.Service:
Expand Down Expand Up @@ -511,7 +513,12 @@ func TransformToK8sService(obj interface{}) (interface{}, error) {
},
},
}, nil
case *slim_corev1.Service:
return obj, nil
case cache.DeletedFinalStateUnknown:
if _, ok := concreteObj.Obj.(*slim_corev1.Service); ok {
return obj, nil
}
svc, ok := concreteObj.Obj.(*v1.Service)
if !ok {
return nil, fmt.Errorf("unknown object type %T", concreteObj.Obj)
Expand Down Expand Up @@ -559,8 +566,10 @@ func TransformToK8sService(obj interface{}) (interface{}, error) {
// *types.SlimCNP without the Status field of the given CNP, or a
// cache.DeletedFinalStateUnknown into a cache.DeletedFinalStateUnknown with a
// *types.SlimCNP, also without the Status field of the given CNP, in its Obj.
// If the given obj can't be cast into either *cilium_v2.CiliumClusterwideNetworkPolicy
// nor cache.DeletedFinalStateUnknown, an error is returned.
// If obj is a *types.SlimCNP or a cache.DeletedFinalStateUnknown with a *types.SlimCNP
// in its Obj, obj is returned without any transformations. If the given obj can't be
// cast into either *cilium_v2.CiliumClusterwideNetworkPolicy nor
// cache.DeletedFinalStateUnknown, an error is returned.
func TransformToCCNP(obj interface{}) (interface{}, error) {
switch concreteObj := obj.(type) {
case *cilium_v2.CiliumClusterwideNetworkPolicy:
Expand All @@ -572,7 +581,12 @@ func TransformToCCNP(obj interface{}) (interface{}, error) {
Specs: concreteObj.Specs,
},
}, nil
case *types.SlimCNP:
return obj, nil
case cache.DeletedFinalStateUnknown:
if _, ok := concreteObj.Obj.(*types.SlimCNP); ok {
return obj, nil
}
ccnp, ok := concreteObj.Obj.(*cilium_v2.CiliumClusterwideNetworkPolicy)
if !ok {
return nil, fmt.Errorf("unknown object type %T", concreteObj.Obj)
Expand Down Expand Up @@ -600,6 +614,8 @@ func TransformToCCNP(obj interface{}) (interface{}, error) {
// *types.SlimCNP without the Status field of the given CNP, or a
// cache.DeletedFinalStateUnknown into a cache.DeletedFinalStateUnknown with a
// *types.SlimCNP, also without the Status field of the given CNP, in its Obj.
// If obj is a *types.SlimCNP or a cache.DeletedFinalStateUnknown with a
// *types.SlimCNP in its Obj, obj is returned without any transformations.
// If the given obj can't be cast into either *cilium_v2.CiliumNetworkPolicy
// nor cache.DeletedFinalStateUnknown, an error is returned.
func TransformToCNP(obj interface{}) (interface{}, error) {
Expand All @@ -613,7 +629,12 @@ func TransformToCNP(obj interface{}) (interface{}, error) {
Specs: concreteObj.Specs,
},
}, nil
case *types.SlimCNP:
return obj, nil
case cache.DeletedFinalStateUnknown:
if _, ok := concreteObj.Obj.(*types.SlimCNP); ok {
return obj, nil
}
cnp, ok := concreteObj.Obj.(*cilium_v2.CiliumNetworkPolicy)
if !ok {
return nil, fmt.Errorf("unknown object type %T", concreteObj.Obj)
Expand Down Expand Up @@ -679,7 +700,9 @@ func convertToTaints(v1Taints []v1.Taint) []slim_corev1.Taint {

// TransformToNode transforms a *v1.Node into a *types.Node or a
// cache.DeletedFinalStateUnknown into a cache.DeletedFinalStateUnknown
// with a *types.Node in its Obj. If the given obj can't be cast into
// with a *types.Node in its Obj. If obj is a *slim_corev1.Node or a
// cache.DeletedFinalStateUnknown with a *slim_corev1.Node in its Obj, obj
// is returned without any transformations. If the given obj can't be cast into
// either *v1.Node nor cache.DeletedFinalStateUnknown, an error is returned.
func TransformToNode(obj interface{}) (interface{}, error) {
switch concreteObj := obj.(type) {
Expand All @@ -706,7 +729,12 @@ func TransformToNode(obj interface{}) (interface{}, error) {
Addresses: convertToAddress(concreteObj.Status.Addresses),
},
}, nil
case *slim_corev1.Node:
return obj, nil
case cache.DeletedFinalStateUnknown:
if _, ok := concreteObj.Obj.(*slim_corev1.Node); ok {
return obj, nil
}
node, ok := concreteObj.Obj.(*v1.Node)
if !ok {
return nil, fmt.Errorf("unknown object type %T", concreteObj.Obj)
Expand Down Expand Up @@ -908,6 +936,8 @@ func ObjToCiliumNode(obj interface{}) *cilium_v2.CiliumNode {
// TransformToCiliumEndpoint transforms a *cilium_v2.CiliumEndpoint into a
// *types.CiliumEndpoint or a cache.DeletedFinalStateUnknown into a
// cache.DeletedFinalStateUnknown with a *types.CiliumEndpoint in its Obj.
// If obj is a *types.CiliumEndpoint or a cache.DeletedFinalStateUnknown with
// a *types.CiliumEndpoint in its Obj, obj is returned without any transformations.
// If the given obj can't be cast into either *cilium_v2.CiliumEndpoint nor
// cache.DeletedFinalStateUnknown, an error is returned.
func TransformToCiliumEndpoint(obj interface{}) (interface{}, error) {
Expand Down Expand Up @@ -936,7 +966,12 @@ func TransformToCiliumEndpoint(obj interface{}) (interface{}, error) {
Networking: concreteObj.Status.Networking,
NamedPorts: concreteObj.Status.NamedPorts,
}, nil
case *types.CiliumEndpoint:
return obj, nil
case cache.DeletedFinalStateUnknown:
if _, ok := concreteObj.Obj.(*types.CiliumEndpoint); ok {
return obj, nil
}
ciliumEndpoint, ok := concreteObj.Obj.(*cilium_v2.CiliumEndpoint)
if !ok {
return nil, fmt.Errorf("unknown object type %T", concreteObj.Obj)
Expand Down
110 changes: 110 additions & 0 deletions pkg/k8s/factory_functions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -926,6 +926,14 @@ func (s *K8sSuite) Test_TransformToK8sService(c *C) {
want: &slim_corev1.Service{},
expected: true,
},
{
name: "transformation unneeded",
args: args{
obj: &slim_corev1.Service{},
},
want: &slim_corev1.Service{},
expected: true,
},
{
name: "delete final state unknown transformation",
args: args{
Expand All @@ -940,6 +948,20 @@ func (s *K8sSuite) Test_TransformToK8sService(c *C) {
},
expected: true,
},
{
name: "delete final state unknown transformation with slim Service",
args: args{
obj: cache.DeletedFinalStateUnknown{
Key: "foo",
Obj: &slim_corev1.Service{},
},
},
want: cache.DeletedFinalStateUnknown{
Key: "foo",
Obj: &slim_corev1.Service{},
},
expected: true,
},
{
name: "unknown object type in delete final state unknown transformation",
args: args{
Expand Down Expand Up @@ -1194,6 +1216,14 @@ func (s *K8sSuite) Test_TransformToCNP(c *C) {
},
expected: true,
},
{
name: "transformation unneeded",
args: args{
obj: &types.SlimCNP{},
},
want: &types.SlimCNP{},
expected: true,
},
{
name: "delete final state unknown transformation",
args: args{
Expand All @@ -1210,6 +1240,20 @@ func (s *K8sSuite) Test_TransformToCNP(c *C) {
},
expected: true,
},
{
name: "delete final state unknown transformation with SlimCNP",
args: args{
obj: cache.DeletedFinalStateUnknown{
Key: "foo",
Obj: &types.SlimCNP{},
},
},
want: cache.DeletedFinalStateUnknown{
Key: "foo",
Obj: &types.SlimCNP{},
},
expected: true,
},
{
name: "unknown object type in delete final state unknown transformation",
args: args{
Expand Down Expand Up @@ -1261,6 +1305,14 @@ func (s *K8sSuite) Test_TransformToCCNP(c *C) {
},
expected: true,
},
{
name: "transformation unneeded",
args: args{
obj: &types.SlimCNP{},
},
want: &types.SlimCNP{},
expected: true,
},
{
name: "A CCNP where it doesn't contain neither a spec nor specs",
args: args{
Expand All @@ -1287,6 +1339,20 @@ func (s *K8sSuite) Test_TransformToCCNP(c *C) {
},
expected: true,
},
{
name: "delete final state unknown transformation with SlimCNP",
args: args{
obj: cache.DeletedFinalStateUnknown{
Key: "foo",
Obj: &types.SlimCNP{},
},
},
want: cache.DeletedFinalStateUnknown{
Key: "foo",
Obj: &types.SlimCNP{},
},
expected: true,
},
{
name: "unknown object type in delete final state unknown transformation",
args: args{
Expand Down Expand Up @@ -1336,6 +1402,14 @@ func (s *K8sSuite) Test_TransformToNode(c *C) {
want: &slim_corev1.Node{},
expected: true,
},
{
name: "transformation unneeded",
args: args{
obj: &slim_corev1.Node{},
},
want: &slim_corev1.Node{},
expected: true,
},
{
name: "delete final state unknown transformation",
args: args{
Expand All @@ -1350,6 +1424,20 @@ func (s *K8sSuite) Test_TransformToNode(c *C) {
},
expected: true,
},
{
name: "delete final state unknown transformation with slim Node",
args: args{
obj: cache.DeletedFinalStateUnknown{
Key: "foo",
Obj: &slim_corev1.Node{},
},
},
want: cache.DeletedFinalStateUnknown{
Key: "foo",
Obj: &slim_corev1.Node{},
},
expected: true,
},
{
name: "unknown object type in delete final state unknown transformation",
args: args{
Expand Down Expand Up @@ -1653,6 +1741,14 @@ func (s *K8sSuite) Test_TransformToCiliumEndpoint(c *C) {
},
expected: true,
},
{
name: "transformation unneeded",
args: args{
obj: &types.CiliumEndpoint{},
},
want: &types.CiliumEndpoint{},
expected: true,
},
{
name: "delete final state unknown transformation",
args: args{
Expand Down Expand Up @@ -1800,6 +1896,20 @@ func (s *K8sSuite) Test_TransformToCiliumEndpoint(c *C) {
want: unknownObjErr,
expected: false,
},
{
name: "delete final state unknown transformation with a types.CiliumEndpoint",
args: args{
obj: cache.DeletedFinalStateUnknown{
Key: "foo",
Obj: &types.CiliumEndpoint{},
},
},
want: cache.DeletedFinalStateUnknown{
Key: "foo",
Obj: &types.CiliumEndpoint{},
},
expected: true,
},
{
name: "unknown object type in transformation",
args: args{
Expand Down