Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
yuwenma committed Mar 23, 2022
1 parent 01efe36 commit 062d7d0
Showing 1 changed file with 59 additions and 7 deletions.
66 changes: 59 additions & 7 deletions functions/go/set-namespace/transformer/namespace.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ package transformer

import (
"fmt"
"reflect"
"regexp"
"strings"

Expand Down Expand Up @@ -55,6 +56,11 @@ func SetNamespace(rl *fn.ResourceList) error {
rl.Results = append(rl.Results, fn.ErrorConfigObjectResult(err, rl.FunctionConfig))
return nil
}

if !tc.Validate(rl.Items) {
rl.Results = append(rl.Results,fn.GeneralResult(strings.Join(tc.Errors, "\n"), fn.Error))
return nil
}
// Update "namespace" to the proper resources.
tc.Transform(rl.Items)
var result *fn.Result
Expand All @@ -69,26 +75,29 @@ func SetNamespace(rl *fn.ResourceList) error {
}

type NamespaceTransformer struct {
Namespace string
CurrentNamespace string
NewNamespace string
DependsOnMap map[string]bool
Errors []string
}

func (p *NamespaceTransformer) Config(o *fn.KubeObject) error {
switch {
case o.IsGVK("v1", "ConfigMap"):
p.Namespace = o.GetStringOrDie("data", "namespace")
if p.Namespace == "" {
p.NewNamespace = o.GetStringOrDie("data", "namespace")
if p.NewNamespace == "" {
return fmt.Errorf("`data.namespace` should not be empty")
}
p.CurrentNamespace = o.GetStringOrDie("data", "namespace_selector")
case o.IsGVK(fnConfigAPIVersion, fnConfigKind):
p.Namespace = o.GetStringOrDie("namespace")
if p.Namespace == "" {
p.NewNamespace = o.GetStringOrDie("namespace")
if p.NewNamespace == "" {
return fmt.Errorf("`namespace` should not be empty")
}
p.CurrentNamespace = o.GetStringOrDie("selector")
case o.IsGVK("v1", "ConfigMap") && o.GetName() == "kptfile.kpt.dev":
p.Namespace = o.GetStringOrDie("data", "name")
if p.Namespace == "" {
p.NewNamespace = o.GetStringOrDie("data", "name")
if p.NewNamespace == "" {
return fmt.Errorf("`data.name` should not be empty")
}
default:
Expand All @@ -98,6 +107,49 @@ func (p *NamespaceTransformer) Config(o *fn.KubeObject) error {
return nil
}

func (p *NamespaceTransformer) Validate(objects []*fn.KubeObject) bool {
// Gather all namespace objects
nsMap := map[string]bool{}
for _, o := range objects {
if o.IsGVK("v1", "Namespace") {
nsMap[o.GetName()] = true
}
}
existingNamespaces := reflect.ValueOf(nsMap).MapKeys()
// Check no more than one namespace object exists.
if len(existingNamespaces) > 1 {
msg := fmt.Sprintf("accept no more than one namespace object but found %v: %v",
len(existingNamespaces), existingNamespaces)
p.Errors = append(p.Errors, msg)
return false
}
// Use the namespace object value as namespace selector.
if len(existingNamespaces) == 1 {
currentNamespace := existingNamespaces[0].String()
if p.CurrentNamespace == "" {
p.CurrentNamespace = currentNamespace
} else if p.CurrentNamespace != currentNamespace {
msg := fmt.Sprintf("detect namespace object %v mismatching to namespace selector %v",
currentNamespace, p.CurrentNamespace)
p.Errors = append(p.Errors, msg)
return false
}
}

// no namespace object
if p.CurrentNamespace != "" {
return true
}

for _, o := range objects {
if o.IsGVK("v1", "Namespace") {
nsMap[o.GetName()] = true
}
}

return true
}

func (p *NamespaceTransformer) Transform(objects []*fn.KubeObject) {
p.SetDependsOnMap(objects)
for _, o := range objects {
Expand Down

0 comments on commit 062d7d0

Please sign in to comment.