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

[profiles] Improve DAP name length error #1254

Merged
merged 7 commits into from
Jul 10, 2024
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
22 changes: 22 additions & 0 deletions pkg/agentprofile/agent_profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ const (
OldProfileLabelKey = "agent.datadoghq.com/profile"
defaultProfileName = "default"
daemonSetNamePrefix = "datadog-agent-with-profile-"
labelValueMaxLength = 63
)

// ProfileToApply validates a profile spec and returns a map that maps each
Expand All @@ -44,6 +45,14 @@ func ProfileToApply(logger logr.Logger, profile *datadoghqv1alpha1.DatadogAgentP
profileStatus.CurrentHash = hash
}

if err := validateProfileName(profile.Name); err != nil {
logger.Error(err, "profile name is invalid, skipping", "datadogagentprofile", profile.Name, "datadogagentprofile_namespace", profile.Namespace)
profileStatus.Conditions = SetDatadogAgentProfileCondition(profileStatus.Conditions, NewDatadogAgentProfileCondition(ValidConditionType, metav1.ConditionFalse, now, InvalidConditionReason, err.Error()))
profileStatus.Valid = metav1.ConditionFalse
UpdateProfileStatus(profile, profileStatus, now)
return profileAppliedByNode, err
}

if err := datadoghqv1alpha1.ValidateDatadogAgentProfileSpec(&profile.Spec); err != nil {
logger.Error(err, "profile spec is invalid, skipping", "datadogagentprofile", profile.Name, "datadogagentprofile_namespace", profile.Namespace)
profileStatus.Conditions = SetDatadogAgentProfileCondition(profileStatus.Conditions, NewDatadogAgentProfileCondition(ValidConditionType, metav1.ConditionFalse, now, InvalidConditionReason, err.Error()))
Expand Down Expand Up @@ -350,3 +359,16 @@ func nodeSelectorOperatorToSelectionOperator(op v1.NodeSelectorOperator) selecti
return ""
}
}

func validateProfileName(profileName string) error {
// Label values can be empty but a profile's name should not be empty
if profileName == "" {
return fmt.Errorf("Profile name cannot be empty")
}
// We add the profile name as a label value, which can be 63 characters max
if len(profileName) > labelValueMaxLength {
return fmt.Errorf("Profile name must be no more than 63 characters")
}

return nil
}
36 changes: 33 additions & 3 deletions pkg/agentprofile/agent_profile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func TestProfileToApply(t *testing.T) {
},
profileAppliedByNode: map[string]types.NamespacedName{},
expectedProfilesAppliedPerNode: map[string]types.NamespacedName{},
expectedErr: fmt.Errorf("profileAffinity must be defined"),
expectedErr: fmt.Errorf("Profile name cannot be empty"),
},
{
name: "empty profile, non-empty profileAppliedByNode",
Expand All @@ -77,7 +77,7 @@ func TestProfileToApply(t *testing.T) {
Name: "linux",
},
},
expectedErr: fmt.Errorf("profileAffinity must be defined"),
expectedErr: fmt.Errorf("Profile name cannot be empty"),
},
{
name: "empty profile, , non-empty profileAppliedByNode, no nodes",
Expand All @@ -95,7 +95,7 @@ func TestProfileToApply(t *testing.T) {
Name: "linux",
},
},
expectedErr: fmt.Errorf("profileAffinity must be defined"),
expectedErr: fmt.Errorf("Profile name cannot be empty"),
},
{
name: "non-conflicting profile, empty profileAppliedByNode",
Expand Down Expand Up @@ -549,3 +549,33 @@ func configWithCPURequestOverrideForCoreAgent(cpuRequest string) *v1alpha1.Confi
},
}
}

func Test_validateProfileName(t *testing.T) {
tests := []struct {
name string
profileName string
expectedError error
}{
{
name: "empty profile name",
profileName: "",
expectedError: fmt.Errorf("Profile name cannot be empty"),
},
{
name: "valid profile name",
profileName: "foo",
expectedError: nil,
},
{
name: "profile name too long",
profileName: "foo123456789012345678901234567890123456789012345678901234567890bar",
expectedError: fmt.Errorf("Profile name must be no more than 63 characters"),
},
}

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
assert.Equal(t, test.expectedError, validateProfileName(test.profileName))
})
}
}
Loading