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

do not truncate wlid if namespace is empty, use string builder #12

Merged
merged 1 commit into from
Dec 11, 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
27 changes: 16 additions & 11 deletions wlid/wlid.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,24 +81,29 @@ func generateWLID(pLevel0, level0, pLevel1, level1, k, name string) string {
kind := strings.ToLower(k)
kind = strings.Replace(kind, "-", "", -1)

wlid := WlidPrefix
wlid += fmt.Sprintf("%s%s", pLevel0, level0)
if level1 == "" {
return wlid
}
wlid += fmt.Sprintf("/%s%s", pLevel1, level1)
var wlid strings.Builder
wlid.WriteString(WlidPrefix)
wlid.WriteString(pLevel0)
wlid.WriteString(level0)

// don't exit if level1 is empty
wlid.WriteString("/")
wlid.WriteString(pLevel1)
wlid.WriteString(level1)

if kind == "" {
return wlid
return wlid.String()
}
wlid += fmt.Sprintf("/%s", kind)
wlid.WriteString("/")
wlid.WriteString(kind)

if name == "" {
return wlid
return wlid.String()
}
wlid += fmt.Sprintf("-%s", name)
wlid.WriteString("-")
wlid.WriteString(name)

return wlid
return wlid.String()
}

// GetWLID get the calculated wlid
Expand Down
49 changes: 49 additions & 0 deletions wlid/wlid_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package wlid

import (
"github.com/stretchr/testify/assert"
"testing"
)

Expand All @@ -27,3 +28,51 @@ func TestSpiffeSIDInfoSuccess(t *testing.T) {
t.Errorf("TestSpiffeSIDInfoSuccess failed to parse %v", SID)
}
}

func Test_generateWLID(t *testing.T) {
type args struct {
pLevel0 string
level0 string
pLevel1 string
level1 string
k string
name string
}
tests := []struct {
name string
args args
want string
}{
{
name: "k8s wlid",
args: args{
pLevel0: ClusterWlidPrefix,
level0: "HipsterShopCluster2",
pLevel1: NamespaceWlidPrefix,
level1: "prod",
k: "Deployment",
name: "cartservice",
},
want: "wlid://cluster-HipsterShopCluster2/namespace-prod/deployment-cartservice",
},
{
name: "k8s wlid no namespace",
args: args{
pLevel0: ClusterWlidPrefix,
level0: "HipsterShopCluster2",
pLevel1: NamespaceWlidPrefix,
level1: "",
k: "ClusterRoleBinding",
name: "cartservice",
},
want: "wlid://cluster-HipsterShopCluster2/namespace-/clusterrolebinding-cartservice",
// FIXME: do we want "wlid://cluster-HipsterShopCluster2/clusterrolebinding-cartservice" instead?
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@dwertent can you check this question?

Copy link
Contributor

Choose a reason for hiding this comment

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

This will break the wlid format...

},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := generateWLID(tt.args.pLevel0, tt.args.level0, tt.args.pLevel1, tt.args.level1, tt.args.k, tt.args.name)
assert.Equal(t, tt.want, got)
})
}
}
Loading