Skip to content

Commit

Permalink
Merge pull request #33 from fabi200123/generate-jsonschema
Browse files Browse the repository at this point in the history
Add json-schema generator
  • Loading branch information
gabriel-samfira authored Sep 19, 2024
2 parents f896860 + c1a9d63 commit bcfe98d
Show file tree
Hide file tree
Showing 43 changed files with 6,368 additions and 305 deletions.
7 changes: 6 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ require (
github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/network/armnetwork/v5 v5.1.1
github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/resources/armresources v1.2.0
github.com/BurntSushi/toml v1.2.1
github.com/cloudbase/garm-provider-common v0.1.4-0.20240906095211-57315d4ac8ae
github.com/cloudbase/garm-provider-common v0.1.4-0.20240912084949-899c120c80ce
github.com/invopop/jsonschema v0.12.0
github.com/stretchr/testify v1.9.0
github.com/xeipuuv/gojsonschema v1.2.0
golang.org/x/crypto v0.26.0
Expand All @@ -18,18 +19,22 @@ require (
require (
github.com/Azure/azure-sdk-for-go/sdk/internal v1.5.2 // indirect
github.com/AzureAD/microsoft-authentication-library-for-go v1.1.1 // indirect
github.com/bahlo/generic-list-go v0.2.0 // indirect
github.com/buger/jsonparser v1.1.1 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/felixge/httpsnoop v1.0.4 // indirect
github.com/golang-jwt/jwt/v5 v5.0.0 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/gorilla/handlers v1.5.2 // indirect
github.com/kylelemons/godebug v1.1.0 // indirect
github.com/mailru/easyjson v0.7.7 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/minio/sio v0.4.0 // indirect
github.com/pkg/browser v0.0.0-20210911075715-681adbf594b8 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
github.com/teris-io/shortid v0.0.0-20220617161101-71ec9f2aa569 // indirect
github.com/wk8/go-ordered-map/v2 v2.1.8 // indirect
github.com/xeipuuv/gojsonpointer v0.0.0-20180127040702-4e3ac2762d5f // indirect
github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415 // indirect
golang.org/x/net v0.28.0 // indirect
Expand Down
15 changes: 13 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,12 @@ github.com/AzureAD/microsoft-authentication-library-for-go v1.1.1 h1:WpB/QDNLpMw
github.com/AzureAD/microsoft-authentication-library-for-go v1.1.1/go.mod h1:wP83P5OoQ5p6ip3ScPr0BAq0BvuPAvacpEuSzyouqAI=
github.com/BurntSushi/toml v1.2.1 h1:9F2/+DoOYIOksmaJFPw1tGFy1eDnIJXg+UHjuD8lTak=
github.com/BurntSushi/toml v1.2.1/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ=
github.com/cloudbase/garm-provider-common v0.1.4-0.20240906095211-57315d4ac8ae h1:GDcVb/ForsCtpGj2kFH+iPu6/IIj0b5pV/h27qL6+GI=
github.com/cloudbase/garm-provider-common v0.1.4-0.20240906095211-57315d4ac8ae/go.mod h1:sK26i2NpjjAjhanNKiWw8iPkqt+XeohTKpFnEP7JdZ4=
github.com/bahlo/generic-list-go v0.2.0 h1:5sz/EEAK+ls5wF+NeqDpk5+iNdMDXrh3z3nPnH1Wvgk=
github.com/bahlo/generic-list-go v0.2.0/go.mod h1:2KvAjgMlE5NNynlg/5iLrrCCZ2+5xWbdbCW3pNTGyYg=
github.com/buger/jsonparser v1.1.1 h1:2PnMjfWD7wBILjqQbt530v576A/cAbQvEW9gGIpYMUs=
github.com/buger/jsonparser v1.1.1/go.mod h1:6RYKKt7H4d4+iWqouImQ9R2FZql3VbhNgx27UK13J/0=
github.com/cloudbase/garm-provider-common v0.1.4-0.20240912084949-899c120c80ce h1:spSF26dB6llkdyEeVPDdobdbv2z09zL10MUJG/YYkoE=
github.com/cloudbase/garm-provider-common v0.1.4-0.20240912084949-899c120c80ce/go.mod h1:sK26i2NpjjAjhanNKiWw8iPkqt+XeohTKpFnEP7JdZ4=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
Expand All @@ -33,8 +37,13 @@ github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/gorilla/handlers v1.5.2 h1:cLTUSsNkgcwhgRqvCNmdbRWG0A3N4F+M2nWKdScwyEE=
github.com/gorilla/handlers v1.5.2/go.mod h1:dX+xVpaxdSw+q0Qek8SSsl3dfMk3jNddUkMzo0GtH0w=
github.com/invopop/jsonschema v0.12.0 h1:6ovsNSuvn9wEQVOyc72aycBMVQFKz7cPdMJn10CvzRI=
github.com/invopop/jsonschema v0.12.0/go.mod h1:ffZ5Km5SWWRAIN6wbDXItl95euhFz2uON45H2qjYt+0=
github.com/josharian/intern v1.0.0/go.mod h1:5DoeVV0s6jJacbCEi61lwdGj/aVlrQvzHFFd8Hwg//Y=
github.com/kylelemons/godebug v1.1.0 h1:RPNrshWIDI6G2gRW9EHilWtl7Z6Sb1BR0xunSBf0SNc=
github.com/kylelemons/godebug v1.1.0/go.mod h1:9/0rRGxNHcop5bhtWyNeEfOS8JIWk580+fNqagV/RAw=
github.com/mailru/easyjson v0.7.7 h1:UGYAvKxe3sBsEDzO8ZeWOSlIQfWFlxbzLZe7hwFURr0=
github.com/mailru/easyjson v0.7.7/go.mod h1:xzfreul335JAWq5oZzymOObrkdz5UnU4kGfJJLY9Nlc=
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/minio/sio v0.4.0 h1:u4SWVEm5lXSqU42ZWawV0D9I5AZ5YMmo2RXpEQ/kRhc=
Expand All @@ -52,6 +61,8 @@ github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsT
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/teris-io/shortid v0.0.0-20220617161101-71ec9f2aa569 h1:xzABM9let0HLLqFypcxvLmlvEciCHL7+Lv+4vwZqecI=
github.com/teris-io/shortid v0.0.0-20220617161101-71ec9f2aa569/go.mod h1:2Ly+NIftZN4de9zRmENdYbvPQeaVIYKWpLFStLFEBgI=
github.com/wk8/go-ordered-map/v2 v2.1.8 h1:5h/BUHu93oj4gIdvHHHGsScSTMijfx5PeYkE/fJgbpc=
github.com/wk8/go-ordered-map/v2 v2.1.8/go.mod h1:5nJHM5DyteebpVlHnWMV0rPz6Zp7+xBAnxjb1X5vnTw=
github.com/xeipuuv/gojsonpointer v0.0.0-20180127040702-4e3ac2762d5f h1:J9EGpcZtP0E/raorCMxlFGSTBrsSlaDGf3jU/qvAE2c=
github.com/xeipuuv/gojsonpointer v0.0.0-20180127040702-4e3ac2762d5f/go.mod h1:N2zxlSyiKSe5eX1tZViRH5QA0qijqEDrYZiPEAiq3wU=
github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415 h1:EzJWgHovont7NscjpAxXsDA8S8BMYve8Y5+7cuRE7R0=
Expand Down
157 changes: 30 additions & 127 deletions internal/spec/spec.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
appdefaults "github.com/cloudbase/garm-provider-common/defaults"
"github.com/cloudbase/garm-provider-common/params"
"github.com/cloudbase/garm-provider-common/util"
"github.com/invopop/jsonschema"
"github.com/xeipuuv/gojsonschema"
"golang.org/x/crypto/ssh"

Expand All @@ -43,121 +44,21 @@ const (
defaultDiskSizeGB int32 = 127
defaultVirtualNetworkCIDR string = "10.10.0.0/16"
defaultEphemeralDiskPlacement string = "ResourceDisk"
jsonSchema string = `
{
"$schema": "http://cloudbase.it/garm-provider-azure/schemas/extra_specs#",
"type": "object",
"description": "Schema defining supported extra specs for the Garm Azure Provider",
"properties": {
"allocate_public_ip": {
"type": "boolean",
"description": "Allocate a public IP to the VM."
},
"confidential": {
"type": "boolean",
"description": "The selected virtual machine size is confidential."
},
"use_ephemeral_storage": {
"type": "boolean",
"description": "Use ephemeral storage for the VM."
},
"use_accelerated_networking": {
"type": "boolean",
"description": "Use accelerated networking for the VM."
},
"open_inbound_ports": {
"type": "object",
"description": "A map of protocol to list of inbound ports to open.",
"properties": {
"Tcp": {
"type": "array",
"description": "List of ports to open.",
"items": {
"type": "integer",
"minimum": 1,
"maximum": 65535
}
},
"Udp": {
"type": "array",
"description": "List of ports to open.",
"items": {
"type": "integer",
"minimum": 1,
"maximum": 65535
}
}
}
},
"storage_account_type": {
"type": "string",
"description": "Azure storage account type. Default is Standard_LRS."
},
"virtual_network_cidr": {
"type": "string",
"description": "The CIDR for the virtual network."
},
"disk_size_gb": {
"type": "integer",
"description": "The size of the root disk in GB. Default is 127 GB."
},
"extra_tags": {
"type": "object",
"description": "Extra tags that will get added to all VMs spawned in a pool."
},
"ssh_public_keys": {
"type": "array",
"description": "SSH public keys to add to the admin user on Linux runners.",
"items": {
"type": "string"
}
},
"vnet_subnet_id": {
"type": "string",
"description": "The ID of the subnet to use for the VM. Must be in the same region as the VM. This is required if disable_isolated_networks is set to true, otherwise it is ignored."
},
"disable_updates": {
"type": "boolean",
"description": "Disable automatic updates on the VM."
},
"enable_boot_debug": {
"type": "boolean",
"description": "Enable boot debug on the VM."
},
"extra_packages": {
"type": "array",
"description": "Extra packages to install on the VM.",
"items": {
"type": "string"
}
},
"runner_install_template": {
"type": "string",
"description": "This option can be used to override the default runner install template. If used, the caller is responsible for the correctness of the template as well as the suitability of the template for the target OS. Use the extra_context extra spec if your template has variables in it that need to be expanded."
},
"extra_context": {
"type": "object",
"description": "Extra context that will be passed to the runner_install_template.",
"additionalProperties": {
"type": "string"
}
},
"pre_install_scripts": {
"type": "object",
"description": "A map of pre-install scripts that will be run before the runner install script. These will run as root and can be used to prep a generic image before we attempt to install the runner. The key of the map is the name of the script as it will be written to disk. The value is a byte array with the contents of the script."
},
"disable_isolated_networks": {
"type": "boolean",
"description": "Disable network isolation for the VM."
}
},
"additionalProperties": false
}
`
)

func generateJSONSchema() *jsonschema.Schema {
reflector := jsonschema.Reflector{
AllowAdditionalProperties: false,
}
// Reflect the extraSpecs struct
schema := reflector.Reflect(extraSpecs{})

return schema
}

func jsonSchemaValidation(schema json.RawMessage) error {
schemaLoader := gojsonschema.NewStringLoader(jsonSchema)
jsonSchema := generateJSONSchema()
schemaLoader := gojsonschema.NewGoLoader(jsonSchema)
extraSpecsLoader := gojsonschema.NewBytesLoader(schema)
result, err := gojsonschema.Validate(schemaLoader, extraSpecsLoader)
if err != nil {
Expand Down Expand Up @@ -202,21 +103,23 @@ func newExtraSpecsFromBootstrapData(data params.BootstrapInstance) (*extraSpecs,
}

type extraSpecs struct {
AllocatePublicIP bool `json:"allocate_public_ip"`
OpenInboundPorts map[armnetwork.SecurityRuleProtocol][]int `json:"open_inbound_ports"`
StorageAccountType armcompute.StorageAccountTypes `json:"storage_account_type"`
DiskSizeGB int32 `json:"disk_size_gb"`
ExtraTags map[string]string `json:"extra_tags"`
SSHPublicKeys []string `json:"ssh_public_keys"`
Confidential bool `json:"confidential"`
UseEphemeralStorage *bool `json:"use_ephemeral_storage"`
VirtualNetworkCIDR string `json:"virtual_network_cidr"`
UseAcceleratedNetworking *bool `json:"use_accelerated_networking"`
VnetSubnetID string `json:"vnet_subnet_id"`
DisableIsolatedNetworks *bool `json:"disable_isolated_networks"`
DisableUpdates *bool `json:"disable_updates"`
EnableBootDebug *bool `json:"enable_boot_debug"`
ExtraPackages []string `json:"extra_packages"`
AllocatePublicIP bool `json:"allocate_public_ip,omitempty" jsonschema:"description=Allocate a public IP to the VM."`
OpenInboundPorts map[armnetwork.SecurityRuleProtocol][]int `json:"open_inbound_ports,omitempty" jsonschema:"description=A map of protocol to list of inbound ports to open."`
StorageAccountType armcompute.StorageAccountTypes `json:"storage_account_type,omitempty" jsonschema:"description=Azure storage account type. Default is Standard_LRS."`
DiskSizeGB int32 `json:"disk_size_gb,omitempty" jsonschema:"description=The size of the root disk in GB. Default is 127 GB."`
ExtraTags map[string]string `json:"extra_tags,omitempty" jsonschema:"description=Extra tags that will get added to all VMs spawned in a pool."`
SSHPublicKeys []string `json:"ssh_public_keys,omitempty" jsonschema:"description=SSH public keys to add to the admin user on Linux runners."`
Confidential bool `json:"confidential,omitempty" jsonschema:"description=The selected virtual machine size is confidential."`
UseEphemeralStorage *bool `json:"use_ephemeral_storage,omitempty" jsonschema:"description=Use ephemeral storage for the VM."`
VirtualNetworkCIDR string `json:"virtual_network_cidr,omitempty" jsonschema:"description=The CIDR for the virtual network."`
UseAcceleratedNetworking *bool `json:"use_accelerated_networking,omitempty" jsonschema:"description=Use accelerated networking for the VM."`
VnetSubnetID string `json:"vnet_subnet_id,omitempty" jsonschema:"description=The ID of the subnet to use for the VM. Must be in the same region as the VM. This is required if disable_isolated_networks is set to true, otherwise it is ignored."`
DisableIsolatedNetworks *bool `json:"disable_isolated_networks,omitempty" jsonschema:"description=Disable network isolation for the VM."`
DisableUpdates *bool `json:"disable_updates,omitempty" jsonschema:"description=Disable automatic updates on the VM."`
EnableBootDebug *bool `json:"enable_boot_debug,omitempty" jsonschema:"description=Enable boot debug on the VM."`
ExtraPackages []string `json:"extra_packages,omitempty" jsonschema:"description=Extra packages to install on the VM."`
// The Cloudconfig struct from common package
cloudconfig.CloudConfigSpec
}

func (e *extraSpecs) cleanInboundPorts() {
Expand Down
Loading

0 comments on commit bcfe98d

Please sign in to comment.