Skip to content

Commit

Permalink
Feature: Fully Qualified App Name (#86)
Browse files Browse the repository at this point in the history
* app name functions and refactored devfile registry labels function for app name getter changes

Signed-off-by: Michael Valdron <[email protected]>

* truncateName & LabelsForDevfileRegistry test cases

Signed-off-by: Michael Valdron <[email protected]>

* app name getter test cases

Signed-off-by: Michael Valdron <[email protected]>

* refactor naming.go functions to use fully qualified app name instead of CR name

Signed-off-by: Michael Valdron <[email protected]>

* change truncated length of app full name part of configmap name to add space for suffix part

Signed-off-by: Michael Valdron <[email protected]>

* naming function getter test cases

Signed-off-by: Michael Valdron <[email protected]>

* truncateName & truncateNameLengthN comment blocks

Signed-off-by: Michael Valdron <[email protected]>

* k8s ingress host name uses app full name rather than CR name to match OpenShift route url

Signed-off-by: Michael Valdron <[email protected]>

* fix full name contains case

Signed-off-by: Michael Valdron <[email protected]>

* fix given CR names in integration test cases to contain default app name devfile-registry

Signed-off-by: Michael Valdron <[email protected]>

---------

Signed-off-by: Michael Valdron <[email protected]>
  • Loading branch information
michael-valdron authored Apr 15, 2024
1 parent c385aba commit 89120e9
Show file tree
Hide file tree
Showing 23 changed files with 1,186 additions and 86 deletions.
4 changes: 2 additions & 2 deletions controllers/devfileregistry_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ func (r *DevfileRegistryReconciler) Reconcile(ctx context.Context, req ctrl.Requ
}

// Generate labels for any subresources generated by the operator
labels := registry.LabelsForDevfileRegistry(devfileRegistry.Name)
labels := registry.LabelsForDevfileRegistry(devfileRegistry)

log.Info("Deploying registry")

Expand Down Expand Up @@ -141,7 +141,7 @@ func (r *DevfileRegistryReconciler) Reconcile(ctx context.Context, req ctrl.Requ

// Get the hostname of the generated devfile route
devfilesRoute := &routev1.Route{}
err = r.Get(ctx, types.NamespacedName{Name: registry.IngressName(devfileRegistry.Name), Namespace: devfileRegistry.Namespace}, devfilesRoute)
err = r.Get(ctx, types.NamespacedName{Name: registry.IngressName(devfileRegistry), Namespace: devfileRegistry.Namespace}, devfilesRoute)
if err != nil {
// Log an error, but requeue, as the controller's cached kube client likely hasn't registered the new route yet.
// See https://github.com/operator-framework/operator-sdk/issues/4013#issuecomment-707267616 for an explanation on why we requeue rather than error out here
Expand Down
16 changes: 8 additions & 8 deletions controllers/ensure.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ import (

func (r *DevfileRegistryReconciler) ensure(ctx context.Context, cr *registryv1alpha1.DevfileRegistry, resource client.Object, labels map[string]string, ingressDomain string) (*reconcile.Result, error) {
resourceType := reflect.TypeOf(resource).Elem().Name()
resourceName := getResourceName(resource, cr.Name)
resourceName := getResourceName(resource, cr)
//use the controller log
// Check to see if the requested resource exists on the cluster. If it doesn't exist, create it and return.
err := r.Get(ctx, types.NamespacedName{Name: resourceName, Namespace: cr.Namespace}, resource)
Expand Down Expand Up @@ -74,20 +74,20 @@ func (r *DevfileRegistryReconciler) ensure(ctx context.Context, cr *registryv1al
return nil, nil
}

func getResourceName(resource runtime.Object, crName string) string {
func getResourceName(resource runtime.Object, cr *registryv1alpha1.DevfileRegistry) string {
switch resource.(type) {
case *appsv1.Deployment:
return registry.DeploymentName(crName)
return registry.DeploymentName(cr)
case *corev1.ConfigMap:
return registry.ConfigMapName(crName)
return registry.ConfigMapName(cr)
case *corev1.PersistentVolumeClaim:
return registry.PVCName(crName)
return registry.PVCName(cr)
case *corev1.Service:
return registry.ServiceName(crName)
return registry.ServiceName(cr)
case *routev1.Route, *networkingv1.Ingress:
return registry.IngressName(crName)
return registry.IngressName(cr)
}
return registry.GenericResourceName(crName)
return registry.GenericResourceName(cr)
}

func (r *DevfileRegistryReconciler) generateResourceObject(cr *registryv1alpha1.DevfileRegistry, resource client.Object, labels map[string]string, ingressDomain string) client.Object {
Expand Down
2 changes: 1 addition & 1 deletion controllers/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ func (r *DevfileRegistryReconciler) deleteOldPVCIfNeeded(ctx context.Context, cr
// Check to see if a PVC exists, if so, need to clean it up because storage was disabled
if !registry.IsStorageEnabled(cr) {
pvc := &corev1.PersistentVolumeClaim{}
err := r.Get(ctx, types.NamespacedName{Name: registry.PVCName(cr.Name), Namespace: cr.Namespace}, pvc)
err := r.Get(ctx, types.NamespacedName{Name: registry.PVCName(cr), Namespace: cr.Namespace}, pvc)
if err != nil {
if errors.IsNotFound(err) {
// PVC not found, so there's no old PVC to delete. Just return nil, nothing to do.
Expand Down
2 changes: 1 addition & 1 deletion pkg/registry/configmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ DEVFILE_REGISTRIES=[{"name":"%s","url":"http://localhost:8080","fqdn":"%s"}]`,
configMapData[".env.registry-viewer"] = viewerEnvfile

cm := &corev1.ConfigMap{
ObjectMeta: generateObjectMeta(ConfigMapName(cr.Name), cr.Namespace, labels),
ObjectMeta: generateObjectMeta(ConfigMapName(cr), cr.Namespace, labels),
Data: configMapData,
}

Expand Down
19 changes: 19 additions & 0 deletions pkg/registry/constants.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
//
//
// Copyright Red Hat
//
// 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 registry

const maxTruncLength = 63
49 changes: 48 additions & 1 deletion pkg/registry/defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
package registry

import (
"fmt"
"strings"

registryv1alpha1 "github.com/devfile/registry-operator/api/v1alpha1"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
Expand Down Expand Up @@ -65,6 +68,9 @@ const (
DefaultHostnameOverride = ""
DefaultNameOverride = ""
DefaultFullnameOverride = ""

// App name default
DefaultAppName = "devfile-registry"
)

// GetRegistryViewerImage returns the container image for the registry viewer to be deployed on the Devfile Registry.
Expand Down Expand Up @@ -159,7 +165,7 @@ func GetDevfileRegistryVolumeSource(cr *registryv1alpha1.DevfileRegistry) corev1
if IsStorageEnabled(cr) {
return corev1.VolumeSource{
PersistentVolumeClaim: &corev1.PersistentVolumeClaimVolumeSource{
ClaimName: PVCName(cr.Name),
ClaimName: PVCName(cr),
},
}
}
Expand Down Expand Up @@ -251,3 +257,44 @@ func getDevfileRegistrySpecContainer(quantity string, defaultValue string) resou
}
return resource.MustParse(defaultValue)
}

// getAppName returns app name of a devfile registry
// truncated to 63 characters max, if `DevfileRegistry.NameOverride`
// is set it will return the override name truncated to 63 characters max
func getAppName(cr *registryv1alpha1.DevfileRegistry) string {
if cr != nil {
nameOverride := GetNameOverride(cr)

if nameOverride == DefaultNameOverride {
return truncateName(DefaultAppName)
}

return truncateName(nameOverride)
}

return truncateName(DefaultAppName)
}

// getAppFullName returns fully qualified app name of a devfile registry
// truncated to 63 characters max, if `DevfileRegistry.FullnameOverride`
// is set it will return the override name truncated to 63 characters max
func getAppFullName(cr *registryv1alpha1.DevfileRegistry) string {
if cr != nil {
fullNameOverride := GetFullnameOverride(cr)

if fullNameOverride == DefaultFullnameOverride {
appName := getAppName(cr)
if cr.Name == "" {
return truncateName(appName)
} else if strings.Contains(cr.Name, appName) {
return truncateName(cr.Name)
} else {
return truncateName(fmt.Sprintf("%s-%s", cr.Name, appName))
}
}

return truncateName(fullNameOverride)
}

return truncateName(DefaultAppName)
}
Loading

0 comments on commit 89120e9

Please sign in to comment.