-
Notifications
You must be signed in to change notification settings - Fork 140
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add validation for CR name length (#2375)
- Loading branch information
Showing
9 changed files
with
161 additions
and
4 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
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,21 @@ | ||
package edgeconnect | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/Dynatrace/dynatrace-operator/pkg/api/v1alpha1/edgeconnect" | ||
) | ||
|
||
const ( | ||
errorNameTooLong = `The length limit for the name of a EdgeConnect is %d, because it is the base for the name of resources related to the EdgeConnect. | ||
The limit is necessary because kubernetes uses the name of some resources for the label value, which has a limit of 63 characters. (see https://kubernetes.io/docs/concepts/overview/working-with-objects/labels/#syntax-and-character-set)` | ||
) | ||
|
||
func nameTooLong(_ context.Context, _ *edgeconnectValidator, edgeConnect *edgeconnect.EdgeConnect) string { | ||
edgeConnectName := edgeConnect.Name | ||
if edgeConnectName != "" && len(edgeConnectName) > edgeconnect.MaxNameLength { | ||
return fmt.Sprintf(errorNameTooLong, edgeconnect.MaxNameLength) | ||
} | ||
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,60 @@ | ||
package edgeconnect | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/Dynatrace/dynatrace-operator/pkg/api/v1alpha1/edgeconnect" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
) | ||
|
||
func TestNameTooLong(t *testing.T) { | ||
type testCase struct { | ||
name string | ||
crNameLength int | ||
allow bool | ||
} | ||
|
||
testCases := []testCase{ | ||
{ | ||
name: "normal length", | ||
crNameLength: 10, | ||
allow: true, | ||
}, | ||
{ | ||
name: "max - 1 ", | ||
crNameLength: edgeconnect.MaxNameLength - 1, | ||
allow: true, | ||
}, | ||
{ | ||
name: "max", | ||
crNameLength: edgeconnect.MaxNameLength, | ||
allow: true, | ||
}, | ||
{ | ||
name: "max + 1 ", | ||
crNameLength: edgeconnect.MaxNameLength + 1, | ||
allow: false, | ||
}, | ||
} | ||
|
||
for _, test := range testCases { | ||
t.Run(test.name, func(t *testing.T) { | ||
ec := &edgeconnect.EdgeConnect{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: strings.Repeat("a", test.crNameLength), | ||
}, | ||
Spec: edgeconnect.EdgeConnectSpec{ | ||
ApiServer: "id." + allowedSuffix[0], | ||
}, | ||
} | ||
if test.allow { | ||
assertAllowedResponse(t, ec) | ||
} else { | ||
errorMessage := fmt.Sprintf(errorNameTooLong, edgeconnect.MaxNameLength) | ||
assertDeniedResponse(t, []string{errorMessage}, ec) | ||
} | ||
}) | ||
} | ||
} |