-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ Move schema validation to sdk to be used by different repos (#21)
* Move schema validation to sdk to be used by different repos Signed-off-by: Mauro Morales <mauro.morales@spectrocloud.com> * remove unused Signed-off-by: Mauro Morales <mauro.morales@spectrocloud.com> * fix recommendations Signed-off-by: Mauro Morales <mauro.morales@spectrocloud.com> --------- Signed-off-by: Mauro Morales <mauro.morales@spectrocloud.com>
- Loading branch information
1 parent
3524a69
commit c90740d
Showing
13 changed files
with
992 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package schema | ||
|
||
import ( | ||
jsonschemago "github.com/swaggest/jsonschema-go" | ||
) | ||
|
||
// InstallSchema represents the install block in the Kairos configuration. It is used to drive automatic installations without user interaction. | ||
type InstallSchema struct { | ||
_ struct{} `title:"Kairos Schema: Install block" description:"The install block is to drive automatic installations without user interaction."` | ||
Auto bool `json:"auto,omitempty" description:"Set to true when installing without Pairing"` | ||
BindMounts []string `json:"bind_mounts,omitempty"` | ||
Bundles []BundleSchema `json:"bundles,omitempty" description:"Add bundles in runtime"` | ||
Device string `json:"device,omitempty" pattern:"^(auto|/|(/[a-zA-Z0-9_-]+)+)$" description:"Device for automated installs" examples:"[\"auto\",\"/dev/sda\"]"` | ||
EphemeralMounts []string `json:"ephemeral_mounts,omitempty"` | ||
EncryptedPartitions []string `json:"encrypted_partitions,omitempty"` | ||
Env []interface{} `json:"env,omitempty"` | ||
GrubOptionsSchema `json:"grub_options,omitempty"` | ||
Image string `json:"image,omitempty" description:"Use a different container image for the installation"` | ||
PowerManagement | ||
SkipEncryptCopyPlugins bool `json:"skip_copy_kcrypt_plugin,omitempty"` | ||
} | ||
|
||
// BundleSchema represents the bundle block which can be used in different places of the Kairos configuration. It is used to reference a bundle and its confguration. | ||
type BundleSchema struct { | ||
DB string `json:"db_path,omitempty"` | ||
LocalFile bool `json:"local_file,omitempty"` | ||
Repository string `json:"repository,omitempty"` | ||
Rootfs string `json:"rootfs_path,omitempty"` | ||
Targets []string `json:"targets,omitempty"` | ||
} | ||
|
||
// GrubOptionsSchema represents the grub options block which can be used in different places of the Kairos configuration. It is used to configure grub. | ||
type GrubOptionsSchema struct { | ||
DefaultFallback string `json:"default_fallback,omitempty" description:"Sets default fallback logic"` | ||
DefaultMenuEntry string `json:"default_menu_entry,omitempty" description:"Change GRUB menu entry"` | ||
ExtraActiveCmdline string `json:"extra_active_cmdline,omitempty" description:"Additional Kernel option cmdline to apply just for active"` | ||
ExtraCmdline string `json:"extra_cmdline,omitempty" description:"Additional Kernel option cmdline to apply"` | ||
ExtraPassiveCmdline string `json:"extra_passive_cmdline,omitempty" description:"Additional Kernel option cmdline to apply just for passive"` | ||
ExtraRecoveryCmdline string `json:"extra_recovery_cmdline,omitempty" description:"Set additional boot commands when booting into recovery"` | ||
NextEntry string `json:"next_entry,omitempty" description:"Set the next reboot entry."` | ||
SavedEntry string `json:"saved_entry,omitempty" description:"Set the default boot entry."` | ||
} | ||
|
||
// PowerManagement is a meta structure to hold the different rules for managing power, which are not compatible between each other. | ||
type PowerManagement struct { | ||
} | ||
|
||
// NoPowerManagement is a meta structure used when the user does not define any power management options or when the user does not want to reboot or poweroff the machine. | ||
type NoPowerManagement struct { | ||
Reboot bool `json:"reboot,omitempty" const:"false" default:"false" description:"Reboot after installation"` | ||
Poweroff bool `json:"poweroff,omitempty" const:"false" default:"false" description:"Power off after installation"` | ||
} | ||
|
||
// RebootOnly is a meta structure used to enforce that when the reboot option is set, the poweroff option is not set. | ||
type RebootOnly struct { | ||
Reboot bool `json:"reboot,omitempty" const:"true" default:"false" required:"true" description:"Reboot after installation"` | ||
Poweroff bool `json:"poweroff,omitempty" const:"false" default:"false" description:"Power off after installation"` | ||
} | ||
|
||
// PowerOffOnly is a meta structure used to enforce that when the poweroff option is set, the reboot option is not set. | ||
type PowerOffOnly struct { | ||
Reboot bool `json:"reboot,omitempty" const:"false" default:"false" description:"Reboot after installation"` | ||
Poweroff bool `json:"poweroff,omitempty" const:"true" default:"false" required:"true" description:"Power off after installation"` | ||
} | ||
|
||
var _ jsonschemago.OneOfExposer = PowerManagement{} | ||
|
||
// The OneOfModel interface is only needed for the tests that check the new schema contain all needed fields | ||
// it can be removed once the new schema is the single source of truth. | ||
type OneOfModel interface { | ||
JSONSchemaOneOf() []interface{} | ||
} | ||
|
||
// JSONSchemaOneOf defines that different which are the different valid power management rules and states that one and only one of them needs to be validated for the entire schema to be valid. | ||
func (PowerManagement) JSONSchemaOneOf() []interface{} { | ||
return []interface{}{ | ||
NoPowerManagement{}, RebootOnly{}, PowerOffOnly{}, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
package schema_test | ||
|
||
import ( | ||
"strings" | ||
|
||
. "github.com/kairos-io/kairos-sdk/schema" | ||
. "github.com/onsi/ginkgo/v2" | ||
. "github.com/onsi/gomega" | ||
) | ||
|
||
var _ = Describe("Install Schema", func() { | ||
var config *KConfig | ||
var err error | ||
var yaml string | ||
|
||
JustBeforeEach(func() { | ||
config, err = NewConfigFromYAML(yaml, InstallSchema{}) | ||
Expect(err).ToNot(HaveOccurred()) | ||
}) | ||
|
||
Context("when device is auto", func() { | ||
BeforeEach(func() { | ||
yaml = `#cloud-config | ||
device: auto` | ||
}) | ||
|
||
It("succeedes", func() { | ||
Expect(config.IsValid()).To(BeTrue()) | ||
}) | ||
}) | ||
|
||
Context("when device is a path", func() { | ||
BeforeEach(func() { | ||
yaml = `#cloud-config | ||
device: /dev/sda` | ||
}) | ||
|
||
It("succeedes", func() { | ||
Expect(config.IsValid()).To(BeTrue()) | ||
}) | ||
}) | ||
|
||
Context("when device is other than a path or auto", func() { | ||
BeforeEach(func() { | ||
yaml = `#cloud-config | ||
device: foobar` | ||
}) | ||
|
||
It("errors", func() { | ||
Expect(config.IsValid()).NotTo(BeTrue()) | ||
Expect( | ||
strings.Contains(config.ValidationError.Error(), | ||
"does not match pattern '^(auto|/|(/[a-zA-Z0-9_-]+)+)$'", | ||
), | ||
).To(BeTrue()) | ||
}) | ||
}) | ||
|
||
Context("when reboot and poweroff are true", func() { | ||
BeforeEach(func() { | ||
yaml = `#cloud-config | ||
device: /dev/sda | ||
reboot: true | ||
poweroff: true` | ||
}) | ||
|
||
It("errors", func() { | ||
Expect(config.IsValid()).NotTo(BeTrue()) | ||
Expect(config.ValidationError.Error()).To(MatchRegexp("value must be false")) | ||
}) | ||
}) | ||
|
||
Context("when reboot is true and poweroff is false", func() { | ||
BeforeEach(func() { | ||
yaml = `#cloud-config | ||
device: /dev/sda | ||
reboot: true | ||
poweroff: false` | ||
}) | ||
|
||
It("succeedes", func() { | ||
Expect(config.IsValid()).To(BeTrue()) | ||
}) | ||
}) | ||
|
||
Context("when reboot is false and poweroff is true", func() { | ||
BeforeEach(func() { | ||
yaml = `#cloud-config | ||
device: /dev/sda | ||
reboot: false | ||
poweroff: true` | ||
}) | ||
|
||
It("succeedes", func() { | ||
Expect(config.IsValid()).To(BeTrue()) | ||
}) | ||
}) | ||
|
||
Context("with no power management set", func() { | ||
BeforeEach(func() { | ||
yaml = `#cloud-config | ||
device: /dev/sda` | ||
}) | ||
|
||
It("succeedes", func() { | ||
Expect(config.IsValid()).To(BeTrue()) | ||
}) | ||
}) | ||
|
||
Context("with all possible options", func() { | ||
BeforeEach(func() { | ||
yaml = `#cloud-config | ||
device: "/dev/sda" | ||
reboot: true | ||
auto: true | ||
image: "docker:.." | ||
bundles: | ||
- rootfs_path: /usr/local/lib/extensions/<name> | ||
targets: | ||
- container://<image> | ||
grub_options: | ||
extra_cmdline: "config_url=http://" | ||
extra_active_cmdline: "config_url=http://" | ||
extra_passive_cmdline: "config_url=http://" | ||
default_menu_entry: "foobar" | ||
env: | ||
- foo=barevice: /dev/sda` | ||
}) | ||
|
||
It("succeedes", func() { | ||
Expect(config.IsValid()).To(BeTrue()) | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package schema | ||
|
||
import ( | ||
jsonschemago "github.com/swaggest/jsonschema-go" | ||
) | ||
|
||
// P2PSchema represents the P2P block in the Kairos configuration. It is used to enables and configure the p2p full-mesh functionalities. | ||
type P2PSchema struct { | ||
_ struct{} `title:"Kairos Schema: P2P block" description:"The p2p block enables the p2p full-mesh functionalities."` | ||
Role string `json:"role,omitempty" default:"none" enum:"[\"master\",\"worker\",\"none\"]"` | ||
NetworkID string `json:"network_id,omitempty" description:"User defined network-id. Can be used to have multiple clusters in the same network"` | ||
DNS bool `json:"dns,omitempty" description:"Enable embedded DNS See also: https://mudler.github.io/edgevpn/docs/concepts/overview/dns/"` | ||
DisableDHT bool `json:"disable_dht,omitempty" default:"true" description:"Disabling DHT makes co-ordination to discover nodes only in the local network"` | ||
P2PNetworkExtended | ||
VPN `json:"vpn,omitempty"` | ||
} | ||
|
||
// KubeVIPSchema represents the kubevip block in the Kairos configuration. It sets the Elastic IP used in KubeVIP. Only valid with p2p. | ||
type KubeVIPSchema struct { | ||
_ struct{} `title:"Kairos Schema: KubeVIP block" description:"Sets the Elastic IP used in KubeVIP. Only valid with p2p"` | ||
EIP string `json:"eip,omitempty" example:"192.168.1.110"` | ||
ManifestURL string `json:"manifest_url,omitempty" description:"Specify a manifest URL for KubeVIP." default:""` | ||
Enable bool `json:"enable,omitempty" description:"Enables KubeVIP"` | ||
Interface bool `json:"interface,omitempty" description:"Specifies a KubeVIP Interface" example:"ens18"` | ||
} | ||
|
||
// P2PNetworkExtended is a meta structure to hold the different rules for managing the P2P network, which are not compatible between each other. | ||
type P2PNetworkExtended struct { | ||
} | ||
|
||
// P2PAutoDisabled is used to validate that when p2p.auto is disabled, then neither p2p.auto.ha not p2p.network_token can be set. | ||
type P2PAutoDisabled struct { | ||
NetworkToken string `json:"network_token,omitempty" const:"" required:"true"` | ||
Auto struct { | ||
Enable bool `json:"enable" const:"false" required:"true"` | ||
Ha struct { | ||
Enable bool `json:"enable" const:"false"` | ||
} `json:"ha"` | ||
} `json:"auto"` | ||
} | ||
|
||
// P2PAutoEnabled is used to validate that when p2p.auto is set, p2p.network_token has to be set. | ||
type P2PAutoEnabled struct { | ||
NetworkToken string `json:"network_token" required:"true" minLength:"1" description:"network_token is the shared secret used by the nodes to co-ordinate with p2p"` | ||
Auto struct { | ||
Enable bool `json:"enable,omitempty" const:"true"` | ||
Ha struct { | ||
Enable bool `json:"enable" const:"true"` | ||
MasterNodes int `json:"master_nodes,omitempty" minimum:"1" description:"Number of HA additional master nodes. A master node is always required for creating the cluster and is implied."` | ||
} `json:"ha"` | ||
} `json:"auto,omitempty"` | ||
} | ||
|
||
var _ jsonschemago.OneOfExposer = P2PNetworkExtended{} | ||
|
||
// JSONSchemaOneOf defines that different which are the different valid p2p network rules and states that one and only one of them needs to be validated for the entire schema to be valid. | ||
func (P2PNetworkExtended) JSONSchemaOneOf() []interface{} { | ||
return []interface{}{ | ||
P2PAutoEnabled{}, P2PAutoDisabled{}, | ||
} | ||
} | ||
|
||
// VPN represents the vpn block in the Kairos configuration. | ||
type VPN struct { | ||
Create bool `json:"vpn,omitempty" default:"true"` | ||
Use bool `json:"use,omitempty" default:"true"` | ||
Envs []interface{} `json:"env,omitempty"` | ||
} |
Oops, something went wrong.