Skip to content

Commit

Permalink
Merge pull request #318 from sabre1041/exclude-invalid-names
Browse files Browse the repository at this point in the history
Support for excluding invalid group names
  • Loading branch information
raffaelespazzoli authored Jul 8, 2024
2 parents d0c36e9 + d68841a commit 1f427b8
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 2 deletions.
11 changes: 11 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,17 @@ BUNDLE_METADATA_OPTS ?= $(BUNDLE_CHANNELS) $(BUNDLE_DEFAULT_CHANNEL)
# example.com/memcached-operator-bundle:$VERSION and example.com/memcached-operator-catalog:$VERSION.
IMAGE_TAG_BASE ?= quay.io/redhat-cop/$(OPERATOR_NAME)

# BUNDLE_GEN_FLAGS are the flags passed to the operator-sdk generate bundle command
BUNDLE_GEN_FLAGS ?= -q --overwrite --version $(VERSION) $(BUNDLE_METADATA_OPTS)

# USE_IMAGE_DIGESTS defines if images are resolved via tags or digests
# You can enable this value if you would like to use SHA Based Digests
# To enable set flag to true
USE_IMAGE_DIGESTS ?= false
ifeq ($(USE_IMAGE_DIGESTS), true)
BUNDLE_GEN_FLAGS += --use-image-digests
endif

# BUNDLE_IMG defines the image:tag used for the bundle.
# You can use it as an arg. (E.g make bundle-build BUNDLE_IMG=<some-registry>/<project-name-bundle>:<tag>)
BUNDLE_IMG ?= $(IMAGE_TAG_BASE)-bundle:v$(VERSION)
Expand Down
5 changes: 5 additions & 0 deletions api/v1alpha1/groupsync_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@ type GroupSyncSpec struct {
// +operator-sdk:csv:customresourcedefinitions:type=spec,displayName="Schedule",xDescriptors={"urn:alm:descriptor:com.tectonic.ui:text"}
// +kubebuilder:validation:Optional
Schedule string `json:"schedule,omitempty"`

// ExcludeInvalidGroupNames excludes Groups with names that are not RFC 1035 compliant.
// +operator-sdk:csv:customresourcedefinitions:type=spec,displayName="Exclude Invalid Group Names",xDescriptors={"urn:alm:descriptor:com.tectonic.ui:booleanSwitch"}
// +kubebuilder:validation:Optional
ExcludeInvalidGroupNames bool `json:"excludeInvalidGroupNames,omitempty"`
}

// GroupSyncStatus defines the observed state of GroupSync
Expand Down
3 changes: 3 additions & 0 deletions config/crd/bases/redhatcop.redhat.io_groupsyncs.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ spec:
spec:
description: GroupSyncSpec defines the desired state of GroupSync
properties:
excludeInvalidGroupNames:
description: ExcludeInvalidGroupNames excludes Groups with names that are not RFC 1035 compliant.
type: boolean
providers:
description: List of Providers that can be mounted by containers belonging to the pod.
items:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@ spec:
kind: GroupSync
name: groupsyncs.redhatcop.redhat.io
specDescriptors:
- description: ExcludeInvalidGroupNames excludes Groups with names that are
not RFC 1035 compliant.
displayName: Exclude Invalid Group Names
path: excludeInvalidGroupNames
x-descriptors:
- urn:alm:descriptor:com.tectonic.ui:booleanSwitch
- description: List of Providers that can be mounted by containers belonging
to the pod.
displayName: Providers
Expand Down Expand Up @@ -721,7 +727,7 @@ spec:
displayName: Last Sync Success Time
path: lastSyncSuccessTime
version: v1alpha1
description: |-
description: |
Synchronizes groups from external providers into OpenShift
## Overview
Expand Down Expand Up @@ -881,7 +887,7 @@ spec:
```shell
oc create secret generic gitlab-group-sync --from-literal=token=<token> --from-literal=tokenType=personal
```
```
The following keys are required for username and password:
Expand Down
11 changes: 11 additions & 0 deletions controllers/groupsync_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package controllers
import (
"context"
"fmt"
"strings"
"time"

"github.com/go-logr/logr"
Expand All @@ -32,6 +33,7 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
utilerrors "k8s.io/apimachinery/pkg/util/errors"
apimachineryvalidation "k8s.io/apimachinery/pkg/util/validation"
kubeclock "k8s.io/utils/clock"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
Expand Down Expand Up @@ -125,6 +127,15 @@ func (r *GroupSyncReconciler) Reconcile(context context.Context, req ctrl.Reques

for i, group := range groups {

// Verify valid Group Names
if instance.Spec.ExcludeInvalidGroupNames {
msgs := apimachineryvalidation.IsDNS1035Label(group.Name)
if len(msgs) > 0 {
r.Log.Info(fmt.Sprintf("Group '%s' contains invalid name: %s", group.Name, strings.Join(msgs, ",")))
continue
}
}

ocpGroup := &userv1.Group{}
err := r.GetClient().Get(context, types.NamespacedName{Name: group.Name, Namespace: ""}, ocpGroup)

Expand Down

0 comments on commit 1f427b8

Please sign in to comment.