Skip to content

Add set-gcp-location function #760

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

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
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
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
15 changes: 15 additions & 0 deletions functions/go/gcp-set-location/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
FROM golang:1.17-alpine3.13
ENV CGO_ENABLED=0
WORKDIR /go/src/

COPY go.mod go.sum ./
RUN go mod download

COPY . .
RUN go build -o /usr/local/bin/function ./

#############################################

FROM alpine:3.13
COPY --from=0 /usr/local/bin/function /usr/local/bin/function
ENTRYPOINT ["function"]
137 changes: 137 additions & 0 deletions functions/go/gcp-set-location/consts/cloudreferences.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
package consts

var CloudRegions = map[string]bool{
"asia-east1": true,
"asia-east2": true,
"asia-northeast1": true,
"asia-northeast2": true,
"asia-northeast3": true,
"asia-south1": true,
"asia-south2": true,
"asia-southeast1": true,
"asia-southeast2": true,
"australia-southeast1": true,
"australia-southeast2": true,
"europe-central2": true,
"europe-north1": true,
"europe-west1": true,
"europe-west2": true,
"europe-west3": true,
"europe-west4": true,
"europe-west5": true,
"europe-west6": true,
"northamerica-northeast1": true,
"northamerica-northeast2": true,
"southamerica-east1": true,
"southamerica-west1": true,
"us-central1": true,
"us-central2": true,
"us-east1": true,
"us-east2": true,
"us-east4": true,
"us-west1": true,
"us-west2": true,
"us-west3": true,
"us-west4": true,
}

var CloudZones = map[string]bool{
"us-east1-b": true,
"us-east1-c": true,
"us-east1-d": true,
"us-east1-a": true,
"us-east2-a": true,
"us-east4-c": true,
"us-east4-b": true,
"us-east4-a": true,
"us-central1-c": true,
"us-central1-a": true,
"us-central1-f": true,
"us-central1-b": true,
"us-central1-d": true,
"us-central2-a": true,
"us-central2-b": true,
"us-central2-c": true,
"us-central2-d": true,
"us-west1-b": true,
"us-west1-c": true,
"us-west1-a": true,
"europe-west4-a": true,
"europe-west4-b": true,
"europe-west4-c": true,
"europe-west1-b": true,
"europe-west1-d": true,
"europe-west1-c": true,
"europe-west3-c": true,
"europe-west3-a": true,
"europe-west3-b": true,
"europe-west2-c": true,
"europe-west2-b": true,
"europe-west2-a": true,
"asia-east1-b": true,
"asia-east1-a": true,
"asia-east1-c": true,
"asia-southeast1-b": true,
"asia-southeast1-a": true,
"asia-southeast1-c": true,
"asia-northeast1-b": true,
"asia-northeast1-c": true,
"asia-northeast1-a": true,
"asia-south1-c": true,
"asia-south1-b": true,
"asia-south1-a": true,
"australia-southeast1-b": true,
"australia-southeast1-c": true,
"australia-southeast1-a": true,
"southamerica-east1-b": true,
"southamerica-east1-c": true,
"southamerica-east1-a": true,
"asia-east2-a": true,
"asia-east2-b": true,
"asia-east2-c": true,
"asia-northeast2-a": true,
"asia-northeast2-b": true,
"asia-northeast2-c": true,
"asia-northeast3-a": true,
"asia-northeast3-b": true,
"asia-northeast3-c": true,
"asia-south2-a": true,
"asia-south2-b": true,
"asia-south2-c": true,
"asia-southeast2-a": true,
"asia-southeast2-b": true,
"asia-southeast2-c": true,
"australia-southeast2-a": true,
"australia-southeast2-b": true,
"australia-southeast2-c": true,
"europe-central2-a": true,
"europe-central2-b": true,
"europe-central2-c": true,
"europe-north1-a": true,
"europe-north1-b": true,
"europe-north1-c": true,
"europe-west5-a": true,
"europe-west5-b": true,
"europe-west5-c": true,
"europe-west6-a": true,
"europe-west6-b": true,
"europe-west6-c": true,
"northamerica-northeast1-a": true,
"northamerica-northeast1-b": true,
"northamerica-northeast1-c": true,
"northamerica-northeast2-a": true,
"northamerica-northeast2-b": true,
"northamerica-northeast2-c": true,
"southamerica-west1-a": true,
"southamerica-west1-b": true,
"southamerica-west1-c": true,
"us-west2-a": true,
"us-west2-b": true,
"us-west2-c": true,
"us-west3-a": true,
"us-west3-b": true,
"us-west3-c": true,
"us-west4-a": true,
"us-west4-b": true,
"us-west4-c": true,
}
26 changes: 26 additions & 0 deletions functions/go/gcp-set-location/filedspec/location.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package filedspec

const LocationFieldSpecs = `
regions:
- path: spec/location
group: storage.cnrm.cloud.google.com
version: v1beta1
kind: StorageBucket

- path: spec/region
group: redis.cnrm.cloud.google.com
version: v1beta1
kind: RedisInstance

- path: spec/config
group: spanner.cnrm.cloud.google.com
version: v1beta1
kind: SpannerInstance
regexPattern: (?P<prefix>regional-)(?P<location>\S+)

zones:
- path: spec/location
apiVersion: test.cnrm.cloud.google.com/v1beta1
kind: Test
create: false
`
86 changes: 86 additions & 0 deletions functions/go/gcp-set-location/filter.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package main

import (
"fmt"
"regexp"

"sigs.k8s.io/kustomize/api/filters/fieldspec"
"sigs.k8s.io/kustomize/api/filters/filtersutil"
"sigs.k8s.io/kustomize/api/types"
"sigs.k8s.io/kustomize/kyaml/kio"
"sigs.k8s.io/kustomize/kyaml/yaml"
)

var _ kio.Filter = LocationFilter{}

// LocationFilter is used as Filter and Transformer
// As a Filter, it provides the Filter() function
// As a Transformer, it provides the Config function but no Transform function (redundant).
type LocationFilter struct {
Region string
Zone string
RegionFsSlice []types.FieldSpec
ZoneFsSlice []types.FieldSpec
}

func (f LocationFilter) Filter(nodes []*yaml.RNode) ([]*yaml.RNode, error) {
_, err := kio.FilterAll(yaml.FilterFunc(
func(node *yaml.RNode) (*yaml.RNode, error) {
var fns []yaml.Filter
for _, fs := range f.RegionFsSlice {
fn := fieldspec.Filter{
SetValue: updateLocationFn(fs.RegexPattern, f.Region),
FieldSpec: fs,
}
fns = append(fns, fn)
}
for _, fs := range f.ZoneFsSlice {
fn := fieldspec.Filter{
SetValue: updateLocationFn(fs.RegexPattern, f.Zone),
FieldSpec: fs,
}
fns = append(fns, fn)
}
return node.Pipe(fns...)
})).Filter(nodes)
return nodes, err
}

func updateLocationFn(regexPath, location string) filtersutil.SetFn {
return func(node *yaml.RNode) (err error) {
if regexPath == "" {
return node.PipeE(updater{location: location})
}
defer func() {
// recover from regex panic.
if recover() != nil {
err = fmt.Errorf("invalid regex pattern %v", regexPath)
}
}()
re := regexp.MustCompile(regexPath)
match := re.FindStringSubmatch(node.YNode().Value)
namedGroup := make(map[string]string)
for i, name := range re.SubexpNames() {
if i != 0 && name != "" {
namedGroup[name] = match[i]
}
}
newLocation := ""
if prefixStr, ok := namedGroup["prefix"]; ok {
newLocation = newLocation + prefixStr
}
newLocation = newLocation + location
if suffixStr, ok := namedGroup["suffix"]; ok {
newLocation = newLocation + suffixStr
}
return node.PipeE(updater{location: newLocation})
}
}

type updater struct {
location string
}

func (u updater) Filter(rn *yaml.RNode) (*yaml.RNode, error) {
return rn.Pipe(yaml.FieldSetter{StringValue: u.location})
}
35 changes: 35 additions & 0 deletions functions/go/gcp-set-location/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
module github.com/GoogleContainerTools/kpt-functions-catalog/functions/go/gcp-set-location

go 1.17

require (
sigs.k8s.io/kustomize/api v0.10.2-0.20211202184144-fe551be87b8d
sigs.k8s.io/kustomize/kyaml v0.13.3
)

require (
github.com/PuerkitoBio/purell v1.1.1 // indirect
github.com/PuerkitoBio/urlesc v0.0.0-20170810143723-de5bf2ad4578 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/go-errors/errors v1.0.1 // indirect
github.com/go-openapi/jsonpointer v0.19.3 // indirect
github.com/go-openapi/jsonreference v0.19.3 // indirect
github.com/go-openapi/swag v0.19.5 // indirect
github.com/inconshreveable/mousetrap v1.0.0 // indirect
github.com/mailru/easyjson v0.7.0 // indirect
github.com/monochromegane/go-gitignore v0.0.0-20200626010858-205db1a8cc00 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/spf13/cobra v1.2.1 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/stretchr/testify v1.7.0 // indirect
github.com/xlab/treeprint v0.0.0-20181112141820-a009c3971eca // indirect
golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4 // indirect
golang.org/x/text v0.3.5 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect
k8s.io/kube-openapi v0.0.0-20210421082810-95288971da7e // indirect
sigs.k8s.io/yaml v1.2.0 // indirect
)

replace sigs.k8s.io/kustomize/api => ./thirdparty/kustomize/api
Loading