Skip to content

Commit

Permalink
refactor: enable revive & resolve all lints (#307)
Browse files Browse the repository at this point in the history
  • Loading branch information
TylerGillson committed Jul 4, 2024
1 parent be8084f commit 7877f71
Show file tree
Hide file tree
Showing 25 changed files with 430 additions and 261 deletions.
1 change: 1 addition & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ linters:
- misspell
- nakedret
- prealloc
- revive
- staticcheck
- typecheck
- unconvert
Expand Down
44 changes: 35 additions & 9 deletions api/v1alpha1/validationresult_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,65 +26,90 @@ import (
clusterv1beta1 "sigs.k8s.io/cluster-api/api/v1beta1"
)

// ValidationResultSpec defines the desired state of ValidationResult
// ValidationResultSpec defines the desired state of ValidationResult.
type ValidationResultSpec struct {
// Plugin is the plugin code of the validator plugin that was executed.
Plugin string `json:"plugin"`

// The number of rules in the validator plugin spec, hence the number of expected ValidationResults.
// +kubebuilder:validation:Minimum=1
ExpectedResults int `json:"expectedResults"`
}

// ValidationState records the overall state of a validation result.
type ValidationState string

const (
ValidationFailed ValidationState = "Failed"
// ValidationFailed indicates that the validation result has failed.
ValidationFailed ValidationState = "Failed"

// ValidationInProgress indicates that the validation result is in progress.
ValidationInProgress ValidationState = "InProgress"
ValidationSucceeded ValidationState = "Succeeded"

// ValidationSucceeded indicates that the validation result has succeeded.
ValidationSucceeded ValidationState = "Succeeded"
)

// SinkEmission is the type of condition that is emitted to the sink.
const SinkEmission clusterv1beta1.ConditionType = "SinkEmission"

// SinkEmitState is the state of the sink emission.
type SinkEmitState string

const (
SinkEmitNA SinkEmitState = "SinkEmitNA"
SinkEmitFailed SinkEmitState = "SinkEmitFailed"
// SinkEmitNA indicates that the sink emission is not applicable.
SinkEmitNA SinkEmitState = "SinkEmitNA"

// SinkEmitFailed indicates that the sink emission has failed.
SinkEmitFailed SinkEmitState = "SinkEmitFailed"

// SinkEmitSucceeded indicates that the sink emission has succeeded.
SinkEmitSucceeded SinkEmitState = "SinkEmitSucceeded"
)

// ValidationResultStatus defines the observed state of ValidationResult
// ValidationResultStatus defines the observed state of ValidationResult.
type ValidationResultStatus struct {
// State is the overall state of the validation result.
State ValidationState `json:"state"`

// Conditions is a list of conditions that describe the current state of the ValidationResult.
// +optional
// +patchMergeKey=type
// +patchStrategy=merge
Conditions []clusterv1beta1.Condition `json:"conditions,omitempty" patchStrategy:"merge" patchMergeKey:"type"`

// ValidationConditions is a list of conditions that describe the validation rules associated with the ValidationResult.
// +optional
// +patchMergeKey=type
// +patchStrategy=merge
ValidationConditions []ValidationCondition `json:"validationConditions,omitempty" patchStrategy:"merge" patchMergeKey:"type"`
}

// ValidationCondition describes the state of a validation rule.
type ValidationCondition struct {
// Unique, one-word description of the validation type associated with the condition.
ValidationType string `json:"validationType"`

// Unique, one-word description of the validation rule associated with the condition.
ValidationRule string `json:"validationRule"`

// Human-readable message indicating details about the last transition.
Message string `json:"message,omitempty"`

// Human-readable messages indicating additional details for the last transition.
Details []string `json:"details,omitempty"`

// Human-readable messages indicating additional failure details for the last transition.
Failures []string `json:"failures,omitempty"`

// True if the validation rule succeeded, otherwise False.
Status corev1.ConditionStatus `json:"status"`

// Timestamp of most recent execution of the validation rule associated with the condition.
LastValidationTime metav1.Time `json:"lastValidationTime"`
}

// DefaultValidationCondition returns a default ValidationCondition
// DefaultValidationCondition returns a default ValidationCondition.
func DefaultValidationCondition() ValidationCondition {
return ValidationCondition{
Details: make([]string, 0),
Expand All @@ -99,7 +124,7 @@ func DefaultValidationCondition() ValidationCondition {
//+kubebuilder:printcolumn:name="Plugin",type="string",JSONPath=".spec.plugin",description="Plugin"
//+kubebuilder:printcolumn:name="State",type="string",JSONPath=".status.state",description="State"

// ValidationResult is the Schema for the validationresults API
// ValidationResult is the Schema for the validationresults API.
type ValidationResult struct {
metav1.TypeMeta `json:",inline"`
metav1.ObjectMeta `json:"metadata,omitempty"`
Expand All @@ -108,6 +133,7 @@ type ValidationResult struct {
Status ValidationResultStatus `json:"status,omitempty"`
}

// Hash returns a hash of the ValidationResult.
func (r *ValidationResult) Hash() string {
digester := crypto.MD5.New()

Expand All @@ -127,7 +153,7 @@ func (r *ValidationResult) Hash() string {

//+kubebuilder:object:root=true

// ValidationResultList contains a list of ValidationResult
// ValidationResultList contains a list of ValidationResult.
type ValidationResultList struct {
metav1.TypeMeta `json:",inline"`
metav1.ListMeta `json:"metadata,omitempty"`
Expand Down
46 changes: 33 additions & 13 deletions api/v1alpha1/validatorconfig_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,31 +21,50 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

// ValidatorConfigSpec defines the desired state of ValidatorConfig
// ValidatorConfigSpec defines the desired state of ValidatorConfig.
type ValidatorConfigSpec struct {
// Plugins defines the configuration for the validator plugins.
Plugins []HelmRelease `json:"plugins,omitempty" yaml:"plugins,omitempty"`
Sink *Sink `json:"sink,omitempty" yaml:"sink,omitempty"`

// Sink defines the configuration for the notification sink.
Sink *Sink `json:"sink,omitempty" yaml:"sink,omitempty"`
}

// Sink defines the configuration for a notification sink.
type Sink struct {
// Type of the sink.
// +kubebuilder:validation:Enum=alertmanager;slack
Type string `json:"type" yaml:"type"`
// Name of a K8s secret containing configuration details for the sink

// SecretName is the name of a K8s secret containing configuration details for the sink.
SecretName string `json:"secretName" yaml:"secretName"`
}

// HelmRelease defines the configuration for a Helm chart release.
type HelmRelease struct {
Chart HelmChart `json:"chart" yaml:"chart"`
Values string `json:"values" yaml:"values"`
// Chart defines the Helm chart to be installed.
Chart HelmChart `json:"chart" yaml:"chart"`

// Values defines the values to be passed to the Helm chart.
Values string `json:"values" yaml:"values"`
}

// HelmChart defines the configuration for a Helm chart.
type HelmChart struct {
Name string `json:"name" yaml:"name"`
Repository string `json:"repository" yaml:"repository"`
Version string `json:"version" yaml:"version"`
CaFile string `json:"caFile,omitempty" yaml:"caFile,omitempty"`
InsecureSkipTlsVerify bool `json:"insecureSkipVerify,omitempty" yaml:"insecureSkipVerify,omitempty"`
AuthSecretName string `json:"authSecretName,omitempty" yaml:"authSecretName,omitempty"`
// Name of the Helm chart.
Name string `json:"name" yaml:"name"`

// Repository URL of the Helm chart.
Repository string `json:"repository" yaml:"repository"`

// Version of the Helm chart.
Version string `json:"version" yaml:"version"`

// InsecureSkipTLSVerify skips the verification of the server's certificate chain and host name.
InsecureSkipTLSVerify bool `json:"insecureSkipVerify,omitempty" yaml:"insecureSkipVerify,omitempty"`

// AuthSecretName is the name of the K8s secret containing the authentication details for the Helm repository.
AuthSecretName string `json:"authSecretName,omitempty" yaml:"authSecretName,omitempty"`
}

// ValidatorConfigStatus defines the observed state of ValidatorConfig
Expand All @@ -56,6 +75,7 @@ type ValidatorConfigStatus struct {
Conditions []ValidatorPluginCondition `json:"conditions,omitempty" patchStrategy:"merge" patchMergeKey:"type"`
}

// ValidatorPluginCondition describes the state of a Validator plugin.
type ValidatorPluginCondition struct {
// Type of condition in CamelCase or in foo.example.com/CamelCase.
// Many .condition.type values are consistent across resources like Available, but because arbitrary conditions
Expand Down Expand Up @@ -92,7 +112,7 @@ const HelmChartDeployedCondition ConditionType = "HelmChartDeployed"
//+kubebuilder:object:root=true
//+kubebuilder:subresource:status

// ValidatorConfig is the Schema for the validatorconfigs API
// ValidatorConfig is the Schema for the validatorconfigs API.
type ValidatorConfig struct {
metav1.TypeMeta `json:",inline"`
metav1.ObjectMeta `json:"metadata,omitempty"`
Expand All @@ -103,7 +123,7 @@ type ValidatorConfig struct {

//+kubebuilder:object:root=true

// ValidatorConfigList contains a list of ValidatorConfig
// ValidatorConfigList contains a list of ValidatorConfig.
type ValidatorConfigList struct {
metav1.TypeMeta `json:",inline"`
metav1.ListMeta `json:"metadata,omitempty"`
Expand Down
4 changes: 3 additions & 1 deletion cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

// Package main initializes the ValidationConfig and ValidationResult controllers.
package main

import (
Expand All @@ -37,6 +38,7 @@ import (
"github.com/validator-labs/validator/internal/kube"
"github.com/validator-labs/validator/internal/sinks"
"github.com/validator-labs/validator/pkg/helm"
"github.com/validator-labs/validator/pkg/helm/release"
//+kubebuilder:scaffold:imports
)

Expand Down Expand Up @@ -128,7 +130,7 @@ func main() {
if err = (&controller.ValidatorConfigReconciler{
Client: mgr.GetClient(),
HelmClient: helm.NewHelmClient(rawConfig),
HelmSecretsClient: helm.NewSecretsClient(mgr.GetClient()),
HelmReleaseClient: release.NewHelmReleaseClient(mgr.GetClient()),
Log: ctrl.Log.WithName("controllers").WithName("ValidatorConfig"),
Scheme: mgr.GetScheme(),
}).SetupWithManager(mgr); err != nil {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ spec:
name: v1alpha1
schema:
openAPIV3Schema:
description: ValidationResult is the Schema for the validationresults API
description: ValidationResult is the Schema for the validationresults API.
properties:
apiVersion:
description: |-
Expand All @@ -50,23 +50,27 @@ spec:
metadata:
type: object
spec:
description: ValidationResultSpec defines the desired state of ValidationResult
description: ValidationResultSpec defines the desired state of ValidationResult.
properties:
expectedResults:
description: The number of rules in the validator plugin spec, hence
the number of expected ValidationResults.
minimum: 1
type: integer
plugin:
description: Plugin is the plugin code of the validator plugin that
was executed.
type: string
required:
- expectedResults
- plugin
type: object
status:
description: ValidationResultStatus defines the observed state of ValidationResult
description: ValidationResultStatus defines the observed state of ValidationResult.
properties:
conditions:
description: Conditions is a list of conditions that describe the
current state of the ValidationResult.
items:
description: Condition defines an observation of a Cluster API resource
operational state.
Expand Down Expand Up @@ -111,9 +115,14 @@ spec:
type: object
type: array
state:
description: State is the overall state of the validation result.
type: string
validationConditions:
description: ValidationConditions is a list of conditions that describe
the validation rules associated with the ValidationResult.
items:
description: ValidationCondition describes the state of a validation
rule.
properties:
details:
description: Human-readable messages indicating additional details
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ spec:
- name: v1alpha1
schema:
openAPIV3Schema:
description: ValidatorConfig is the Schema for the validatorconfigs API
description: ValidatorConfig is the Schema for the validatorconfigs API.
properties:
apiVersion:
description: |-
Expand All @@ -37,44 +37,57 @@ spec:
metadata:
type: object
spec:
description: ValidatorConfigSpec defines the desired state of ValidatorConfig
description: ValidatorConfigSpec defines the desired state of ValidatorConfig.
properties:
plugins:
description: Plugins defines the configuration for the validator plugins.
items:
description: HelmRelease defines the configuration for a Helm chart
release.
properties:
chart:
description: Chart defines the Helm chart to be installed.
properties:
authSecretName:
type: string
caFile:
description: AuthSecretName is the name of the K8s secret
containing the authentication details for the Helm repository.
type: string
insecureSkipVerify:
description: InsecureSkipTLSVerify skips the verification
of the server's certificate chain and host name.
type: boolean
name:
description: Name of the Helm chart.
type: string
repository:
description: Repository URL of the Helm chart.
type: string
version:
description: Version of the Helm chart.
type: string
required:
- name
- repository
- version
type: object
values:
description: Values defines the values to be passed to the Helm
chart.
type: string
required:
- chart
- values
type: object
type: array
sink:
description: Sink defines the configuration for the notification sink.
properties:
secretName:
description: Name of a K8s secret containing configuration details
for the sink
description: SecretName is the name of a K8s secret containing
configuration details for the sink.
type: string
type:
description: Type of the sink.
enum:
- alertmanager
- slack
Expand All @@ -89,6 +102,8 @@ spec:
properties:
conditions:
items:
description: ValidatorPluginCondition describes the state of a Validator
plugin.
properties:
lastUpdatedTime:
description: |-
Expand Down
3 changes: 2 additions & 1 deletion internal/controller/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ import (
"github.com/validator-labs/validator/internal/kube"
"github.com/validator-labs/validator/internal/sinks"
"github.com/validator-labs/validator/pkg/helm"
"github.com/validator-labs/validator/pkg/helm/release"
"github.com/validator-labs/validator/pkg/util"
//+kubebuilder:scaffold:imports
)
Expand Down Expand Up @@ -145,7 +146,7 @@ var _ = BeforeSuite(func() {
err = (&ValidatorConfigReconciler{
Client: k8sManager.GetClient(),
HelmClient: helm.NewHelmClient(rawConfig),
HelmSecretsClient: helm.NewSecretsClient(k8sManager.GetClient()),
HelmReleaseClient: release.NewHelmReleaseClient(k8sManager.GetClient()),
Log: ctrl.Log.WithName("controllers").WithName("ValidatorConfig"),
Scheme: k8sManager.GetScheme(),
}).SetupWithManager(k8sManager)
Expand Down
Loading

0 comments on commit 7877f71

Please sign in to comment.