Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

EnvoyFleet name validation #213

Merged
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
22 changes: 21 additions & 1 deletion api/v1alpha1/envoyfleet_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,14 @@ import (
)

// log is for logging in this package.
var envoyfleetlog = logf.Log.WithName("envoyfleet-resource")
var (
envoyfleetlog = logf.Log.WithName("envoyfleet-resource")
)

const (
EnvoyFleetValidatingWebhookPath = "/validate-gateway-kusk-io-v1alpha1-envoyfleet"

EnvoyResourceNamePrefix = "kgw-envoy-"
)

// TODO(user): change verbs to "verbs=create;update;delete" if you want to enable deletion validation.
Expand Down Expand Up @@ -79,13 +83,29 @@ func (e *EnvoyFleetValidator) Handle(ctx context.Context, req admission.Request)
}

func (e *EnvoyFleetValidator) validate(ctx context.Context, envoyFleet *EnvoyFleet) error {

if err := e.validateNameWithinSizeBound(envoyFleet.Name); err != nil {
return err
}

if err := e.validateNoOverlappingSANSInTLS(ctx, envoyFleet.Spec.TLS.TlsSecrets); err != nil {
return err
}

return nil
}

func (e *EnvoyFleetValidator) validateNameWithinSizeBound(name string) error {
if kubernetesMaxNameLength := 64; len(EnvoyResourceNamePrefix+name) > kubernetesMaxNameLength {
return fmt.Errorf(
"resulting name of envoy resources (%s) is larger than the kubernetes max allowed name of %d",
name,
kubernetesMaxNameLength,
)
}
return nil
}

func (e *EnvoyFleetValidator) validateNoOverlappingSANSInTLS(ctx context.Context, secrets []TLSSecrets) error {
getSecret := func(tlsSecret TLSSecrets) (*v1.Secret, error) {
var secret v1.Secret
Expand Down
7 changes: 4 additions & 3 deletions controllers/envoyfleet_resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ func (e *EnvoyFleetResources) generateConfigMap(ctx context.Context) error {
labels[key] = value
}

configMapName := "kgw-envoy-" + e.fleet.Name
configMapName := gateway.EnvoyResourceNamePrefix + e.fleet.Name

xdsLabels := map[string]string{"app.kubernetes.io/name": "kusk-gateway", "app.kubernetes.io/component": "xds-service"}
xdsServices, err := k8sutils.GetServicesByLabels(ctx, e.client, xdsLabels)
Expand Down Expand Up @@ -125,7 +125,7 @@ func (e *EnvoyFleetResources) generateDeployment() {
labels[key] = value
}

deploymentName := "kgw-envoy-" + e.fleet.Name
deploymentName := gateway.EnvoyResourceNamePrefix + e.fleet.Name

configMapName := e.configMap.Name

Expand Down Expand Up @@ -230,7 +230,8 @@ func (e *EnvoyFleetResources) generateService() {
for key, value := range e.sharedLabels {
labels[key] = value
}
serviceName := "kgw-envoy-" + e.fleet.Name
serviceName := gateway.EnvoyResourceNamePrefix + e.fleet.Name

e.service = &corev1.Service{
TypeMeta: metav1.TypeMeta{
Kind: "Service",
Expand Down