Skip to content

Conversation

@hessjcg
Copy link
Collaborator

@hessjcg hessjcg commented Sep 21, 2022

Adds a new internal/names package to the project which has functions to create
kubernetes-safe names of appropriate formatting and length. This will
be used to format the names of containers and volumes that are added to workloads.

Adds a new internal/names package to the project which has functions to create
safe kubernetes names of appropriate formatting and length. This will
be used to format modified workload sidecar container names.
@hessjcg hessjcg requested a review from a team September 21, 2022 14:15
cloudsqlapi "github.com/GoogleCloudPlatform/cloud-sql-proxy-operator/api/v1alpha1"
)

const maxNameLen = 63
Copy link
Member

Choose a reason for hiding this comment

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

nit:

const (
  maxNameLen = 63
  hashLen = 8
  dividerLen = 2
)

Also what are these numbers?

Copy link
Member

Choose a reason for hiding this comment

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

I see 63 characters is the max length imposed by Kubernetes. A comment to that effect here would be good. Or if these are only used once, inlining them with a comment would be fine too.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I moved these into SafePrefixedName, merged them into one declaration, and added some comments.

nameSuffix := instName[len(instName)-truncateLen:]
checksum := hash(instName)
return strings.ToLower(fmt.Sprintf("%s%s-%s-%x", prefix, namePrefix, nameSuffix, checksum))
} else {
Copy link
Member

Choose a reason for hiding this comment

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

Here's another unnecessary else.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

fixed.

}

// hash simply returns the checksum for a string
func hash(s string) uint32 {
Copy link
Member

Choose a reason for hiding this comment

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

Could this take a slice of bytes instead of a string? It's more common to work on bytes and convert to strings when a user is going to see it.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Fixed.

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

func TestSafeDnsLabel(t *testing.T) {
Copy link
Member

Choose a reason for hiding this comment

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

DNS

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Fixed. it shouldn't be called "DNS" anymore.

)

func TestSafeDnsLabel(t *testing.T) {
t.Parallel()
Copy link
Member

Choose a reason for hiding this comment

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

Is this necessary? These tests are already pretty fast.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

deleted.


}

// TestContainerName container names are a public
Copy link
Member

Choose a reason for hiding this comment

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

full sentences please. But I'm not sure this comment adds much given the docs are already good.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

deleted.


}

// TestVolumeName container names are a public
Copy link
Member

Choose a reason for hiding this comment

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

Probably just delete this?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

deleted.

ObjectMeta: metav1.ObjectMeta{Name: name, Namespace: namespace},
Spec: cloudsqlapi.AuthProxyWorkloadSpec{
Workload: cloudsqlapi.WorkloadSelectorSpec{
Kind: "",
Copy link
Member

Choose a reason for hiding this comment

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

The zero value is already empty string. This is unnecessary.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

deleted.


}

func mustMakeCsql(name string, namespace string) *cloudsqlapi.AuthProxyWorkload {
Copy link
Member

Choose a reason for hiding this comment

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

Must implies there's an error you're failing on. But I don't see any error. So this could just be authProxyWorkload.

Copy link
Collaborator Author

@hessjcg hessjcg Sep 21, 2022

Choose a reason for hiding this comment

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

renamed to authProxyWorkload()

@hessjcg hessjcg requested a review from enocom September 21, 2022 14:45
Workload: cloudsqlapi.WorkloadSelectorSpec{
Selector: &metav1.LabelSelector{
MatchLabels: map[string]string{"app": "hello"},
MatchExpressions: nil,
Copy link
Member

Choose a reason for hiding this comment

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

Don't worry about setting the zero value. The language does that for you.

@hessjcg hessjcg merged commit ee1e649 into main Sep 21, 2022
@hessjcg hessjcg deleted the internal-names branch September 21, 2022 19:53
This was referenced Dec 7, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants