Skip to content
Merged
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
10 changes: 5 additions & 5 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.24'
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
version: v2.1.0
# Disable package caching to avoid a double cache with setup-go.
skip-pkg-cache: true
args: --timeout 10m
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ bin

# editor and IDE paraphernalia
.idea
.vscode
*.swp
*.swo
*~
Expand Down
8 changes: 6 additions & 2 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
version: "2"

run:
timeout: 10m
skip-dirs:
- "api/v1alpha3"

linters:
disable:
- staticcheck
2 changes: 1 addition & 1 deletion api/v1alpha3/zz_generated.conversion.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion api/v1beta1/condition_consts.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package v1beta1

import clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
import clusterv1 "sigs.k8s.io/cluster-api/api/core/v1beta1"

const (
// EtcdMachinesSpecUpToDateCondition documents that the spec of the machines controlled by the EtcdadmCluster
Expand Down
2 changes: 1 addition & 1 deletion api/v1beta1/etcdadmcluster_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import (
etcdbp "github.com/aws/etcdadm-bootstrap-provider/api/v1beta1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
clusterv1 "sigs.k8s.io/cluster-api/api/core/v1beta1"
)

const (
Expand Down
70 changes: 48 additions & 22 deletions api/v1beta1/etcdadmcluster_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ limitations under the License.
package v1beta1

import (
"context"

apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/util/validation/field"
Expand All @@ -32,56 +34,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(ctx context.Context, obj runtime.Object) error {
cluster, ok := obj.(*EtcdadmCluster)
if !ok {
return apierrors.NewBadRequest("expected an EtcdadmCluster but got a different type")
}

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

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

if r.Spec.InfrastructureTemplate.Namespace == "" {
r.Spec.InfrastructureTemplate.Namespace = r.Namespace
if cluster.Spec.InfrastructureTemplate.Namespace == "" {
cluster.Spec.InfrastructureTemplate.Namespace = cluster.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(ctx context.Context, obj runtime.Object) (admission.Warnings, error) {
cluster, ok := obj.(*EtcdadmCluster)
if !ok {
return nil, apierrors.NewBadRequest("expected an EtcdadmCluster but got a different type")
}

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

allErrs := cluster.validateCommon()
if len(allErrs) > 0 {
return nil, apierrors.NewInvalid(GroupVersion.WithKind("EtcdadmCluster").GroupKind(), r.Name, allErrs)
return nil, apierrors.NewInvalid(GroupVersion.WithKind("EtcdadmCluster").GroupKind(), cluster.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(ctx context.Context, oldObj, newObj runtime.Object) (admission.Warnings, error) {
cluster, ok := newObj.(*EtcdadmCluster)
if !ok {
return nil, apierrors.NewBadRequest("expected an EtcdadmCluster but got a different type")
}

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

allErrs := r.validateCommon()
allErrs := cluster.validateCommon()
if len(allErrs) > 0 {
return nil, apierrors.NewInvalid(GroupVersion.WithKind("EtcdadmCluster").GroupKind(), r.Name, allErrs)
return nil, apierrors.NewInvalid(GroupVersion.WithKind("EtcdadmCluster").GroupKind(), cluster.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(ctx context.Context, obj runtime.Object) (admission.Warnings, error) {
cluster, ok := obj.(*EtcdadmCluster)
if !ok {
return nil, apierrors.NewBadRequest("expected an EtcdadmCluster but got a different type")
}

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

// TODO(user): fill in your validation logic upon object deletion.
return nil, nil
Expand Down
7 changes: 5 additions & 2 deletions api/v1beta1/etcdadmcluster_webhook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ limitations under the License.
package v1beta1

import (
"context"
"testing"

. "github.com/onsi/gomega"
Expand Down Expand Up @@ -79,7 +80,8 @@ func TestValidateCreate(t *testing.T) {
for name, tt := range cases {
t.Run(name, func(t *testing.T) {
g := NewWithT(t)
_, err := tt.in.ValidateCreate()
webhook := &EtcdadmCluster{}
_, err := webhook.ValidateCreate(context.Background(), tt.in)
if tt.expectErr == "" {
g.Expect(err).To(BeNil())
} else {
Expand Down Expand Up @@ -143,7 +145,8 @@ func TestValidateUpdate(t *testing.T) {
for name, tt := range cases {
t.Run(name, func(t *testing.T) {
g := NewWithT(t)
_, err := tt.newConf.ValidateUpdate(tt.oldConf)
webhook := &EtcdadmCluster{}
_, err := webhook.ValidateUpdate(context.Background(), tt.oldConf, tt.newConf)
if tt.expectErr != "" {
g.Expect(err).To(MatchError(ContainSubstring(tt.expectErr)))
} else {
Expand Down
2 changes: 1 addition & 1 deletion api/v1beta1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions controllers/certs.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ import (
"encoding/pem"
"fmt"

"sigs.k8s.io/cluster-api/util/conditions"

"path/filepath"

etcdv1 "github.com/aws/etcdadm-controller/api/v1beta1"
Expand All @@ -17,9 +15,11 @@ import (
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
certutil "k8s.io/client-go/util/cert"
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
clusterv1beta1 "sigs.k8s.io/cluster-api/api/core/v1beta1"
clusterv1 "sigs.k8s.io/cluster-api/api/core/v1beta2"
"sigs.k8s.io/cluster-api/util"
"sigs.k8s.io/cluster-api/util/certs"
v1beta1conditions "sigs.k8s.io/cluster-api/util/deprecated/v1beta1/conditions"
"sigs.k8s.io/cluster-api/util/secret"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
Expand Down Expand Up @@ -123,7 +123,7 @@ func (r *EtcdadmClusterReconciler) generateCAandClientCertSecrets(ctx context.Co
}

log.Info("Saved etcd ca cert as secret")
conditions.MarkTrue(etcdCluster, etcdv1.EtcdCertificatesAvailableCondition)
v1beta1conditions.MarkTrue(etcdCluster, clusterv1beta1.ConditionType(etcdv1.EtcdCertificatesAvailableCondition))
return nil
}

Expand Down
Loading
Loading