Skip to content

Commit

Permalink
Improve some map initializations (gardener#8070)
Browse files Browse the repository at this point in the history
  • Loading branch information
dimityrmirchev authored Jun 21, 2023
1 parent e56ac10 commit 292970d
Show file tree
Hide file tree
Showing 15 changed files with 16 additions and 16 deletions.
2 changes: 1 addition & 1 deletion extensions/pkg/controller/controlplane/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import (

// MergeSecretMaps merges the 2 given secret maps.
func MergeSecretMaps(a, b map[string]*corev1.Secret) map[string]*corev1.Secret {
x := make(map[string]*corev1.Secret)
x := make(map[string]*corev1.Secret, len(a))
for _, m := range []map[string]*corev1.Secret{a, b} {
for k, v := range m {
x[k] = v
Expand Down
2 changes: 1 addition & 1 deletion pkg/admissioncontroller/seedidentity/identity.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func convertAuthenticationV1ExtraValueToUserInfoExtra(extra map[string]authentic
if extra == nil {
return nil
}
ret := map[string][]string{}
ret := make(map[string][]string, len(extra))
for k, v := range extra {
ret[k] = v
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/admissioncontroller/webhook/auth/seed/attributes.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ func convertToUserInfoExtra(extra map[string]authorizationv1.ExtraValue) map[str
if extra == nil {
return nil
}
ret := map[string][]string{}
ret := make(map[string][]string, len(extra))
for k, v := range extra {
ret[k] = v
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/apis/core/v1beta1/conversions.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ func Convert_v1beta1_InternalSecret_To_core_InternalSecret(in *InternalSecret, o
// StringData overwrites Data
if len(in.StringData) > 0 {
if out.Data == nil {
out.Data = map[string][]byte{}
out.Data = make(map[string][]byte, len(in.StringData))
}
for k, v := range in.StringData {
out.Data[k] = []byte(v)
Expand Down
2 changes: 1 addition & 1 deletion pkg/apis/core/validation/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ func ValidateProjectMember(member core.ProjectMember, fldPath *field.Path) field

allErrs = append(allErrs, ValidateSubject(member.Subject, fldPath)...)

foundRoles := map[string]struct{}{}
foundRoles := make(map[string]struct{}, len(member.Roles))
for i, role := range member.Roles {
rolesPath := fldPath.Child("roles").Index(i)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func (component) Name() string {
}

func (component) Config(ctx components.Context) ([]extensionsv1alpha1.Unit, []extensionsv1alpha1.File, error) {
var newData = map[string]string{}
var newData = make(map[string]string, len(data))

for key, value := range data {
newData[key] = value
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ func (r *Reconciler) Reconcile(ctx context.Context, request reconcile.Request) (

isInFinalState bool
finalState string
extra = make(map[string]authorizationv1.ExtraValue)
)

ctx, cancel := controllerutils.GetMainReconciliationContext(ctx, controllerutils.DefaultReconciliationTimeout)
Expand All @@ -77,6 +76,7 @@ func (r *Reconciler) Reconcile(ctx context.Context, request reconcile.Request) (
}
}

extra := make(map[string]authorizationv1.ExtraValue, len(csr.Spec.Extra))
for k, v := range csr.Spec.Extra {
extra[k] = authorizationv1.ExtraValue(v)
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/operation/operation.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ func (b *Builder) Build(
if err != nil {
return nil, err
}
secrets := make(map[string]*corev1.Secret)
secrets := make(map[string]*corev1.Secret, len(secretsMap))
for k, v := range secretsMap {
secrets[k] = v
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/resourcemanager/controller/managedresource/merger.go
Original file line number Diff line number Diff line change
Expand Up @@ -415,7 +415,7 @@ func mergeServiceAccount(scheme *runtime.Scheme, oldObj, newObj runtime.Object)
// It takes an optional map of old desired values and removes any keys/values from the resulting map
// that were once desired (part of `old`) but are not desired anymore.
func mergeMapsBasedOnOldMap(desired, current, old map[string]string) map[string]string {
out := map[string]string{}
out := make(map[string]string, len(current))
// use current as base
for k, v := range current {
out[k] = v
Expand Down
2 changes: 1 addition & 1 deletion pkg/resourcemanager/controller/networkpolicy/reconciler.go
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ func (r *Reconciler) reconcileDesiredPolicies(ctx context.Context, service *core
}

func (r *Reconciler) deleteStalePolicies(networkPolicyList *metav1.PartialObjectMetadataList, desiredObjectMetaKeys []string) []flow.TaskFn {
objectMetaKeysForDesiredPolicies := make(map[string]struct{})
objectMetaKeysForDesiredPolicies := make(map[string]struct{}, len(desiredObjectMetaKeys))
for _, objectMetaKey := range desiredObjectMetaKeys {
objectMetaKeysForDesiredPolicies[objectMetaKey] = struct{}{}
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/utils/imagevector/imagevector.go
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,7 @@ func (v ImageVector) FindImage(name string, opts ...FindOptionFunc) (*Image, err
// In case multiple images match the search, the first which was found is returned.
// In case no image was found, an error is returned.
func FindImages(v ImageVector, names []string, opts ...FindOptionFunc) (map[string]*Image, error) {
images := map[string]*Image{}
images := make(map[string]*Image, len(names))
for _, imageName := range names {
image, err := v.FindImage(imageName, opts...)
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions pkg/utils/miscellaneous.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func ValueExists(value string, list []string) bool {
// MergeMaps takes two maps <a>, <b> and merges them. If <b> defines a value with a key
// already existing in the <a> map, the <a> value for that key will be overwritten.
func MergeMaps(a, b map[string]interface{}) map[string]interface{} {
var values = map[string]interface{}{}
var values = make(map[string]interface{}, len(b))

for i, v := range b {
existing, ok := a[i]
Expand Down Expand Up @@ -73,7 +73,7 @@ func MergeStringMaps[T any](oldMap map[string]T, newMaps ...map[string]T) map[st
var out map[string]T

if oldMap != nil {
out = make(map[string]T)
out = make(map[string]T, len(oldMap))
}
for k, v := range oldMap {
out[k] = v
Expand Down
2 changes: 1 addition & 1 deletion pkg/utils/secrets/basic_auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ func (s *BasicAuthSecretConfig) Generate() (DataInterface, error) {

// SecretData computes the data map which can be used in a Kubernetes secret.
func (b *BasicAuth) SecretData() map[string][]byte {
data := map[string][]byte{}
data := make(map[string][]byte, 3)

data[DataKeyUserName] = []byte(b.Username)
data[DataKeyPassword] = []byte(b.Password)
Expand Down
2 changes: 1 addition & 1 deletion test/framework/shootmigrationtest.go
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ func (t *ShootMigrationTest) GetPersistedSecrets(ctx context.Context, seedClient
return nil, err
}

secretsMap := map[string]corev1.Secret{}
secretsMap := make(map[string]corev1.Secret, len(secretList.Items))
for _, secret := range secretList.Items {
secretsMap[secret.Name] = secret
}
Expand Down
2 changes: 1 addition & 1 deletion test/utils/shoots/update/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ func verifyKubernetesVersions(ctx context.Context, shootClient kubernetes.Interf
return fmt.Errorf("control plane version is %q but expected %q", shootClient.Version(), expectedControlPlaneKubernetesVersion)
}

poolNameToKubernetesVersion := map[string]string{}
poolNameToKubernetesVersion := make(map[string]string, len(shoot.Spec.Provider.Workers))
for _, worker := range shoot.Spec.Provider.Workers {
poolKubernetesVersion, err := v1beta1helper.CalculateEffectiveKubernetesVersion(controlPlaneKubernetesVersion, worker.Kubernetes)
if err != nil {
Expand Down

0 comments on commit 292970d

Please sign in to comment.