Skip to content

Commit 89120e9

Browse files
Feature: Fully Qualified App Name (#86)
* 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]>
1 parent c385aba commit 89120e9

23 files changed

+1186
-86
lines changed

controllers/devfileregistry_controller.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ func (r *DevfileRegistryReconciler) Reconcile(ctx context.Context, req ctrl.Requ
9494
}
9595

9696
// Generate labels for any subresources generated by the operator
97-
labels := registry.LabelsForDevfileRegistry(devfileRegistry.Name)
97+
labels := registry.LabelsForDevfileRegistry(devfileRegistry)
9898

9999
log.Info("Deploying registry")
100100

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

142142
// Get the hostname of the generated devfile route
143143
devfilesRoute := &routev1.Route{}
144-
err = r.Get(ctx, types.NamespacedName{Name: registry.IngressName(devfileRegistry.Name), Namespace: devfileRegistry.Namespace}, devfilesRoute)
144+
err = r.Get(ctx, types.NamespacedName{Name: registry.IngressName(devfileRegistry), Namespace: devfileRegistry.Namespace}, devfilesRoute)
145145
if err != nil {
146146
// Log an error, but requeue, as the controller's cached kube client likely hasn't registered the new route yet.
147147
// See https://github.com/operator-framework/operator-sdk/issues/4013#issuecomment-707267616 for an explanation on why we requeue rather than error out here

controllers/ensure.go

+8-8
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ import (
3636

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

77-
func getResourceName(resource runtime.Object, crName string) string {
77+
func getResourceName(resource runtime.Object, cr *registryv1alpha1.DevfileRegistry) string {
7878
switch resource.(type) {
7979
case *appsv1.Deployment:
80-
return registry.DeploymentName(crName)
80+
return registry.DeploymentName(cr)
8181
case *corev1.ConfigMap:
82-
return registry.ConfigMapName(crName)
82+
return registry.ConfigMapName(cr)
8383
case *corev1.PersistentVolumeClaim:
84-
return registry.PVCName(crName)
84+
return registry.PVCName(cr)
8585
case *corev1.Service:
86-
return registry.ServiceName(crName)
86+
return registry.ServiceName(cr)
8787
case *routev1.Route, *networkingv1.Ingress:
88-
return registry.IngressName(crName)
88+
return registry.IngressName(cr)
8989
}
90-
return registry.GenericResourceName(crName)
90+
return registry.GenericResourceName(cr)
9191
}
9292

9393
func (r *DevfileRegistryReconciler) generateResourceObject(cr *registryv1alpha1.DevfileRegistry, resource client.Object, labels map[string]string, ingressDomain string) client.Object {

controllers/update.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ func (r *DevfileRegistryReconciler) deleteOldPVCIfNeeded(ctx context.Context, cr
197197
// Check to see if a PVC exists, if so, need to clean it up because storage was disabled
198198
if !registry.IsStorageEnabled(cr) {
199199
pvc := &corev1.PersistentVolumeClaim{}
200-
err := r.Get(ctx, types.NamespacedName{Name: registry.PVCName(cr.Name), Namespace: cr.Namespace}, pvc)
200+
err := r.Get(ctx, types.NamespacedName{Name: registry.PVCName(cr), Namespace: cr.Namespace}, pvc)
201201
if err != nil {
202202
if errors.IsNotFound(err) {
203203
// PVC not found, so there's no old PVC to delete. Just return nil, nothing to do.

pkg/registry/configmap.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ DEVFILE_REGISTRIES=[{"name":"%s","url":"http://localhost:8080","fqdn":"%s"}]`,
5959
configMapData[".env.registry-viewer"] = viewerEnvfile
6060

6161
cm := &corev1.ConfigMap{
62-
ObjectMeta: generateObjectMeta(ConfigMapName(cr.Name), cr.Namespace, labels),
62+
ObjectMeta: generateObjectMeta(ConfigMapName(cr), cr.Namespace, labels),
6363
Data: configMapData,
6464
}
6565

pkg/registry/constants.go

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
//
2+
//
3+
// Copyright Red Hat
4+
//
5+
// Licensed under the Apache License, Version 2.0 (the "License");
6+
// you may not use this file except in compliance with the License.
7+
// You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing, software
12+
// distributed under the License is distributed on an "AS IS" BASIS,
13+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
// See the License for the specific language governing permissions and
15+
// limitations under the License.
16+
17+
package registry
18+
19+
const maxTruncLength = 63

pkg/registry/defaults.go

+48-1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@
1717
package registry
1818

1919
import (
20+
"fmt"
21+
"strings"
22+
2023
registryv1alpha1 "github.com/devfile/registry-operator/api/v1alpha1"
2124
corev1 "k8s.io/api/core/v1"
2225
"k8s.io/apimachinery/pkg/api/resource"
@@ -65,6 +68,9 @@ const (
6568
DefaultHostnameOverride = ""
6669
DefaultNameOverride = ""
6770
DefaultFullnameOverride = ""
71+
72+
// App name default
73+
DefaultAppName = "devfile-registry"
6874
)
6975

7076
// GetRegistryViewerImage returns the container image for the registry viewer to be deployed on the Devfile Registry.
@@ -159,7 +165,7 @@ func GetDevfileRegistryVolumeSource(cr *registryv1alpha1.DevfileRegistry) corev1
159165
if IsStorageEnabled(cr) {
160166
return corev1.VolumeSource{
161167
PersistentVolumeClaim: &corev1.PersistentVolumeClaimVolumeSource{
162-
ClaimName: PVCName(cr.Name),
168+
ClaimName: PVCName(cr),
163169
},
164170
}
165171
}
@@ -251,3 +257,44 @@ func getDevfileRegistrySpecContainer(quantity string, defaultValue string) resou
251257
}
252258
return resource.MustParse(defaultValue)
253259
}
260+
261+
// getAppName returns app name of a devfile registry
262+
// truncated to 63 characters max, if `DevfileRegistry.NameOverride`
263+
// is set it will return the override name truncated to 63 characters max
264+
func getAppName(cr *registryv1alpha1.DevfileRegistry) string {
265+
if cr != nil {
266+
nameOverride := GetNameOverride(cr)
267+
268+
if nameOverride == DefaultNameOverride {
269+
return truncateName(DefaultAppName)
270+
}
271+
272+
return truncateName(nameOverride)
273+
}
274+
275+
return truncateName(DefaultAppName)
276+
}
277+
278+
// getAppFullName returns fully qualified app name of a devfile registry
279+
// truncated to 63 characters max, if `DevfileRegistry.FullnameOverride`
280+
// is set it will return the override name truncated to 63 characters max
281+
func getAppFullName(cr *registryv1alpha1.DevfileRegistry) string {
282+
if cr != nil {
283+
fullNameOverride := GetFullnameOverride(cr)
284+
285+
if fullNameOverride == DefaultFullnameOverride {
286+
appName := getAppName(cr)
287+
if cr.Name == "" {
288+
return truncateName(appName)
289+
} else if strings.Contains(cr.Name, appName) {
290+
return truncateName(cr.Name)
291+
} else {
292+
return truncateName(fmt.Sprintf("%s-%s", cr.Name, appName))
293+
}
294+
}
295+
296+
return truncateName(fullNameOverride)
297+
}
298+
299+
return truncateName(DefaultAppName)
300+
}

0 commit comments

Comments
 (0)