Skip to content

Commit fd60853

Browse files
authored
Create set-name-prefix function (#925)
1 parent ae4385b commit fd60853

File tree

5 files changed

+114
-3
lines changed

5 files changed

+114
-3
lines changed

functions/go/set-gcp-resource-ids/pkg/kpt/packagecontext.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package common
1+
package kpt
22

33
import (
44
"fmt"

functions/go/set-gcp-resource-ids/pkg/kpt/resourcegroup.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package common
1+
package kpt
22

33
import (
44
"github.com/GoogleContainerTools/kpt-functions-sdk/go/fn"
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# GCP project to use for development
2+
export GCP_PROJECT_ID ?= $(shell gcloud config get-value project)
3+
export IMAGE_REPO ?= gcr.io/$(GCP_PROJECT_ID)
4+
export IMAGE_TAG ?= latest
5+
6+
build:
7+
KO_DOCKER_REPO=ko.local ko build -B --tags=${IMAGE_TAG} .
8+
9+
push:
10+
KO_DOCKER_REPO=${IMAGE_REPO} ko build -B --tags=${IMAGE_TAG} .
11+
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"os"
6+
"strings"
7+
8+
"github.com/GoogleContainerTools/kpt-functions-catalog/functions/go/bind/pkg/rename"
9+
"github.com/GoogleContainerTools/kpt-functions-catalog/functions/go/set-gcp-resource-ids/pkg/kpt"
10+
"github.com/GoogleContainerTools/kpt-functions-sdk/go/fn"
11+
"k8s.io/apimachinery/pkg/runtime/schema"
12+
)
13+
14+
func main() {
15+
// TODO: fn.AsMain should support an "easy mode" where it runs against a directory
16+
if err := fn.AsMain(fn.ResourceListProcessorFunc(Run)); err != nil {
17+
os.Exit(1)
18+
}
19+
}
20+
21+
type SetNamePrefix struct {
22+
Prefix string `json:"prefix,omitempty"`
23+
OldPrefix string `json:"oldPrefix,omitempty"`
24+
}
25+
26+
func Run(rl *fn.ResourceList) (bool, error) {
27+
f := SetNamePrefix{
28+
OldPrefix: "packagename",
29+
}
30+
31+
packageContext, err := kpt.FindPackageContext(rl.Items)
32+
if err != nil {
33+
return false, err
34+
}
35+
f.Prefix = packageContext.Name
36+
37+
if err := f.LoadConfig(rl.FunctionConfig); err != nil {
38+
rl.Results = append(rl.Results, fn.ErrorConfigObjectResult(fmt.Errorf("functionConfig error: %w", err), rl.FunctionConfig))
39+
return true, nil
40+
}
41+
42+
if f.Prefix == "" {
43+
return false, fmt.Errorf("`data.prefix` should not be empty")
44+
}
45+
46+
if err := f.Transform(rl.Items); err != nil {
47+
rl.Results = append(rl.Results, fn.ErrorResult(err))
48+
}
49+
return true, nil
50+
}
51+
52+
func (f *SetNamePrefix) LoadConfig(fnConfig *fn.KubeObject) error {
53+
if fnConfig != nil {
54+
switch { //TODO: fnConfig.GroupVersionKind()
55+
case fnConfig.IsGVK("", "v1", "ConfigMap"):
56+
data := fnConfig.UpsertMap("data") // TODO: Why does GetMap fail?
57+
if s, ok, _ := data.NestedString("prefix"); ok {
58+
f.Prefix = s
59+
}
60+
if s, ok, _ := data.NestedString("oldPrefix"); ok {
61+
f.OldPrefix = s
62+
}
63+
64+
default:
65+
gvk := schema.GroupVersionKind{}
66+
return fmt.Errorf("unknown functionConfig Kind %v", gvk) //TODO: fnConfig.GroupVersionKind())
67+
}
68+
}
69+
70+
return nil
71+
}
72+
73+
func (f *SetNamePrefix) Transform(objects fn.KubeObjects) error {
74+
for _, object := range objects {
75+
if object.IsLocalConfig() {
76+
continue
77+
}
78+
if kpt.IsResourceGroup(object) {
79+
continue // Should ResourceGroup be marked as local config?
80+
}
81+
oldName := object.GetName()
82+
newNamespace := object.GetNamespace() // do not change namespace
83+
if oldName != "" {
84+
if oldName == f.OldPrefix {
85+
newName := f.Prefix
86+
if err := rename.Rename(object, newName, newNamespace, objects); err != nil {
87+
return err
88+
}
89+
} else if strings.HasPrefix(oldName, f.OldPrefix+"-") {
90+
suffix := strings.TrimPrefix(oldName, f.OldPrefix+"-")
91+
newName := f.Prefix + "-" + suffix
92+
if err := rename.Rename(object, newName, newNamespace, objects); err != nil {
93+
return err
94+
}
95+
}
96+
}
97+
}
98+
99+
return nil
100+
}

scripts/verify-docs.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
functions_directory = 'functions'
3838
lang_dirs = ['go', 'ts']
3939
examples_directories_to_skip = ['_template']
40-
fn_directories_to_skip = ['bind', 'inflate-helm-chart', 'set-gcp-resource-ids']
40+
fn_directories_to_skip = ['bind', 'inflate-helm-chart', 'set-gcp-resource-ids', 'set-name-prefix']
4141
required_fields = ['image', 'description', 'tags', 'sourceURL', 'examplePackageURLs', 'emails', 'license']
4242
kpt_team_email = '[email protected]'
4343
disallowed_kpt_commands = ['kpt fn run', 'kpt cfg', 'kpt pkg cat']

0 commit comments

Comments
 (0)