-
Notifications
You must be signed in to change notification settings - Fork 324
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Peering webhooks #1310
Merged
Merged
Peering webhooks #1310
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
4009908
wip
ndhanushkodi dc4dd58
peering acceptor webhook wiring works
ndhanushkodi c1b962d
pluralize
ndhanushkodi 92f946a
add some validation for peering acceptor
ndhanushkodi e51403b
add tests
ndhanushkodi 68c22b3
remove consul meta, add dialer webhook tests
ndhanushkodi a73c0f4
add bats test
ndhanushkodi 92042e6
add changelog
ndhanushkodi dd59aa7
constant and change test descriptions
ndhanushkodi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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 |
---|---|---|
@@ -0,0 +1,87 @@ | ||
package v1alpha1 | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
) | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Tests CR level validations (fields cannot be nil) |
||
func TestPeeringAcceptor_Validate(t *testing.T) { | ||
cases := map[string]struct { | ||
acceptor *PeeringAcceptor | ||
expectedErrMsgs []string | ||
}{ | ||
"valid": { | ||
acceptor: &PeeringAcceptor{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "api", | ||
}, | ||
Spec: PeeringAcceptorSpec{ | ||
Peer: &Peer{ | ||
Secret: &Secret{ | ||
Name: "api-token", | ||
Key: "data", | ||
Backend: SecretBackendTypeKubernetes, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
"no peer specified": { | ||
acceptor: &PeeringAcceptor{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "api", | ||
}, | ||
Spec: PeeringAcceptorSpec{}, | ||
}, | ||
expectedErrMsgs: []string{ | ||
`spec.peer: Invalid value: "null": peer must be specified`, | ||
}, | ||
}, | ||
"no secret specified": { | ||
acceptor: &PeeringAcceptor{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "api", | ||
}, | ||
Spec: PeeringAcceptorSpec{ | ||
Peer: &Peer{}, | ||
}, | ||
}, | ||
expectedErrMsgs: []string{ | ||
`spec.peer.secret: Invalid value: "null": secret must be specified`, | ||
}, | ||
}, | ||
"invalid secret backend": { | ||
acceptor: &PeeringAcceptor{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "api", | ||
}, | ||
Spec: PeeringAcceptorSpec{ | ||
Peer: &Peer{ | ||
Secret: &Secret{ | ||
Backend: "invalid", | ||
}, | ||
}, | ||
}, | ||
}, | ||
expectedErrMsgs: []string{ | ||
`spec.peer.secret.backend: Invalid value: "invalid": backend must be "kubernetes"`, | ||
}, | ||
}, | ||
} | ||
|
||
for name, testCase := range cases { | ||
t.Run(name, func(t *testing.T) { | ||
err := testCase.acceptor.Validate() | ||
if len(testCase.expectedErrMsgs) != 0 { | ||
require.Error(t, err) | ||
for _, s := range testCase.expectedErrMsgs { | ||
require.Contains(t, err.Error(), s) | ||
} | ||
} else { | ||
require.NoError(t, err) | ||
} | ||
}) | ||
} | ||
} |
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,69 @@ | ||
package v1alpha1 | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"net/http" | ||
|
||
"github.com/go-logr/logr" | ||
capi "github.com/hashicorp/consul/api" | ||
admissionv1 "k8s.io/api/admission/v1" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
"sigs.k8s.io/controller-runtime/pkg/webhook/admission" | ||
) | ||
|
||
// +kubebuilder:object:generate=false | ||
|
||
type PeeringAcceptorWebhook struct { | ||
client.Client | ||
ConsulClient *capi.Client | ||
Logger logr.Logger | ||
decoder *admission.Decoder | ||
//ConsulMeta common.ConsulMeta | ||
} | ||
|
||
// NOTE: The path value in the below line is the path to the webhook. | ||
// If it is updated, run code-gen, update subcommand/controller/command.go | ||
// and the consul-helm value for the path to the webhook. | ||
// | ||
// NOTE: The below line cannot be combined with any other comment. If it is | ||
// it will break the code generation. | ||
// | ||
// +kubebuilder:webhook:verbs=create;update,path=/mutate-v1alpha1-peeringacceptors,mutating=true,failurePolicy=fail,groups=consul.hashicorp.com,resources=peeringacceptors,versions=v1alpha1,name=mutate-peeringacceptors.consul.hashicorp.com,sideEffects=None,admissionReviewVersions=v1beta1;v1 | ||
|
||
func (v *PeeringAcceptorWebhook) Handle(ctx context.Context, req admission.Request) admission.Response { | ||
var acceptor PeeringAcceptor | ||
var acceptorList PeeringAcceptorList | ||
err := v.decoder.Decode(req, &acceptor) | ||
if err != nil { | ||
return admission.Errored(http.StatusBadRequest, err) | ||
} | ||
|
||
// Call validate first to ensure all the fields are validated before checking for secret name duplicates. | ||
if err := acceptor.Validate(); err != nil { | ||
return admission.Errored(http.StatusBadRequest, err) | ||
} | ||
|
||
if req.Operation == admissionv1.Create { | ||
v.Logger.Info("validate create", "name", acceptor.KubernetesName()) | ||
|
||
if err := v.Client.List(ctx, &acceptorList); err != nil { | ||
return admission.Errored(http.StatusInternalServerError, err) | ||
} | ||
|
||
for _, item := range acceptorList.Items { | ||
// If any peering acceptor resource has the same secret name as this one, reject it. | ||
if item.Namespace == acceptor.Namespace && item.Secret().Name == acceptor.Secret().Name { | ||
return admission.Errored(http.StatusBadRequest, | ||
fmt.Errorf("an existing PeeringAcceptor resource has the same secret name `name: %s, namespace: %s`", acceptor.Secret().Name, acceptor.Namespace)) | ||
} | ||
} | ||
} | ||
|
||
return admission.Allowed(fmt.Sprintf("valid %s request", acceptor.KubeKind())) | ||
} | ||
|
||
func (v *PeeringAcceptorWebhook) InjectDecoder(d *admission.Decoder) error { | ||
v.decoder = d | ||
return nil | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These functions are similarly named to the config entry webhooks but we don't need all the functions here because we don't need to implement the config entry webhook interface.