Skip to content

Commit 4cadc05

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

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
@@ -69,6 +69,7 @@ type pathAnnotationPlugin struct {
6969

7070
var pathAnnotationResources = sets.New[string](
7171
apisv1alpha2.Resource("apiexports").String(),
72+
apisv1alpha2.Resource("apibindings").String(),
7273
tenancyv1alpha1.Resource("workspacetypes").String(),
7374
)
7475

@@ -105,6 +106,10 @@ func (p *pathAnnotationPlugin) Admit(ctx context.Context, a admission.Attributes
105106

106107
logicalCluster, err := p.getLogicalCluster(clusterName, corev1alpha1.LogicalClusterName)
107108
if err != nil {
109+
// We skip adding for system bindings if the logical cluster is not found during creation. This is racy during workspace bootstrap.
110+
if apierrors.IsNotFound(err) && a.GetResource().GroupResource() == apisv1alpha2.Resource("apibindings") {
111+
return nil
112+
}
108113
return admission.NewForbidden(a, fmt.Errorf("cannot get this workspace: %w", err))
109114
}
110115
thisPath := logicalCluster.Annotations[core.LogicalClusterPathAnnotationKey]
@@ -136,25 +141,33 @@ func (p *pathAnnotationPlugin) Validate(ctx context.Context, a admission.Attribu
136141
if a.GetResource().GroupResource() == corev1alpha1.Resource("logicalclusters") {
137142
return nil
138143
}
144+
isAPIBinding := a.GetResource().GroupResource() == apisv1alpha2.Resource("apibindings")
139145

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

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

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

0 commit comments

Comments
 (0)