Skip to content

Commit

Permalink
Validating policy ID only when running in fleet mode for Elastic Agent (
Browse files Browse the repository at this point in the history
#6938)

* Validating policy ID only when running in fleet mode for Elastic Agent
  • Loading branch information
kvalliyurnatt committed Jun 22, 2023
1 parent 5422e73 commit e96992a
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
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 {
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

0 comments on commit e96992a

Please sign in to comment.