Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add maas plugin #160

Merged
merged 19 commits into from
Aug 13, 2024
Merged
Show file tree
Hide file tree
Changes from 18 commits
Commits
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
3 changes: 2 additions & 1 deletion .gitleaksignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ e7156f3be822e9da88949cd9405e8eedde836e5f:tests/integration/_validator/testcases/
e7156f3be822e9da88949cd9405e8eedde836e5f:tests/integration/_validator/testcases/data/validator.yaml:generic-api-key:51
481cdf910c0bc555363ae4278b2f57a66a72ed6b:tests/integration/_validator/testcases/data/validator.yaml:generic-api-key:27
481cdf910c0bc555363ae4278b2f57a66a72ed6b:tests/integration/_validator/testcases/data/validator.yaml:generic-api-key:51
hack/validator.tmpl:generic-api-key:538
hack/validator.tmpl:generic-api-key:538
hack/validator.tmpl:generic-api-key:865
1 change: 1 addition & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
"DISABLE_KIND_CLUSTER_CHECK": "true",
"KUBECONFIG": "/Users/tylergillson/Downloads/vdev.kubeconfig",
"CLI_VERSION": "0.0.4-dev",
"HELM_PRESERVE_FILES": "true"
}
}
]
Expand Down
15 changes: 13 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ go 1.22.5
require (
emperror.dev/errors v0.8.1
github.com/L30Bola/aws-policy v0.0.0-20230126045340-5e6118545ac1
github.com/canonical/gomaasclient v0.6.0
github.com/fsnotify/fsnotify v1.7.0
github.com/go-logr/logr v1.4.2
github.com/google/uuid v1.6.0
Expand All @@ -19,6 +20,7 @@ require (
github.com/validator-labs/validator v0.1.4
github.com/validator-labs/validator-plugin-aws v0.1.4
github.com/validator-labs/validator-plugin-azure v0.0.16
github.com/validator-labs/validator-plugin-maas v0.0.8-0.20240809210245-5894f5118612
github.com/validator-labs/validator-plugin-network v0.0.23
github.com/validator-labs/validator-plugin-oci v0.2.0
github.com/validator-labs/validator-plugin-vsphere v0.0.30
Expand Down Expand Up @@ -108,6 +110,7 @@ require (
github.com/cyberphone/json-canonicalization v0.0.0-20231011164504-785e29786b46 // indirect
github.com/cyphar/filepath-securejoin v0.2.5 // indirect
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
github.com/deckarep/golang-set/v2 v2.6.0 // indirect
github.com/digitorus/pkcs7 v0.0.0-20230818184609-3a137a874352 // indirect
github.com/digitorus/timestamp v0.0.0-20231217203849-220c5c2851b7 // indirect
github.com/dimchansky/utfbom v1.1.1 // indirect
Expand Down Expand Up @@ -163,6 +166,13 @@ require (
github.com/jmespath/go-jmespath v0.4.0 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/juju/collections v1.0.4 // indirect
github.com/juju/errors v1.0.0 // indirect
github.com/juju/gomaasapi/v2 v2.3.0 // indirect
github.com/juju/loggo v1.0.0 // indirect
github.com/juju/mgo/v2 v2.0.2 // indirect
github.com/juju/schema v1.0.1 // indirect
github.com/juju/version v0.0.0-20210303051006-2015802527a8 // indirect
github.com/klauspost/compress v1.17.8 // indirect
github.com/kr/text v0.2.0 // indirect
github.com/kylelemons/godebug v1.1.0 // indirect
Expand Down Expand Up @@ -242,6 +252,7 @@ require (
google.golang.org/protobuf v1.34.2 // indirect
gopkg.in/inf.v0 v0.9.1 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/mgo.v2 v2.0.0-20190816093944-a6b53ec6cb22 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
k8s.io/apiextensions-apiserver v0.30.3 // indirect
k8s.io/klog/v2 v2.130.1 // indirect
Expand All @@ -253,6 +264,6 @@ require (
sigs.k8s.io/structured-merge-diff/v4 v4.4.1 // indirect
)

// replace github.com/spectrocloud-labs/prompts-tui => ../../spectrocloud-labs/prompts-tui

// replace github.com/validator-labs/validator => ../validator

replace github.com/canonical/gomaasclient v0.6.0 => github.com/arturshadnik/gomaasclient v0.0.0-20240731231205-8239cfb13409
131 changes: 131 additions & 0 deletions go.sum

Large diffs are not rendered by default.

38 changes: 38 additions & 0 deletions hack/validator.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -826,3 +826,41 @@ azurePlugin:
auth:
implicit: false
secretName: azure-creds
maasPlugin:
enabled: true
helmRelease:
chart:
name: validator-plugin-maas
repository: validator-plugin-maas
version: v${MAAS_VERSION}
values: ""
validator:
internalDNSRules:
- maasDomain: example.com
dnsResources:
- fqdn: foo.example.com
dnsRecords:
- type: A
ip: 0.0.0.0
ttl: 10
upstreamDNSRules:
- name: upstream dns
numDNSServers: 1
imageRules:
- name: Image Rule
images:
- name: example
architecture: amd64/ga-20.04
resourceAvailabilityRules:
- name: AZ1
az: az1
resources:
- numMachines: 1
numCPU: 16
ram: 16
disk: 100
host: "http://example.com/MAAS"
auth:
secretName: maas-creds
tokenKey: MAAS_API_KEY
maasApiToken: "jG81nj7n8feUpHaIU/gFH2tLaqcnSshrpCiziFt+0JZ7OA=="
1 change: 1 addition & 0 deletions hack/versions.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ var ValidatorChartVersions = map[string]string{
Validator: "v${VALIDATOR_VERSION}",
ValidatorPluginAws: "v${AWS_VERSION}",
ValidatorPluginAzure: "v${AZURE_VERSION}",
ValidatorPluginMaas: "v${MAAS_VERSION}",
ValidatorPluginNetwork: "v${NETWORK_VERSION}",
ValidatorPluginOci: "v${OCI_VERSION}",
ValidatorPluginVsphere: "v${VSPHERE_VERSION}",
Expand Down
56 changes: 50 additions & 6 deletions pkg/cmd/validator/validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
awsval "github.com/validator-labs/validator-plugin-aws/pkg/validate"
azureapi "github.com/validator-labs/validator-plugin-azure/api/v1alpha1"
azureval "github.com/validator-labs/validator-plugin-azure/pkg/validate"
maasapi "github.com/validator-labs/validator-plugin-maas/api/v1alpha1"
maasval "github.com/validator-labs/validator-plugin-maas/pkg/validate"
netapi "github.com/validator-labs/validator-plugin-network/api/v1alpha1"
netval "github.com/validator-labs/validator-plugin-network/pkg/validate"
ociapi "github.com/validator-labs/validator-plugin-oci/api/v1alpha1"
Expand Down Expand Up @@ -538,6 +540,22 @@
results = append(results, vr)
}

if vc.MaasPlugin.Enabled {
v := &maasapi.MaasValidator{
ObjectMeta: metav1.ObjectMeta{
Name: "maas-validator",
Namespace: "N/A",
},
Spec: *vc.MaasPlugin.Validator,

Check warning on line 549 in pkg/cmd/validator/validator.go

View check run for this annotation

Codecov / codecov/patch

pkg/cmd/validator/validator.go#L543-L549

Added lines #L543 - L549 were not covered by tests
}
vr := vres.Build(v)
vrr := maasval.Validate(*vc.MaasPlugin.Validator, vc.MaasPlugin.Validator.Host, vc.MaasPlugin.MaasAPIToken, l)
if err := vres.Finalize(vr, vrr, l); err != nil {
return err

Check warning on line 554 in pkg/cmd/validator/validator.go

View check run for this annotation

Codecov / codecov/patch

pkg/cmd/validator/validator.go#L551-L554

Added lines #L551 - L554 were not covered by tests
}
results = append(results, vr)

Check warning on line 556 in pkg/cmd/validator/validator.go

View check run for this annotation

Codecov / codecov/patch

pkg/cmd/validator/validator.go#L556

Added line #L556 was not covered by tests
}

if vc.NetworkPlugin.Enabled {
v := &netapi.NetworkValidator{
ObjectMeta: metav1.ObjectMeta{
Expand Down Expand Up @@ -739,6 +757,22 @@
pluginCount++
}

if vc.MaasPlugin.Enabled {
args := map[string]interface{}{
"Config": vc.MaasPlugin,
"ImageRegistry": vc.ImageRegistry,
}
values, err := embed.EFS.RenderTemplateBytes(args, cfg.Validator, "validator-plugin-maas-values.tmpl")
if err != nil {
return errors.Wrap(err, "failed to render validator plugin maas values.yaml")

Check warning on line 767 in pkg/cmd/validator/validator.go

View check run for this annotation

Codecov / codecov/patch

pkg/cmd/validator/validator.go#L767

Added line #L767 was not covered by tests
}
validatorSpec.Plugins = append(validatorSpec.Plugins, vapi.HelmRelease{
Chart: vc.MaasPlugin.Release.Chart,
Values: string(values),
})
pluginCount++
}

if vc.NetworkPlugin.Enabled {
args := map[string]interface{}{
"Config": vc.NetworkPlugin,
Expand Down Expand Up @@ -799,6 +833,7 @@
"SinkConfig": vc.SinkConfig,
"AWSPlugin": vc.AWSPlugin,
"AzurePlugin": vc.AzurePlugin,
"MAASPlugin": vc.MaasPlugin,
"NetworkPlugin": vc.NetworkPlugin,
"OCIPlugin": vc.OCIPlugin,
"VspherePlugin": vc.VspherePlugin,
Expand Down Expand Up @@ -984,10 +1019,19 @@
}
}

if vc.VspherePlugin.Enabled {
log.InfoCLI("\n==== Applying vSphere plugin validator(s) ====")
if vc.AzurePlugin.Enabled {
log.InfoCLI("\n==== Applying Azure plugin validator(s) ====")

Check warning on line 1023 in pkg/cmd/validator/validator.go

View check run for this annotation

Codecov / codecov/patch

pkg/cmd/validator/validator.go#L1022-L1023

Added lines #L1022 - L1023 were not covered by tests
if err := createValidator(
vc.Kubeconfig, c.RunLoc, cfg.ValidatorPluginVsphere, cfg.ValidatorPluginVsphereTemplate, *vc.VspherePlugin.Validator,
vc.Kubeconfig, c.RunLoc, cfg.ValidatorPluginAzure, cfg.ValidatorPluginAzureTemplate, *vc.AzurePlugin.Validator,
); err != nil {
return err

Check warning on line 1027 in pkg/cmd/validator/validator.go

View check run for this annotation

Codecov / codecov/patch

pkg/cmd/validator/validator.go#L1025-L1027

Added lines #L1025 - L1027 were not covered by tests
}
}

if vc.MaasPlugin.Enabled {
log.InfoCLI("\n==== Applying MAAS plugin validator(s) ====")
if err := createValidator(
vc.Kubeconfig, c.RunLoc, cfg.ValidatorPluginMaas, cfg.ValidatorPluginMaasTemplate, *vc.MaasPlugin.Validator,

Check warning on line 1034 in pkg/cmd/validator/validator.go

View check run for this annotation

Codecov / codecov/patch

pkg/cmd/validator/validator.go#L1031-L1034

Added lines #L1031 - L1034 were not covered by tests
); err != nil {
return err
}
Expand All @@ -1011,10 +1055,10 @@
}
}

if vc.AzurePlugin.Enabled {
log.InfoCLI("\n==== Applying Azure plugin validator(s) ====")
if vc.VspherePlugin.Enabled {
log.InfoCLI("\n==== Applying vSphere plugin validator(s) ====")

Check warning on line 1059 in pkg/cmd/validator/validator.go

View check run for this annotation

Codecov / codecov/patch

pkg/cmd/validator/validator.go#L1058-L1059

Added lines #L1058 - L1059 were not covered by tests
if err := createValidator(
vc.Kubeconfig, c.RunLoc, cfg.ValidatorPluginAzure, cfg.ValidatorPluginAzureTemplate, *vc.AzurePlugin.Validator,
vc.Kubeconfig, c.RunLoc, cfg.ValidatorPluginVsphere, cfg.ValidatorPluginVsphereTemplate, *vc.VspherePlugin.Validator,

Check warning on line 1061 in pkg/cmd/validator/validator.go

View check run for this annotation

Codecov / codecov/patch

pkg/cmd/validator/validator.go#L1061

Added line #L1061 was not covered by tests
); err != nil {
return err
}
Expand Down
62 changes: 55 additions & 7 deletions pkg/components/validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

aws "github.com/validator-labs/validator-plugin-aws/api/v1alpha1"
azure "github.com/validator-labs/validator-plugin-azure/api/v1alpha1"
maas "github.com/validator-labs/validator-plugin-maas/api/v1alpha1"
network "github.com/validator-labs/validator-plugin-network/api/v1alpha1"
oci "github.com/validator-labs/validator-plugin-oci/api/v1alpha1"
vsphereapi "github.com/validator-labs/validator-plugin-vsphere/api/v1alpha1"
Expand All @@ -36,6 +37,7 @@

AWSPlugin *AWSPluginConfig `yaml:"awsPlugin,omitempty"`
AzurePlugin *AzurePluginConfig `yaml:"azurePlugin,omitempty"`
MaasPlugin *MaasPluginConfig `yaml:"maasPlugin,omitempty"`
NetworkPlugin *NetworkPluginConfig `yaml:"networkPlugin,omitempty"`
OCIPlugin *OCIPluginConfig `yaml:"ociPlugin,omitempty"`
VspherePlugin *VspherePluginConfig `yaml:"vspherePlugin,omitempty"`
Expand Down Expand Up @@ -78,6 +80,10 @@
StaticDeploymentTypes: make(map[int]string),
StaticDeploymentValues: make(map[int]*AzureStaticDeploymentValues),
},
MaasPlugin: &MaasPluginConfig{
Release: &validator.HelmRelease{},
Validator: &maas.MaasValidatorSpec{},
},
NetworkPlugin: &NetworkPluginConfig{
Release: &validator.HelmRelease{},
HTTPFileAuths: make([][]string, 0),
Expand All @@ -100,7 +106,7 @@

// AnyPluginEnabled returns true if any plugin is enabled.
func (c *ValidatorConfig) AnyPluginEnabled() bool {
return c.AWSPlugin.Enabled || c.NetworkPlugin.Enabled || c.VspherePlugin.Enabled || c.OCIPlugin.Enabled || c.AzurePlugin.Enabled
return c.AWSPlugin.Enabled || c.NetworkPlugin.Enabled || c.VspherePlugin.Enabled || c.OCIPlugin.Enabled || c.AzurePlugin.Enabled || c.MaasPlugin.Enabled
}

// EnabledPluginsHaveRules returns true if all enabled plugins have at least one rule configured.
Expand All @@ -111,19 +117,23 @@
invalidPlugins = append(invalidPlugins, c.AWSPlugin.Validator.PluginCode())
}
if c.AzurePlugin.Enabled && c.AzurePlugin.Validator.ResultCount() == 0 {
invalidPlugins = append(invalidPlugins, "Azure")
// invalidPlugins = append(invalidPlugins, c.AzurePlugin.Validator.PluginCode())
invalidPlugins = append(invalidPlugins, c.AzurePlugin.Validator.PluginCode())

Check warning on line 120 in pkg/components/validator.go

View check run for this annotation

Codecov / codecov/patch

pkg/components/validator.go#L120

Added line #L120 was not covered by tests
}
if c.MaasPlugin.Enabled && c.MaasPlugin.Validator.ResultCount() == 0 {
invalidPlugins = append(invalidPlugins, c.MaasPlugin.Validator.PluginCode())

Check warning on line 123 in pkg/components/validator.go

View check run for this annotation

Codecov / codecov/patch

pkg/components/validator.go#L122-L123

Added lines #L122 - L123 were not covered by tests
}
if c.MaasPlugin.Enabled && c.MaasPlugin.Validator.ResultCount() == 0 {

Check warning on line 125 in pkg/components/validator.go

View check run for this annotation

Codecov / codecov/patch

pkg/components/validator.go#L125

Added line #L125 was not covered by tests
// invalidPlugins = append(invalidPlugins, c.MaasPlugin.Validator.PluginCode())
arturshadnik marked this conversation as resolved.
Show resolved Hide resolved
invalidPlugins = append(invalidPlugins, "MAAS")

Check warning on line 127 in pkg/components/validator.go

View check run for this annotation

Codecov / codecov/patch

pkg/components/validator.go#L127

Added line #L127 was not covered by tests
}
if c.NetworkPlugin.Enabled && c.NetworkPlugin.Validator.ResultCount() == 0 {
invalidPlugins = append(invalidPlugins, c.NetworkPlugin.Validator.PluginCode())
}
if c.OCIPlugin.Enabled && c.OCIPlugin.Validator.ResultCount() == 0 {
invalidPlugins = append(invalidPlugins, "OCI")
// invalidPlugins = append(invalidPlugins, c.OCIPlugin.Validator.PluginCode())
invalidPlugins = append(invalidPlugins, c.OCIPlugin.Validator.PluginCode())

Check warning on line 133 in pkg/components/validator.go

View check run for this annotation

Codecov / codecov/patch

pkg/components/validator.go#L133

Added line #L133 was not covered by tests
}
if c.VspherePlugin.Enabled && c.VspherePlugin.Validator.ResultCount() == 0 {
invalidPlugins = append(invalidPlugins, "vSphere")
// invalidPlugins = append(invalidPlugins, c.VspherePlugin.Validator.PluginCode())
invalidPlugins = append(invalidPlugins, c.VspherePlugin.Validator.PluginCode())

Check warning on line 136 in pkg/components/validator.go

View check run for this annotation

Codecov / codecov/patch

pkg/components/validator.go#L136

Added line #L136 was not covered by tests
}
if len(invalidPlugins) == 0 {
ok = true
Expand Down Expand Up @@ -152,6 +162,11 @@
return errors.Wrap(err, "failed to decrypt Azure plugin configuration")
}
}
if c.MaasPlugin != nil {
if err := c.MaasPlugin.decrypt(); err != nil {
return errors.Wrap(err, "failed to decrypt MAAS plugin configuration")

Check warning on line 167 in pkg/components/validator.go

View check run for this annotation

Codecov / codecov/patch

pkg/components/validator.go#L167

Added line #L167 was not covered by tests
}
}
if c.NetworkPlugin != nil {
if err := c.NetworkPlugin.decrypt(); err != nil {
return errors.Wrap(err, "failed to decrypt Network plugin configuration")
Expand Down Expand Up @@ -192,6 +207,11 @@
return errors.Wrap(err, "failed to encrypt Azure plugin configuration")
}
}
if c.MaasPlugin != nil {
if err := c.MaasPlugin.encrypt(); err != nil {
return errors.Wrap(err, "failed to encrypt MAAS plugin configuration")

Check warning on line 212 in pkg/components/validator.go

View check run for this annotation

Codecov / codecov/patch

pkg/components/validator.go#L212

Added line #L212 was not covered by tests
}
}
if c.NetworkPlugin != nil {
if err := c.NetworkPlugin.encrypt(); err != nil {
return errors.Wrap(err, "failed to encrypt Network plugin configuration")
Expand Down Expand Up @@ -394,6 +414,34 @@
ComputeGallery string `yaml:"computeGalleryUuid"`
}

// MaasPluginConfig represents the MAAS plugin configuration.
type MaasPluginConfig struct {
Enabled bool `yaml:"enabled"`
Release *validator.HelmRelease `yaml:"helmRelease"`
Validator *maas.MaasValidatorSpec `yaml:"validator"`
MaasAPIToken string `yaml:"maasApiToken"`
}

func (c *MaasPluginConfig) encrypt() error {
token, err := crypto.EncryptB64([]byte(c.MaasAPIToken))
if err != nil {
return errors.Wrap(err, "failed to encrypt token")

Check warning on line 428 in pkg/components/validator.go

View check run for this annotation

Codecov / codecov/patch

pkg/components/validator.go#L428

Added line #L428 was not covered by tests
}
c.MaasAPIToken = token

return nil
}

func (c *MaasPluginConfig) decrypt() error {
bytes, err := crypto.DecryptB64(c.MaasAPIToken)
if err != nil {
return errors.Wrap(err, "failed to decrypt token")

Check warning on line 438 in pkg/components/validator.go

View check run for this annotation

Codecov / codecov/patch

pkg/components/validator.go#L438

Added line #L438 was not covered by tests
}
c.MaasAPIToken = string(*bytes)

return nil
}

// NetworkPluginConfig represents the network plugin configuration.
type NetworkPluginConfig struct {
Enabled bool `yaml:"enabled"`
Expand Down
3 changes: 3 additions & 0 deletions pkg/config/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,14 @@ const (

ValidatorPluginAws = "validator-plugin-aws"
ValidatorPluginAzure = "validator-plugin-azure"
ValidatorPluginMaas = "validator-plugin-maas"
ValidatorPluginNetwork = "validator-plugin-network"
ValidatorPluginOci = "validator-plugin-oci"
ValidatorPluginVsphere = "validator-plugin-vsphere"

ValidatorPluginAwsTemplate = "validator-rules-aws.tmpl"
ValidatorPluginAzureTemplate = "validator-rules-azure.tmpl"
ValidatorPluginMaasTemplate = "validator-rules-maas.tmpl"
ValidatorPluginNetworkTemplate = "validator-rules-network.tmpl"
ValidatorPluginOciTemplate = "validator-rules-oci.tmpl"
ValidatorPluginVsphereTemplate = "validator-rules-vsphere.tmpl"
Expand Down Expand Up @@ -77,6 +79,7 @@ var (
RegistryMirrors = []string{"docker.io", "gcr.io", "ghcr.io", "k8s.gcr.io", "registry.k8s.io", "quay.io", "*"}
RegistryMirrorSeparator = "::"
FileInputs = []string{LocalFilepath, FileEditor}
DNSRecordTypes = []string{"A", "AAAA", "CNAME", "TXT", "MX", "NS", "SRV", "SSHFP"}

// Command dirs
ValidatorSubdirs = []string{"logs", "manifests"}
Expand Down
1 change: 1 addition & 0 deletions pkg/config/versions.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ var ValidatorChartVersions = map[string]string{
Validator: "v0.1.4",
ValidatorPluginAws: "v0.1.4",
ValidatorPluginAzure: "v0.0.16",
ValidatorPluginMaas: "v0.0.7",
ValidatorPluginNetwork: "v0.0.23",
ValidatorPluginOci: "v0.2.0",
ValidatorPluginVsphere: "v0.0.30",
Expand Down
Loading