Skip to content

Commit

Permalink
Add license restrictions
Browse files Browse the repository at this point in the history
Signed-off-by: Tamal Saha <[email protected]>
  • Loading branch information
tamalsaha committed Jan 13, 2025
1 parent 7dd4fbe commit a064312
Show file tree
Hide file tree
Showing 5 changed files with 188 additions and 0 deletions.
25 changes: 25 additions & 0 deletions apis/config/v1alpha1/license_constraints_types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
Copyright AppsCode Inc. and Contributors
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 v1alpha1

type LicenseConstraints map[string]Restrictions

type Restrictions struct {
Constraint string `json:"constraint"`
// +optional
Distributions []string `json:"distributions,omitempty"`
}
35 changes: 35 additions & 0 deletions apis/config/v1alpha1/openapi_generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

43 changes: 43 additions & 0 deletions apis/config/v1alpha1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions pkg/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package controller
import (
"time"

configapi "kubedb.dev/apimachinery/apis/config/v1alpha1"
cs "kubedb.dev/apimachinery/client/clientset/versioned"
kubedbinformers "kubedb.dev/apimachinery/client/informers/externalversions"

Expand Down Expand Up @@ -88,6 +89,7 @@ type Config struct {

// Only watch or reconcile objects in this namespace (usually for license reasons)
RestrictToNamespace string
LicenseConstraints *configapi.LicenseConstraints
ResyncPeriod time.Duration
ReadinessProbeInterval time.Duration
MaxNumRequeues int
Expand Down
83 changes: 83 additions & 0 deletions pkg/license/lib.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/*
Copyright AppsCode Inc. and Contributors
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 license

import (
"context"

catalogapi "kubedb.dev/apimachinery/apis/catalog/v1alpha1"
configapi "kubedb.dev/apimachinery/apis/config/v1alpha1"

"github.com/Masterminds/semver/v3"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime/schema"
"sigs.k8s.io/controller-runtime/pkg/client"
)

func MeetsLicenseConstraints(kc client.Client, constraints configapi.LicenseConstraints, dbGK schema.GroupKind, dbVersion string) (bool, error) {
if len(constraints) == 0 {
return true, nil
}
restriction, found := constraints[dbGK.Kind]
if !found {
return false, nil
}

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
}

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

c, err := semver.NewConstraint(restriction.Constraint)
if err != nil {
return false, err
}
if !c.Check(v) {
// write reason ?
return false, nil
}
if len(restriction.Distributions) > 0 {
strDistro, ok, err := unstructured.NestedString(dbv.UnstructuredContent(), "spec", "distribution")
if err != nil || !ok {
return false, err
}
if !contains(restriction.Distributions, strDistro) {
return false, nil
}
}
return true, nil
}

func contains(list []string, str string) bool {
for _, v := range list {
if v == str {
return true
}
}
return false
}

0 comments on commit a064312

Please sign in to comment.