Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
51 commits
Select commit Hold shift + click to select a range
9407cf8
export some functions from compare lib
rafal-slominskii Dec 1, 2022
00e0036
Add repository with program to create compare dirs executable
rafal-slominskii Dec 1, 2022
f1416c4
Merge branch 'main' into NPT-689
rafal-slominskii Dec 2, 2022
e72f492
after review fixes
rafal-slominskii Dec 2, 2022
b47ad5c
add model apply
rafal-slominskii Dec 5, 2022
269718f
add tests
rafal-slominskii Dec 6, 2022
664dc6d
review fixes #1
rafal-slominskii Dec 7, 2022
763cee6
review fixes #2
rafal-slominskii Dec 7, 2022
ec56499
review fixes #3
rafal-slominskii Dec 7, 2022
d78694a
move fetchCrds (renamed listCrds)
rafal-slominskii Dec 7, 2022
e91d1fd
delete outdated crds
rafal-slominskii Dec 8, 2022
f4e5b85
delete outdated crds only in applied group
rafal-slominskii Dec 8, 2022
ab685d1
Merge branch 'main' into NPT-689
rafal-slominskii Dec 8, 2022
9b6570d
get group name from file
rafal-slominskii Dec 12, 2022
27d8a3e
Merge branch 'main' into NPT-689
rafal-slominskii Dec 13, 2022
81e04c8
review fixes #2
rafal-slominskii Dec 13, 2022
93313e1
get crs from dynamic client
rafal-slominskii Dec 14, 2022
d1aa642
add comment to new crd situation
rafal-slominskii Dec 14, 2022
826992a
NPT-644 Add support to upgrade nexus DM using compiler
Nov 21, 2022
3415482
Fix pkg import
Nov 30, 2022
a54ae25
Fix makefile
Nov 30, 2022
72aa912
review fix #3
rafal-slominskii Dec 15, 2022
00df6d1
add installer bin to dockerfile build directory
rafal-slominskii Dec 15, 2022
8a67e72
Fix review remarks
Dec 7, 2022
5176e5c
Fix tests
pavithra-kavach Dec 17, 2022
377795f
make file changes
pavithra-kavach Dec 19, 2022
611d581
pass force flag to installer job
rafal-slominskii Dec 19, 2022
e72c21f
Handle empty crds file
pavithra-kavach Dec 19, 2022
725fdb7
Revert "pass force flag to installer job"
rafal-slominskii Dec 19, 2022
a885a78
change WORKDIR to previous location
rafal-slominskii Dec 19, 2022
a2dd401
move install.so to build dir
rafal-slominskii Dec 21, 2022
8a76e97
move install.so to build dir#2
rafal-slominskii Dec 21, 2022
ae15c7b
install.so as static library
rafal-slominskii Dec 22, 2022
613da0e
review fix #4
rafal-slominskii Jan 2, 2023
27d9227
Merge branch 'main' into NPT-689
rafal-slominskii Jan 2, 2023
62ca8b9
Merge branch 'main' into npt-644-upgrade
pavithra-kavach Jan 3, 2023
e49ba25
Merge branch 'NPT-689' into dm-upgrade-integration
pavithra-kavach Jan 3, 2023
80d45d9
change crd_force env to force in install-validator
rafal-slominskii Jan 3, 2023
b9952fa
mark adding required field as backward incompatible
rafal-slominskii Jan 5, 2023
18acc82
NPT-784 Fix group matching
pavithra-kavach Jan 6, 2023
f6ac060
mark adding required field as backward incompatible - fix
rafal-slominskii Jan 10, 2023
6d5738d
Ignore go mod errors
pavithra-kavach Jan 11, 2023
79a3750
Update go.mod with common-library
pavithra-kavach Jan 11, 2023
656b8d4
change permisssions to _generated folder
janakiramanmesh7 Jan 13, 2023
eff5a7d
Merge branch 'dm-upgrade-integration' of ssh://github.com/vmware-tanz…
janakiramanmesh7 Jan 13, 2023
cada720
allow to add new map of required fields
rafal-slominskii Jan 13, 2023
d56c6d0
change permission for build directory instead of _generated
janakiramanmesh7 Jan 16, 2023
f6cd934
Generate code
pavithra-kavach Jan 16, 2023
df233fc
Revert "Generate code"
pavithra-kavach Jan 19, 2023
0260ed2
Revert "change permission for build directory instead of _generated"
pavithra-kavach Jan 19, 2023
7665e7a
Revert "change permisssions to _generated folder"
pavithra-kavach Jan 19, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 14 additions & 11 deletions common-library/pkg/nexus-compare/compare.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,19 @@ import (
)

const (
SpecMatch = ".*properties\\/spec.*"
StatusMatch = ".*properties\\/status.*"
SingletonPath = "/is_singleton"
ApiGenPath = "/nexus-rest-api-gen"
SpecMatch = ".*properties\\/spec.*"
StatusMatch = ".*properties\\/status.*"
SingletonPath = "/is_singleton"
ApiGenPath = "/nexus-rest-api-gen"
RequiredString = "required"
)

func CompareFiles(data1, data2 []byte) (bool, *bytes.Buffer, error) {
buffer := new(bytes.Buffer)
headerColor, _ := colorful.Hex("#B9311B")
fileColor, _ := colorful.Hex("#088F8F")

name, err := getSpecName(data1)
name, err := GetSpecName(data1)
if err != nil {
return true, nil, err
}
Expand Down Expand Up @@ -163,8 +164,10 @@ func filterReport(r *dyff.Report) *dyff.Report {
for _, di := range r.Diffs {
var ds []dyff.Detail
for _, d := range di.Details {
if d.From == nil && d.To != nil { // allow to add stuff
continue
if d.From == nil && d.To != nil && !strings.HasSuffix(di.Path.String(), fmt.Sprintf("/%s", RequiredString)) { // allow to add stuff, but not in required list
if len(d.To.Content) == 0 || d.To.Content[0].Value != RequiredString { //allow to add required map field
continue
}
}
if di.Path.String() == SingletonPath && d.From.Value == strconv.FormatBool(true) && d.To.Value == strconv.FormatBool(false) { // allow singleton change to false
continue
Expand All @@ -186,11 +189,11 @@ func filterReport(r *dyff.Report) *dyff.Report {
}

func getAnnotationReport(data1, data2 []byte) (dyff.Report, error) {
aNexus, err := getMapNode(data1, []string{"metadata", "annotations", "nexus"})
aNexus, err := GetMapNode(data1, []string{"metadata", "annotations", "nexus"})
if err != nil {
return dyff.Report{}, err
}
bNexus, err := getMapNode(data2, []string{"metadata", "annotations", "nexus"})
bNexus, err := GetMapNode(data2, []string{"metadata", "annotations", "nexus"})
if err != nil {
return dyff.Report{}, err
}
Expand Down Expand Up @@ -246,7 +249,7 @@ func writeTempFile(data []byte) (*os.File, error) {
return aFile, err
}

func getSpecName(data []byte) (string, error) {
func GetSpecName(data []byte) (string, error) {
t := make(map[string]interface{})
err := yaml.Unmarshal(data, &t)
if err != nil {
Expand All @@ -255,7 +258,7 @@ func getSpecName(data []byte) (string, error) {
return t["metadata"].(map[string]interface{})["name"].(string), nil
}

func getMapNode(data []byte, path []string) (interface{}, error) {
func GetMapNode(data []byte, path []string) (interface{}, error) {
var t interface{}
var ok bool
errPath := "root"
Expand Down
63 changes: 60 additions & 3 deletions common-library/pkg/nexus-compare/compare_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,18 @@ var _ = Describe("Compare lib tests", func() {
Expect(text.String()).Should(ContainSubstring(v))
}
})
It("should return false when adding field", func() {
It("should return false and only notice change in required", func() {
ans, text, err := CompareFiles([]byte(baseSpec), []byte(addedField))
Expect(err).NotTo(HaveOccurred())
Expect(ans).To(BeFalse())
Expect(text.String()).To(BeEmpty())
Expect(ans).To(BeTrue())
changeCheck := []string{"/spec/versions/name=v1/schema/openAPIV3Schema/properties/status/properties/nexus/required", "one required field added", "- testaddrequire"}
changeCheckNot := []string{"/spec/versions/name=v1/schema/openAPIV3Schema/properties/status/properties/nexus/properties/addedField"}
for _, v := range changeCheck {
Expect(text.String()).Should(ContainSubstring(v))
}
for _, v := range changeCheckNot {
Expect(text.String()).ShouldNot(ContainSubstring(v))
}
})
It("should report change in nexus annotation", func() {
ans, text, err := CompareFiles([]byte(baseSpec), []byte(changeAnnotation))
Expand All @@ -59,6 +66,15 @@ var _ = Describe("Compare lib tests", func() {
Expect(text.String()).Should(ContainSubstring(v))
}
})
It("it shouldn't filter out added required field", func() {
ans, text, err := CompareFiles([]byte(removeRequired), []byte(baseSpec))
Expect(err).ToNot(HaveOccurred())
Expect(ans).To(BeTrue())
changeCheck := []string{"spec changes", "/spec/versions/name=v1/schema/openAPIV3Schema/properties/spec", "one field added", "required", "name"}
for _, v := range changeCheck {
Expect(text.String()).Should(ContainSubstring(v))
}
})
})

var baseSpec = `
Expand Down Expand Up @@ -228,6 +244,7 @@ spec:
required:
- sourceGeneration
- remoteGeneration
- testaddrequire
type: object
type: object
type: object
Expand Down Expand Up @@ -919,3 +936,43 @@ status:
- v1

`

var removeRequired = `
apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
annotations:
nexus: |
{"name":"gns.IgnoreChild","hierarchy":["roots.root.tsm.tanzu.vmware.com","configs.config.tsm.tanzu.vmware.com","gnses.gns.tsm.tanzu.vmware.com"],"is_singleton":false,"nexus-rest-api-gen":{"uris":null}}
creationTimestamp: null
name: ignorechilds.gns.tsm.tanzu.vmware.com
spec:
conversion:
strategy: None
group: gns.tsm.tanzu.vmware.com
versions:
- name: v1
schema:
openAPIV3Schema:
properties:
spec:
properties:
name:
type: string
type: object
status:
properties:
nexus:
properties:
remoteGeneration:
format: int64
type: integer
sourceGeneration:
format: int64
type: integer
required:
- sourceGeneration
- remoteGeneration
type: object
type: object
type: object`
7 changes: 6 additions & 1 deletion compiler/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ ADD cmd/nexus-openapi-gen /go/bin/nexus-openapi-gen
ADD cmd/gqlgen /go/bin/gqlgen

WORKDIR /go/src/github.com/vmware-tanzu/graph-framework-for-microservices/compiler
RUN mv .git .gitmodules kube-openapi gqlgen nexus ..
RUN mv .git .gitmodules kube-openapi gqlgen nexus install-validator common-library ..
RUN git config --global --add safe.directory '*' && git config --global --add url."https://github.com/".insteadOf "git@github.com:"
RUN make init_submodules

Expand All @@ -23,4 +23,9 @@ RUN go mod download
WORKDIR /go/src/github.com/vmware-tanzu/graph-framework-for-microservices/compiler
RUN go mod download

WORKDIR /go/src/github.com/vmware-tanzu/graph-framework-for-microservices/install-validator
RUN CGO_ENABLED=0 go build --trimpath -o install.so ./cmd/validate

WORKDIR /go/src/github.com/vmware-tanzu/graph-framework-for-microservices/compiler

CMD make docker.gitlab_credentials && make generate_code
21 changes: 16 additions & 5 deletions compiler/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ DATAMODEL_PATH ?= datamodel
CONFIG_FILE ?= ""
GENERATED_OUTPUT_DIRECTORY ?= generated
COMPILER_SRC_DIRECTORY ?= ""
FORCE ?= false

NEXUS_KUBEOPENAPI_VERSION ?= 7416bd4754d3c0dd8b3fa37fff53d36594f11607
NEXUS_GRAPHQLGEN_VERSION ?= 32f028bce22efeb70b47a640195bd969dbb337f0
Expand Down Expand Up @@ -57,9 +58,10 @@ docker.builder:
docker build --no-cache -t ${BUILDER_NAME}:${BUILDER_TAG} builder/

.PHONY: docker
docker: init_submodules ${BUILDER_NAME}\:${BUILDER_TAG}.image.exists build_openapigen_in_container build_gqlgen_in_container
docker: init_submodules ${BUILDER_NAME}\:${BUILDER_TAG}.image.exists build_openapigen_in_container build_gqlgen_in_container build_install_validator_in_container
git archive -o compiler.tar --format=tar HEAD
tar -rf compiler.tar ../.git ../.gitmodules ../gqlgen ../kube-openapi ../nexus
ls ..
tar -rf compiler.tar ../.git ../.gitmodules ../gqlgen ../kube-openapi ../nexus ../install-validator ../common-library
docker build --no-cache \
--build-arg BUILDER_TAG=${BUILDER_TAG} \
-t ${IMAGE_NAME}:${TAG} .
Expand Down Expand Up @@ -97,6 +99,12 @@ build_gqlgen_in_container:
build_gqlgen:
GOBIN=${PKG_NAME}/cmd go install ../gqlgen

build_install_validator_in_container:
$(call run_in_container,make build_install_validator)

build_install_validator:
cd ../install-validator && GOBIN=${PKG_NAME}/cmd go install ./cmd/validate

.PHONY: unit-test
unit-test:
ginkgo -cover ./pkg/...
Expand Down Expand Up @@ -149,14 +157,15 @@ generate_code:
CRD_MODULE_PATH=${CRD_MODULE_PATH} go run cmd/nexus-sdk/main.go -config-file ${CONFIG_FILE} -dsl ${DATAMODEL_PATH} -crd-output _generated -log-level ${LOG_LEVEL}
mv _generated/api_names.sh scripts/
@echo "Nexus Compiler: Resolving datamodel dependencies"
cd _generated && ../scripts/pin_deps.sh ${COMPILER_SRC_DIRECTORY} && go mod tidy -e
cd _generated && ../scripts/pin_deps.sh ${COMPILER_SRC_DIRECTORY} && go mod tidy -e 2>/dev/null
@echo "Nexus Compiler: Generating kuberenetes APIs"
./scripts/generate_k8s_api.sh
@echo "Nexus Compiler: Generating openapi schema"
./scripts/generate_openapi_schema.sh
@echo "Nexus Compiler: Generating CRD yamls"
go run cmd/generate-openapischema/generate-openapischema.go -yamls-path _generated/crds
go run cmd/generate-openapischema/generate-openapischema.go -yamls-path _generated/crds -existing-CRDs-Path ${GENERATED_OUTPUT_DIRECTORY}/crds -force ${FORCE}
git checkout -- pkg/openapi_generator/openapi/openapi_generated.go
rm -rf ${GENERATED_OUTPUT_DIRECTORY}/{client,apis,crds,common,nexus-client,helper,nexus-gql}
cp -r _generated/{client,apis,crds,common,nexus-client,helper,nexus-gql} ${GENERATED_OUTPUT_DIRECTORY}
@echo "Nexus Compiler: Generating GRAPHQL PKG"
cd _generated && goimports -w .
Expand All @@ -165,7 +174,7 @@ generate_code:
cd ${GOPATH}/src/nexustempmodule && cd nexus-gql && go mod init && \
go mod edit -replace nexustempmodule=${GOPATH}/src/nexustempmodule && \
${COMPILER_SRC_DIRECTORY}/scripts/pin_graphql_build_version.sh ${COMPILER_SRC_DIRECTORY} && \
go mod tidy -e && \
go mod tidy -e 2>/dev/null && \
CGO_ENABLED=1 GOOS=linux \
go build --trimpath -o graphql.so -buildmode=plugin server.go
@echo "Updating module name"
Expand All @@ -176,6 +185,8 @@ generate_code:
@echo "Nexus Compiler: Moving files to output directory"
cp -r _generated/{client,apis,crds,common,nexus-client,helper,nexus-gql} ${GENERATED_OUTPUT_DIRECTORY}
cp -r ${GOPATH}/src/nexustempmodule/nexus-gql/graphql.so ${GENERATED_OUTPUT_DIRECTORY}/nexus-gql
mkdir -p ${GENERATED_OUTPUT_DIRECTORY}/install-validator
cp -r ${GOPATH}/src/github.com/vmware-tanzu/graph-framework-for-microservices/install-validator/install.so ${GENERATED_OUTPUT_DIRECTORY}/install-validator/install.so || echo "Could not find install.so file, skipping"
@echo "Nexus Compiler: Compiler code generation completed"

.PHONY: test_generate_code_in_container
Expand Down
18 changes: 17 additions & 1 deletion compiler/cmd/generate-openapischema/generate-openapischema.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"flag"
"fmt"
"strconv"
"strings"

generator "github.com/vmware-tanzu/graph-framework-for-microservices/compiler/pkg/openapi_generator"
Expand All @@ -11,13 +12,24 @@ import (
)

func main() {
var yamlsPath string
var (
yamlsPath string
existingCRDsPath string
forceUpgrade string // Used to denote the forced upgrade of a data model.
)
flag.StringVar(&yamlsPath, "yamls-path", "", "Path to directory containing CRD YAML definitions")
flag.StringVar(&existingCRDsPath, "existing-CRDs-Path", "", "Path to directory containing existing CRD YAML definitions")
flag.StringVar(&forceUpgrade, "force", "", "Set to true to force the nexus datamodel upgrade. \"+\n\t\t\t\"Defaults to `false`")
flag.Parse()
if yamlsPath == "" {
panic("yamls-path is empty. Run with -h for help")
}

force, err := strconv.ParseBool(forceUpgrade)
if err != nil {
panic(fmt.Sprintf("invalid flag for nexus datamodel upgrade %v", err))
}

ref := func(pkg string) spec.Ref {
r, err := spec.NewRef(strings.ToLower(pkg))
if err != nil {
Expand Down Expand Up @@ -48,4 +60,8 @@ func main() {
if err != nil {
panic(err)
}

if err = generator.CheckBackwardCompatibility(existingCRDsPath, yamlsPath, force); err != nil {
panic(fmt.Sprintf("Error occurred when checking datamodel compatibility: %v", err))
}
}
40 changes: 30 additions & 10 deletions compiler/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,51 +7,69 @@ require (
github.com/ghodss/yaml v1.0.0
github.com/gogo/protobuf v1.3.2
github.com/onsi/ginkgo v1.16.5
github.com/onsi/gomega v1.19.0
github.com/onsi/gomega v1.20.2
github.com/sirupsen/logrus v1.8.1
github.com/vmware-tanzu/graph-framework-for-microservices/common-library v0.0.0-20221129104902-06818e531062
github.com/vmware-tanzu/graph-framework-for-microservices/gqlgen v0.0.0-00010101000000-000000000000
github.com/vmware-tanzu/graph-framework-for-microservices/kube-openapi v0.0.0-20220603123335-7416bd4754d3
github.com/vmware-tanzu/graph-framework-for-microservices/nexus v0.0.0-00010101000000-000000000000
golang.org/x/mod v0.6.0-dev.0.20220106191415-9b9b3d81d5e3
golang.org/x/text v0.3.7
golang.org/x/tools v0.1.10
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4
golang.org/x/text v0.3.8
golang.org/x/tools v0.1.12
k8s.io/apiextensions-apiserver v0.24.0
k8s.io/apimachinery v0.24.0
k8s.io/gengo v0.0.0-20220307231824-4627b89bbf1b
)

require (
github.com/BurntSushi/toml v1.2.1 // indirect
github.com/agnivade/levenshtein v1.1.1 // indirect
github.com/cpuguy83/go-md2man/v2 v2.0.1 // indirect
github.com/elliotchance/orderedmap v1.4.0 // indirect
github.com/emicklei/go-restful v2.15.0+incompatible // indirect
github.com/fsnotify/fsnotify v1.5.1 // indirect
github.com/fsnotify/fsnotify v1.5.4 // indirect
github.com/go-logr/logr v1.2.3 // indirect
github.com/go-openapi/jsonpointer v0.19.5 // indirect
github.com/go-openapi/jsonreference v0.20.0 // indirect
github.com/go-openapi/swag v0.21.1 // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/gonvenience/bunt v1.3.4 // indirect
github.com/gonvenience/neat v1.3.11 // indirect
github.com/gonvenience/term v1.0.2 // indirect
github.com/gonvenience/text v1.0.7 // indirect
github.com/gonvenience/wrap v1.1.2 // indirect
github.com/gonvenience/ytbx v1.4.4 // indirect
github.com/google/gnostic v0.6.9 // indirect
github.com/google/go-cmp v0.5.9 // indirect
github.com/google/gofuzz v1.2.0 // indirect
github.com/homeport/dyff v1.5.6 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/lucasb-eyer/go-colorful v1.2.0 // indirect
github.com/mailru/easyjson v0.7.6 // indirect
github.com/mattn/go-ciede2000 v0.0.0-20170301095244-782e8c62fec3 // indirect
github.com/mattn/go-isatty v0.0.16 // indirect
github.com/mitchellh/go-ps v1.0.0 // indirect
github.com/mitchellh/hashstructure v1.1.0 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/nxadm/tail v1.4.8 // indirect
github.com/russross/blackfriday/v2 v2.1.0 // indirect
github.com/sergi/go-diff v1.2.0 // indirect
github.com/texttheater/golang-levenshtein v1.0.1 // indirect
github.com/urfave/cli/v2 v2.8.1 // indirect
github.com/vektah/gqlparser/v2 v2.5.0 // indirect
github.com/virtuald/go-ordered-json v0.0.0-20170621173500-b18e6e673d74 // indirect
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 // indirect
golang.org/x/net v0.0.0-20220225172249-27dd8689420f // indirect
golang.org/x/sys v0.0.0-20220209214540-3681064d5158 // indirect
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect
golang.org/x/net v0.0.0-20220722155237-a158d28d115b // indirect
golang.org/x/sync v0.0.0-20220819030929-7fc1605a5dde // indirect
golang.org/x/sys v0.1.0 // indirect
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211 // indirect
google.golang.org/protobuf v1.28.0 // indirect
gopkg.in/check.v1 v1.0.0-20200902074654-038fdea0a05b // indirect
gopkg.in/inf.v0 v0.9.1 // indirect
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
k8s.io/apimachinery v0.24.0 // indirect
k8s.io/klog/v2 v2.60.1 // indirect
k8s.io/utils v0.0.0-20220210201930-3a6ce19ff2f9 // indirect
sigs.k8s.io/json v0.0.0-20211208200746-9f7c6b3444d2 // indirect
Expand All @@ -63,3 +81,5 @@ replace github.com/vmware-tanzu/graph-framework-for-microservices/kube-openapi =
replace github.com/vmware-tanzu/graph-framework-for-microservices/gqlgen => ../gqlgen

replace github.com/vmware-tanzu/graph-framework-for-microservices/nexus => ../nexus

replace github.com/vmware-tanzu/graph-framework-for-microservices/common-library => ../common-library
Loading