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

BUG 2082099: Fix finalizer string too long #1178

Merged
merged 1 commit into from
Sep 28, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 11 additions & 1 deletion internal/pkg/nodestatus/nodestatus.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ import (
"sigs.k8s.io/security-profiles-operator/internal/pkg/util"
)

const (
// finalizer name length limit.
finalizerNameLenLimit = 63
)

type StatusClient struct {
pol client.Object
nodeName string
Expand All @@ -42,13 +47,18 @@ type StatusClient struct {

func NewForProfile(pol client.Object, c client.Client) (*StatusClient, error) {
nodeName, ok := os.LookupEnv(config.NodeNameEnvKey)
finalizerString := nodeName + "-deleted"
if !ok {
return nil, errors.New("cannot determine node name")
}
// Make sure the length of finalizer is not longer than 63 characters
if len(nodeName)+len("-deleted") > finalizerNameLenLimit {
finalizerString = nodeName[:finalizerNameLenLimit-len("-deleted")] + "-deleted"
}
return &StatusClient{
pol: pol,
nodeName: nodeName,
finalizerString: nodeName + "-delete",
finalizerString: finalizerString,
client: c,
}, nil
}
Expand Down
58 changes: 58 additions & 0 deletions internal/pkg/nodestatus/nodestatus_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
Copyright 2020 The Kubernetes Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package nodestatus

import (
"testing"

"github.com/stretchr/testify/require"

"sigs.k8s.io/security-profiles-operator/internal/pkg/config"
)

// Expected shorten the node name if length exceed the limit.
//
//nolint:paralleltest
func TestShortenNodeName(t *testing.T) {
cases := []struct {
name string
nodeName string
wantFinalizerName string
}{
{
name: "NodeNameLongerThanLimit",
nodeName: "somenode-1234a-hhbhz-worker-c-xswffw.c.testlongnodename.internal",
wantFinalizerName: "somenode-1234a-hhbhz-worker-c-xswffw.c.testlongnodename-deleted",
},
{
name: "NodeNameShorterThanLimit",
nodeName: "somenode-1234a.internal",
wantFinalizerName: "somenode-1234a.internal-deleted",
},
}

for _, tc := range cases {
tc := tc
t.Run(tc.name, func(t *testing.T) {
t.Setenv(config.NodeNameEnvKey, tc.nodeName)
sc, err := NewForProfile(nil, nil)
require.NoError(t, err)

require.Equal(t, tc.wantFinalizerName, sc.finalizerString)
})
}
}