Skip to content

Commit

Permalink
Merge pull request #1547 from openshift-cherrypick-robot/cherry-pick-…
Browse files Browse the repository at this point in the history
…1542-to-release-0.6

[release-0.6] server: add --shard-external-url
  • Loading branch information
openshift-ci[bot] authored Jul 16, 2022
2 parents 9ace7ff + c794ff6 commit e87906f
Show file tree
Hide file tree
Showing 7 changed files with 398 additions and 461 deletions.
53 changes: 33 additions & 20 deletions pkg/admission/clusterworkspaceshard/admission.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,16 @@ package clusterworkspaceshard

import (
"context"
"errors"
"fmt"
"io"

"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/util/validation/field"
"k8s.io/apiserver/pkg/admission"

"github.com/kcp-dev/kcp/pkg/admission/initializers"
tenancyv1alpha1 "github.com/kcp-dev/kcp/pkg/apis/tenancy/v1alpha1"
tenancyhelper "github.com/kcp-dev/kcp/pkg/apis/tenancy/v1alpha1/helper"
)

// Validate ClusterWorkspace creation and updates for
Expand All @@ -53,18 +52,20 @@ type clusterWorkspaceShard struct {
*admission.Handler

shardBaseURL string
shardExternalURL string
externalAddressProvider func() string
}

// Ensure that the required admission interfaces are implemented.
var _ = admission.ValidationInterface(&clusterWorkspaceShard{})
var _ = admission.MutationInterface(&clusterWorkspaceShard{})
var _ = initializers.WantsExternalAddressProvider(&clusterWorkspaceShard{})
var _ = initializers.WantsShardExternalURL(&clusterWorkspaceShard{})

// Validate ensures that
// - baseURL is set
// - externalURL is set
func (o *clusterWorkspaceShard) Validate(ctx context.Context, a admission.Attributes, _ admission.ObjectInterfaces) (err error) {
func (o *clusterWorkspaceShard) Validate(_ context.Context, a admission.Attributes, _ admission.ObjectInterfaces) (err error) {
if a.GetResource().GroupResource() != tenancyv1alpha1.Resource("clusterworkspaceshards") {
return nil
}
Expand All @@ -78,18 +79,24 @@ func (o *clusterWorkspaceShard) Validate(ctx context.Context, a admission.Attrib
return fmt.Errorf("failed to convert unstructured to ClusterWorkspace: %w", err)
}

var errs field.ErrorList

if cws.Spec.BaseURL == "" {
return admission.NewForbidden(a, errors.New("spec.baseURL must be set"))
errs = append(errs, field.Required(field.NewPath("spec", "baseURL"), ""))
}
if cws.Spec.ExternalURL == "" {
return admission.NewForbidden(a, errors.New("spec.externalURL must be set"))
errs = append(errs, field.Required(field.NewPath("spec", "externalURL"), ""))
}

if len(errs) > 0 {
return admission.NewForbidden(a, errs.ToAggregate())
}

return nil
}

// Admit defaults the baseURL and externalURL to the shards external hostname.
func (o *clusterWorkspaceShard) Admit(ctx context.Context, a admission.Attributes, _ admission.ObjectInterfaces) (err error) {
// Admit sets.
func (o *clusterWorkspaceShard) Admit(_ context.Context, a admission.Attributes, _ admission.ObjectInterfaces) (err error) {
if a.GetResource().GroupResource() != tenancyv1alpha1.Resource("clusterworkspaceshards") {
return nil
}
Expand All @@ -103,25 +110,27 @@ func (o *clusterWorkspaceShard) Admit(ctx context.Context, a admission.Attribute
return fmt.Errorf("failed to convert unstructured to ClusterWorkspaceShard: %w", err)
}

if cws.Spec.BaseURL == "" {
defaultExternalAddress := ""
if o.externalAddressProvider != nil {
defaultExternalAddress = o.externalAddressProvider()
}

if o.shardBaseURL == "" && defaultExternalAddress == "" {
return fmt.Errorf("cannot default baseURL for ClusterWorkspaceShard %q: no default shard base URL and no default external address provided", tenancyhelper.QualifiedObjectName(cws))
}
externalAddress := ""
if o.externalAddressProvider != nil {
externalAddress = o.externalAddressProvider()
}

if o.shardBaseURL != "" {
if cws.Spec.BaseURL == "" {
switch {
case o.shardBaseURL != "":
cws.Spec.BaseURL = o.shardBaseURL
} else {
cws.Spec.BaseURL = "https://" + defaultExternalAddress
case externalAddress != "":
cws.Spec.BaseURL = "https://" + externalAddress
}
}

if cws.Spec.ExternalURL == "" {
cws.Spec.ExternalURL = cws.Spec.BaseURL
switch {
case o.shardExternalURL != "":
cws.Spec.ExternalURL = o.shardExternalURL
case externalAddress != "":
cws.Spec.ExternalURL = "https://" + externalAddress
}
}

raw, err := runtime.DefaultUnstructuredConverter.ToUnstructured(cws)
Expand All @@ -137,6 +146,10 @@ func (o *clusterWorkspaceShard) SetShardBaseURL(shardBaseURL string) {
o.shardBaseURL = shardBaseURL
}

func (o *clusterWorkspaceShard) SetShardExternalURL(shardExternalURL string) {
o.shardExternalURL = shardExternalURL
}

func (o *clusterWorkspaceShard) SetExternalAddressProvider(externalAddressProvider func() string) {
o.externalAddressProvider = externalAddressProvider
}
Loading

0 comments on commit e87906f

Please sign in to comment.