Skip to content

Commit

Permalink
Make the conversion whebhook of v1alpha3 and v1beta1 preserve the…
Browse files Browse the repository at this point in the history
… way a service is addressed in `clientIntent.Spec.call[x].name` instead of changing it to an explicit form (#460)
  • Loading branch information
omris94 authored Jul 30, 2024
1 parent 83ff0b1 commit c30272c
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 2 deletions.
14 changes: 13 additions & 1 deletion src/operator/api/v1alpha3/webhooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,13 @@ func (in *ClientIntents) ConvertTo(dstRaw conversion.Hub) error {
dst.Spec.Targets = make([]v2alpha1.Target, len(in.Spec.Calls))
for i, call := range in.Spec.Calls {
if call.IsTargetInCluster() && call.Type != IntentTypeKafka {
kubernetesTarget := v2alpha1.KubernetesTarget{Name: call.GetServerFullyQualifiedName(in.Namespace), Kind: call.GetTargetServerKind()}
var name string
if call.Name == call.GetTargetServerName() {
name = call.Name
} else {
name = call.GetServerFullyQualifiedName(in.Namespace)
}
kubernetesTarget := v2alpha1.KubernetesTarget{Name: name, Kind: call.GetTargetServerKind()}
if kubernetesTarget.Kind == serviceidentity.KindOtterizeLegacy {
// This is an internal kind the user doesn't need to see it
kubernetesTarget.Kind = ""
Expand Down Expand Up @@ -267,6 +273,12 @@ func (in *ClientIntents) ConvertFrom(srcRaw conversion.Hub) error {
in.Spec.Service.Kind = src.Spec.Workload.Kind
in.Spec.Calls = make([]Intent, len(src.Spec.Targets))
for i, target := range src.Spec.Targets {
if target.IsTargetTheKubernetesAPIServer(src.Namespace) {
// Using "svc:kubernetes.default" was a common use case in v1alpha3 -
// therefore we prefer to convert to this form.
in.Spec.Calls[i] = Intent{Name: "svc:" + target.Kubernetes.Name}
continue
}
if target.Kubernetes != nil {
in.Spec.Calls[i] = Intent{Name: target.Kubernetes.Name, Kind: target.Kubernetes.Kind}
if target.Kubernetes.HTTP != nil {
Expand Down
41 changes: 41 additions & 0 deletions src/operator/api/v1alpha3/webhooks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,47 @@ func (t *WebhooksTestSuite) TestProtectedServiceConversion() {
t.Require().Equal(original.Spec, converted.Spec)
}

func (t *WebhooksTestSuite) TestClientIntentsKubernetes() {
// Create a ClientIntents with random data
original := &ClientIntents{
ObjectMeta: metav1.ObjectMeta{
Name: "test",
Namespace: "test",
},
Spec: &IntentsSpec{
Service: Service{
Name: "test",
Kind: "test",
},
Calls: []Intent{
{
Name: "test.test",
},
{
Name: "test2",
},
{
Name: "test3.other-namespace",
},
{
Name: "svc:kubernetes.default",
},
},
}}

// ConvertTo
dstRaw := &v2alpha1.ClientIntents{}
err := original.ConvertTo(dstRaw)
t.Require().NoError(err)

// ConvertFrom
converted := &ClientIntents{}
err = converted.ConvertFrom(dstRaw)
t.Require().NoError(err)

t.Require().Equal(original.Spec, converted.Spec)
}

func TestWebhooksTestSuite(t *testing.T) {
suite.Run(t, new(WebhooksTestSuite))
}
14 changes: 13 additions & 1 deletion src/operator/api/v1beta1/webhooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,13 @@ func (in *ClientIntents) ConvertTo(dstRaw conversion.Hub) error {
dst.Spec.Targets = make([]v2alpha1.Target, len(in.Spec.Calls))
for i, call := range in.Spec.Calls {
if call.IsTargetInCluster() && call.Type != IntentTypeKafka {
kubernetesTarget := v2alpha1.KubernetesTarget{Name: call.GetServerFullyQualifiedName(in.Namespace), Kind: call.GetTargetServerKind()}
var name string
if call.Name == call.GetTargetServerName() {
name = call.Name
} else {
name = call.GetServerFullyQualifiedName(in.Namespace)
}
kubernetesTarget := v2alpha1.KubernetesTarget{Name: name, Kind: call.GetTargetServerKind()}
if kubernetesTarget.Kind == serviceidentity.KindOtterizeLegacy {
// This is an internal kind the user doesn't need to see it
kubernetesTarget.Kind = ""
Expand Down Expand Up @@ -267,6 +273,12 @@ func (in *ClientIntents) ConvertFrom(srcRaw conversion.Hub) error {
in.Spec.Service.Kind = src.Spec.Workload.Kind
in.Spec.Calls = make([]Intent, len(src.Spec.Targets))
for i, target := range src.Spec.Targets {
if target.IsTargetTheKubernetesAPIServer(src.Namespace) {
// Using "svc:kubernetes.default" was a common use case in v1alpha3 -
// therefore we prefer to convert to this form.
in.Spec.Calls[i] = Intent{Name: "svc:" + target.Kubernetes.Name}
continue
}
if target.Kubernetes != nil {
in.Spec.Calls[i] = Intent{Name: target.Kubernetes.Name, Kind: target.Kubernetes.Kind}
if target.Kubernetes.HTTP != nil {
Expand Down
41 changes: 41 additions & 0 deletions src/operator/api/v1beta1/webhooks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,47 @@ func (t *WebhooksTestSuite) TestProtectedServiceConversion() {
t.Require().Equal(original.Spec, converted.Spec)
}

func (t *WebhooksTestSuite) TestClientIntentsKubernetes() {
// Create a ClientIntents with random data
original := &ClientIntents{
ObjectMeta: metav1.ObjectMeta{
Name: "test",
Namespace: "test",
},
Spec: &IntentsSpec{
Service: Service{
Name: "test",
Kind: "test",
},
Calls: []Intent{
{
Name: "test.test",
},
{
Name: "test2",
},
{
Name: "test3.other-namespace",
},
{
Name: "svc:kubernetes.default",
},
},
}}

// ConvertTo
dstRaw := &v2alpha1.ClientIntents{}
err := original.ConvertTo(dstRaw)
t.Require().NoError(err)

// ConvertFrom
converted := &ClientIntents{}
err = converted.ConvertFrom(dstRaw)
t.Require().NoError(err)

t.Require().Equal(original.Spec, converted.Spec)
}

func TestWebhooksTestSuite(t *testing.T) {
suite.Run(t, new(WebhooksTestSuite))
}

0 comments on commit c30272c

Please sign in to comment.