Skip to content

Commit

Permalink
updating per comments
Browse files Browse the repository at this point in the history
  • Loading branch information
Lakshmi Javadekar authored and Lakshmi Javadekar committed Nov 28, 2022
1 parent ae1c771 commit 7ce13d5
Show file tree
Hide file tree
Showing 7 changed files with 171 additions and 215 deletions.
4 changes: 2 additions & 2 deletions pkg/corerp/backend/deployment/deploymentprocessor.go
Original file line number Diff line number Diff line change
Expand Up @@ -406,7 +406,7 @@ func (dp *deploymentProcessor) getEnvOptions(ctx context.Context, env *corerp_dm
}

// Get Environment KubernetesMetadata Info
if envExt := env.Properties.FindExtension(corerp_dm.KubernetesMetadata); envExt != nil && envExt.KubernetesMetadata != nil {
if envExt := corerp_dm.FindExtension(env.Properties.Extensions, corerp_dm.KubernetesMetadata); envExt != nil && envExt.KubernetesMetadata != nil {
envOpts.KubernetesMetadata = envExt.KubernetesMetadata
}

Expand Down Expand Up @@ -464,7 +464,7 @@ func (dp *deploymentProcessor) getAppOptions(ctx context.Context, appProp *corer
appOpts := renderers.ApplicationOptions{}

// Get Application KubernetesMetadata Info
if ext := appProp.FindExtension(corerp_dm.KubernetesMetadata); ext != nil && ext.KubernetesMetadata != nil {
if ext := corerp_dm.FindExtension(appProp.Extensions, corerp_dm.KubernetesMetadata); ext != nil && ext.KubernetesMetadata != nil {
appOpts.KubernetesMetadata = ext.KubernetesMetadata
}

Expand Down
10 changes: 0 additions & 10 deletions pkg/corerp/datamodel/application.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,3 @@ type ApplicationProperties struct {
rp.BasicResourceProperties
Extensions []Extension `json:"extensions,omitempty"`
}

// FindExtension finds the right extension.
func (a *ApplicationProperties) FindExtension(kind ExtensionKind) *Extension {
for _, ext := range a.Extensions {
if ext.Kind == kind {
return &ext
}
}
return nil
}
10 changes: 0 additions & 10 deletions pkg/corerp/datamodel/environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,13 +72,3 @@ type Providers struct {
type ProvidersAzure struct {
Scope string `json:"scope,omitempty"`
}

// FindExtension finds the right extension.
func (a *EnvironmentProperties) FindExtension(kind ExtensionKind) *Extension {
for _, ext := range a.Extensions {
if ext.Kind == kind {
return &ext
}
}
return nil
}
10 changes: 10 additions & 0 deletions pkg/corerp/datamodel/extension.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,13 @@ type KubeMetadataExtension struct {
type KubeNamespaceOverrideExtension struct {
Namespace string `json:"namespace,omitempty"`
}

// FindExtension finds the extension.
func FindExtension(exts []Extension, kind ExtensionKind) *Extension {
for _, ext := range exts {
if ext.Kind == kind {
return &ext
}
}
return nil
}
29 changes: 12 additions & 17 deletions pkg/corerp/renderers/kubernetesmetadata/render.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ package kubernetesmetadata

import (
"context"
"errors"

"github.com/project-radius/radius/pkg/armrpc/api/conv"
"github.com/project-radius/radius/pkg/corerp/datamodel"
Expand All @@ -16,7 +15,6 @@ import (
"github.com/project-radius/radius/pkg/ucp/resources"
appsv1 "k8s.io/api/apps/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/runtime"
)

// Renderer is the renderers.Renderer implementation for the kubernetesmetadata extension.
Expand Down Expand Up @@ -60,11 +58,8 @@ func (r *Renderer) Render(ctx context.Context, dm conv.DataModelInterface, optio
// Not a Kubernetes resource
continue
}
o, ok := ores.Resource.(runtime.Object)
if !ok {
return renderers.RendererOutput{}, errors.New("found Kubernetes resource with non-Kubernetes payload")
}
dep, ok := o.(*appsv1.Deployment)

dep, ok := ores.Resource.(*appsv1.Deployment)
if !ok {
continue
}
Expand All @@ -79,7 +74,7 @@ func (r *Renderer) Render(ctx context.Context, dm conv.DataModelInterface, optio
existingMetaAnnotations, existingSpecAnnotations := getAnnotations(dep)

// Merge cumulative annotation values from Env->App->Container->InputExt kubernetes metadata. In case of collisions, rightmost entity wins
existingMetaAnnotations, existingSpecAnnotations = mergeKubernetesMetadataAnnotations(options, inputAnnotations, existingMetaAnnotations, existingSpecAnnotations)
existingMetaAnnotations, existingSpecAnnotations = mergeAnnotations(options, inputAnnotations, existingMetaAnnotations, existingSpecAnnotations)
setAnnotations(dep, existingMetaAnnotations, existingSpecAnnotations)

if kubeMetadataExt != nil && kubeMetadataExt.Labels != nil {
Expand All @@ -89,7 +84,7 @@ func (r *Renderer) Render(ctx context.Context, dm conv.DataModelInterface, optio
existingMetaLabels, existingSpecLabels := getLabels(dep)

// Merge cumulative label values from Env->App->Container->InputExt kubernetes metadata. In case of collisions, rightmost entity wins
existingMetaLabels, existingSpecLabels = mergeKubernetesMetadataLabels(options, inputLabels, existingMetaLabels, existingSpecLabels)
existingMetaLabels, existingSpecLabels = mergeLabels(options, inputLabels, existingMetaLabels, existingSpecLabels)
setLabels(dep, existingMetaLabels, existingSpecLabels)

}
Expand Down Expand Up @@ -147,8 +142,8 @@ func setAnnotations(dep *appsv1.Deployment, metaAnnotations map[string]string, s
}
}

// mergeKubernetesMetadataAnnotations merges environment, application annotations with current values
func mergeKubernetesMetadataAnnotations(options renderers.RenderOptions, currAnnotations map[string]string, existingMetaAnnotations map[string]string, existingSpecAnnotations map[string]string) (map[string]string, map[string]string) {
// mergeAnnotations merges environment, application annotations with current values
func mergeAnnotations(options renderers.RenderOptions, currAnnotations map[string]string, existingMetaAnnotations map[string]string, existingSpecAnnotations map[string]string) (map[string]string, map[string]string) {
envOpts := &options.Environment
appOpts := &options.Application
mergeAnnotations := map[string]string{}
Expand All @@ -162,13 +157,13 @@ func mergeKubernetesMetadataAnnotations(options renderers.RenderOptions, currAnn
}

// Cumulative Env+App Annotations is now merged with input annotations. Existing metaAnnotations and specAnnotations are subsequently merged with the result map.
existingMetaAnnotations, existingSpecAnnotations = mergeKubernetesMetadata(mergeAnnotations, currAnnotations, existingMetaAnnotations, existingSpecAnnotations)
existingMetaAnnotations, existingSpecAnnotations = mergeMaps(mergeAnnotations, currAnnotations, existingMetaAnnotations, existingSpecAnnotations)

return existingMetaAnnotations, existingSpecAnnotations
}

// mergeKubernetesMetadataLabels merges environment, application labels with current values
func mergeKubernetesMetadataLabels(options renderers.RenderOptions, currLabels map[string]string, existingMetaLabels map[string]string, existingSpecLabels map[string]string) (map[string]string, map[string]string) {
// mergeLabels merges environment, application labels with current values
func mergeLabels(options renderers.RenderOptions, currLabels map[string]string, existingMetaLabels map[string]string, existingSpecLabels map[string]string) (map[string]string, map[string]string) {
envOpts := &options.Environment
appOpts := &options.Application
mergeLabels := map[string]string{}
Expand All @@ -182,13 +177,13 @@ func mergeKubernetesMetadataLabels(options renderers.RenderOptions, currLabels m
}

// Cumulative Env+App Labels is now merged with input labels. Existing metaLabels and specLabels are subsequently merged with the result map.
existingMetaLabels, existingSpecLabels = mergeKubernetesMetadata(mergeLabels, currLabels, existingMetaLabels, existingSpecLabels)
existingMetaLabels, existingSpecLabels = mergeMaps(mergeLabels, currLabels, existingMetaLabels, existingSpecLabels)

return existingMetaLabels, existingSpecLabels
}

// mergeKubernetesMetadata merges four maps
func mergeKubernetesMetadata(mergeMap map[string]string, newInputMap map[string]string, existingMetaMap map[string]string, existingSpecMap map[string]string) (map[string]string, map[string]string) {
// mergeMaps merges four maps
func mergeMaps(mergeMap map[string]string, newInputMap map[string]string, existingMetaMap map[string]string, existingSpecMap map[string]string) (map[string]string, map[string]string) {

// Cumulative Env+App Labels (mergeMap) is now merged with new input map. Existing metaLabels and specLabels are subsequently merged with the result map.
mergeMap = labels.Merge(mergeMap, newInputMap)
Expand Down
Loading

0 comments on commit 7ce13d5

Please sign in to comment.