Skip to content
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
9 changes: 2 additions & 7 deletions cmd/check-edge-label/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package main
import (
"fmt"
"os"
"slices"

kmapi "kmodules.xyz/client-go/api/v1"
"kmodules.xyz/resource-metadata/apis/meta/v1alpha1"
Expand All @@ -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())
Expand Down
8 changes: 4 additions & 4 deletions cmd/gen-docs/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion hub/clusterprofiles/lib.go
Original file line number Diff line number Diff line change
Expand Up @@ -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]()))
}
},
)
Expand Down
2 changes: 1 addition & 1 deletion hub/kv_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
2 changes: 1 addition & 1 deletion hub/menuoutlines/lib.go
Original file line number Diff line number Diff line change
Expand Up @@ -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]()))
}
},
)
Expand Down
2 changes: 1 addition & 1 deletion hub/resourceblockdefinitions/lib.go
Original file line number Diff line number Diff line change
Expand Up @@ -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]()))
}
},
)
Expand Down
2 changes: 1 addition & 1 deletion hub/resourcedashboards/lib.go
Original file line number Diff line number Diff line change
Expand Up @@ -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]()))
}
},
)
Expand Down
2 changes: 1 addition & 1 deletion hub/resourcedescriptors/lib.go
Original file line number Diff line number Diff line change
Expand Up @@ -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]()))
}
},
)
Expand Down
2 changes: 1 addition & 1 deletion hub/resourceeditors/lib.go
Original file line number Diff line number Diff line change
Expand Up @@ -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]()))
}
},
)
Expand Down
8 changes: 4 additions & 4 deletions hub/resourceoutlines/lib.go
Original file line number Diff line number Diff line change
Expand Up @@ -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]()))
}
},
)
Expand Down
6 changes: 3 additions & 3 deletions hub/resourcetabledefinitions/lib.go
Original file line number Diff line number Diff line change
Expand Up @@ -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]()))
}
},
)
Expand Down
20 changes: 9 additions & 11 deletions pkg/tableconvertor/templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package tableconvertor

import (
"fmt"
"maps"
"strconv"
"strings"
"text/template"
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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) {
Expand Down
29 changes: 15 additions & 14 deletions pkg/tableconvertor/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading