Skip to content

Commit eab18f5

Browse files
committed
remove defaultLinodeClient used by webhooks in favor of always using an authenticated client
1 parent d83700f commit eab18f5

File tree

6 files changed

+29
-56
lines changed

6 files changed

+29
-56
lines changed

internal/webhook/v1alpha2/linodecluster_webhook.go

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@ func SetupLinodeClusterWebhookWithManager(mgr ctrl.Manager) error {
4747
Complete()
4848
}
4949

50-
// TODO(user): change verbs to "verbs=create;update;delete" if you want to enable update and deletion validation.
5150
// +kubebuilder:webhook:path=/validate-infrastructure-cluster-x-k8s-io-v1alpha2-linodecluster,mutating=false,failurePolicy=fail,sideEffects=None,groups=infrastructure.cluster.x-k8s.io,resources=linodeclusters,verbs=create,versions=v1alpha2,name=validation.linodecluster.infrastructure.cluster.x-k8s.io,admissionReviewVersions=v1
5251

5352
// ValidateCreate implements webhook.Validator so a webhook will be registered for the type
@@ -59,19 +58,13 @@ func (r *linodeClusterValidator) ValidateCreate(ctx context.Context, obj runtime
5958
spec := cluster.Spec
6059
linodeclusterlog.Info("validate create", "name", cluster.Name)
6160

62-
var linodeclient clients.LinodeClient = defaultLinodeClient
63-
skipAPIValidation := false
61+
skipAPIValidation, linodeClient := setupClientWithCredentials(ctx, r.Client, spec.CredentialsRef,
62+
cluster.Name, cluster.GetNamespace(), linodeclusterlog)
6463

65-
// Handle credentials if provided
66-
if spec.CredentialsRef != nil {
67-
skipAPIValidation, linodeclient = setupClientWithCredentials(ctx, r.Client, spec.CredentialsRef,
68-
cluster.Name, cluster.GetNamespace(), linodeclusterlog)
69-
}
70-
71-
// TODO: instrument with tracing, might need refactor to preserve readibility
64+
// TODO: instrument with tracing, might need refactor to preserve readability
7265
var errs field.ErrorList
7366

74-
if err := r.validateLinodeClusterSpec(ctx, linodeclient, spec, skipAPIValidation); err != nil {
67+
if err := r.validateLinodeClusterSpec(ctx, linodeClient, spec, skipAPIValidation); err != nil {
7568
errs = slices.Concat(errs, err)
7669
}
7770

internal/webhook/v1alpha2/linodeclustertemplate_webhook.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,5 +33,3 @@ func SetupLinodeClusterTemplateWebhookWithManager(mgr ctrl.Manager) error {
3333
return ctrl.NewWebhookManagedBy(mgr).For(&infrav1alpha2.LinodeClusterTemplate{}).
3434
Complete()
3535
}
36-
37-
// TODO(user): EDIT THIS FILE! THIS IS SCAFFOLDING FOR YOU TO OWN!

internal/webhook/v1alpha2/linodemachine_webhook.go

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -65,16 +65,11 @@ func SetupLinodeMachineWebhookWithManager(mgr ctrl.Manager) error {
6565
Complete()
6666
}
6767

68-
// TODO(user): EDIT THIS FILE! THIS IS SCAFFOLDING FOR YOU TO OWN!
69-
70-
// TODO(user): change verbs to "verbs=create;update;delete" if you want to enable update and deletion validation.
7168
// +kubebuilder:webhook:path=/validate-infrastructure-cluster-x-k8s-io-v1alpha2-linodemachine,mutating=false,failurePolicy=fail,sideEffects=None,groups=infrastructure.cluster.x-k8s.io,resources=linodemachines,verbs=create,versions=v1alpha2,name=validation.linodemachine.infrastructure.cluster.x-k8s.io,admissionReviewVersions=v1
7269

7370
// ValidateCreate implements webhook.Validator so a webhook will be registered for the type
7471
func (r *linodeMachineValidator) ValidateCreate(ctx context.Context, obj runtime.Object) (admission.Warnings, error) {
75-
var linodeclient clients.LinodeClient = defaultLinodeClient
7672
var errs field.ErrorList
77-
skipAPIValidation := false
7873

7974
machine, ok := obj.(*infrav1alpha2.LinodeMachine)
8075
if !ok {
@@ -83,13 +78,10 @@ func (r *linodeMachineValidator) ValidateCreate(ctx context.Context, obj runtime
8378
spec := machine.Spec
8479
linodemachinelog.Info("validate create", "name", machine.Name)
8580

86-
// Handle credentials if provided
87-
if spec.CredentialsRef != nil {
88-
skipAPIValidation, linodeclient = setupClientWithCredentials(ctx, r.Client, spec.CredentialsRef,
89-
machine.Name, machine.GetNamespace(), linodemachinelog)
90-
}
81+
skipAPIValidation, linodeClient := setupClientWithCredentials(ctx, r.Client, spec.CredentialsRef,
82+
machine.Name, machine.GetNamespace(), linodemachinelog)
9183

92-
if err := r.validateLinodeMachineSpec(ctx, linodeclient, spec, skipAPIValidation); err != nil {
84+
if err := r.validateLinodeMachineSpec(ctx, linodeClient, spec, skipAPIValidation); err != nil {
9385
errs = slices.Concat(errs, err)
9486
}
9587

internal/webhook/v1alpha2/linodeplacementgroup_webhook.go

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -68,18 +68,12 @@ func (v *LinodePlacementGroupCustomValidator) ValidateCreate(ctx context.Context
6868
}
6969
linodeplacementgrouplog.Info("Validation for LinodePlacementGroup upon creation", "name", pg.GetName())
7070

71-
var linodeclient clients.LinodeClient = defaultLinodeClient
72-
skipAPIValidation := false
73-
74-
// Handle credentials if provided
75-
if pg.Spec.CredentialsRef != nil {
76-
skipAPIValidation, linodeclient = setupClientWithCredentials(ctx, v.Client, pg.Spec.CredentialsRef,
77-
pg.Name, pg.GetNamespace(), linodeplacementgrouplog)
78-
}
71+
skipAPIValidation, linodeClient := setupClientWithCredentials(ctx, v.Client, pg.Spec.CredentialsRef,
72+
pg.Name, pg.GetNamespace(), linodeplacementgrouplog)
7973

8074
var errs field.ErrorList
8175

82-
if err := v.validateLinodePlacementGroupSpec(ctx, linodeclient, pg.Spec, pg.Name, skipAPIValidation); err != nil {
76+
if err := v.validateLinodePlacementGroupSpec(ctx, linodeClient, pg.Spec, pg.Name, skipAPIValidation); err != nil {
8377
errs = slices.Concat(errs, err)
8478
}
8579

internal/webhook/v1alpha2/linodevpc_webhook.go

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,6 @@ func SetupLinodeVPCWebhookWithManager(mgr ctrl.Manager) error {
8585
Complete()
8686
}
8787

88-
// TODO(user): change verbs to "verbs=create;update;delete" if you want to enable update and deletion validation.
8988
// +kubebuilder:webhook:path=/validate-infrastructure-cluster-x-k8s-io-v1alpha2-linodevpc,mutating=false,failurePolicy=fail,sideEffects=None,groups=infrastructure.cluster.x-k8s.io,resources=linodevpcs,verbs=create,versions=v1alpha2,name=validation.linodevpc.infrastructure.cluster.x-k8s.io,admissionReviewVersions=v1
9089

9190
// ValidateCreate implements webhook.Validator so a webhook will be registered for the type
@@ -97,17 +96,11 @@ func (r *linodeVPCValidator) ValidateCreate(ctx context.Context, obj runtime.Obj
9796
spec := vpc.Spec
9897
linodevpclog.Info("validate create", "name", vpc.Name)
9998

100-
var linodeclient clients.LinodeClient = defaultLinodeClient
101-
skipAPIValidation := false
99+
skipAPIValidation, linodeClient := setupClientWithCredentials(ctx, r.Client, spec.CredentialsRef,
100+
vpc.Name, vpc.GetNamespace(), linodevpclog)
102101

103-
// Handle credentials if provided
104-
if spec.CredentialsRef != nil {
105-
skipAPIValidation, linodeclient = setupClientWithCredentials(ctx, r.Client, spec.CredentialsRef,
106-
vpc.Name, vpc.GetNamespace(), linodevpclog)
107-
}
108-
109-
// TODO: instrument with tracing, might need refactor to preserve readibility
110-
errs := r.validateLinodeVPCSpec(ctx, linodeclient, spec, skipAPIValidation)
102+
// TODO: instrument with tracing, might need refactor to preserve readability
103+
errs := r.validateLinodeVPCSpec(ctx, linodeClient, spec, skipAPIValidation)
111104

112105
if len(errs) == 0 {
113106
return nil, nil

internal/webhook/v1alpha2/webhook_helpers.go

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"context"
2121
"fmt"
2222
"net/http"
23+
"os"
2324
"regexp"
2425
"slices"
2526
"time"
@@ -41,14 +42,6 @@ const (
4142
defaultClientTimeout = time.Second * 10
4243
)
4344

44-
var (
45-
// defaultLinodeClient is an unauthenticated Linode client
46-
defaultLinodeClient = linodeclient.NewLinodeClientWithTracing(
47-
ptr.To(linodego.NewClient(&http.Client{Timeout: defaultClientTimeout})),
48-
linodeclient.DefaultDecorator(),
49-
)
50-
)
51-
5245
func validateRegion(ctx context.Context, linodegoclient clients.LinodeClient, id string, path *field.Path, capabilities ...string) *field.Error {
5346
region, err := linodegoclient.GetRegion(ctx, id)
5447
if err != nil {
@@ -132,14 +125,24 @@ func getCredentials(ctx context.Context, crClient clients.K8sClient, credentials
132125
return &credSecret, nil
133126
}
134127

135-
// setupClientWithCredentials configures a Linode client with credentials from a secret reference
128+
// setupClientWithCredentials configures a Linode client with credentials the LINODE_TOKEN env variable or
129+
// a secret reference if it is provided
136130
// Returns (skipAPIValidation, client) - skipAPIValidation will be true if credentials cannot be found
137131
// and API validation should be skipped
138132
func setupClientWithCredentials(ctx context.Context, crClient clients.K8sClient, credRef *corev1.SecretReference,
139133
resourceName, namespace string, logger logr.Logger) (bool, clients.LinodeClient) {
140-
linodeClient := defaultLinodeClient
134+
linodeClient := linodeclient.NewLinodeClientWithTracing(
135+
ptr.To(linodego.NewClient(&http.Client{Timeout: defaultClientTimeout})),
136+
linodeclient.DefaultDecorator(),
137+
)
138+
credName := ""
139+
apiToken := []byte(os.Getenv("LINODE_TOKEN"))
140+
var err error
141+
if credRef != nil {
142+
credName = credRef.Name
143+
apiToken, err = getCredentialDataFromRef(ctx, crClient, *credRef, namespace)
144+
}
141145

142-
apiToken, err := getCredentialDataFromRef(ctx, crClient, *credRef, namespace)
143146
if err == nil {
144147
logger.Info("creating a verified linode client for create request", "name", resourceName)
145148
linodeClient.SetToken(string(apiToken))
@@ -149,7 +152,7 @@ func setupClientWithCredentials(ctx context.Context, crClient clients.K8sClient,
149152
// Handle error cases
150153
if apierrors.IsNotFound(err) {
151154
logger.Info("credentials secret not found, skipping API validation",
152-
"name", resourceName, "secret", credRef.Name)
155+
"name", resourceName, "secret", credName)
153156
return true, linodeClient
154157
}
155158

0 commit comments

Comments
 (0)