From 00954edb8219925093789b9a4c0e0ef7e1fe2352 Mon Sep 17 00:00:00 2001 From: ehila Date: Mon, 17 Apr 2023 20:22:50 -0400 Subject: [PATCH 01/13] feat: add yaml-gen tool added tool to convert config crd to yaml wwith comments and kubebuilder syntax Signed-off-by: ehila --- hack/verify-config.sh | 21 +++ hack/yaml-gen/README.md | 77 ++++++++ hack/yaml-gen/cmd.go | 32 ++++ hack/yaml-gen/configcrd/config_crd.go | 19 ++ hack/yaml-gen/opts.go | 108 +++++++++++ hack/yaml-gen/parser.go | 173 ++++++++++++++++++ .../templates/config-file.template.yaml | 3 + hack/yaml-gen/templates/custom-templates.tpl | 29 +++ hack/yaml-gen/util.go | 165 +++++++++++++++++ 9 files changed, 627 insertions(+) create mode 100755 hack/verify-config.sh create mode 100644 hack/yaml-gen/README.md create mode 100644 hack/yaml-gen/cmd.go create mode 100644 hack/yaml-gen/configcrd/config_crd.go create mode 100644 hack/yaml-gen/opts.go create mode 100644 hack/yaml-gen/parser.go create mode 100644 hack/yaml-gen/templates/config-file.template.yaml create mode 100644 hack/yaml-gen/templates/custom-templates.tpl create mode 100644 hack/yaml-gen/util.go diff --git a/hack/verify-config.sh b/hack/verify-config.sh new file mode 100755 index 0000000000..00e6ab43a8 --- /dev/null +++ b/hack/verify-config.sh @@ -0,0 +1,21 @@ +#!/bin/bash + +config_spec_path=cockpit-plugin/packaging/config-openapi-spec.json +config_file_path=packaging/microshift/config.yaml +config_doc_path=docs/howto_config.md + +config_changed=$(git diff HEAD --name-only $config_file_path) +doc_changed=$(git diff HEAD --name-only $config_doc_path) +openapi_spec_changed=$(git diff HEAD --name-only $config_spec_path) + +if [ -n "$config_changed" ] || [ -n "$doc_changed" ] || [ -n "$openapi_spec_changed" ]; then + cat - < +```yaml +{{ parseToConfigYamlOpts . true true }} +``` + +```yaml +{{ parseToConfigYamlOpts . true false }} +``` + ```yaml -dns: - baseDomain: "" -network: - clusterNetwork: - - cidr: "" - serviceNetwork: - - "" - serviceNodePortRange: "" -node: - hostnameOverride: "" - nodeIP: "" apiServer: - subjectAltNames: - - "" + advertiseAddress: "" + subjectAltNames: + - "" debugging: - logLevel: "" + logLevel: "" +dns: + baseDomain: "" etcd: - memoryLimitMB: 0 + memoryLimitMB: 0 +network: + clusterNetwork: + - cidr: "" + serviceNetwork: + - "" + serviceNodePortRange: "" +node: + hostnameOverride: "" + nodeIP: "" + ``` + ## Default Settings In case `config.yaml` is not provided, the following default settings will be used. - + ```yaml -dns: - baseDomain: microshift.example.com -network: - clusterNetwork: - - cidr: 10.42.0.0/16 - serviceNetwork: - - 10.43.0.0/16 - serviceNodePortRange: 30000-32767 -node: - hostnameOverride: "" - nodeIP: '' apiServer: - subjectAltNames: [] + advertiseAddress: "" + subjectAltNames: + - "" debugging: - logLevel: "Normal" + logLevel: Normal +dns: + baseDomain: example.com etcd: - memoryLimitMB: 0 + memoryLimitMB: 0 +network: + clusterNetwork: + - cidr: 10.42.0.0/16 + serviceNetwork: + - 10.43.0.0/16 + serviceNodePortRange: 30000-32767 +node: + hostnameOverride: "" + nodeIP: "" + ``` + ## Service NodePort range diff --git a/packaging/microshift/config.yaml b/packaging/microshift/config.yaml index 70cbbaae63..70399d3c13 100644 --- a/packaging/microshift/config.yaml +++ b/packaging/microshift/config.yaml @@ -1,35 +1,37 @@ +#!! Do Not Edit +#!! This is a generated file +apiServer: + # Kube apiserver advertise address to work around the certificates issue when requiring external access using the node IP. This will turn into the IP configured in the endpoint slice for kubernetes service. Must be a reachable IP from pods. Defaults to service network CIDR first address. + advertiseAddress: "" + # SubjectAltNames added to API server certs + subjectAltNames: + - "" +debugging: + # Valid values are: "Normal", "Debug", "Trace", "TraceAll". Defaults to "Normal". + logLevel: Normal dns: - # Base domain of the cluster. All managed DNS records will be sub-domains of this base. - #baseDomain: microshift.example.com - + # baseDomain is the base domain of the cluster. All managed DNS records will be sub-domains of this base. + # For example, given the base domain `example.com`, router exposed domains will be formed as `*.apps.example.com` by default, and API service will have a DNS entry for `api.example.com`, as well as "api-int.example.com" for internal k8s API access. + # Once set, this field cannot be changed. + # example: + # microshift.example.com + baseDomain: example.com +etcd: + # Set a memory limit on the etcd process; etcd will begin paging memory when it gets to this value. 0 means no limit. + memoryLimitMB: 0 network: - clusterNetwork: - # IP range for use by the cluster - #- cidr: 10.42.0.0/16 - - serviceNetwork: - # IP range for services in the cluster - #- 10.43.0.0/16 - - # Node ports allowed for services - #serviceNodePortRange: 30000-32767 - + # IP address pool to use for pod IPs. This field is immutable after installation. + clusterNetwork: + - # The complete block for pod IPs. + cidr: 10.42.0.0/16 + # IP address pool for services. Currently, we only support a single entry here. This field is immutable after installation. + serviceNetwork: + - 10.43.0.0/16 + # The port range allowed for Services of type NodePort. If not specified, the default of 30000-32767 will be used. Such Services without a NodePort specified will have one automatically allocated from this range. This parameter can be updated after the cluster is installed. + serviceNodePortRange: 30000-32767 node: - # If non-empty, use this string to identify the node instead of the hostname - #hostnameOverride: '' + # If non-empty, will use this string to identify the node instead of the hostname + hostnameOverride: "" + # IP address of the node, passed to the kubelet. If not specified, kubelet will use the node's default IP address. + nodeIP: "" - # IP address of the node, passed to the kubelet. - # If not specified, kubelet will use the node's default IP address. - #nodeIP: '' - -apiServer: - # The Subject Alternative Names for the external certificates in API server (defaults to hostname -A) - #subjectAltNames: [] - -debugging: - # Log verbosity ('Normal', 'Debug', 'Trace', 'TraceAll'): - #logLevel: 'Normal' - -etcd: - # Memory limit for etcd, in Megabytes: 0 is no limit. - #memoryLimitMB: 0 From 842beba1af04dca34bf58992065c5ca0e8950c81 Mon Sep 17 00:00:00 2001 From: ehila Date: Tue, 18 Apr 2023 02:14:14 -0400 Subject: [PATCH 04/13] feat: added controller-gen to be fetched and ran at verify task Signed-off-by: ehila --- Makefile | 3 ++- scripts/fetch_tools.sh | 5 +++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/Makefile b/Makefile index 1967862dd7..4c9e497256 100644 --- a/Makefile +++ b/Makefile @@ -316,7 +316,8 @@ verify-vendor-etcd: vendor-etcd # Use helper `go generate script` to dynamically config information into packaging info as well as documentation. .PHONY: generate-config verify-config generate-config: - @go generate -mod vendor ./pkg/config + ./scripts/fetch_tools.sh controller-gen && \ + go generate -mod vendor ./pkg/config verify-config: generate-config ./hack/verify-config.sh \ No newline at end of file diff --git a/scripts/fetch_tools.sh b/scripts/fetch_tools.sh index 09197cac42..2b21a233b1 100755 --- a/scripts/fetch_tools.sh +++ b/scripts/fetch_tools.sh @@ -153,6 +153,11 @@ get_lichen() { GOBIN=${DEST_DIR} GOFLAGS="" go install github.com/uw-labs/lichen@${ver} } +get_controller-gen() { + local ver="v0.11.3" + GOBIN=${DEST_DIR} GOFLAGS="" go install sigs.k8s.io/controller-tools/cmd/controller-gen@${ver} +} + tool_getters=$(declare -F | cut -d' ' -f3 | grep "get_" | sed 's/get_//g') usage() { From b0598cd6ac37765e1c9adeb9daec22f9ec450893 Mon Sep 17 00:00:00 2001 From: ehila Date: Tue, 18 Apr 2023 02:14:30 -0400 Subject: [PATCH 05/13] feat: added helper script to run generate command Signed-off-by: ehila --- hack/generate-config.sh | 20 ++++++++++++++++++++ pkg/config/config.go | 3 +-- 2 files changed, 21 insertions(+), 2 deletions(-) create mode 100755 hack/generate-config.sh diff --git a/hack/generate-config.sh b/hack/generate-config.sh new file mode 100755 index 0000000000..28b20c8226 --- /dev/null +++ b/hack/generate-config.sh @@ -0,0 +1,20 @@ +#!/bin/bash + +set -euo pipefail + +ROOTDIR=$(git rev-parse --show-toplevel) +CONTROLLER_BIN="${ROOTDIR}/_output/bin/controller-gen" + +generate_crd() { + ${CONTROLLER_BIN} crd paths=../../hack/yaml-gen/configcrd output:stdout +} + +echo "Generating packaging/microshift/config.yaml and cockpit-plugin/packaging/config-openapi-spec.json" +generate_crd | go run -mod vendor ../../hack/yaml-gen \ +-a ../../cockpit-plugin/packaging/config-openapi-spec.json \ +-o ../../packaging/microshift/config.yaml + +echo "Updating docs/howto_config.md" +generate_crd | go run -mod vendor ../../hack/yaml-gen \ +-o ../../docs/howto_config.md \ +-t ../../docs/howto_config.md diff --git a/pkg/config/config.go b/pkg/config/config.go index ed3f58a43a..da4adbc2e7 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -1,7 +1,6 @@ package config -//go:generate sh -c "controller-gen crd paths=../../hack/yaml-gen/configcrd output:stdout | go run -mod vendor ../../hack/yaml-gen -a ../../cockpit-plugin/packaging/config-openapi-spec.json -o ../../packaging/microshift/config.yaml" -//go:generate sh -c "controller-gen crd paths=../../hack/yaml-gen/configcrd output:stdout | go run -mod vendor ../../hack/yaml-gen -o ../../docs/howto_config.md -t ../../docs/howto_config.md" +//go:generate ../../hack/generate-config.sh import ( "bytes" From 93313692d75a38c6333692a5ab7b1593eabc9706 Mon Sep 17 00:00:00 2001 From: ehila Date: Tue, 18 Apr 2023 02:15:39 -0400 Subject: [PATCH 06/13] feat: added verify-config to verify task Signed-off-by: ehila --- Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Makefile b/Makefile index 4c9e497256..1607e3b01e 100644 --- a/Makefile +++ b/Makefile @@ -121,7 +121,7 @@ verify: verify-fast # Fast verification checks that developers can/should run locally .PHONY: verify-fast -verify-fast: verify-go verify-assets verify-sh verify-py +verify-fast: verify-go verify-assets verify-sh verify-py verify-config # Full verification checks that should run in CI .PHONY: verify-ci From a54f2b31942624c7278d3391dec510923ffa64a4 Mon Sep 17 00:00:00 2001 From: ehila Date: Tue, 18 Apr 2023 09:27:10 -0400 Subject: [PATCH 07/13] feat: renamed to yaml-gen to config-gen Signed-off-by: ehila --- hack/{yaml-gen => config-gen}/README.md | 0 hack/{yaml-gen => config-gen}/cmd.go | 0 hack/{yaml-gen => config-gen}/configcrd/config_crd.go | 0 hack/{yaml-gen => config-gen}/opts.go | 0 hack/{yaml-gen => config-gen}/parser.go | 0 .../templates/config-file.template.yaml | 0 .../{yaml-gen => config-gen}/templates/custom-templates.tpl | 0 hack/{yaml-gen => config-gen}/util.go | 0 hack/generate-config.sh | 6 +++--- 9 files changed, 3 insertions(+), 3 deletions(-) rename hack/{yaml-gen => config-gen}/README.md (100%) rename hack/{yaml-gen => config-gen}/cmd.go (100%) rename hack/{yaml-gen => config-gen}/configcrd/config_crd.go (100%) rename hack/{yaml-gen => config-gen}/opts.go (100%) rename hack/{yaml-gen => config-gen}/parser.go (100%) rename hack/{yaml-gen => config-gen}/templates/config-file.template.yaml (100%) rename hack/{yaml-gen => config-gen}/templates/custom-templates.tpl (100%) rename hack/{yaml-gen => config-gen}/util.go (100%) diff --git a/hack/yaml-gen/README.md b/hack/config-gen/README.md similarity index 100% rename from hack/yaml-gen/README.md rename to hack/config-gen/README.md diff --git a/hack/yaml-gen/cmd.go b/hack/config-gen/cmd.go similarity index 100% rename from hack/yaml-gen/cmd.go rename to hack/config-gen/cmd.go diff --git a/hack/yaml-gen/configcrd/config_crd.go b/hack/config-gen/configcrd/config_crd.go similarity index 100% rename from hack/yaml-gen/configcrd/config_crd.go rename to hack/config-gen/configcrd/config_crd.go diff --git a/hack/yaml-gen/opts.go b/hack/config-gen/opts.go similarity index 100% rename from hack/yaml-gen/opts.go rename to hack/config-gen/opts.go diff --git a/hack/yaml-gen/parser.go b/hack/config-gen/parser.go similarity index 100% rename from hack/yaml-gen/parser.go rename to hack/config-gen/parser.go diff --git a/hack/yaml-gen/templates/config-file.template.yaml b/hack/config-gen/templates/config-file.template.yaml similarity index 100% rename from hack/yaml-gen/templates/config-file.template.yaml rename to hack/config-gen/templates/config-file.template.yaml diff --git a/hack/yaml-gen/templates/custom-templates.tpl b/hack/config-gen/templates/custom-templates.tpl similarity index 100% rename from hack/yaml-gen/templates/custom-templates.tpl rename to hack/config-gen/templates/custom-templates.tpl diff --git a/hack/yaml-gen/util.go b/hack/config-gen/util.go similarity index 100% rename from hack/yaml-gen/util.go rename to hack/config-gen/util.go diff --git a/hack/generate-config.sh b/hack/generate-config.sh index 28b20c8226..b46246fb3f 100755 --- a/hack/generate-config.sh +++ b/hack/generate-config.sh @@ -6,15 +6,15 @@ ROOTDIR=$(git rev-parse --show-toplevel) CONTROLLER_BIN="${ROOTDIR}/_output/bin/controller-gen" generate_crd() { - ${CONTROLLER_BIN} crd paths=../../hack/yaml-gen/configcrd output:stdout + ${CONTROLLER_BIN} crd paths=../../hack/config-gen/configcrd output:stdout } echo "Generating packaging/microshift/config.yaml and cockpit-plugin/packaging/config-openapi-spec.json" -generate_crd | go run -mod vendor ../../hack/yaml-gen \ +generate_crd | go run -mod vendor ../../hack/config-gen \ -a ../../cockpit-plugin/packaging/config-openapi-spec.json \ -o ../../packaging/microshift/config.yaml echo "Updating docs/howto_config.md" -generate_crd | go run -mod vendor ../../hack/yaml-gen \ +generate_crd | go run -mod vendor ../../hack/config-gen \ -o ../../docs/howto_config.md \ -t ../../docs/howto_config.md From 74c63987fca9ed18071d30bb7ee5d3e80bb111e2 Mon Sep 17 00:00:00 2001 From: ehila Date: Tue, 18 Apr 2023 09:29:25 -0400 Subject: [PATCH 08/13] feat: changed readercloser to now close stdin Signed-off-by: ehila --- hack/config-gen/opts.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/hack/config-gen/opts.go b/hack/config-gen/opts.go index 9256c84353..c3fbccfdd0 100644 --- a/hack/config-gen/opts.go +++ b/hack/config-gen/opts.go @@ -65,10 +65,10 @@ func (opt configGenOpts) Run() error { return err } dataReader = f + defer dataReader.Close() default: dataReader = os.Stdin } - defer dataReader.Close() crdRawData, err := ioutil.ReadAll(dataReader) if err != nil { From b7484f0abbfc7c3bd731400dc400df0ae2b9b010 Mon Sep 17 00:00:00 2001 From: ehila Date: Tue, 18 Apr 2023 10:22:10 -0400 Subject: [PATCH 09/13] feat: updated comments and error messages Signed-off-by: ehila --- hack/config-gen/util.go | 33 ++++++++++++++++++++------------- 1 file changed, 20 insertions(+), 13 deletions(-) diff --git a/hack/config-gen/util.go b/hack/config-gen/util.go index 6c30e2115f..8bf3de7691 100644 --- a/hack/config-gen/util.go +++ b/hack/config-gen/util.go @@ -2,6 +2,7 @@ package main import ( "encoding/json" + "fmt" "sort" "strconv" @@ -71,17 +72,21 @@ func parseScalarJSONExample(rawJson *v1ext.JSON) string { return toYaml(results) } -// Best effort to parse raw json examples, controller-gen will break if the examples -func parseArrayJSONValue(rawJson *v1ext.JSON) []*yaml.Node { +// Best effort to parse raw json examples, controller-gen will break if the values are not valid json types. +// If the values are valid json types but in, incorrect types (i.e. string in array), we panic and spit out +// a meanginful message to show which raw json failed to be parsed into which type. There are two main reasons +// we panic here, we control the input data to change as needed and these functions will primarly execute in +// the template engine where we want to halt if anything isn't right before we write any output. +func parseArrayJSONValue(rawJson *v1ext.JSON) (node []*yaml.Node) { if rawJson == nil { return nil } var arrayContainer []interface{} if err := json.Unmarshal(rawJson.Raw, &arrayContainer); err != nil { - panic(err) + panic(fmt.Errorf("failed to parse %s into []interface{} type err: %w", string(rawJson.Raw), err)) } - _, node := parseJSONValue(arrayContainer) - return node + _, node = parseJSONValue(arrayContainer) + return } func parseMapJSONValue(rawJson *v1ext.JSON) (node []*yaml.Node) { @@ -90,10 +95,10 @@ func parseMapJSONValue(rawJson *v1ext.JSON) (node []*yaml.Node) { } var mapContainer map[string]interface{} if err := json.Unmarshal(rawJson.Raw, &mapContainer); err != nil { - panic(err) + panic(fmt.Errorf("failed to parse %s into map[string]interface{} type err: %w", string(rawJson.Raw), err)) } _, node = parseJSONValue(mapContainer) - return node + return } func parseScalarJSONValue(rawJson *v1ext.JSON) string { @@ -102,7 +107,7 @@ func parseScalarJSONValue(rawJson *v1ext.JSON) string { } var scalarContainer interface{} if err := json.Unmarshal(rawJson.Raw, &scalarContainer); err != nil { - panic(err) + panic(fmt.Errorf("failed to parse %s into interface{} type err: %w", string(rawJson.Raw), err)) } scalar, _ := parseJSONValue(scalarContainer) return scalar @@ -120,17 +125,19 @@ func parseJSONValue(jsonType interface{}) (value string, node []*yaml.Node) { value = primativeToString(jsonVal) case []interface{}: for _, k := range jsonVal { + // Note: []interface{} can be an array of objects, currently we make an assumption on primitives only. + // We don't currently use complex structures in our defaults and examples. We might want to revisit later + // if we decide to default/example full structs. value := primativeToString(k) node = append(node, &yaml.Node{Kind: yaml.ScalarNode, Value: value}) } case map[string]interface{}: // TODO: parse objects and merge into a yaml node + // Currently none of our examples or defaults in the kubebuilder comments contain + // full objects. We might want to change that in the future but for now it's un-needed complexity. yamlBytes, err := yaml.Marshal(jsonVal) if err != nil { - // Something has gone very wrong if we get to this point - // crd generation will fail if either example/default values are not complient during - // the controller-gen action. - panic(err) + panic(fmt.Errorf("failed to parse %+v into map[string]interface{} type err: %w", jsonVal, err)) } value = string(yamlBytes) } @@ -139,7 +146,7 @@ func parseJSONValue(jsonType interface{}) (value string, node []*yaml.Node) { func schemaKeyToOrderedArray[K string | int, V any](schemaProperties map[K]V) []K { var ordered = []K{} - for k, _ := range schemaProperties { + for k := range schemaProperties { ordered = append(ordered, k) } sort.Slice(ordered, func(i, j int) bool { From bf1e5f3f75b3e7a2d834679ca4b4413a6fd84d3b Mon Sep 17 00:00:00 2001 From: ehila Date: Tue, 18 Apr 2023 10:28:51 -0400 Subject: [PATCH 10/13] feat: add generate-config to all task in makefile Signed-off-by: ehila --- Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Makefile b/Makefile index 1607e3b01e..abf848e88d 100644 --- a/Makefile +++ b/Makefile @@ -94,7 +94,7 @@ GO_BUILD_FLAGS :=-tags 'include_gcs include_oss containers_image_openpgp gssapi GO_TEST_FLAGS=$(GO_BUILD_FLAGS) GO_TEST_PACKAGES=./cmd/... ./pkg/... -all: microshift etcd +all: generate-config microshift etcd # target "build:" defined in vendor/github.com/openshift/build-machinery-go/make/targets/golang/build.mk # Disable CGO when building microshift binary From 9c0fd86aae8d7f548bb85a28e01538b241b04bd0 Mon Sep 17 00:00:00 2001 From: ehila Date: Tue, 18 Apr 2023 10:44:19 -0400 Subject: [PATCH 11/13] nit: small fixes Signed-off-by: ehila --- hack/config-gen/opts.go | 2 +- hack/config-gen/parser.go | 4 ++++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/hack/config-gen/opts.go b/hack/config-gen/opts.go index c3fbccfdd0..ef46065377 100644 --- a/hack/config-gen/opts.go +++ b/hack/config-gen/opts.go @@ -99,8 +99,8 @@ func (opt configGenOpts) Run() error { if err != nil { return err } - defer file.Close() dataWriter = file + defer dataWriter.Close() } else { dataWriter = os.Stdout } diff --git a/hack/config-gen/parser.go b/hack/config-gen/parser.go index 5cc1a826db..3a6b16d01e 100644 --- a/hack/config-gen/parser.go +++ b/hack/config-gen/parser.go @@ -29,6 +29,10 @@ func (p crdParser) parseToJsonSchema(data []byte) (v1ext.JSONSchemaProps, error) return v1ext.JSONSchemaProps{}, fmt.Errorf("failed to unmarshal custom resource config: %w", err) } + if len(crd.Spec.Versions) != 1 { + return v1ext.JSONSchemaProps{}, fmt.Errorf("expected length of crd.spec.versions to be 1 but got %d", len(crd.Spec.Versions)) + } + configData := crd.Spec.Versions[0].Schema.OpenAPIV3Schema.Properties["config"] return configData, nil } From 6b7f3034c6a33ea13afd4b702f31a668e18a662b Mon Sep 17 00:00:00 2001 From: ehila Date: Tue, 18 Apr 2023 14:27:11 -0400 Subject: [PATCH 12/13] upkeep: updated docs and vendor moved wording to config-gen for the tool ran make vendor-etcd removed header info from generated config yaml Signed-off-by: ehila --- .../openshift/microshift/pkg/config/config.go | 2 ++ .../openshift/microshift/pkg/config/debugging.go | 1 + .../openshift/microshift/pkg/config/dns.go | 2 ++ .../openshift/microshift/pkg/config/network.go | 3 +++ hack/config-gen/README.md | 15 ++++++++------- hack/config-gen/cmd.go | 4 ++-- .../templates/config-file.template.yaml | 2 -- packaging/microshift/config.yaml | 2 -- 8 files changed, 18 insertions(+), 13 deletions(-) diff --git a/etcd/vendor/github.com/openshift/microshift/pkg/config/config.go b/etcd/vendor/github.com/openshift/microshift/pkg/config/config.go index e7c7e17576..da4adbc2e7 100644 --- a/etcd/vendor/github.com/openshift/microshift/pkg/config/config.go +++ b/etcd/vendor/github.com/openshift/microshift/pkg/config/config.go @@ -1,5 +1,7 @@ package config +//go:generate ../../hack/generate-config.sh + import ( "bytes" "fmt" diff --git a/etcd/vendor/github.com/openshift/microshift/pkg/config/debugging.go b/etcd/vendor/github.com/openshift/microshift/pkg/config/debugging.go index a1c40b8ed4..ca1017525d 100644 --- a/etcd/vendor/github.com/openshift/microshift/pkg/config/debugging.go +++ b/etcd/vendor/github.com/openshift/microshift/pkg/config/debugging.go @@ -5,6 +5,7 @@ import "strings" type Debugging struct { // Valid values are: "Normal", "Debug", "Trace", "TraceAll". // Defaults to "Normal". + // +kubebuilder:default="Normal" LogLevel string `json:"logLevel"` } diff --git a/etcd/vendor/github.com/openshift/microshift/pkg/config/dns.go b/etcd/vendor/github.com/openshift/microshift/pkg/config/dns.go index ca6f11e88e..a0b9cd24d6 100644 --- a/etcd/vendor/github.com/openshift/microshift/pkg/config/dns.go +++ b/etcd/vendor/github.com/openshift/microshift/pkg/config/dns.go @@ -10,5 +10,7 @@ type DNS struct { // as well as "api-int.example.com" for internal k8s API access. // // Once set, this field cannot be changed. + // +kubebuilder:default=example.com + // +kubebuilder:example=microshift.example.com BaseDomain string `json:"baseDomain"` } diff --git a/etcd/vendor/github.com/openshift/microshift/pkg/config/network.go b/etcd/vendor/github.com/openshift/microshift/pkg/config/network.go index 3a3f8cc211..62408402a5 100644 --- a/etcd/vendor/github.com/openshift/microshift/pkg/config/network.go +++ b/etcd/vendor/github.com/openshift/microshift/pkg/config/network.go @@ -15,6 +15,7 @@ type Network struct { // IP address pool for services. // Currently, we only support a single entry here. // This field is immutable after installation. + // +kubebuilder:default={"10.43.0.0/16"} ServiceNetwork []string `json:"serviceNetwork"` // The port range allowed for Services of type NodePort. @@ -24,6 +25,7 @@ type Network struct { // This parameter can be updated after the cluster is // installed. // +kubebuilder:validation:Pattern=`^([0-9]{1,4}|[1-5][0-9]{4}|6[0-4][0-9]{3}|65[0-4][0-9]{2}|655[0-2][0-9]|6553[0-5])-([0-9]{1,4}|[1-5][0-9]{4}|6[0-4][0-9]{3}|65[0-4][0-9]{2}|655[0-2][0-9]|6553[0-5])$` + // +kubebuilder:default="30000-32767" ServiceNodePortRange string `json:"serviceNodePortRange"` // The DNS server to use @@ -32,6 +34,7 @@ type Network struct { type ClusterNetworkEntry struct { // The complete block for pod IPs. + // +kubebuilder:default="10.42.0.0/16" CIDR string `json:"cidr"` } diff --git a/hack/config-gen/README.md b/hack/config-gen/README.md index a75f20997b..d4073eb78a 100644 --- a/hack/config-gen/README.md +++ b/hack/config-gen/README.md @@ -1,4 +1,4 @@ -# YAML-Gen +# Config-Gen This is a simple generator tool that will read files for a specific struct and generate a yaml representation of it with comments to help keep things in sync. This is meant to be used as part of the `//go:generate` command but can also be installed and used as a stand alone binary. @@ -14,26 +14,27 @@ CLI flags. ```sh Usage: - yaml-gen [flags] + config-gen [flags] Flags: -a, --api-output string output path for openapi spec if desired -f, --file string default is stdin - -h, --help help for yaml-gen + -h, --help help for config-gen -o, --output string output path, default is stdout -t, --template string template file to use + -v, --v Level number for the log level verbosity ``` Use as a go generate command example ```go -//go:generate sh -c "controller-gen crd paths=../../hack/yaml-gen/configcrd output:stdout | go run -mod vendor ../../hack/yaml-gen -a ../../cockpit-plugin/packaging/config-openapi-spec.json -o ../../packaging/microshift/config.yaml" -//go:generate sh -c "controller-gen crd paths=../../hack/yaml-gen/configcrd output:stdout | go run -mod vendor ../../hack/yaml-gen -o ../../docs/howto_config.md -t ../../docs/howto_config.md" +//go:generate sh -c "controller-gen crd paths=../../hack/config-gen/configcrd output:stdout | go run -mod vendor ../../hack/config-gen -a ../../cockpit-plugin/packaging/config-openapi-spec.json -o ../../packaging/microshift/config.yaml" +//go:generate sh -c "controller-gen crd paths=../../hack/config-gen/configcrd output:stdout | go run -mod vendor ../../hack/config-gen -o ../../docs/howto_config.md -t ../../docs/howto_config.md" ``` -Use the example test to see it in action, run the command from the `hack/yaml-gen` directory. +Use the example test to see it in action, run the command from the `hack/config-gen` directory. ```sh -controller-gen crd paths=../../hack/yaml-gen/configcrd output:stdout | go run -mod vendor ../../hack/yaml-gen +controller-gen crd paths=../../hack/config-gen/configcrd output:stdout | go run -mod vendor ../../hack/config-gen ``` The sample output should be. diff --git a/hack/config-gen/cmd.go b/hack/config-gen/cmd.go index 3d547dbe68..322b07b1d5 100644 --- a/hack/config-gen/cmd.go +++ b/hack/config-gen/cmd.go @@ -17,8 +17,8 @@ func newCommand() *cobra.Command { opt := configGenOpts{} cmd := &cobra.Command{ - Use: "yaml-gen", - Short: "use structs to generate yaml or embed in files", + Use: "config-gen", + Short: "use openapiv3 schemas in CRDs format to generate yaml or embed in files", RunE: func(cmd *cobra.Command, args []string) error { if err := opt.Options(); err != nil { return err diff --git a/hack/config-gen/templates/config-file.template.yaml b/hack/config-gen/templates/config-file.template.yaml index 7a0e590d90..7389508cd6 100644 --- a/hack/config-gen/templates/config-file.template.yaml +++ b/hack/config-gen/templates/config-file.template.yaml @@ -1,3 +1 @@ -#!! Do Not Edit -#!! This is a generated file {{ parseToConfigYaml . }} diff --git a/packaging/microshift/config.yaml b/packaging/microshift/config.yaml index 70399d3c13..c41c12ad09 100644 --- a/packaging/microshift/config.yaml +++ b/packaging/microshift/config.yaml @@ -1,5 +1,3 @@ -#!! Do Not Edit -#!! This is a generated file apiServer: # Kube apiserver advertise address to work around the certificates issue when requiring external access using the node IP. This will turn into the IP configured in the endpoint slice for kubernetes service. Must be a reachable IP from pods. Defaults to service network CIDR first address. advertiseAddress: "" From 30d711c277a13bb0175402f27b6fba313537de56 Mon Sep 17 00:00:00 2001 From: ehila Date: Tue, 18 Apr 2023 16:08:56 -0400 Subject: [PATCH 13/13] fix: resolved linter errors Signed-off-by: ehila --- hack/config-gen/configcrd/config_crd.go | 1 + hack/config-gen/opts.go | 4 +- hack/config-gen/parser.go | 66 +++++++++++++------------ hack/config-gen/util.go | 2 +- 4 files changed, 38 insertions(+), 35 deletions(-) diff --git a/hack/config-gen/configcrd/config_crd.go b/hack/config-gen/configcrd/config_crd.go index 2d5f6cfc83..c17dd4343d 100644 --- a/hack/config-gen/configcrd/config_crd.go +++ b/hack/config-gen/configcrd/config_crd.go @@ -11,6 +11,7 @@ import ( metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" ) +//nolint:unused type configSpec struct { metav1.TypeMeta `json:",inline"` metav1.ObjectMeta `json:"metadata,omitempty"` diff --git a/hack/config-gen/opts.go b/hack/config-gen/opts.go index ef46065377..c9f00c6bd1 100644 --- a/hack/config-gen/opts.go +++ b/hack/config-gen/opts.go @@ -2,7 +2,6 @@ package main import ( "io" - "io/ioutil" "os" "text/template" @@ -46,7 +45,6 @@ func (opt *configGenOpts) Options() error { } func (opt configGenOpts) Run() error { - yamlTemplate, err := template.New("yamlTemplate").Funcs(defaultTemplateFuncs).Parse(customTemplateBlocks) if err != nil { return err @@ -70,7 +68,7 @@ func (opt configGenOpts) Run() error { dataReader = os.Stdin } - crdRawData, err := ioutil.ReadAll(dataReader) + crdRawData, err := io.ReadAll(dataReader) if err != nil { return err } diff --git a/hack/config-gen/parser.go b/hack/config-gen/parser.go index 3a6b16d01e..76323b2703 100644 --- a/hack/config-gen/parser.go +++ b/hack/config-gen/parser.go @@ -68,48 +68,52 @@ func (p crdParser) toYamlNodeObject(val map[string]v1ext.JSONSchemaProps) *yaml. orderedKeyNameArray := schemaKeyToOrderedArray(val) for _, schemaKeyName := range orderedKeyNameArray { - if field, ok := val[schemaKeyName]; ok { - keyNode := &yaml.Node{ - Value: schemaKeyName, - Kind: yaml.ScalarNode, - } + field, ok := val[schemaKeyName] + if !ok { + // This should never happen since the ordered key array is created from the keys in val. + // This would definitely mean it's time to panic. + panic(fmt.Errorf("failed to find %s in the map of JSONSchemaProps: \nData ===\n%+v\n==", schemaKeyName, val)) + } - if !p.NoComments { - keyNode.HeadComment = field.Description - } + keyNode := &yaml.Node{ + Value: schemaKeyName, + Kind: yaml.ScalarNode, + } - valueNode := &yaml.Node{} - switch field.Type { - case jsonTypeArray: + if !p.NoComments { + keyNode.HeadComment = field.Description + } - valueNode = p.toYamlNodeArray(field.Items) - if nodes := parseArrayJSONValue(field.Default); nodes != nil && !p.NoDefaults { - valueNode.Content = nodes - } + var valueNode *yaml.Node + switch field.Type { + case jsonTypeArray: - if exampleValue := parseArrayJSONExample(field.Example); exampleValue != "" && !p.NoComments { - keyNode.HeadComment = fmt.Sprintf("%s\nexample:\n %s", keyNode.HeadComment, exampleValue) - } + valueNode = p.toYamlNodeArray(field.Items) + if nodes := parseArrayJSONValue(field.Default); nodes != nil && !p.NoDefaults { + valueNode.Content = nodes + } - case jsonTypeObject: + if exampleValue := parseArrayJSONExample(field.Example); exampleValue != "" && !p.NoComments { + keyNode.HeadComment = fmt.Sprintf("%s\nexample:\n %s", keyNode.HeadComment, exampleValue) + } - valueNode = p.toYamlNodeObject(field.Properties) + case jsonTypeObject: - if exampleValue := parseMapJSONExample(field.Example); exampleValue != "" && !p.NoComments { - keyNode.HeadComment = fmt.Sprintf("%s\nexample:\n %s", keyNode.HeadComment, exampleValue) - } + valueNode = p.toYamlNodeObject(field.Properties) - default: + if exampleValue := parseMapJSONExample(field.Example); exampleValue != "" && !p.NoComments { + keyNode.HeadComment = fmt.Sprintf("%s\nexample:\n %s", keyNode.HeadComment, exampleValue) + } - valueNode = p.toYamlNodeValue(field) + default: - if exampleValue := parseScalarJSONExample(field.Example); exampleValue != "" && !p.NoComments { - keyNode.HeadComment = fmt.Sprintf("%s\nexample:\n %s", keyNode.HeadComment, exampleValue) - } + valueNode = p.toYamlNodeValue(field) + if exampleValue := parseScalarJSONExample(field.Example); exampleValue != "" && !p.NoComments { + keyNode.HeadComment = fmt.Sprintf("%s\nexample:\n %s", keyNode.HeadComment, exampleValue) } - node.Content = append(node.Content, keyNode, valueNode) } + node.Content = append(node.Content, keyNode, valueNode) } return node } @@ -123,7 +127,7 @@ func (p crdParser) toYamlNodeArray(val *v1ext.JSONSchemaPropsOrArray) *yaml.Node } if val.Schema != nil { - valueNode := &yaml.Node{} + var valueNode *yaml.Node switch val.Schema.Type { case jsonTypeObject: valueNode = p.toYamlNodeObject(val.Schema.Properties) @@ -138,7 +142,7 @@ func (p crdParser) toYamlNodeArray(val *v1ext.JSONSchemaPropsOrArray) *yaml.Node } for _, field := range val.JSONSchemas { - valueNode := &yaml.Node{} + var valueNode *yaml.Node switch field.Type { case jsonTypeObject: valueNode = p.toYamlNodeObject(field.Properties) diff --git a/hack/config-gen/util.go b/hack/config-gen/util.go index 8bf3de7691..3d8e9b7dd0 100644 --- a/hack/config-gen/util.go +++ b/hack/config-gen/util.go @@ -75,7 +75,7 @@ func parseScalarJSONExample(rawJson *v1ext.JSON) string { // Best effort to parse raw json examples, controller-gen will break if the values are not valid json types. // If the values are valid json types but in, incorrect types (i.e. string in array), we panic and spit out // a meanginful message to show which raw json failed to be parsed into which type. There are two main reasons -// we panic here, we control the input data to change as needed and these functions will primarly execute in +// we panic here, we control the input data to change as needed and these functions will primarily execute in // the template engine where we want to halt if anything isn't right before we write any output. func parseArrayJSONValue(rawJson *v1ext.JSON) (node []*yaml.Node) { if rawJson == nil {