diff --git a/cmd/check-edge-label/main.go b/cmd/check-edge-label/main.go index d1aa187ee3..4c622fc2e1 100644 --- a/cmd/check-edge-label/main.go +++ b/cmd/check-edge-label/main.go @@ -19,6 +19,7 @@ package main import ( "fmt" "os" + "slices" kmapi "kmodules.xyz/client-go/api/v1" "kmodules.xyz/resource-metadata/apis/meta/v1alpha1" @@ -32,13 +33,7 @@ func main() { for _, c := range rdSrc.Spec.Connections { rdTarget, _ := reg.LoadByGVK(c.Target.GroupVersionKind()) - var offshoot bool - for _, lbl := range c.Labels { - if lbl == kmapi.EdgeLabelOffshoot { - offshoot = true - break - } - } + offshoot := slices.Contains(c.Labels, kmapi.EdgeLabelOffshoot) if offshoot { if rdSrc.Spec.Resource.Scope != rdTarget.Spec.Resource.Scope { fmt.Printf("%+v has an offshoot label edge with %+v, but their scope does not match\n", rdSrc.Spec.Resource.GroupVersionKind(), rdTarget.Spec.Resource.GroupVersionKind()) diff --git a/cmd/gen-docs/main.go b/cmd/gen-docs/main.go index 70c7ff4c02..073e989fc1 100644 --- a/cmd/gen-docs/main.go +++ b/cmd/gen-docs/main.go @@ -94,13 +94,13 @@ func GenDescriptor(dir string) error { schema.Schema = "http://json-schema.org/schema#" if prop, ok := schema.Properties["apiVersion"]; ok { prop.Enum = []crdv1.JSON{ - {Raw: []byte(fmt.Sprintf("%q", rd.Spec.Resource.GroupVersion()))}, + {Raw: fmt.Appendf(nil, "%q", rd.Spec.Resource.GroupVersion())}, } schema.Properties["apiVersion"] = prop } if prop, ok := schema.Properties["kind"]; ok { prop.Enum = []crdv1.JSON{ - {Raw: []byte(fmt.Sprintf("%q", rd.Spec.Resource.Kind))}, + {Raw: fmt.Appendf(nil, "%q", rd.Spec.Resource.Kind)}, } schema.Properties["kind"] = prop } @@ -191,8 +191,8 @@ func ImportCRD(dir string) error { if err != nil { return err } - lines := strings.Split(string(data), "\n") - for _, line := range lines { + lines := strings.SplitSeq(string(data), "\n") + for line := range lines { line = strings.TrimSpace(line) if line == "" { continue diff --git a/hub/clusterprofiles/lib.go b/hub/clusterprofiles/lib.go index 53ee6e2d69..d8ef7fb0e5 100644 --- a/hub/clusterprofiles/lib.go +++ b/hub/clusterprofiles/lib.go @@ -69,7 +69,7 @@ var ( cpMap[obj.Name] = &obj return nil }); err != nil { - panic(errors.Wrapf(err, "failed to load %s", reflect.TypeOf(v1alpha1.ClusterProfile{}))) + panic(errors.Wrapf(err, "failed to load %s", reflect.TypeFor[v1alpha1.ClusterProfile]())) } }, ) diff --git a/hub/kv_test.go b/hub/kv_test.go index a594bbd923..3a8364f0a4 100644 --- a/hub/kv_test.go +++ b/hub/kv_test.go @@ -46,7 +46,7 @@ func TestRegister(t *testing.T) { Resource: "prometheuses", } reg := NewRegistry(config.Host, NewKVLocal()) - assert.NoError(t, reg.Register(gvr, config)) + assert.NoError(t, reg.Rediscover(gvr, config)) rd1, err := resourcedescriptors.LoadByGVR(gvr) assert.NoError(t, err) rd2, err := resourcedescriptors.LoadByName("monitoring.coreos.com-v1-prometheuses") diff --git a/hub/menuoutlines/lib.go b/hub/menuoutlines/lib.go index 2cd3035efb..2737a24f85 100644 --- a/hub/menuoutlines/lib.go +++ b/hub/menuoutlines/lib.go @@ -69,7 +69,7 @@ var ( moMap[obj.Name] = &obj return nil }); err != nil { - panic(errors.Wrapf(err, "failed to load %s", reflect.TypeOf(v1alpha1.MenuOutline{}))) + panic(errors.Wrapf(err, "failed to load %s", reflect.TypeFor[v1alpha1.MenuOutline]())) } }, ) diff --git a/hub/resourceblockdefinitions/lib.go b/hub/resourceblockdefinitions/lib.go index db5d893ceb..eac15ed965 100644 --- a/hub/resourceblockdefinitions/lib.go +++ b/hub/resourceblockdefinitions/lib.go @@ -66,7 +66,7 @@ var ( rbMap[obj.Name] = &obj return nil }); err != nil { - panic(errors.Wrapf(err, "failed to load %s", reflect.TypeOf(v1alpha1.ResourceBlockDefinition{}))) + panic(errors.Wrapf(err, "failed to load %s", reflect.TypeFor[v1alpha1.ResourceBlockDefinition]())) } }, ) diff --git a/hub/resourcedashboards/lib.go b/hub/resourcedashboards/lib.go index 4c3011d9d2..828b0a31aa 100644 --- a/hub/resourcedashboards/lib.go +++ b/hub/resourcedashboards/lib.go @@ -73,7 +73,7 @@ var ( return nil }); err != nil { - panic(errors.Wrapf(err, "failed to load %s", reflect.TypeOf(v1alpha1.ResourceDashboard{}))) + panic(errors.Wrapf(err, "failed to load %s", reflect.TypeFor[v1alpha1.ResourceDashboard]())) } }, ) diff --git a/hub/resourcedescriptors/lib.go b/hub/resourcedescriptors/lib.go index e7a07d051f..63e70c16bd 100644 --- a/hub/resourcedescriptors/lib.go +++ b/hub/resourcedescriptors/lib.go @@ -80,7 +80,7 @@ var ( return err }) if e2 != nil { - panic(errors.Wrapf(e2, "failed to load %s", reflect.TypeOf(v1alpha1.ResourceDescriptor{}))) + panic(errors.Wrapf(e2, "failed to load %s", reflect.TypeFor[v1alpha1.ResourceDescriptor]())) } }, ) diff --git a/hub/resourceeditors/lib.go b/hub/resourceeditors/lib.go index cb388909fe..4a72860367 100644 --- a/hub/resourceeditors/lib.go +++ b/hub/resourceeditors/lib.go @@ -73,7 +73,7 @@ var ( return nil }); err != nil { - panic(errors.Wrapf(err, "failed to load %s", reflect.TypeOf(v1alpha1.ResourceEditor{}))) + panic(errors.Wrapf(err, "failed to load %s", reflect.TypeFor[v1alpha1.ResourceEditor]())) } }, ) diff --git a/hub/resourceoutlines/lib.go b/hub/resourceoutlines/lib.go index 5ad0dd2803..26243d3a44 100644 --- a/hub/resourceoutlines/lib.go +++ b/hub/resourceoutlines/lib.go @@ -75,24 +75,24 @@ var ( gvr := obj.Spec.Resource.GroupVersionResource() expectedName := DefaultLayoutName(gvr) if obj.Name != expectedName { - return fmt.Errorf("expected default %s name to be %s, found %s", reflect.TypeOf(v1alpha1.ResourceOutline{}), expectedName, obj.Name) + return fmt.Errorf("expected default %s name to be %s, found %s", reflect.TypeFor[v1alpha1.ResourceOutline](), expectedName, obj.Name) } gvk := obj.Spec.Resource.GroupVersionKind() if rv, ok := rlPerGK[gvk]; !ok { rlPerGK[gvk] = &obj } else { - return fmt.Errorf("multiple %s found for %+v: %s and %s", reflect.TypeOf(v1alpha1.ResourceOutline{}), gvk, rv.Name, obj.Name) + return fmt.Errorf("multiple %s found for %+v: %s and %s", reflect.TypeFor[v1alpha1.ResourceOutline](), gvk, rv.Name, obj.Name) } if rv, ok := rlPerGR[gvr]; !ok { rlPerGR[gvr] = &obj } else { - return fmt.Errorf("multiple %s found for %+v: %s and %s", reflect.TypeOf(v1alpha1.ResourceOutline{}), gvk, rv.Name, obj.Name) + return fmt.Errorf("multiple %s found for %+v: %s and %s", reflect.TypeFor[v1alpha1.ResourceOutline](), gvk, rv.Name, obj.Name) } } return nil }); err != nil { - panic(errors.Wrapf(err, "failed to load %s", reflect.TypeOf(v1alpha1.ResourceOutline{}))) + panic(errors.Wrapf(err, "failed to load %s", reflect.TypeFor[v1alpha1.ResourceOutline]())) } }, ) diff --git a/hub/resourcetabledefinitions/lib.go b/hub/resourcetabledefinitions/lib.go index 9d5df74e46..9092739714 100644 --- a/hub/resourcetabledefinitions/lib.go +++ b/hub/resourcetabledefinitions/lib.go @@ -76,18 +76,18 @@ var ( if rv, ok := rtdPerGK[gvk]; !ok { rtdPerGK[gvk] = &obj } else { - return fmt.Errorf("multiple %s found for %+v: %s and %s", reflect.TypeOf(v1alpha1.ResourceTableDefinition{}), gvk, rv.Name, obj.Name) + return fmt.Errorf("multiple %s found for %+v: %s and %s", reflect.TypeFor[v1alpha1.ResourceTableDefinition](), gvk, rv.Name, obj.Name) } gvr := obj.Spec.Resource.GroupVersionResource() if rv, ok := rtdPerGR[gvr]; !ok { rtdPerGR[gvr] = &obj } else { - return fmt.Errorf("multiple %s found for %+v: %s and %s", reflect.TypeOf(v1alpha1.ResourceTableDefinition{}), gvk, rv.Name, obj.Name) + return fmt.Errorf("multiple %s found for %+v: %s and %s", reflect.TypeFor[v1alpha1.ResourceTableDefinition](), gvk, rv.Name, obj.Name) } } return nil }); err != nil { - panic(errors.Wrapf(err, "failed to load %s", reflect.TypeOf(v1alpha1.ResourceTableDefinition{}))) + panic(errors.Wrapf(err, "failed to load %s", reflect.TypeFor[v1alpha1.ResourceTableDefinition]())) } }, ) diff --git a/pkg/tableconvertor/templates.go b/pkg/tableconvertor/templates.go index cbdc8d81e4..1123579153 100644 --- a/pkg/tableconvertor/templates.go +++ b/pkg/tableconvertor/templates.go @@ -18,6 +18,7 @@ package tableconvertor import ( "fmt" + "maps" "strconv" "strings" "text/template" @@ -67,17 +68,13 @@ func init() { templateFns["count_managed_clusters"] = managedClusterSetFn templateFns["get_cluster_profile"] = getClusterProfile // ref: https://github.com/kmodules/resource-metrics/blob/bf6b257f8922a5572ccd20bf1cbab6bbedf4fcb4/template.go#L26-L36 - for name, fn := range resourcemetrics.TxtFuncMap() { - templateFns[name] = fn - } + maps.Copy(templateFns, resourcemetrics.TxtFuncMap()) } // TxtFuncMap returns a 'text/template'.FuncMap func TxtFuncMap() template.FuncMap { gfm := make(map[string]any, len(templateFns)) - for k, v := range templateFns { - gfm[k] = v - } + maps.Copy(gfm, templateFns) return gfm } @@ -241,15 +238,16 @@ func volumesFn(data any) (string, error) { } } - ss := "[" + var ss strings.Builder + ss.WriteString("[") for i := range volumes { - ss += describeVolume(volumes[i]) + ss.WriteString(describeVolume(volumes[i])) if i < len(volumes)-1 { - ss += "," + ss.WriteString(",") } } - ss += "]" - return ss, nil + ss.WriteString("]") + return ss.String(), nil } func volumeMountsFn(data any) (string, error) { diff --git a/pkg/tableconvertor/util.go b/pkg/tableconvertor/util.go index 7765acf100..226e75951d 100644 --- a/pkg/tableconvertor/util.go +++ b/pkg/tableconvertor/util.go @@ -314,33 +314,34 @@ func describeVolume(volume core.Volume) string { "}", volume.Name, flocker.DatasetName, flocker.DatasetUUID) case volume.Projected != nil: projected := volume.Projected - sources := "[" + var sources strings.Builder + sources.WriteString("[") for i, source := range projected.Sources { - sources += "{" + sources.WriteString("{") if source.Secret != nil { - sources += "\"Type\": \"Secret\"," - sources += fmt.Sprintf("\"SecretName\": %q", source.Secret.Name) + sources.WriteString("\"Type\": \"Secret\",") + sources.WriteString(fmt.Sprintf("\"SecretName\": %q", source.Secret.Name)) } else if source.DownwardAPI != nil { - sources += "\"Type\": \"DownwardAPI\"," - sources += "\"DownwardAPI\": \"true\"" + sources.WriteString("\"Type\": \"DownwardAPI\",") + sources.WriteString("\"DownwardAPI\": \"true\"") } else if source.ConfigMap != nil { - sources += "\"Type\": \"ConfigMap\"," - sources += fmt.Sprintf("\"ConfigMapName\": %q", source.ConfigMap.Name) + sources.WriteString("\"Type\": \"ConfigMap\",") + sources.WriteString(fmt.Sprintf("\"ConfigMapName\": %q", source.ConfigMap.Name)) } else if source.ServiceAccountToken != nil { - sources += "\"Type\": \"ServiceAccountToken\"," - sources += fmt.Sprintf("\"TokenExpirationSeconds\": \"%v\"", source.ServiceAccountToken.ExpirationSeconds) + sources.WriteString("\"Type\": \"ServiceAccountToken\",") + sources.WriteString(fmt.Sprintf("\"TokenExpirationSeconds\": \"%v\"", source.ServiceAccountToken.ExpirationSeconds)) } - sources += "}" + sources.WriteString("}") if i < len(projected.Sources)-1 { - sources += "," + sources.WriteString(",") } } - sources += "]" + sources.WriteString("]") return fmt.Sprintf("{"+ "\"Name\": %q,"+ "\"Type\": \"Projected\","+ "\"sources\": %v"+ - "}", volume.Name, sources) + "}", volume.Name, sources.String()) case volume.CSI != nil: csi := volume.CSI var readOnly bool