Skip to content

Commit

Permalink
cast to unstructured.Unstructured doesn't work
Browse files Browse the repository at this point in the history
  • Loading branch information
Lakshmi Javadekar authored and Lakshmi Javadekar committed Nov 17, 2022
1 parent 9f4bc44 commit b51445b
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 9 deletions.
16 changes: 14 additions & 2 deletions pkg/corerp/renderers/kubernetesmetadata/render.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,11 @@ func (r *Renderer) Render(ctx context.Context, dm conv.DataModelInterface, optio
if lbls, ok := r.getLabels(o); ok {
var lbl map[string]string
lbl = labels.Merge(lbls, e.KubernetesMetadata.Labels)
r.setLabelsAnnotations(o, lbl, true)
// r.setLabelsAnnotations(o, ann, true) -- real call
r.setLabels(o, lbl, true)
}
}

}
}
default:
Expand Down Expand Up @@ -109,7 +111,9 @@ func (r *Renderer) getAnnotations(o runtime.Object) (map[string]string, bool) {

// setAnnotations sets the value of annotations/labels
func (r *Renderer) setLabelsAnnotations(o runtime.Object, keyvalue map[string]string, isLabel bool) {
if un, ok := o.(*unstructured.Unstructured); ok {
// this is unable to be cast.. why why why?7
un, ok := o.(*unstructured.Unstructured)
if ok {
if isLabel {
un.SetLabels(keyvalue)
} else {
Expand All @@ -118,6 +122,14 @@ func (r *Renderer) setLabelsAnnotations(o runtime.Object, keyvalue map[string]st
}
}

// setAnnotations sets the value of annotations/labels
// this works but we should try and make unstructured.Unstructured (setLabelsAnnotations above) work
func (r *Renderer) setLabels(o runtime.Object, keyvalue map[string]string, isLabel bool) {
if dep, ok := o.(*appsv1.Deployment); ok {
dep.Spec.Template.Labels = keyvalue
}
}

func (r *Renderer) getLabels(o runtime.Object) (map[string]string, bool) {
if dep, ok := o.(*appsv1.Deployment); ok {
if dep.Spec.Template.Labels == nil {
Expand Down
55 changes: 48 additions & 7 deletions pkg/corerp/renderers/kubernetesmetadata/render_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/project-radius/radius/pkg/corerp/datamodel"
"github.com/project-radius/radius/pkg/kubernetes"
"github.com/project-radius/radius/pkg/resourcekinds"
"github.com/project-radius/radius/pkg/resourcemodel"
"github.com/project-radius/radius/pkg/rp"
"github.com/project-radius/radius/pkg/rp/outputresource"
"github.com/project-radius/radius/pkg/ucp/resources"
Expand All @@ -29,20 +30,34 @@ func (r *noop) GetDependencyIDs(ctx context.Context, resource conv.DataModelInte
}

func (r *noop) Render(ctx context.Context, dm conv.DataModelInterface, options renderers.RenderOptions) (renderers.RendererOutput, error) {
// Return a deployment so the manualscale extension can modify it
// Return a deployment so the extension can modify it
deployment := appsv1.Deployment{}
resources := []outputresource.OutputResource{outputresource.NewKubernetesOutputResource(resourcekinds.Deployment, outputresource.LocalIDDeployment, &deployment, deployment.ObjectMeta)}
return renderers.RendererOutput{Resources: resources}, nil

deploymentResource := outputresource.OutputResource{
Resource: &deployment,
ResourceType: resourcemodel.ResourceType{
Type: resourcekinds.Deployment,
Provider: resourcemodel.ProviderKubernetes,
},
LocalID: outputresource.LocalIDDeployment,
}

output := renderers.RendererOutput{
Resources: []outputresource.OutputResource{deploymentResource},
}

return output, nil
}

func Test_Render_Success(t *testing.T) {
renderer := &Renderer{Inner: &noop{}}
var ann = map[string]string{
ann := map[string]string{
"test.ann1": "ann1.val",
"test.ann2": "ann1.val",
"test.ann3": "ann1.val",
}
var lbl = map[string]string{

lbl := map[string]string{
"test.lbl1": "lbl1.val",
"test.lbl2": "lbl2.val",
"test.lbl3": "lbl3.val",
Expand All @@ -59,8 +74,34 @@ func Test_Render_Success(t *testing.T) {
deployment, _ := kubernetes.FindDeployment(output.Resources)
require.NotNil(t, deployment)

require.Equal(t, ann, deployment.Spec.Template.ObjectMeta.Annotations)
require.Equal(t, lbl, deployment.Spec.Template.ObjectMeta.Labels)
require.Equal(t, ann, deployment.Spec.Template.Annotations)
require.Equal(t, lbl, deployment.Spec.Template.Labels)
}

func Test_Render_NoExtension(t *testing.T) {
renderer := &Renderer{Inner: &noop{}}

properties := datamodel.ContainerProperties{
BasicResourceProperties: rp.BasicResourceProperties{
Application: "/subscriptions/test-sub-id/resourceGroups/test-rg/providers/Applications.Core/applications/test-app",
},
Container: datamodel.Container{
Image: "someimage:latest",
},
}

resource := makeResource(t, properties)
dependencies := map[string]renderers.RendererDependency{}

output, err := renderer.Render(context.Background(), resource, renderers.RenderOptions{Dependencies: dependencies})
require.NoError(t, err)
require.Len(t, output.Resources, 1)

deployment, _ := kubernetes.FindDeployment(output.Resources)
require.NotNil(t, deployment)

require.Nil(t, deployment.Spec.Template.Annotations)
require.Nil(t, deployment.Spec.Template.Labels)
}

func makeResource(t *testing.T, properties datamodel.ContainerProperties) *datamodel.ContainerResource {
Expand Down

0 comments on commit b51445b

Please sign in to comment.