Skip to content

Commit

Permalink
Return the reason for not allowing license restrictions (#1392)
Browse files Browse the repository at this point in the history
Signed-off-by: Arnob kumar saha <arnob@appscode.com>
  • Loading branch information
ArnobKumarSaha authored Feb 10, 2025
1 parent d3ee4ca commit 7f58d03
Showing 1 changed file with 12 additions and 11 deletions.
23 changes: 12 additions & 11 deletions pkg/license/lib.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package license

import (
"context"
"fmt"

catalogapi "kubedb.dev/apimachinery/apis/catalog/v1alpha1"
configapi "kubedb.dev/apimachinery/apis/config/v1alpha1"
Expand All @@ -28,49 +29,49 @@ import (
"sigs.k8s.io/controller-runtime/pkg/client"
)

func MeetsLicenseRestrictions(kc client.Client, restrictions configapi.LicenseRestrictions, dbGK schema.GroupKind, dbVersion string) (bool, error) {
func MeetsLicenseRestrictions(kc client.Client, restrictions configapi.LicenseRestrictions, dbGK schema.GroupKind, dbVersion string) (bool, string, error) {
if len(restrictions) == 0 {
return true, nil
return true, "", nil
}
restriction, found := restrictions[dbGK.Kind]
if !found {
return false, nil
return false, fmt.Sprintf("Kind is not found in the restrictions map"), nil

Check failure on line 38 in pkg/license/lib.go

View workflow job for this annotation

GitHub Actions / Build

S1039: unnecessary use of fmt.Sprintf (gosimple)
}

var dbv unstructured.Unstructured
dbv.SetGroupVersionKind(catalogapi.SchemeGroupVersion.WithKind(dbGK.Kind + "Version"))
err := kc.Get(context.TODO(), client.ObjectKey{Name: dbVersion}, &dbv)
if err != nil {
return false, err
return false, "", err
}

strVer, ok, err := unstructured.NestedString(dbv.UnstructuredContent(), "spec", "version")
if err != nil || !ok {
return false, err
return false, "", err
}
v, err := semver.NewVersion(strVer)
if err != nil {
return false, err
return false, "", err
}

c, err := semver.NewConstraint(restriction.VersionConstraint)
if err != nil {
return false, err
return false, "", err
}
if !c.Check(v) {
// write reason ?
return false, nil
return false, fmt.Sprintf("Doesn't satisfy the constraint: %v", restriction.VersionConstraint), nil
}
if len(restriction.Distributions) > 0 {
strDistro, ok, err := unstructured.NestedString(dbv.UnstructuredContent(), "spec", "distribution")
if err != nil || !ok {
return false, err
return false, "", err
}
if !contains(restriction.Distributions, strDistro) {
return false, nil
return false, fmt.Sprintf("%v distro is not present in the allowed distributions list: %v", strDistro, restriction.Distributions), nil
}
}
return true, nil
return true, "", nil
}

func contains(list []string, str string) bool {
Expand Down

0 comments on commit 7f58d03

Please sign in to comment.