Skip to content
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
2 changes: 1 addition & 1 deletion .github/workflows/codespell.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:
runs-on: ubuntu-latest
steps:
- name: Harden Runner
uses: step-security/harden-runner@a5ad31d6a139d249332a2605b85202e8c0b78450 # v2.19.1
uses: step-security/harden-runner@9ca718d3bf646d6534007c269a635b3e54cadf99 # v2.19.2
with:
egress-policy: audit

Expand Down
84 changes: 84 additions & 0 deletions pkg/admissionpolicymanager/commons.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/*
Copyright 2026 The KubeFleet Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package admissionpolicymanager

import (
"context"
"regexp"
"strings"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/controller-runtime/pkg/client"

"go.goms.io/fleet/pkg/utils/errors"
)

const (
// illegalCELStringChars is a string of characters that should not be used in CEL string literals.
illegalCELStringChars = `'"\`
)

var (
// reservedNamespacePrefixRegexp matches valid namespace prefix characters (DNS label subset).
reservedNamespacePrefixRegexp = regexp.MustCompile(`^[a-z0-9-]+$`)
)

var (
managedByAndPartOfKubeFleetLabelSelector = client.MatchingLabels{
VAPManagedByKubeFleetLabelKey: VAPManagedByKubeFleetLabelValue,
VAPPartOfKubeFleetLabelKey: VAPPartOfKubeFleetLabelValue,
VAPComponentKubeFleetLabelKey: VAPComponentAdmissionPolicyManagerLabelValue,
}
)

var (
// buildRetryUnlessCtxErr returns a function that is used with retry.OnError to stop
// retrying if the parent context has been cancelled or is erred.
buildRetryUnlessCtxErr = func(ctx context.Context) func(error) bool {
return func(err error) bool {
if err := ctx.Err(); err != nil {
return false
}
return true
}
}
)

// addManagedByPartOfAndComponentLabels adds labels to the given object to indicate that it is managed by
// KubeFleet and belongs to the admission policy manager component.
func addManagedByPartOfAndComponentLabels(obj metav1.Object) {
labels := obj.GetLabels()
if labels == nil {
labels = make(map[string]string)
}

labels[VAPManagedByKubeFleetLabelKey] = VAPManagedByKubeFleetLabelValue
labels[VAPPartOfKubeFleetLabelKey] = VAPPartOfKubeFleetLabelValue
labels[VAPComponentKubeFleetLabelKey] = VAPComponentAdmissionPolicyManagerLabelValue
obj.SetLabels(labels)
}

// validateCELStringLiterals checks if any of the provided strings contains characters that are illegal
// in CEL string literals (e.g., backslash, double quotes, and single quotes).
func validateCELStringLiterals(strs ...string) error {
for _, str := range strs {
if strings.ContainsAny(str, illegalCELStringChars) {
return errors.NewUserError(nil, "string literal contains illegal characters for a CEL expression", "value", str)
}
}
return nil
}
41 changes: 41 additions & 0 deletions pkg/admissionpolicymanager/configs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
Copyright 2026 The KubeFleet Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package admissionpolicymanager

import (
"go.goms.io/fleet/pkg/utils"
)

// PolicyGeneratorConfigs holds the configurations for all available admission policy
// generators.
//
// This type is exposed so that users can provide a configuration object (in its serialized form)
// that specifies individual configurations for each generator.
type PolicyGeneratorConfigs struct {
PodsAndReplicaSetsVAPGeneratorConfig *PodsAndReplicaSetsValidatingAdmissionPolicyGenerator `json:"denyPodsAndReplicaSetsOutsideReservedNamespaces,omitempty"`
SvcAccountsAndTokenRequestsVAPGeneratorConfig *ServiceAccountsAndTokenRequestsValidatingAdmissionPolicyGenerator `json:"denyServiceAccountsAndTokenRequestsInReservedNamespaces,omitempty"`
}

// DefaultPolicyGeneratorConfigs is the default configuration for all available admission policy generators.
var DefaultPolicyGeneratorConfigs = &PolicyGeneratorConfigs{
PodsAndReplicaSetsVAPGeneratorConfig: &PodsAndReplicaSetsValidatingAdmissionPolicyGenerator{
ReservedNamespacePrefixes: []string{utils.FleetNSNamePrefix, utils.KubeNSNamePrefix},
},
SvcAccountsAndTokenRequestsVAPGeneratorConfig: &ServiceAccountsAndTokenRequestsValidatingAdmissionPolicyGenerator{
ReservedNamespacePrefixes: []string{utils.FleetNSNamePrefix, utils.KubeNSNamePrefix},
},
}
Loading
Loading