diff --git a/.lighthouse/jenkins-x/release.yaml b/.lighthouse/jenkins-x/release.yaml index 95fdb2d..05fa990 100755 --- a/.lighthouse/jenkins-x/release.yaml +++ b/.lighthouse/jenkins-x/release.yaml @@ -28,6 +28,12 @@ spec: resources: {} - name: build-make-build resources: {} + - image: golang:1.15 + name: build-gen-schema + resources: {} + script: | + #!/bin/sh + make gen-schema - name: promote-changelog resources: {} podTemplate: {} diff --git a/Makefile b/Makefile index 0bcb54d..6fc797d 100644 --- a/Makefile +++ b/Makefile @@ -6,9 +6,13 @@ GOTEST := $(GO) test PACKAGE_DIRS := $(shell $(GO) list ./... | grep -v /vendor/) GO_DEPENDENCIES := $(shell find . -type f -name '*.go') +.PHONY: build build: $(GO) build ./... +.PHONY: linux +linux: build + test: build $(GOTEST) -coverprofile=coverage.out ./... @@ -46,8 +50,13 @@ cover: code-generate: ./hack/generate.sh -.PHONY: docs +.PHONY: docs gen-schema docs: generate-refdocs +.PHONY: gen-schema +gen-schema: + mkdir -p schema + go run cmd/schemagen/main.go + include Makefile.codegen diff --git a/cmd/schemagen/main.go b/cmd/schemagen/main.go new file mode 100644 index 0000000..b432f16 --- /dev/null +++ b/cmd/schemagen/main.go @@ -0,0 +1,83 @@ +package main + +import ( + "bytes" + "encoding/json" + "fmt" + "github.com/jenkins-x/jx-api/v4/pkg/apis/core/v4beta1" + "github.com/jenkins-x/jx-api/v4/pkg/util" + "github.com/jenkins-x/jx-logging/v3/pkg/log" + "github.com/pkg/errors" + "io/ioutil" + "os" +) + +const () + +func main() { + o := &Options{} + if len(os.Args) > 1 { + o.Out = os.Args[1] + } + err := o.Run() + if err != nil { + log.Logger().Errorf("failed: %v", err) + os.Exit(1) + } + log.Logger().Infof("completed the plugin generator") + os.Exit(0) +} + +type Options struct { + Out string +} + +// Run implements this command +func (o *Options) Run() error { + if o.Out == "" { + o.Out = "schema/jx-requirements.json" + } + return o.Generate("jx-requirements.yml", &v4beta1.Requirements{}) +} + +// Generate generates the schema document +func (o *Options) Generate(schemaName string, schemaTarget interface{}) error { + schema := util.GenerateSchema(schemaTarget) + if schema == nil { + return fmt.Errorf("could not generate schema for %s", schemaName) + } + + output := prettyPrintJSON(schema) + + if output == "" { + tempOutput, err := json.Marshal(schema) + if err != nil { + return errors.Wrapf(err, "error outputting schema for %s", schemaName) + } + output = string(tempOutput) + } + log.Logger().Infof("JSON schema for %s:", schemaName) + + if o.Out != "" { + err := ioutil.WriteFile(o.Out, []byte(output), util.DefaultWritePermissions) + if err != nil { + return errors.Wrapf(err, "failed to save file %s", o.Out) + } + log.Logger().Infof("wrote file %s", o.Out) + return nil + } + log.Logger().Infof("%s", output) + return nil +} + +func prettyPrintJSON(input interface{}) string { + output := &bytes.Buffer{} + if err := json.NewEncoder(output).Encode(input); err != nil { + return "" + } + formatted := &bytes.Buffer{} + if err := json.Indent(formatted, output.Bytes(), "", " "); err != nil { + return "" + } + return string(formatted.Bytes()) +} diff --git a/schema/jx-requirements.json b/schema/jx-requirements.json new file mode 100755 index 0000000..9e4d04d --- /dev/null +++ b/schema/jx-requirements.json @@ -0,0 +1,450 @@ +{ + "$schema": "http://json-schema.org/draft-04/schema#", + "$ref": "#/definitions/Requirements", + "definitions": { + "AutoUpdateConfig": { + "properties": { + "autoMerge": { + "type": "boolean" + }, + "enabled": { + "type": "boolean" + }, + "schedule": { + "type": "string" + } + }, + "additionalProperties": false, + "type": "object" + }, + "AzureConfig": { + "properties": { + "dns": { + "$schema": "http://json-schema.org/draft-04/schema#", + "$ref": "#/definitions/AzureDNSConfig" + }, + "registrySubscription": { + "type": "string" + }, + "secretStorage": { + "$schema": "http://json-schema.org/draft-04/schema#", + "$ref": "#/definitions/AzureSecretConfig" + }, + "storage": { + "$schema": "http://json-schema.org/draft-04/schema#", + "$ref": "#/definitions/AzureStorageConfig" + } + }, + "additionalProperties": false, + "type": "object" + }, + "AzureDNSConfig": { + "properties": { + "resourceGroup": { + "type": "string" + }, + "subscriptionId": { + "type": "string" + }, + "tenantId": { + "type": "string" + } + }, + "additionalProperties": false, + "type": "object" + }, + "AzureSecretConfig": { + "properties": { + "keyVaultName": { + "type": "string" + } + }, + "additionalProperties": false, + "type": "object" + }, + "AzureStorageConfig": { + "properties": { + "storageAccountName": { + "type": "string" + } + }, + "additionalProperties": false, + "type": "object" + }, + "ClusterConfig": { + "properties": { + "azure": { + "$schema": "http://json-schema.org/draft-04/schema#", + "$ref": "#/definitions/AzureConfig" + }, + "chartKind": { + "type": "string" + }, + "chartRepository": { + "type": "string" + }, + "chartSecret": { + "type": "string" + }, + "clusterName": { + "type": "string" + }, + "devEnvApprovers": { + "items": { + "type": "string" + }, + "type": "array" + }, + "dockerRegistryOrg": { + "type": "string" + }, + "environmentGitOwner": { + "type": "string" + }, + "environmentGitPublic": { + "type": "boolean" + }, + "externalDNSSAName": { + "type": "string" + }, + "gitKind": { + "type": "string" + }, + "gitName": { + "type": "string" + }, + "gitPublic": { + "type": "boolean" + }, + "gitServer": { + "type": "string" + }, + "gke": { + "$schema": "http://json-schema.org/draft-04/schema#", + "$ref": "#/definitions/GKEConfig" + }, + "kanikoFlags": { + "type": "string" + }, + "kanikoSAName": { + "type": "string" + }, + "project": { + "type": "string" + }, + "provider": { + "type": "string" + }, + "region": { + "type": "string" + }, + "registry": { + "type": "string" + }, + "zone": { + "type": "string" + } + }, + "additionalProperties": false, + "type": "object" + }, + "EnvironmentConfig": { + "properties": { + "gitKind": { + "type": "string" + }, + "gitServer": { + "type": "string" + }, + "gitUrl": { + "type": "string" + }, + "ingress": { + "$schema": "http://json-schema.org/draft-04/schema#", + "$ref": "#/definitions/IngressConfig" + }, + "key": { + "type": "string" + }, + "namespace": { + "type": "string" + }, + "owner": { + "type": "string" + }, + "promotionStrategy": { + "type": "string" + }, + "remoteCluster": { + "type": "boolean" + }, + "repository": { + "type": "string" + } + }, + "additionalProperties": false, + "type": "object" + }, + "GKEConfig": { + "properties": { + "projectNumber": { + "type": "string" + } + }, + "additionalProperties": false, + "type": "object" + }, + "IngressConfig": { + "properties": { + "annotations": { + "patternProperties": { + ".*": { + "type": "string" + } + }, + "type": "object" + }, + "cloud_dns_secret_name": { + "type": "string" + }, + "domain": { + "type": "string" + }, + "externalDNS": { + "type": "boolean" + }, + "ignoreLoadBalancer": { + "type": "boolean" + }, + "kind": { + "type": "string" + }, + "namespaceSubDomain": { + "type": "string" + }, + "tls": { + "$schema": "http://json-schema.org/draft-04/schema#", + "$ref": "#/definitions/TLSConfig" + } + }, + "additionalProperties": false, + "type": "object" + }, + "Requirements": { + "properties": { + "apiVersion": { + "type": "string" + }, + "kind": { + "type": "string" + }, + "spec": { + "$schema": "http://json-schema.org/draft-04/schema#", + "$ref": "#/definitions/RequirementsConfig" + } + }, + "additionalProperties": false, + "type": "object" + }, + "RequirementsConfig": { + "properties": { + "autoUpdate": { + "$schema": "http://json-schema.org/draft-04/schema#", + "$ref": "#/definitions/AutoUpdateConfig" + }, + "cluster": { + "$schema": "http://json-schema.org/draft-04/schema#", + "$ref": "#/definitions/ClusterConfig" + }, + "environments": { + "items": { + "$schema": "http://json-schema.org/draft-04/schema#", + "$ref": "#/definitions/EnvironmentConfig" + }, + "type": "array" + }, + "ingress": { + "$ref": "#/definitions/IngressConfig" + }, + "kuberhealthy": { + "type": "boolean" + }, + "pipelineUser": { + "$schema": "http://json-schema.org/draft-04/schema#", + "$ref": "#/definitions/UserNameEmailConfig" + }, + "repository": { + "type": "string" + }, + "secretStorage": { + "type": "string" + }, + "storage": { + "items": { + "$schema": "http://json-schema.org/draft-04/schema#", + "$ref": "#/definitions/StorageConfig" + }, + "type": "array" + }, + "terraform": { + "type": "boolean" + }, + "terraformVault": { + "type": "boolean" + }, + "vault": { + "$schema": "http://json-schema.org/draft-04/schema#", + "$ref": "#/definitions/VaultConfig" + }, + "webhook": { + "type": "string" + } + }, + "additionalProperties": false, + "type": "object" + }, + "StorageConfig": { + "properties": { + "name": { + "type": "string" + }, + "url": { + "type": "string" + } + }, + "additionalProperties": false, + "type": "object" + }, + "TLSConfig": { + "properties": { + "email": { + "type": "string" + }, + "enabled": { + "type": "boolean" + }, + "production": { + "type": "boolean" + }, + "secretName": { + "type": "string" + } + }, + "additionalProperties": false, + "type": "object" + }, + "UserNameEmailConfig": { + "properties": { + "email": { + "type": "string" + }, + "username": { + "type": "string" + } + }, + "additionalProperties": false, + "type": "object" + }, + "VaultAWSConfig": { + "properties": { + "autoCreate": { + "type": "boolean" + }, + "dynamoDBRegion": { + "type": "string" + }, + "dynamoDBTable": { + "type": "string" + }, + "iamUserName": { + "type": "string" + }, + "kmsKeyId": { + "type": "string" + }, + "kmsRegion": { + "type": "string" + }, + "s3Bucket": { + "type": "string" + }, + "s3Prefix": { + "type": "string" + }, + "s3Region": { + "type": "string" + } + }, + "additionalProperties": false, + "type": "object" + }, + "VaultAzureConfig": { + "properties": { + "containerName": { + "type": "string" + }, + "keyName": { + "type": "string" + }, + "storageAccountName": { + "type": "string" + }, + "tenantId": { + "type": "string" + }, + "vaultName": { + "type": "string" + } + }, + "additionalProperties": false, + "type": "object" + }, + "VaultConfig": { + "properties": { + "aws": { + "$schema": "http://json-schema.org/draft-04/schema#", + "$ref": "#/definitions/VaultAWSConfig" + }, + "azure": { + "$schema": "http://json-schema.org/draft-04/schema#", + "$ref": "#/definitions/VaultAzureConfig" + }, + "bucket": { + "type": "string" + }, + "disableURLDiscovery": { + "type": "boolean" + }, + "key": { + "type": "string" + }, + "keyring": { + "type": "string" + }, + "kubernetesAuthPath": { + "type": "string" + }, + "name": { + "type": "string" + }, + "namespace": { + "type": "string" + }, + "recreateBucket": { + "type": "boolean" + }, + "secretEngineMountPoint": { + "type": "string" + }, + "serviceAccount": { + "type": "string" + }, + "url": { + "type": "string" + } + }, + "additionalProperties": false, + "type": "object" + } + } +}