Skip to content

Commit

Permalink
Add license restrictions (#1382)
Browse files Browse the repository at this point in the history
Signed-off-by: Tamal Saha <[email protected]>
  • Loading branch information
tamalsaha authored Jan 13, 2025
1 parent 051792b commit ce659b3
Show file tree
Hide file tree
Showing 12 changed files with 277 additions and 30 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 LicenseRestrictions map[string]Restriction

type Restriction struct {
VersionConstraint string `json:"versionConstraint"`
// +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.

6 changes: 3 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ require (
github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring v0.75.2
github.com/prometheus-operator/prometheus-operator/pkg/client v0.75.2
go.bytebuilders.dev/audit v0.0.39
go.bytebuilders.dev/license-verifier/kubernetes v0.14.4
go.bytebuilders.dev/license-verifier/kubernetes v0.14.5
gomodules.xyz/encoding v0.0.8
gomodules.xyz/pointer v0.1.0
gomodules.xyz/runtime v0.3.0
Expand Down Expand Up @@ -151,8 +151,8 @@ require (
github.com/yudai/gojsondiff v1.0.0 // indirect
github.com/yudai/golcs v0.0.0-20170316035057-ecda9a501e82 // indirect
github.com/zeebo/xxh3 v1.0.2 // indirect
go.bytebuilders.dev/license-proxyserver v0.0.19 // indirect
go.bytebuilders.dev/license-verifier v0.14.4 // indirect
go.bytebuilders.dev/license-proxyserver v0.0.20 // indirect
go.bytebuilders.dev/license-verifier v0.14.5 // indirect
go.etcd.io/etcd/api/v3 v3.5.14 // indirect
go.etcd.io/etcd/client/pkg/v3 v3.5.14 // indirect
go.etcd.io/etcd/client/v3 v3.5.14 // indirect
Expand Down
12 changes: 6 additions & 6 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -385,12 +385,12 @@ github.com/zeebo/xxh3 v1.0.2 h1:xZmwmqxHZA8AI603jOQ0tMqmBr9lPeFwGg6d+xy9DC0=
github.com/zeebo/xxh3 v1.0.2/go.mod h1:5NWz9Sef7zIDm2JHfFlcQvNekmcEl9ekUZQQKCYaDcA=
go.bytebuilders.dev/audit v0.0.39 h1:JNwXIwqFZc+x5uvl0yyR2LdSVtfiLX85ypybKHTrNhA=
go.bytebuilders.dev/audit v0.0.39/go.mod h1:XbqHg9V/MldBx6qEsvs/McufBYiBkAg5r/cCfEit94Y=
go.bytebuilders.dev/license-proxyserver v0.0.19 h1:mY7zPDN0JCw2a1UajOuQUQKQKjjm5KBx2CbkT/+a1N8=
go.bytebuilders.dev/license-proxyserver v0.0.19/go.mod h1:B3Ig2Fo1qUollSV9GgfyFK8tXBI0RmUSpP1KFMZ2N7Q=
go.bytebuilders.dev/license-verifier v0.14.4 h1:JwTGQFew4nudwv8Pk3BdfQRts8KfgUQ5xhu138w1wt4=
go.bytebuilders.dev/license-verifier v0.14.4/go.mod h1:LqWXJKee5ofDcCYM6T5WilYlUc4NlKeZz58tHwO8GEs=
go.bytebuilders.dev/license-verifier/kubernetes v0.14.4 h1:NeHq6SuVhRIVaMW2kSXdr8DcuUOg2jVL9rsODIQp9Fc=
go.bytebuilders.dev/license-verifier/kubernetes v0.14.4/go.mod h1:1C7SaOJShC60mIXP1hXBaDWGpb0hrHQ4p4nUEvI6YQY=
go.bytebuilders.dev/license-proxyserver v0.0.20 h1:gzRSwUmX/LSwPVE6T9oy5RLIutU1EeI7hmS+QGsYBY4=
go.bytebuilders.dev/license-proxyserver v0.0.20/go.mod h1:2PJmjMCXncVyeP3fIVQ+hwZnuhmWSTmbcuEMBrFKIac=
go.bytebuilders.dev/license-verifier v0.14.5 h1:4P+eoi7n8QaEUFpM2TtXASmIdg4mjtXbMsvexWr2GWU=
go.bytebuilders.dev/license-verifier v0.14.5/go.mod h1:LqWXJKee5ofDcCYM6T5WilYlUc4NlKeZz58tHwO8GEs=
go.bytebuilders.dev/license-verifier/kubernetes v0.14.5 h1:pB2OkG/rDK8m8IQzS5l8ibJhfZwd/A2JQsbZtTxT0+8=
go.bytebuilders.dev/license-verifier/kubernetes v0.14.5/go.mod h1:yhEnPHRAlQ8t3Ini2K4qFI4dn+zWFCi3cW8GXS0Fj1A=
go.etcd.io/bbolt v1.3.8 h1:xs88BrvEv273UsB79e0hcVrlUWmS0a8upikMFhSyAtA=
go.etcd.io/bbolt v1.3.8/go.mod h1:N9Mkw9X8x5fupy0IKsmuqVtoGDyxsaDlbk4Rd05IAQw=
go.etcd.io/etcd/api/v3 v3.5.14 h1:vHObSCxyB9zlF60w7qzAdTcGaglbJOpSj1Xj9+WGxq0=
Expand Down
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
LicenseRestrictions configapi.LicenseRestrictions
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 MeetsLicenseRestrictions(kc client.Client, restrictions configapi.LicenseRestrictions, dbGK schema.GroupKind, dbVersion string) (bool, error) {
if len(restrictions) == 0 {
return true, nil
}
restriction, found := restrictions[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.VersionConstraint)
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
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ limitations under the License.
package v1alpha1

func (l License) DisableAnalytics() bool {
return len(l.FeatureFlags) > 0 && l.FeatureFlags["DisableAnalytics"] == "true"
return len(l.FeatureFlags) > 0 && l.FeatureFlags[FeatureDisableAnalytics] == "true"
}

func (i *License) Less(j *License) bool {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,11 @@ limitations under the License.
package v1alpha1

import (
"fmt"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/errors"
"k8s.io/apimachinery/pkg/util/sets"
)

// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
Expand All @@ -26,20 +30,20 @@ import (
type License struct {
metav1.TypeMeta `json:",inline,omitempty"`

Data []byte `json:"-"`
Issuer string `json:"issuer,omitempty"` // byte.builders
ProductLine string `json:"productLine,omitempty"`
TierName string `json:"tierName,omitempty"`
PlanName string `json:"planName,omitempty"`
Features []string `json:"features,omitempty"`
FeatureFlags map[string]string `json:"featureFlags,omitempty"`
Clusters []string `json:"clusters,omitempty"` // cluster_id ?
User *User `json:"user,omitempty"`
NotBefore *metav1.Time `json:"notBefore,omitempty"` // start of subscription start
NotAfter *metav1.Time `json:"notAfter,omitempty"` // if set, use this
ID string `json:"id,omitempty"` // license ID
Status LicenseStatus `json:"status"`
Reason string `json:"reason"`
Data []byte `json:"-"`
Issuer string `json:"issuer,omitempty"` // byte.builders
ProductLine string `json:"productLine,omitempty"`
TierName string `json:"tierName,omitempty"`
PlanName string `json:"planName,omitempty"`
Features []string `json:"features,omitempty"`
FeatureFlags FeatureFlags `json:"featureFlags,omitempty"`
Clusters []string `json:"clusters,omitempty"` // cluster_id ?
User *User `json:"user,omitempty"`
NotBefore *metav1.Time `json:"notBefore,omitempty"` // start of subscription start
NotAfter *metav1.Time `json:"notAfter,omitempty"` // if set, use this
ID string `json:"id,omitempty"` // license ID
Status LicenseStatus `json:"status"`
Reason string `json:"reason"`
}

type User struct {
Expand All @@ -62,3 +66,36 @@ type Contract struct {
StartTimestamp metav1.Time `json:"startTimestamp"`
ExpiryTimestamp metav1.Time `json:"expiryTimestamp"`
}

type FeatureFlag string

const (
FeatureDisableAnalytics FeatureFlag = "DisableAnalytics"
FeatureRestrictions FeatureFlag = "Restrictions"
FeatureEnableClientBilling FeatureFlag = "EnableClientBilling"
)

var knownFlags = sets.New[FeatureFlag](FeatureDisableAnalytics, FeatureRestrictions, FeatureEnableClientBilling)

type FeatureFlags map[FeatureFlag]string

func (f FeatureFlags) IsValid() error {
var errs []error
for k := range f {
if !knownFlags.Has(k) {
errs = append(errs, fmt.Errorf("unknown feature flag %q", k))
}
}
return errors.NewAggregate(errs)
}

func (f FeatureFlags) ToSlice() []string {
if len(f) == 0 {
return nil
}
result := make([]string, 0, len(f))
for k, v := range f {
result = append(result, fmt.Sprintf("%s=%s", k, v))
}
return result
}
Loading

0 comments on commit ce659b3

Please sign in to comment.