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

Update check for application in manual processing of Dapr Components #6282

Merged
merged 2 commits into from
Sep 22, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 21 additions & 6 deletions pkg/daprrp/processors/pubsubbrokers/processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,16 @@ func (p *Processor) Process(ctx context.Context, resource *datamodel.DaprPubSubB
// If the resource is being provisioned manually then *we* are responsible for creating the Dapr Component.
// Let's do this now.

applicationID, err := resources.ParseResource(resource.Properties.Application)
if err != nil {
return err // This should already be validated by this point.
// DaprPubSubBroker resources may or may not be application scoped.
// Some Dapr Components can be specific to a single application, they would be application scoped and have
// resource.Properties.Application populated, while others could be shared across multiple applications and
// would not have resource.Properties.Application populated.
var applicationID resources.ID
if resource.Properties.Application != "" {
applicationID, err = resources.ParseResource(resource.Properties.Application)
if err != nil {
return err // This should already be validated by this point.
}
kachawla marked this conversation as resolved.
Show resolved Hide resolved
}

component, err := dapr.ConstructDaprGeneric(
Expand Down Expand Up @@ -113,9 +120,17 @@ func (p *Processor) Delete(ctx context.Context, resource *datamodel.DaprPubSubBr
return nil
}

applicationID, err := resources.ParseResource(resource.Properties.Application)
if err != nil {
return err // This should already be validated by this point.
// DaprPubSubBroker resources may or may not be application scoped.
// Some Dapr Components can be specific to a single application, they would be application scoped and have
// resource.Properties.Application populated, while others could be shared across multiple applications and
// would not have resource.Properties.Application populated.
var err error
var applicationID resources.ID
if resource.Properties.Application != "" {
applicationID, err = resources.ParseResource(resource.Properties.Application)
if err != nil {
return err
}
}

component := unstructured.Unstructured{
Expand Down
89 changes: 89 additions & 0 deletions pkg/daprrp/processors/pubsubbrokers/processor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,95 @@ func Test_Process(t *testing.T) {
require.Equal(t, []unstructured.Unstructured{*generated}, components.Items)
})

t.Run("success - manual (no application)", func(t *testing.T) {
processor := Processor{
Client: k8sutil.NewFakeKubeClient(scheme.Scheme, &corev1.Namespace{ObjectMeta: metav1.ObjectMeta{Name: "test-namespace"}}),
}

resource := &datamodel.DaprPubSubBroker{
BaseResource: v1.BaseResource{
TrackedResource: v1.TrackedResource{
Name: "some-other-name",
},
},
Properties: datamodel.DaprPubSubBrokerProperties{
BasicResourceProperties: rpv1.BasicResourceProperties{
Environment: envID,
},
BasicDaprResourceProperties: rpv1.BasicDaprResourceProperties{
ComponentName: componentName,
},
ResourceProvisioning: portableresources.ResourceProvisioningManual,
Metadata: map[string]any{
"config": "extrasecure",
},
Resources: []*portableresources.ResourceReference{{ID: externalResourceID1}},
Type: "pubsub.redis",
Version: "v1",
},
}

options := processors.Options{
RuntimeConfiguration: recipes.RuntimeConfiguration{
Kubernetes: &recipes.KubernetesRuntime{
Namespace: "test-namespace",
},
},
}

err := processor.Process(context.Background(), resource, options)
require.NoError(t, err)

require.Equal(t, componentName, resource.Properties.ComponentName)

expectedValues := map[string]any{
"componentName": componentName,
}
expectedSecrets := map[string]rpv1.SecretValueReference{}

expectedOutputResources, err := processors.GetOutputResourcesFromResourcesField(resource.Properties.Resources)

generated := &unstructured.Unstructured{
Object: map[string]any{
"apiVersion": dapr.DaprAPIVersion,
"kind": dapr.DaprKind,
"metadata": map[string]any{
"namespace": "test-namespace",
"name": "test-dapr-pubsub-broker",
"labels": kubernetes.MakeDescriptiveDaprLabels("", "some-other-name", portableresources.DaprPubSubBrokersResourceType),
"resourceVersion": "1",
},
"spec": map[string]any{
"type": "pubsub.redis",
"version": "v1",
"metadata": []any{
map[string]any{
"name": "config",
"value": "extrasecure",
},
},
},
},
}

component := rpv1.NewKubernetesOutputResource("Component", generated, metav1.ObjectMeta{Name: generated.GetName(), Namespace: generated.GetNamespace()})
component.RadiusManaged = to.Ptr(true)
expectedOutputResources = append(expectedOutputResources, component)
require.NoError(t, err)

require.Equal(t, expectedValues, resource.ComputedValues)
require.Equal(t, expectedSecrets, resource.SecretValues)
require.Equal(t, expectedOutputResources, resource.Properties.Status.OutputResources)

components := unstructured.UnstructuredList{}
components.SetAPIVersion("dapr.io/v1alpha1")
components.SetKind("Component")
err = processor.Client.List(context.Background(), &components, &client.ListOptions{Namespace: options.RuntimeConfiguration.Kubernetes.Namespace})
require.NoError(t, err)
require.NotEmpty(t, components.Items)
require.Equal(t, []unstructured.Unstructured{*generated}, components.Items)
})

t.Run("success - recipe with overrides", func(t *testing.T) {
processor := Processor{
Client: k8sutil.NewFakeKubeClient(scheme.Scheme),
Expand Down
27 changes: 21 additions & 6 deletions pkg/daprrp/processors/secretstores/processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,16 @@ func (p *Processor) Process(ctx context.Context, resource *datamodel.DaprSecretS
// If the resource is being provisioned manually then *we* are responsible for creating the Dapr Component.
// Let's do this now.

applicationID, err := resources.ParseResource(resource.Properties.Application)
if err != nil {
return err // This should already be validated by this point.
// DaprSecretStore resources may or may not be application scoped.
// Some Dapr Components can be specific to a single application, they would be application scoped and have
// resource.Properties.Application populated, while others could be shared across multiple applications and
// would not have resource.Properties.Application populated.
var applicationID resources.ID
if resource.Properties.Application != "" {
applicationID, err = resources.ParseResource(resource.Properties.Application)
if err != nil {
return err // This should already be validated by this point.
}
}

component, err := dapr.ConstructDaprGeneric(
Expand Down Expand Up @@ -110,9 +117,17 @@ func (p *Processor) Delete(ctx context.Context, resource *datamodel.DaprSecretSt
return nil
}

applicationID, err := resources.ParseResource(resource.Properties.Application)
if err != nil {
return err // This should already be validated by this point.
// DaprSecretStore resources may or may not be application scoped.
// Some Dapr Components can be specific to a single application, they would be application scoped and have
// resource.Properties.Application populated, while others could be shared across multiple applications and
// would not have resource.Properties.Application populated.
var err error
var applicationID resources.ID
if resource.Properties.Application != "" {
applicationID, err = resources.ParseResource(resource.Properties.Application)
if err != nil {
return err
}
}

component := unstructured.Unstructured{
Expand Down
84 changes: 84 additions & 0 deletions pkg/daprrp/processors/secretstores/processor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ import (
func Test_Process(t *testing.T) {
const kubernetesResource = "/planes/kubernetes/local/namespaces/test-namespace/providers/dapr.io/Component/test-component"
const applicationID = "/planes/radius/local/resourceGroups/test-rg/providers/Applications.Core/applications/test-app"
const envID = "/planes/radius/local/resourceGroups/test-rg/providers/Applications.Core/environments/test-env"
const componentName = "test-component"

t.Run("success - recipe", func(t *testing.T) {
Expand Down Expand Up @@ -189,6 +190,89 @@ func Test_Process(t *testing.T) {
require.Equal(t, []unstructured.Unstructured{*generated}, components.Items)
})

t.Run("success - values (no application)", func(t *testing.T) {
processor := Processor{
Client: k8sutil.NewFakeKubeClient(scheme.Scheme, &corev1.Namespace{ObjectMeta: metav1.ObjectMeta{Name: "test-namespace"}}),
}

resource := &datamodel.DaprSecretStore{
BaseResource: v1.BaseResource{
TrackedResource: v1.TrackedResource{
Name: "some-other-name",
},
},
Properties: datamodel.DaprSecretStoreProperties{
BasicResourceProperties: rpv1.BasicResourceProperties{
Environment: envID,
},
BasicDaprResourceProperties: rpv1.BasicDaprResourceProperties{
ComponentName: componentName,
},
ResourceProvisioning: portableresources.ResourceProvisioningManual,
Metadata: map[string]any{"config": "extrasecure"},
Type: "secretstores.kubernetes",
Version: "v1",
},
}

options := processors.Options{
RuntimeConfiguration: recipes.RuntimeConfiguration{
Kubernetes: &recipes.KubernetesRuntime{
Namespace: "test-namespace",
},
},
}

err := processor.Process(context.Background(), resource, options)
require.NoError(t, err)

require.Equal(t, componentName, resource.Properties.ComponentName)

expectedValues := map[string]any{
"componentName": componentName,
}
expectedSecrets := map[string]rpv1.SecretValueReference{}
generated := &unstructured.Unstructured{
Object: map[string]any{
"apiVersion": dapr.DaprAPIVersion,
"kind": dapr.DaprKind,
"metadata": map[string]any{
"namespace": "test-namespace",
"name": "test-component",
"labels": kubernetes.MakeDescriptiveDaprLabels("", "some-other-name", portableresources.DaprSecretStoresResourceType),
"resourceVersion": "1",
},
"spec": map[string]any{
"type": "secretstores.kubernetes",
"version": "v1",
"metadata": []any{
map[string]any{
"name": "config",
"value": "extrasecure",
},
},
},
},
}

component := rpv1.NewKubernetesOutputResource("Component", generated, metav1.ObjectMeta{Name: generated.GetName(), Namespace: generated.GetNamespace()})
component.RadiusManaged = to.Ptr(true)
expectedOutputResources := []rpv1.OutputResource{component}
require.NoError(t, err)

require.Equal(t, expectedValues, resource.ComputedValues)
require.Equal(t, expectedSecrets, resource.SecretValues)
require.Equal(t, expectedOutputResources, resource.Properties.Status.OutputResources)

components := unstructured.UnstructuredList{}
components.SetAPIVersion("dapr.io/v1alpha1")
components.SetKind("Component")
err = processor.Client.List(context.Background(), &components, &client.ListOptions{Namespace: options.RuntimeConfiguration.Kubernetes.Namespace})
require.NoError(t, err)
require.NotEmpty(t, components.Items)
require.Equal(t, []unstructured.Unstructured{*generated}, components.Items)
})

t.Run("success - recipe with value overrides", func(t *testing.T) {
processor := Processor{
Client: k8sutil.NewFakeKubeClient(scheme.Scheme),
Expand Down
27 changes: 21 additions & 6 deletions pkg/daprrp/processors/statestores/processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,16 @@ func (p *Processor) Process(ctx context.Context, resource *datamodel.DaprStateSt
// If the resource is being provisioned manually then *we* are responsible for creating the Dapr Component.
// Let's do this now.

applicationID, err := resources.ParseResource(resource.Properties.Application)
if err != nil {
return err // This should already be validated by this point.
// DaprStateStore resources may or may not be application scoped.
// Some Dapr Components can be specific to a single application, they would be application scoped and have
// resource.Properties.Application populated, while others could be shared across multiple applications and
// would not have resource.Properties.Application populated.
var applicationID resources.ID
if resource.Properties.Application != "" {
applicationID, err = resources.ParseResource(resource.Properties.Application)
if err != nil {
return err // This should already be validated by this point.
}
}

component, err := dapr.ConstructDaprGeneric(
Expand Down Expand Up @@ -113,9 +120,17 @@ func (p *Processor) Delete(ctx context.Context, resource *datamodel.DaprStateSto
return nil
}

applicationID, err := resources.ParseResource(resource.Properties.Application)
if err != nil {
return err // This should already be validated by this point.
// DaprStateStore resources may or may not be application scoped.
// Some Dapr Components can be specific to a single application, they would be application scoped and have
// resource.Properties.Application populated, while others could be shared across multiple applications and
// would not have resource.Properties.Application populated.
var err error
var applicationID resources.ID
if resource.Properties.Application != "" {
applicationID, err = resources.ParseResource(resource.Properties.Application)
if err != nil {
return err
}
}

component := unstructured.Unstructured{
Expand Down
Loading