Skip to content

Commit 4864f43

Browse files
committed
Update shard validation
1 parent d4dbbdb commit 4864f43

File tree

3 files changed

+14
-5
lines changed

3 files changed

+14
-5
lines changed

pkg/admission/shard/admission.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,15 @@ func (o *shard) Admit(_ context.Context, a admission.Attributes, _ admission.Obj
6565
return fmt.Errorf("failed to convert unstructured to Shard: %w", err)
6666
}
6767

68+
// If you reading this - this is probably not what you looking for.
69+
// Defaulting happens in pkg/server/config.go#L561, and these becomes no-op.
70+
// The only case where these will be used, if somebody is creating Shard
71+
// objects directly or using kcp as a library and skipping defaulting in config.go.
72+
// So if you not sure what is happening - its probably better to be explicit and set these flags.
73+
// But if you are doing that - you probably know what you are doing :)
74+
if wShard.Spec.BaseURL == "" {
75+
return fmt.Errorf("spec.baseURL is required")
76+
}
6877
if wShard.Spec.ExternalURL == "" {
6978
wShard.Spec.ExternalURL = wShard.Spec.BaseURL
7079
}

pkg/admission/shard/admission_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -128,9 +128,9 @@ func TestAdmit(t *testing.T) {
128128
expected *corev1alpha1.Shard
129129
}{
130130
{
131-
name: "nothing set",
132-
shard: newShard().Shard,
133-
expected: newShard().Shard,
131+
name: "only base URL set (default)",
132+
shard: newShard().baseURL("https://test").Shard,
133+
expected: newShard().baseURL("https://test").externalURL("https://test").virtualWorkspaceURL("https://test").Shard,
134134
},
135135
{
136136
name: "all set",

pkg/server/config.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -562,13 +562,13 @@ func NewConfig(ctx context.Context, opts kcpserveroptions.CompletedOptions) (*Co
562562
if opts.Extra.ShardExternalURL != "" {
563563
return opts.Extra.ShardExternalURL
564564
}
565-
return "https://" + c.GenericConfig.ExternalAddress
565+
return c.ShardBaseURL()
566566
}
567567
c.ShardVirtualWorkspaceURL = func() string {
568568
if opts.Extra.ShardVirtualWorkspaceURL != "" {
569569
return opts.Extra.ShardVirtualWorkspaceURL
570570
}
571-
return "https://" + c.GenericConfig.ExternalAddress
571+
return c.ShardBaseURL()
572572
}
573573

574574
serviceResolver := webhook.NewDefaultServiceResolver()

0 commit comments

Comments
 (0)