Skip to content

Commit

Permalink
refactor: remove explicit TypeMetas; use vapi constants (#154)
Browse files Browse the repository at this point in the history
## Issue
N/A

## Description
Remove explicit TypeMeta initialization for direct rule evaluation. This
is no longer needed now that the `ValidationRule` interface in validator
has been updated to include `GetKind`, all plugins are updated to
satisfy the updated interface, and validator's `Build` validation result
method is now setting `APIVersion` and `Kind`.

---------

Signed-off-by: Tyler Gillson <tyler.gillson@gmail.com>
  • Loading branch information
TylerGillson authored Aug 10, 2024
1 parent 94ddd90 commit 28b321c
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 59 deletions.
18 changes: 10 additions & 8 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ require (
github.com/spectrocloud-labs/prompts-tui v0.1.1
github.com/spf13/cobra v1.8.1
github.com/spf13/viper v1.19.0
github.com/validator-labs/validator v0.1.2
github.com/validator-labs/validator-plugin-aws v0.1.3
github.com/validator-labs/validator-plugin-azure v0.0.16-0.20240809035651-b4687e5f1cec
github.com/validator-labs/validator-plugin-network v0.0.22
github.com/validator-labs/validator-plugin-oci v0.1.0
github.com/validator-labs/validator-plugin-vsphere v0.0.30-0.20240808221109-a93cb7014075
github.com/validator-labs/validator v0.1.3-0.20240809205255-c05a36eebd3c
github.com/validator-labs/validator-plugin-aws v0.1.4-0.20240809205833-8085473f77df
github.com/validator-labs/validator-plugin-azure v0.0.16-0.20240809210033-fab1ab878d10
github.com/validator-labs/validator-plugin-network v0.0.23-0.20240809205936-18ffc3b792c4
github.com/validator-labs/validator-plugin-oci v0.1.1-0.20240809205756-c1942b7a7104
github.com/validator-labs/validator-plugin-vsphere v0.0.30-0.20240809210242-a318dfc2be38
github.com/vmware/govmomi v0.39.0
golang.org/x/exp v0.0.0-20240808152545-0cdaa3abc0fa
gopkg.in/yaml.v2 v2.4.0
Expand Down Expand Up @@ -80,7 +80,7 @@ require (
github.com/aws/aws-sdk-go-v2/internal/configsources v1.3.15 // indirect
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.6.15 // indirect
github.com/aws/aws-sdk-go-v2/internal/ini v1.8.0 // indirect
github.com/aws/aws-sdk-go-v2/service/ec2 v1.173.0 // indirect
github.com/aws/aws-sdk-go-v2/service/ec2 v1.174.0 // indirect
github.com/aws/aws-sdk-go-v2/service/ecr v1.32.0 // indirect
github.com/aws/aws-sdk-go-v2/service/ecrpublic v1.25.3 // indirect
github.com/aws/aws-sdk-go-v2/service/efs v1.31.3 // indirect
Expand All @@ -94,7 +94,7 @@ require (
github.com/aws/aws-sdk-go-v2/service/ssooidc v1.26.4 // indirect
github.com/aws/aws-sdk-go-v2/service/sts v1.30.3 // indirect
github.com/aws/smithy-go v1.20.3 // indirect
github.com/awslabs/amazon-ecr-credential-helper/ecr-login v0.0.0-20240807144652-27a1eeba0782 // indirect
github.com/awslabs/amazon-ecr-credential-helper/ecr-login v0.0.0-20240809155957-ac94a3401898 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/blang/semver v3.5.1+incompatible // indirect
github.com/blang/semver/v4 v4.0.0 // indirect
Expand Down Expand Up @@ -254,3 +254,5 @@ require (
)

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

// replace github.com/validator-labs/validator => ../validator
32 changes: 16 additions & 16 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -178,8 +178,8 @@ github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.6.15 h1:C6WHdGnTDIYETAm5iE
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.6.15/go.mod h1:ZQLZqhcu+JhSrA9/NXRm8SkDvsycE+JkV3WGY41e+IM=
github.com/aws/aws-sdk-go-v2/internal/ini v1.8.0 h1:hT8rVHwugYE2lEfdFE0QWVo81lF7jMrYJVDWI+f+VxU=
github.com/aws/aws-sdk-go-v2/internal/ini v1.8.0/go.mod h1:8tu/lYfQfFe6IGnaOdrpVgEL2IrrDOf6/m9RQum4NkY=
github.com/aws/aws-sdk-go-v2/service/ec2 v1.173.0 h1:ta62lid9JkIpKZtZZXSj6rP2AqY5x1qYGq53ffxqD9Q=
github.com/aws/aws-sdk-go-v2/service/ec2 v1.173.0/go.mod h1:o6QDjdVKpP5EF0dp/VlvqckzuSDATr1rLdHt3A5m0YY=
github.com/aws/aws-sdk-go-v2/service/ec2 v1.174.0 h1:xY25etxTt0mYfWVWv8DjIUcJQyX2cwBeInpIvg6Sbsw=
github.com/aws/aws-sdk-go-v2/service/ec2 v1.174.0/go.mod h1:o6QDjdVKpP5EF0dp/VlvqckzuSDATr1rLdHt3A5m0YY=
github.com/aws/aws-sdk-go-v2/service/ecr v1.32.0 h1:lZoKOTEQUf5Oi9qVaZM/Hb0Z6SHIwwpDjbLFOVgB2t8=
github.com/aws/aws-sdk-go-v2/service/ecr v1.32.0/go.mod h1:RhaP7Wil0+uuuhiE4FzOOEFZwkmFAk1ZflXzK+O3ptU=
github.com/aws/aws-sdk-go-v2/service/ecrpublic v1.25.3 h1:n2eqzO9VabUkd77b88Hos6OEtbGohB/TRrtXLTZi38Y=
Expand Down Expand Up @@ -208,8 +208,8 @@ github.com/aws/aws-sdk-go-v2/service/sts v1.30.3 h1:ZsDKRLXGWHk8WdtyYMoGNO7bTudr
github.com/aws/aws-sdk-go-v2/service/sts v1.30.3/go.mod h1:zwySh8fpFyXp9yOr/KVzxOl8SRqgf/IDw5aUt9UKFcQ=
github.com/aws/smithy-go v1.20.3 h1:ryHwveWzPV5BIof6fyDvor6V3iUL7nTfiTKXHiW05nE=
github.com/aws/smithy-go v1.20.3/go.mod h1:krry+ya/rV9RDcV/Q16kpu6ypI4K2czasz0NC3qS14E=
github.com/awslabs/amazon-ecr-credential-helper/ecr-login v0.0.0-20240807144652-27a1eeba0782 h1:eBIQewfIMpNPGZRXNjjJOW6ccbIokR5NDOtDOMZkBjg=
github.com/awslabs/amazon-ecr-credential-helper/ecr-login v0.0.0-20240807144652-27a1eeba0782/go.mod h1:/1+gxjBqwEu/k2A01NbATEo0+X82cbaZA9+SzYeG634=
github.com/awslabs/amazon-ecr-credential-helper/ecr-login v0.0.0-20240809155957-ac94a3401898 h1:AvFUc3gOvaw2avFhMTgbyWoa8lTAiwUqiHQzRyfJgOE=
github.com/awslabs/amazon-ecr-credential-helper/ecr-login v0.0.0-20240809155957-ac94a3401898/go.mod h1:g/jwspvaX2Kvy+cuE6NRlboLhoS2kHeYDmfc880tS1Y=
github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM=
github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw=
github.com/blang/semver v3.5.1+incompatible h1:cQNTCjp13qL8KC3Nbxr/y2Bqb63oX6wdnnjpJbkM4JQ=
Expand Down Expand Up @@ -752,18 +752,18 @@ github.com/tjfoc/gmsm v1.4.1 h1:aMe1GlZb+0bLjn+cKTPEvvn9oUEBlJitaZiiBwsbgho=
github.com/tjfoc/gmsm v1.4.1/go.mod h1:j4INPkHWMrhJb38G+J6W4Tw0AbuN8Thu3PbdVYhVcTE=
github.com/transparency-dev/merkle v0.0.2 h1:Q9nBoQcZcgPamMkGn7ghV8XiTZ/kRxn1yCG81+twTK4=
github.com/transparency-dev/merkle v0.0.2/go.mod h1:pqSy+OXefQ1EDUVmAJ8MUhHB9TXGuzVAT58PqBoHz1A=
github.com/validator-labs/validator v0.1.2 h1:2KqwetsZFeK5ol7Hd/VGO1Ccz8WQFVs5QwfLf8blp10=
github.com/validator-labs/validator v0.1.2/go.mod h1:AdpMOk2xYjlpUjmPJ2CN6MNDFN4NLH/q4lqUziseHpU=
github.com/validator-labs/validator-plugin-aws v0.1.3 h1:8C/cprrt0RW8j97LOPSyuqyy1EDuHZfJthpS+tZY5PE=
github.com/validator-labs/validator-plugin-aws v0.1.3/go.mod h1:aY+4x9iA76XzjjS7fTXWJPytxCFospvzIOhJgnlpkq0=
github.com/validator-labs/validator-plugin-azure v0.0.16-0.20240809035651-b4687e5f1cec h1:07OqIrf6LEdH/ghuTM3xgln2OZVsgZVTIpNKUoRDLlA=
github.com/validator-labs/validator-plugin-azure v0.0.16-0.20240809035651-b4687e5f1cec/go.mod h1:5rNcu7SqOk9zXfbCyvxnKlYn/J0l7iOu1zvHEqSVkx0=
github.com/validator-labs/validator-plugin-network v0.0.22 h1:tlyyA8wq2j9/tmNjxP5JuQEY0Xh6aLedFp9s2943CVg=
github.com/validator-labs/validator-plugin-network v0.0.22/go.mod h1:0zlqCtTNHxPAp7jKN/1abzRBXRXvBNWSQzVEhkQCrXg=
github.com/validator-labs/validator-plugin-oci v0.1.0 h1:odFaKQQJ3AYxXSdjlkfn26HlEEYyA/UMtouj3I/Sw5M=
github.com/validator-labs/validator-plugin-oci v0.1.0/go.mod h1:R39V5VgdKiN8ugjYDfbOZ+7s2keGlRq3SvGcrV6ub8M=
github.com/validator-labs/validator-plugin-vsphere v0.0.30-0.20240808221109-a93cb7014075 h1:dKHPwBBEdkljZCZsKDBLHivFYBKlVrrJNkTe/NRqlK8=
github.com/validator-labs/validator-plugin-vsphere v0.0.30-0.20240808221109-a93cb7014075/go.mod h1:jANTSAkr2NQmSa2rOMe2qspWoOLsU0X2+8kztpurdSw=
github.com/validator-labs/validator v0.1.3-0.20240809205255-c05a36eebd3c h1:9Dy8DW7qdSQflkwiNPcBPkf/wgAOT+ddmSDvOabjnVQ=
github.com/validator-labs/validator v0.1.3-0.20240809205255-c05a36eebd3c/go.mod h1:TU04WjenuJxvJI/uhRsBJRym3ysA+dvSwUs2qvnak1Q=
github.com/validator-labs/validator-plugin-aws v0.1.4-0.20240809205833-8085473f77df h1:ryzjKVisPy4yRIBOBUyKO4k7/CAL8wDHHN+9T6+bPUE=
github.com/validator-labs/validator-plugin-aws v0.1.4-0.20240809205833-8085473f77df/go.mod h1:NAPw04DYgOQUKN7GmGy65gETac19lDAjCAPtE4nLe5Q=
github.com/validator-labs/validator-plugin-azure v0.0.16-0.20240809210033-fab1ab878d10 h1:BFlJUBviwxKnrgYnemEqW643Ohj32XvDr6VNbQYzmOI=
github.com/validator-labs/validator-plugin-azure v0.0.16-0.20240809210033-fab1ab878d10/go.mod h1:Lg4K6Uc7BkNi3KIr0gEPYDcwfjmFNI0uYHm4tbVyswo=
github.com/validator-labs/validator-plugin-network v0.0.23-0.20240809205936-18ffc3b792c4 h1:iHmrJU1FBgfBzMuQ1tcYZiy2nZ5Qwmvu6GZ787b5odo=
github.com/validator-labs/validator-plugin-network v0.0.23-0.20240809205936-18ffc3b792c4/go.mod h1:zA+EcB7wV2qj67SiFpzpTq1Bv08BIqJWU/+XrbeBRZM=
github.com/validator-labs/validator-plugin-oci v0.1.1-0.20240809205756-c1942b7a7104 h1:5A/j5XFU720xFvmuqunGM4Rz1NB1KIWVodQqhleP4Jk=
github.com/validator-labs/validator-plugin-oci v0.1.1-0.20240809205756-c1942b7a7104/go.mod h1:lC/pR8XKRKLKlDYObm8v6eDaj7NkmBjAskheuQXE7V0=
github.com/validator-labs/validator-plugin-vsphere v0.0.30-0.20240809210242-a318dfc2be38 h1:hYw3ZTbHBdhCJoFeXkhYipWHkVpdLufwWsSxuoWN1aE=
github.com/validator-labs/validator-plugin-vsphere v0.0.30-0.20240809210242-a318dfc2be38/go.mod h1:hBGQsKzO2TCG0QArBnZHNUpofxT02J9WYBV/rtDVcok=
github.com/vbatts/tar-split v0.11.5 h1:3bHCTIheBm1qFTcgh9oPu+nNBtX+XJIupG/vacinCts=
github.com/vbatts/tar-split v0.11.5/go.mod h1:yZbwRsSeGjusneWgA781EKej9HF8vme8okylkAeNKLk=
github.com/vmware/govmomi v0.39.0 h1:soLZ08Q2zvjRSinNup8xVlw0KDDCJPPA1rIDmBhi7As=
Expand Down
33 changes: 4 additions & 29 deletions pkg/cmd/validator/validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -380,8 +380,8 @@ func getValidationResultsCRDClient(tc *cfg.TaskConfig) (dynamic.NamespaceableRes
log.Debug("Using kubeconfig from validator configuration file: %s", vc.Kubeconfig)
}

gv := kube.GetGroupVersion("validation.spectrocloud.labs", "v1alpha1")
kClient, err := kube.GetCRDClient(gv, "validationresults")
gv := kube.GetGroupVersion(vapi.GroupVersion.Group, vapi.GroupVersion.Version)
kClient, err := kube.GetCRDClient(gv, vapi.ValidationResultGroupResource)
if err != nil {
return nil, errors.Wrap(err, "failed to get validation result client")
}
Expand Down Expand Up @@ -509,11 +509,6 @@ func executePlugins(c *cfg.Config, vc *components.ValidatorConfig) error {
Spec: *vc.AWSPlugin.Validator,
}
vr := vres.Build(v)
// TODO: set TypeMeta in vres.Build
vr.TypeMeta = metav1.TypeMeta{
APIVersion: "validation.spectrocloud.labs/v1alpha1",
Kind: "AwsValidator",
}
vrr := awsval.Validate(*vc.AWSPlugin.Validator, l)
if err := vres.Finalize(vr, vrr, l); err != nil {
return err
Expand All @@ -533,11 +528,6 @@ func executePlugins(c *cfg.Config, vc *components.ValidatorConfig) error {
Spec: *vc.AzurePlugin.Validator,
}
vr := vres.Build(v)
// TODO: set TypeMeta in vres.Build
vr.TypeMeta = metav1.TypeMeta{
APIVersion: "validation.spectrocloud.labs/v1alpha1",
Kind: "AzureValidator",
}
vrr := azureval.Validate(context.Background(), *vc.AzurePlugin.Validator, l)
if err := vres.Finalize(vr, vrr, l); err != nil {
return err
Expand All @@ -557,11 +547,6 @@ func executePlugins(c *cfg.Config, vc *components.ValidatorConfig) error {
Spec: *vc.NetworkPlugin.Validator,
}
vr := vres.Build(v)
// TODO: set TypeMeta in vres.Build
vr.TypeMeta = metav1.TypeMeta{
APIVersion: "validation.spectrocloud.labs/v1alpha1",
Kind: "NetworkValidator",
}
vrr := netval.Validate(*vc.NetworkPlugin.Validator,
vc.NetworkPlugin.Validator.CACerts.RawCerts(),
vc.NetworkPlugin.HTTPFileAuthBytes(), l,
Expand All @@ -584,11 +569,6 @@ func executePlugins(c *cfg.Config, vc *components.ValidatorConfig) error {
Spec: *vc.OCIPlugin.Validator,
}
vr := vres.Build(v)
// TODO: set TypeMeta in vres.Build
vr.TypeMeta = metav1.TypeMeta{
APIVersion: "validation.spectrocloud.labs/v1alpha1",
Kind: "OciValidator",
}
vrr := ocival.Validate(*vc.OCIPlugin.Validator,
vc.OCIPlugin.BasicAuths(),
vc.OCIPlugin.AllPubKeys(), l,
Expand All @@ -611,11 +591,6 @@ func executePlugins(c *cfg.Config, vc *components.ValidatorConfig) error {
Spec: *vc.VspherePlugin.Validator,
}
vr := vres.Build(v)
// TODO: set TypeMeta in vres.Build
vr.TypeMeta = metav1.TypeMeta{
APIVersion: "validation.spectrocloud.labs/v1alpha1",
Kind: "VsphereValidator",
}
vrr := vsphereval.Validate(context.Background(), *vc.VspherePlugin.Validator, vc.VspherePlugin.Account, l)
if err := vres.Finalize(vr, vrr, l); err != nil {
return err
Expand Down Expand Up @@ -943,8 +918,8 @@ func applyValidator(c *cfg.Config, vc *components.ValidatorConfig) error {
func watchValidatorConfig(numPlugins int) (bool, error) {
log.InfoCLI("\nWatching validator config, waiting for plugins to be installed or failed")

gv := kube.GetGroupVersion("validation.spectrocloud.labs", "v1alpha1")
kClient, err := kube.GetCRDClient(gv, "validatorconfigs")
gv := kube.GetGroupVersion(vapi.GroupVersion.Group, vapi.GroupVersion.Version)
kClient, err := kube.GetCRDClient(gv, vapi.ValidatorConfigGroupResource)
if err != nil {
return false, errors.Wrap(err, "failed to get validator config client")
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/services/validator/oci.go
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ func readOciRegistryRule(c *components.OCIPluginConfig, r *plug.OciRegistryRule,
return err
}
if authSecretName != notApplicable {
r.Auth = plug.Auth{SecretName: authSecretName}
r.Auth = plug.Auth{SecretName: &authSecretName}
}

if err := readArtifactRefs(r); err != nil {
Expand Down
7 changes: 2 additions & 5 deletions pkg/utils/kube/kube.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,6 @@ type KubectlCmd struct {
DelayMsg string
}

// Crd represents a custom resource definition
type Crd string

// KubectlCommand executes a kubectl command with the given parameters
func KubectlCommand(params []string, kConfig string) (out, stderr string, err error) {
params = append(params, fmt.Sprintf("--kubeconfig=%s", kConfig))
Expand Down Expand Up @@ -70,7 +67,7 @@ func GetGroupVersion(group, version string) schema.GroupVersion {
}

// GetCRDClient returns a dynamic client for the given CRD
func GetCRDClient(groupVersion schema.GroupVersion, crd Crd) (dynamic.NamespaceableResourceInterface, error) {
func GetCRDClient(groupVersion schema.GroupVersion, groupResource schema.GroupResource) (dynamic.NamespaceableResourceInterface, error) {
dynClient, err := getDynamicClient()
if err != nil {
return nil, err
Expand All @@ -79,7 +76,7 @@ func GetCRDClient(groupVersion schema.GroupVersion, crd Crd) (dynamic.Namespacea
version := schema.GroupVersionResource{
Group: groupVersion.Group,
Version: groupVersion.Version,
Resource: string(crd),
Resource: groupResource.Resource,
}

return dynClient.Resource(version), nil
Expand Down

0 comments on commit 28b321c

Please sign in to comment.