Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
14 changes: 7 additions & 7 deletions .github/workflows/golangci-lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,16 @@ jobs:
name: lint
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-go@v3
- uses: actions/checkout@v4
- uses: actions/setup-go@v5
with:
go-version: '1.22'
go-version: '1.23'
check-latest: true
cache: true
- name: golangci-lint
uses: golangci/golangci-lint-action@v3
uses: golangci/golangci-lint-action@v8
with:
version: v1.55.2
# Disable package caching to avoid a double cache with setup-go.
skip-pkg-cache: true
version: v2.1.0
only-new-issues: true
args: --timeout 10m

2 changes: 1 addition & 1 deletion .github/workflows/vulnerability.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ jobs:
with:
show-progress: false
- name: "Dependency Review"
uses: actions/dependency-review-action@v3
uses: actions/dependency-review-action@v4
with:
base-ref: ${{ github.event.pull_request.base.sha || github.event.before || github.sha }}
head-ref: ${{ github.event.pull_request.head.sha || github.sha }}
Expand Down
49 changes: 47 additions & 2 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,49 @@
version: "2"
run:
timeout: 10m
skip-dirs:
- "api/v1alpha3"
linters:
enable:
- gocyclo
- godot
- nakedret
- revive
settings:
gocyclo:
min-complexity: 10
godot:
exclude:
- (?i)^\s*todo
period: true
exclusions:
generated: lax
presets:
- common-false-positives
- std-error-handling
paths:
- zz_generated.*\.go$
- .*/mocks
- internal/aws-sdk-go-v2
- builtin$
- examples$
issues:
max-issues-per-linter: 0
max-same-issues: 0
formatters:
enable:
- gci
- gofumpt
settings:
gci:
sections:
- standard
- default
- prefix(github.com/aws/etcdadm-controller)
custom-order: true
exclusions:
generated: lax
paths:
- zz_generated.*\.go$
- .*/mocks
- internal/aws-sdk-go-v2
- builtin$
- examples$
71 changes: 49 additions & 22 deletions api/v1beta1/etcdadmcluster_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ limitations under the License.
package v1beta1

import (
"context"
"fmt"

apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/util/validation/field"
Expand All @@ -32,56 +35,80 @@ var etcdadmclusterlog = logf.Log.WithName("etcdadmcluster-resource")
func (r *EtcdadmCluster) SetupWebhookWithManager(mgr ctrl.Manager) error {
return ctrl.NewWebhookManagedBy(mgr).
For(r).
WithDefaulter(r).
WithValidator(r).
Complete()
}

// +kubebuilder:webhook:verbs=create;update,path=/mutate-etcdcluster-cluster-x-k8s-io-v1beta1-etcdadmcluster,mutating=true,failurePolicy=fail,groups=etcdcluster.cluster.x-k8s.io,resources=etcdadmclusters,versions=v1beta1,name=metcdadmcluster.kb.io,sideEffects=None,admissionReviewVersions=v1;v1beta1

var _ webhook.Defaulter = &EtcdadmCluster{}
var _ webhook.CustomDefaulter = &EtcdadmCluster{}

// +kubebuilder:webhook:verbs=create;update,path=/validate-etcdcluster-cluster-x-k8s-io-v1beta1-etcdadmcluster,mutating=false,failurePolicy=fail,groups=etcdcluster.cluster.x-k8s.io,resources=etcdadmclusters,versions=v1beta1,name=vetcdadmcluster.kb.io,sideEffects=None,admissionReviewVersions=v1;v1beta1

var _ webhook.Validator = &EtcdadmCluster{}
var _ webhook.CustomValidator = &EtcdadmCluster{}

// Default implements webhook.Defaulter so a webhook will be registered for the type
func (r *EtcdadmCluster) Default() {
etcdadmclusterlog.Info("default", "name", r.Name)
// Default implements webhook.CustomDefaulter so a webhook will be registered for the type
func (r *EtcdadmCluster) Default(_ context.Context, obj runtime.Object) error {
etcdadmCluster, ok := obj.(*EtcdadmCluster)
if !ok {
return fmt.Errorf("expected an EtcdadmCluster but got %T", obj)
}

if r.Spec.Replicas == nil {
etcdadmclusterlog.Info("default", "name", etcdadmCluster.Name)

if etcdadmCluster.Spec.Replicas == nil {
replicas := int32(1)
r.Spec.Replicas = &replicas
etcdadmCluster.Spec.Replicas = &replicas
}

if r.Spec.InfrastructureTemplate.Namespace == "" {
r.Spec.InfrastructureTemplate.Namespace = r.Namespace
if etcdadmCluster.Spec.InfrastructureTemplate.Namespace == "" {
etcdadmCluster.Spec.InfrastructureTemplate.Namespace = etcdadmCluster.Namespace
}

return nil
}

// ValidateCreate implements webhook.Validator so a webhook will be registered for the type
func (r *EtcdadmCluster) ValidateCreate() (admission.Warnings, error) {
etcdadmclusterlog.Info("validate create", "name", r.Name)
// ValidateCreate implements webhook.CustomValidator so a webhook will be registered for the type
func (r *EtcdadmCluster) ValidateCreate(_ context.Context, obj runtime.Object) (admission.Warnings, error) {
etcdadmCluster, ok := obj.(*EtcdadmCluster)
if !ok {
return nil, fmt.Errorf("expected an EtcdadmCluster but got %T", obj)
}

allErrs := r.validateCommon()
etcdadmclusterlog.Info("validate create", "name", etcdadmCluster.Name)

allErrs := etcdadmCluster.validateCommon()
if len(allErrs) > 0 {
return nil, apierrors.NewInvalid(GroupVersion.WithKind("EtcdadmCluster").GroupKind(), r.Name, allErrs)
return nil, apierrors.NewInvalid(GroupVersion.WithKind("EtcdadmCluster").GroupKind(), etcdadmCluster.Name, allErrs)
}
return nil, nil
}

// ValidateUpdate implements webhook.Validator so a webhook will be registered for the type
func (r *EtcdadmCluster) ValidateUpdate(old runtime.Object) (admission.Warnings, error) {
etcdadmclusterlog.Info("validate update", "name", r.Name)
// ValidateUpdate implements webhook.CustomValidator so a webhook will be registered for the type
func (r *EtcdadmCluster) ValidateUpdate(_ context.Context, old, obj runtime.Object) (admission.Warnings, error) {
etcdadmCluster, ok := obj.(*EtcdadmCluster)
if !ok {
return nil, fmt.Errorf("expected an EtcdadmCluster but got %T", obj)
}

etcdadmclusterlog.Info("validate update", "name", etcdadmCluster.Name)

allErrs := r.validateCommon()
allErrs := etcdadmCluster.validateCommon()
if len(allErrs) > 0 {
return nil, apierrors.NewInvalid(GroupVersion.WithKind("EtcdadmCluster").GroupKind(), r.Name, allErrs)
return nil, apierrors.NewInvalid(GroupVersion.WithKind("EtcdadmCluster").GroupKind(), etcdadmCluster.Name, allErrs)
}
return nil, nil
}

// ValidateDelete implements webhook.Validator so a webhook will be registered for the type
func (r *EtcdadmCluster) ValidateDelete() (admission.Warnings, error) {
etcdadmclusterlog.Info("validate delete", "name", r.Name)
// ValidateDelete implements webhook.CustomValidator so a webhook will be registered for the type
func (r *EtcdadmCluster) ValidateDelete(_ context.Context, obj runtime.Object) (admission.Warnings, error) {
etcdadmCluster, ok := obj.(*EtcdadmCluster)
if !ok {
return nil, fmt.Errorf("expected an EtcdadmCluster but got %T", obj)
}

etcdadmclusterlog.Info("validate delete", "name", etcdadmCluster.Name)

// TODO(user): fill in your validation logic upon object deletion.
return nil, nil
Expand Down
Loading
Loading