- Add three application metadata labels to HelmRelease: - apps.cozystack.io/application.kind - apps.cozystack.io/application.group - apps.cozystack.io/application.name - Replace shouldIncludeHelmRelease filtering with label-based filtering in Get, List, and Update methods - Always add kind and group label requirements in List for precise filtering - Update CozystackResourceDefinitionController to watch only HelmReleases with cozystack.io/ui=true label - Update LineageControllerWebhook to extract metadata directly from HelmRelease labels instead of using mapping configuration - Add functionality to update HelmRelease chart from CozystackResourceDefinition using label selectors Signed-off-by: Andrei Kvapil <kvapss@gmail.com>
73 lines
2.1 KiB
Go
73 lines
2.1 KiB
Go
package lineagecontrollerwebhook
|
|
|
|
import (
|
|
"fmt"
|
|
"strings"
|
|
|
|
cozyv1alpha1 "github.com/cozystack/cozystack/api/v1alpha1"
|
|
helmv2 "github.com/fluxcd/helm-controller/api/v2"
|
|
)
|
|
|
|
type appRef struct {
|
|
group string
|
|
kind string
|
|
}
|
|
|
|
type runtimeConfig struct {
|
|
appCRDMap map[appRef]*cozyv1alpha1.CozystackResourceDefinition
|
|
}
|
|
|
|
func (l *LineageControllerWebhook) initConfig() {
|
|
l.initOnce.Do(func() {
|
|
if l.config.Load() == nil {
|
|
l.config.Store(&runtimeConfig{
|
|
appCRDMap: make(map[appRef]*cozyv1alpha1.CozystackResourceDefinition),
|
|
})
|
|
}
|
|
})
|
|
}
|
|
|
|
// getApplicationLabel safely extracts an application label from HelmRelease
|
|
func getApplicationLabel(hr *helmv2.HelmRelease, key string) (string, error) {
|
|
if hr.Labels == nil {
|
|
return "", fmt.Errorf("cannot map helm release %s/%s to dynamic app: labels are nil", hr.Namespace, hr.Name)
|
|
}
|
|
val, ok := hr.Labels[key]
|
|
if !ok {
|
|
return "", fmt.Errorf("cannot map helm release %s/%s to dynamic app: missing %s label", hr.Namespace, hr.Name, key)
|
|
}
|
|
return val, nil
|
|
}
|
|
|
|
func (l *LineageControllerWebhook) Map(hr *helmv2.HelmRelease) (string, string, string, error) {
|
|
// Extract application metadata from labels
|
|
appKind, err := getApplicationLabel(hr, "apps.cozystack.io/application.kind")
|
|
if err != nil {
|
|
return "", "", "", err
|
|
}
|
|
|
|
appGroup, err := getApplicationLabel(hr, "apps.cozystack.io/application.group")
|
|
if err != nil {
|
|
return "", "", "", err
|
|
}
|
|
|
|
appName, err := getApplicationLabel(hr, "apps.cozystack.io/application.name")
|
|
if err != nil {
|
|
return "", "", "", err
|
|
}
|
|
|
|
// Construct API version from group
|
|
apiVersion := fmt.Sprintf("%s/v1alpha1", appGroup)
|
|
|
|
// Extract prefix from HelmRelease name by removing the application name
|
|
// HelmRelease name format: <prefix><application-name>
|
|
prefix := strings.TrimSuffix(hr.Name, appName)
|
|
|
|
// Validate the derived prefix
|
|
// This ensures correctness when appName appears multiple times in hr.Name
|
|
if prefix+appName != hr.Name {
|
|
return "", "", "", fmt.Errorf("cannot derive prefix from helm release %s/%s: name does not end with application name %s", hr.Namespace, hr.Name, appName)
|
|
}
|
|
|
|
return apiVersion, appKind, prefix, nil
|
|
}
|