use k8s.io/apimachinery/pkg/util/sets for FeatureSet

Signed-off-by: Tim Ramlot <42113979+inteon@users.noreply.github.com>
This commit is contained in:
Tim Ramlot 2024-01-05 19:19:10 +01:00
parent 8848559d3d
commit 224cf06208
No known key found for this signature in database
GPG Key ID: 47428728E0C2878D
5 changed files with 14 additions and 103 deletions

View File

@ -16,72 +16,19 @@ limitations under the License.
package featureset
import "strings"
import (
"k8s.io/apimachinery/pkg/util/sets"
)
// NewFeatureSet constructs a new feature set with the given features.
func NewFeatureSet(feats ...Feature) FeatureSet {
fs := make(FeatureSet)
for _, f := range feats {
fs.Add(f)
}
return fs
return FeatureSet(sets.New(feats...))
}
// FeatureSet represents a set of features.
// This type does not indicate whether or not features are enabled, rather it
// just defines a grouping of features (i.e. a 'set').
type FeatureSet map[Feature]struct{}
// Add adds features to the set
func (fs FeatureSet) Add(f ...Feature) FeatureSet {
for _, feat := range f {
fs[feat] = struct{}{}
}
return fs
}
// Delete removes a feature from the set
func (fs FeatureSet) Delete(f Feature) {
delete(fs, f)
}
// Contains returns true if the FeatureSet contains the given feature
func (fs FeatureSet) Contains(f Feature) bool {
_, ok := fs[f]
return ok
}
// Copy returns a new copy of an existing Feature Set.
// It is not safe to be called by multiple goroutines.
func (fs FeatureSet) Copy() FeatureSet {
new := make(FeatureSet)
for k, v := range fs {
new[k] = v
}
return new
}
// List returns a slice of all features in the set.
func (fs FeatureSet) List() []Feature {
var ret []Feature
for k := range fs {
ret = append(ret, k)
}
return ret
}
// String returns this FeatureSet as a comma separated string
func (fs FeatureSet) String() string {
featsSlice := make([]string, len(fs))
i := 0
for f := range fs {
featsSlice[i] = string(f)
i++
}
return strings.Join(featsSlice, ", ")
}
type FeatureSet = sets.Set[Feature]
type Feature string

View File

@ -75,18 +75,18 @@ func CertificateSetForUnsupportedFeatureSet(fs featureset.FeatureSet) []certific
certificates.ExpectValidBasicConstraints,
}
if !fs.Contains(featureset.URISANsFeature) {
if !fs.Has(featureset.URISANsFeature) {
out = append(out, certificates.ExpectCertificateURIsToMatch)
}
if !fs.Contains(featureset.EmailSANsFeature) {
if !fs.Has(featureset.EmailSANsFeature) {
out = append(out, certificates.ExpectEmailsToMatch)
}
if !fs.Contains(featureset.SaveCAToSecret) {
if !fs.Has(featureset.SaveCAToSecret) {
out = append(out, certificates.ExpectCorrectTrustChain)
if !fs.Contains(featureset.SaveRootCAToSecret) {
if !fs.Has(featureset.SaveRootCAToSecret) {
out = append(out, certificates.ExpectCARootCertificate)
}
}
@ -97,7 +97,7 @@ func CertificateSetForUnsupportedFeatureSet(fs featureset.FeatureSet) []certific
func CertificateSigningRequestSetForUnsupportedFeatureSet(fs featureset.FeatureSet) []certificatesigningrequests.ValidationFunc {
validations := DefaultCertificateSigningRequestSet()
if !fs.Contains(featureset.DurationFeature) {
if !fs.Has(featureset.DurationFeature) {
validations = append(validations, certificatesigningrequests.ExpectValidDuration)
}

View File

@ -62,7 +62,7 @@ func runACMEIssuerTests(eab *cmacme.ACMEExternalAccountBinding) {
featureset.OtherNamesFeature,
)
var unsupportedHTTP01GatewayFeatures = unsupportedHTTP01Features.Copy().Add(
var unsupportedHTTP01GatewayFeatures = unsupportedHTTP01Features.Clone().Insert(
// Gateway API does not allow raw IP addresses to be specified
// in HTTPRoutes, so challenges for an IP address will never work.
featureset.IPAddressFeature,
@ -85,7 +85,7 @@ func runACMEIssuerTests(eab *cmacme.ACMEExternalAccountBinding) {
// UnsupportedPublicACMEServerFeatures are additional ACME features not supported by
// public ACME servers
var unsupportedPublicACMEServerFeatures = unsupportedHTTP01Features.Copy().Add(
var unsupportedPublicACMEServerFeatures = unsupportedHTTP01Features.Clone().Insert(
// Let's Encrypt doesn't yet support IP Address certificates.
featureset.IPAddressFeature,
// Ed25519 is not yet approved by the CA Browser forum.

View File

@ -96,7 +96,7 @@ func (s *Suite) complete(f *framework.Framework) {
// it is called by the tests to in Define() to setup and run the test
func (s *Suite) it(f *framework.Framework, name string, fn func(cmmeta.ObjectReference), requiredFeatures ...featureset.Feature) {
if !s.checkFeatures(requiredFeatures...) {
if s.UnsupportedFeatures.HasAny(requiredFeatures...) {
return
}
It(name, func() {
@ -111,21 +111,3 @@ func (s *Suite) it(f *framework.Framework, name string, fn func(cmmeta.ObjectRef
fn(issuerRef)
})
}
// checkFeatures is a helper function that is used to ensure that the features
// required for a given test case are supported by the suite.
// It will return 'true' if all features are supported and the test should run,
// or return 'false' if any required feature is not supported.
func (s *Suite) checkFeatures(fs ...featureset.Feature) bool {
unsupported := make(featureset.FeatureSet)
for _, f := range fs {
if s.UnsupportedFeatures.Contains(f) {
unsupported.Add(f)
}
}
// all features supported, return early!
if len(unsupported) == 0 {
return true
}
return false
}

View File

@ -103,7 +103,7 @@ func (s *Suite) complete(f *framework.Framework) {
// it is called by the tests to in Define() to setup and run the test
func (s *Suite) it(f *framework.Framework, name string, fn func(string), requiredFeatures ...featureset.Feature) {
if !s.checkFeatures(requiredFeatures...) {
if s.UnsupportedFeatures.HasAny(requiredFeatures...) {
return
}
It(name, func() {
@ -120,21 +120,3 @@ func (s *Suite) it(f *framework.Framework, name string, fn func(string), require
fn(signerName)
})
}
// checkFeatures is a helper function that is used to ensure that the features
// required for a given test case are supported by the suite.
// It will return 'true' if all features are supported and the test should run,
// or return 'false' if any required feature is not supported.
func (s *Suite) checkFeatures(fs ...featureset.Feature) bool {
unsupported := make(featureset.FeatureSet)
for _, f := range fs {
if s.UnsupportedFeatures.Contains(f) {
unsupported.Add(f)
}
}
// all features supported, return early!
if len(unsupported) == 0 {
return true
}
return false
}