Skip to content
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

Validating policy ID only when running in Fleet mode for Elastic Agent #6938

Merged
merged 2 commits into from
Jun 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pkg/apis/agent/v1alpha1/webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func (a *Agent) GetWarnings() []string {
if a == nil {
return nil
}
if len(a.Spec.PolicyID) == 0 {
if a.Spec.Mode == AgentFleetMode && len(a.Spec.PolicyID) == 0 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this change makes perfect sense, but I also think it's a perfect opportunity for a set of unit tests around this function.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are existing unit tests, will fix those and add a new one

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was only looking for a webhooks_test.go file and didn't see it. My apologies.

return []string{fmt.Sprintf("%s %s/%s: %s", Kind, a.Namespace, a.Name, MissingPolicyIDMessage)}
}
return nil
Expand Down
31 changes: 30 additions & 1 deletion pkg/controller/common/webhook/webhook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func Test_validatingWebhook_Handle(t *testing.T) {
want: admission.Allowed(""),
},
{
name: "no policy id is allowed but it should return a warning.",
name: "no policy id when agent running in standalone mode should not return a warning",
fields: fields{
set.Make("elastic"),
&agentv1alpha1.Agent{},
Expand All @@ -94,6 +94,35 @@ func Test_validatingWebhook_Handle(t *testing.T) {
},
},
},
want: admission.Allowed(""),
},
{
name: "no policy id is allowed when agent running in fleet mode but it should return a warning",
fields: fields{
set.Make("elastic"),
&agentv1alpha1.Agent{},
},
req: admission.Request{
AdmissionRequest: admissionv1.AdmissionRequest{
Operation: admissionv1.Create,
Object: runtime.RawExtension{
Raw: asJSON(&agentv1alpha1.Agent{
ObjectMeta: metav1.ObjectMeta{
Name: "testAgent",
Namespace: "elastic",
Labels: map[string]string{
"test": "label1",
},
},
Spec: agentv1alpha1.AgentSpec{
Version: "7.14.0",
Deployment: &agentv1alpha1.DeploymentSpec{},
Mode: agentv1alpha1.AgentFleetMode,
},
}),
},
},
},
want: admission.Allowed("").WithWarnings("Agent elastic/testAgent: spec.PolicyID is empty, spec.PolicyID will become mandatory in a future release"),
},
{
Expand Down