Skip to content

Commit 2448833

Browse files
committed
Add path annotation to apibindings
Signed-off-by: Mangirdas Judeikis <[email protected]> On-behalf-of: @SAP [email protected]
1 parent 1736b49 commit 2448833

File tree

1 file changed

+16
-3
lines changed

1 file changed

+16
-3
lines changed

pkg/admission/pathannotation/pathannotation_admission.go

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ type pathAnnotationPlugin struct {
7171
var pathAnnotationResources = sets.New[string](
7272
apisv1alpha2.Resource("apiexports").String(),
7373
cachev1alpha1.Resource("cachedresources").String(),
74+
apisv1alpha2.Resource("apibindings").String(),
7475
tenancyv1alpha1.Resource("workspacetypes").String(),
7576
)
7677

@@ -107,6 +108,10 @@ func (p *pathAnnotationPlugin) Admit(ctx context.Context, a admission.Attributes
107108

108109
logicalCluster, err := p.getLogicalCluster(clusterName, corev1alpha1.LogicalClusterName)
109110
if err != nil {
111+
// We skip adding for system bindings if the logical cluster is not found during creation. This is racy during workspace bootstrap.
112+
if apierrors.IsNotFound(err) && a.GetResource().GroupResource() == apisv1alpha2.Resource("apibindings") {
113+
return nil
114+
}
110115
return admission.NewForbidden(a, fmt.Errorf("cannot get this workspace: %w", err))
111116
}
112117
thisPath := logicalCluster.Annotations[core.LogicalClusterPathAnnotationKey]
@@ -138,25 +143,33 @@ func (p *pathAnnotationPlugin) Validate(ctx context.Context, a admission.Attribu
138143
if a.GetResource().GroupResource() == corev1alpha1.Resource("logicalclusters") {
139144
return nil
140145
}
146+
isAPIBinding := a.GetResource().GroupResource() == apisv1alpha2.Resource("apibindings")
141147

142148
u, ok := a.GetObject().(metav1.Object)
143149
if !ok {
144150
return fmt.Errorf("unexpected type %T", a.GetObject())
145151
}
146152

147-
value, found := u.GetAnnotations()[core.LogicalClusterPathAnnotationKey]
153+
annotations := u.GetAnnotations()
154+
value, found := annotations[core.LogicalClusterPathAnnotationKey]
148155
if pathAnnotationResources.Has(a.GetResource().GroupResource().String()) || found {
149156
logicalCluster, err := p.getLogicalCluster(clusterName, corev1alpha1.LogicalClusterName)
150157
if err != nil {
158+
// We skip adding for system bindings if the logical cluster is not found during creation. This is racy during workspace bootstrap.
159+
if apierrors.IsNotFound(err) && isAPIBinding {
160+
return nil
161+
}
151162
return admission.NewForbidden(a, fmt.Errorf("cannot get this workspace: %w", err))
152163
}
153164
thisPath := logicalCluster.Annotations[core.LogicalClusterPathAnnotationKey]
154165
if thisPath == "" {
155166
thisPath = logicalcluster.From(logicalCluster).Path().String()
156167
}
157168

158-
if value != thisPath {
159-
return admission.NewForbidden(a, fmt.Errorf("annotation %q must match canonical path %q", core.LogicalClusterPathAnnotationKey, thisPath))
169+
// Only validate if annotation is explicitly set (found=true) and paths don't match.
170+
// This prevents admission of the objects without the annotation (with exception of APIBindings).
171+
if value != thisPath && !isAPIBinding {
172+
return admission.NewForbidden(a, fmt.Errorf("annotation for %s, %q must match canonical path %q, but got %q", a.GetName(), core.LogicalClusterPathAnnotationKey, thisPath, value))
160173
}
161174
}
162175

0 commit comments

Comments
 (0)