forked from projectcapsule/capsule
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9f9ccf0
commit bca70e6
Showing
36 changed files
with
2,155 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
// Copyright 2020-2021 Clastix Labs | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
//nolint:dupl | ||
package v1beta1 | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
// Copyright 2020-2021 Clastix Labs | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
//nolint:dupl | ||
package v1beta1 | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
// Copyright 2020-2021 Clastix Labs | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package v1beta2 | ||
|
||
type AdditionalMetadataSpec struct { | ||
Labels map[string]string `json:"labels,omitempty"` | ||
Annotations map[string]string `json:"annotations,omitempty"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
// Copyright 2020-2021 Clastix Labs | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package v1beta2 | ||
|
||
import rbacv1 "k8s.io/api/rbac/v1" | ||
|
||
type AdditionalRoleBindingsSpec struct { | ||
ClusterRoleName string `json:"clusterRoleName"` | ||
// kubebuilder:validation:Minimum=1 | ||
Subjects []rbacv1.Subject `json:"subjects"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
// Copyright 2020-2021 Clastix Labs | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package v1beta2 | ||
|
||
import ( | ||
"regexp" | ||
"sort" | ||
"strings" | ||
) | ||
|
||
type AllowedListSpec struct { | ||
Exact []string `json:"allowed,omitempty"` | ||
Regex string `json:"allowedRegex,omitempty"` | ||
} | ||
|
||
func (in *AllowedListSpec) ExactMatch(value string) (ok bool) { | ||
if len(in.Exact) > 0 { | ||
sort.SliceStable(in.Exact, func(i, j int) bool { | ||
return strings.ToLower(in.Exact[i]) < strings.ToLower(in.Exact[j]) | ||
}) | ||
|
||
i := sort.SearchStrings(in.Exact, value) | ||
|
||
ok = i < len(in.Exact) && in.Exact[i] == value | ||
} | ||
|
||
return | ||
} | ||
|
||
func (in *AllowedListSpec) RegexMatch(value string) (ok bool) { | ||
if len(in.Regex) > 0 { | ||
ok = regexp.MustCompile(in.Regex).MatchString(value) | ||
} | ||
|
||
return | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
// Copyright 2020-2021 Clastix Labs | ||
// SPDX-License-Identifier: Apache-2.0 | ||
//nolint:dupl | ||
package v1beta2 | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestAllowedListSpec_ExactMatch(t *testing.T) { | ||
type tc struct { | ||
In []string | ||
True []string | ||
False []string | ||
} | ||
|
||
for _, tc := range []tc{ | ||
{ | ||
[]string{"foo", "bar", "bizz", "buzz"}, | ||
[]string{"foo", "bar", "bizz", "buzz"}, | ||
[]string{"bing", "bong"}, | ||
}, | ||
{ | ||
[]string{"one", "two", "three"}, | ||
[]string{"one", "two", "three"}, | ||
[]string{"a", "b", "c"}, | ||
}, | ||
{ | ||
nil, | ||
nil, | ||
[]string{"any", "value"}, | ||
}, | ||
} { | ||
a := AllowedListSpec{ | ||
Exact: tc.In, | ||
} | ||
|
||
for _, ok := range tc.True { | ||
assert.True(t, a.ExactMatch(ok)) | ||
} | ||
|
||
for _, ko := range tc.False { | ||
assert.False(t, a.ExactMatch(ko)) | ||
} | ||
} | ||
} | ||
|
||
func TestAllowedListSpec_RegexMatch(t *testing.T) { | ||
type tc struct { | ||
Regex string | ||
True []string | ||
False []string | ||
} | ||
|
||
for _, tc := range []tc{ | ||
{`first-\w+-pattern`, []string{"first-date-pattern", "first-year-pattern"}, []string{"broken", "first-year", "second-date-pattern"}}, | ||
{``, nil, []string{"any", "value"}}, | ||
} { | ||
a := AllowedListSpec{ | ||
Regex: tc.Regex, | ||
} | ||
|
||
for _, ok := range tc.True { | ||
assert.True(t, a.RegexMatch(ok)) | ||
} | ||
|
||
for _, ko := range tc.False { | ||
assert.False(t, a.RegexMatch(ko)) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
// Copyright 2020-2021 Clastix Labs | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package v1beta2 | ||
|
||
import ( | ||
"fmt" | ||
"strconv" | ||
"strings" | ||
|
||
"sigs.k8s.io/controller-runtime/pkg/conversion" | ||
|
||
capsulev1alpha1 "github.com/clastix/capsule/api/v1alpha1" | ||
) | ||
|
||
func (in *CapsuleConfiguration) ConvertTo(raw conversion.Hub) error { | ||
dst, ok := raw.(*capsulev1alpha1.CapsuleConfiguration) | ||
if !ok { | ||
return fmt.Errorf("expected type *capsulev1alpha1.CapsuleConfiguration, got %T", dst) | ||
} | ||
|
||
dst.ObjectMeta = in.ObjectMeta | ||
dst.Spec.ProtectedNamespaceRegexpString = in.Spec.ProtectedNamespaceRegexpString | ||
dst.Spec.UserGroups = in.Spec.UserGroups | ||
dst.Spec.ProtectedNamespaceRegexpString = in.Spec.ProtectedNamespaceRegexpString | ||
|
||
annotations := dst.GetAnnotations() | ||
if annotations == nil { | ||
annotations = make(map[string]string) | ||
} | ||
|
||
if in.Spec.NodeMetadata != nil { | ||
annotations[capsulev1alpha1.ForbiddenNodeLabelsAnnotation] = strings.Join(in.Spec.NodeMetadata.ForbiddenLabels.Exact, ",") | ||
annotations[capsulev1alpha1.ForbiddenNodeLabelsRegexpAnnotation] = in.Spec.NodeMetadata.ForbiddenLabels.Regex | ||
annotations[capsulev1alpha1.ForbiddenNodeAnnotationsAnnotation] = strings.Join(in.Spec.NodeMetadata.ForbiddenAnnotations.Exact, ",") | ||
annotations[capsulev1alpha1.ForbiddenNodeAnnotationsRegexpAnnotation] = in.Spec.NodeMetadata.ForbiddenAnnotations.Regex | ||
} | ||
|
||
annotations[capsulev1alpha1.EnableTLSConfigurationAnnotationName] = fmt.Sprintf("%t", in.Spec.EnableTLSReconciler) | ||
annotations[capsulev1alpha1.TLSSecretNameAnnotation] = in.Spec.CapsuleResources.TLSSecretName | ||
annotations[capsulev1alpha1.MutatingWebhookConfigurationName] = in.Spec.CapsuleResources.MutatingWebhookConfigurationName | ||
annotations[capsulev1alpha1.ValidatingWebhookConfigurationName] = in.Spec.CapsuleResources.ValidatingWebhookConfigurationName | ||
|
||
dst.SetAnnotations(annotations) | ||
|
||
return nil | ||
} | ||
|
||
func (in *CapsuleConfiguration) ConvertFrom(raw conversion.Hub) error { | ||
src, ok := raw.(*capsulev1alpha1.CapsuleConfiguration) | ||
if !ok { | ||
return fmt.Errorf("expected type *capsulev1alpha1.CapsuleConfiguration, got %T", src) | ||
} | ||
|
||
in.ObjectMeta = src.ObjectMeta | ||
in.Spec.ProtectedNamespaceRegexpString = src.Spec.ProtectedNamespaceRegexpString | ||
in.Spec.UserGroups = src.Spec.UserGroups | ||
in.Spec.ProtectedNamespaceRegexpString = src.Spec.ProtectedNamespaceRegexpString | ||
|
||
annotations := src.GetAnnotations() | ||
|
||
if value, found := annotations[capsulev1alpha1.ForbiddenNodeLabelsAnnotation]; found { | ||
if in.Spec.NodeMetadata == nil { | ||
in.Spec.NodeMetadata = &NodeMetadata{} | ||
} | ||
|
||
in.Spec.NodeMetadata.ForbiddenLabels.Exact = strings.Split(value, ",") | ||
|
||
delete(annotations, capsulev1alpha1.ForbiddenNodeLabelsAnnotation) | ||
} | ||
|
||
if value, found := annotations[capsulev1alpha1.ForbiddenNodeLabelsRegexpAnnotation]; found { | ||
if in.Spec.NodeMetadata == nil { | ||
in.Spec.NodeMetadata = &NodeMetadata{} | ||
} | ||
|
||
in.Spec.NodeMetadata.ForbiddenLabels.Regex = value | ||
|
||
delete(annotations, capsulev1alpha1.ForbiddenNodeLabelsRegexpAnnotation) | ||
} | ||
|
||
if value, found := annotations[capsulev1alpha1.ForbiddenNodeAnnotationsAnnotation]; found { | ||
if in.Spec.NodeMetadata == nil { | ||
in.Spec.NodeMetadata = &NodeMetadata{} | ||
} | ||
|
||
in.Spec.NodeMetadata.ForbiddenAnnotations.Exact = strings.Split(value, ",") | ||
|
||
delete(annotations, capsulev1alpha1.ForbiddenNodeAnnotationsAnnotation) | ||
} | ||
|
||
if value, found := annotations[capsulev1alpha1.ForbiddenNodeAnnotationsRegexpAnnotation]; found { | ||
if in.Spec.NodeMetadata == nil { | ||
in.Spec.NodeMetadata = &NodeMetadata{} | ||
} | ||
|
||
in.Spec.NodeMetadata.ForbiddenAnnotations.Regex = value | ||
|
||
delete(annotations, capsulev1alpha1.ForbiddenNodeAnnotationsRegexpAnnotation) | ||
} | ||
|
||
if value, found := annotations[capsulev1alpha1.EnableTLSConfigurationAnnotationName]; found { | ||
v, _ := strconv.ParseBool(value) | ||
|
||
in.Spec.EnableTLSReconciler = v | ||
|
||
delete(annotations, capsulev1alpha1.EnableTLSConfigurationAnnotationName) | ||
} | ||
|
||
if value, found := annotations[capsulev1alpha1.TLSSecretNameAnnotation]; found { | ||
in.Spec.CapsuleResources.TLSSecretName = value | ||
|
||
delete(annotations, capsulev1alpha1.TLSSecretNameAnnotation) | ||
} | ||
|
||
if value, found := annotations[capsulev1alpha1.MutatingWebhookConfigurationName]; found { | ||
in.Spec.CapsuleResources.MutatingWebhookConfigurationName = value | ||
|
||
delete(annotations, capsulev1alpha1.MutatingWebhookConfigurationName) | ||
} | ||
|
||
if value, found := annotations[capsulev1alpha1.ValidatingWebhookConfigurationName]; found { | ||
in.Spec.CapsuleResources.ValidatingWebhookConfigurationName = value | ||
|
||
delete(annotations, capsulev1alpha1.ValidatingWebhookConfigurationName) | ||
} | ||
|
||
in.SetAnnotations(annotations) | ||
|
||
return nil | ||
} |
Oops, something went wrong.