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
18 changes: 2 additions & 16 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,16 @@ package cmd

import (
"flag"
"log"
"strings"

"github.com/platform-mesh/virtual-workspaces/pkg/config"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"github.com/spf13/viper"
genericapiserveroptions "k8s.io/apiserver/pkg/server/options"
"k8s.io/klog/v2"
)

var (
v *viper.Viper
cfg config.ServiceConfig
cfg = config.NewServiceConfig()
secureServing = genericapiserveroptions.SecureServingOptions{}
delegatingAuthenticationOption = genericapiserveroptions.DelegatingAuthenticationOptions{}
)
Expand All @@ -26,18 +22,8 @@ var rootCmd = &cobra.Command{
}

func init() {
v = viper.NewWithOptions(
viper.EnvKeyReplacer(strings.NewReplacer("-", "_")),
)

v.AutomaticEnv()

rootCmd.AddCommand(startCmd)

err := config.BindConfigToFlags(v, startCmd, &cfg)
if err != nil {
log.Fatalln(err)
}
cfg.AddFlags(startCmd.Flags())

delegatingAuthenticationOption = *genericapiserveroptions.NewDelegatingAuthenticationOptions()

Expand Down
11 changes: 2 additions & 9 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ require (
github.com/platform-mesh/extension-manager-operator/api v0.2.193
github.com/spf13/cobra v1.10.2
github.com/spf13/pflag v1.0.10
github.com/spf13/viper v1.21.0
github.com/stretchr/testify v1.10.0
k8s.io/apiextensions-apiserver v0.34.3
k8s.io/apimachinery v0.34.3
k8s.io/apiserver v0.34.3
Expand Down Expand Up @@ -40,7 +40,6 @@ require (
github.com/go-openapi/jsonpointer v0.21.0 // indirect
github.com/go-openapi/jsonreference v0.21.0 // indirect
github.com/go-openapi/swag v0.23.0 // indirect
github.com/go-viper/mapstructure/v2 v2.4.0 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/protobuf v1.5.4 // indirect
github.com/google/btree v1.1.3 // indirect
Expand All @@ -66,19 +65,14 @@ require (
github.com/onsi/gomega v1.35.1 // indirect
github.com/opencontainers/go-digest v1.0.0 // indirect
github.com/opencontainers/selinux v1.11.1 // indirect
github.com/pelletier/go-toml/v2 v2.2.4 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
github.com/pquerna/cachecontrol v0.1.0 // indirect
github.com/prometheus/client_golang v1.22.0 // indirect
github.com/prometheus/client_model v0.6.1 // indirect
github.com/prometheus/common v0.62.0 // indirect
github.com/prometheus/procfs v0.15.1 // indirect
github.com/sagikazarmark/locafero v0.11.0 // indirect
github.com/sourcegraph/conc v0.3.1-0.20240121214520-5f936abd7ae8 // indirect
github.com/spf13/afero v1.15.0 // indirect
github.com/spf13/cast v1.10.0 // indirect
github.com/stoewer/go-strcase v1.3.0 // indirect
github.com/subosito/gotenv v1.6.0 // indirect
github.com/x448/float16 v0.8.4 // indirect
go.etcd.io/etcd/api/v3 v3.5.21 // indirect
go.etcd.io/etcd/client/pkg/v3 v3.5.21 // indirect
Expand All @@ -95,7 +89,6 @@ require (
go.opentelemetry.io/proto/otlp v1.4.0 // indirect
go.uber.org/multierr v1.11.0 // indirect
go.uber.org/zap v1.27.0 // indirect
go.yaml.in/yaml/v3 v3.0.4 // indirect
golang.org/x/crypto v0.40.0 // indirect
golang.org/x/exp v0.0.0-20250408133849-7e4ce0ab07d0 // indirect
golang.org/x/net v0.42.0 // indirect
Expand Down
23 changes: 2 additions & 21 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,6 @@ github.com/emicklei/go-restful/v3 v3.12.1 h1:PJMDIM/ak7btuL8Ex0iYET9hxM3CI2sjZtz
github.com/emicklei/go-restful/v3 v3.12.1/go.mod h1:6n3XBCmQQb25CM2LCACGz8ukIrRry+4bhvbpWn3mrbc=
github.com/felixge/httpsnoop v1.0.4 h1:NFTV2Zj1bL4mc9sqWACXbQFVBBg2W3GPvqp8/ESS2Wg=
github.com/felixge/httpsnoop v1.0.4/go.mod h1:m8KPJKqk1gH5J9DgRY2ASl2lWCfGKXixSwevea8zH2U=
github.com/frankban/quicktest v1.14.6 h1:7Xjx+VpznH+oBnejlPUj8oUpdxnVs4f8XU8WnHkI4W8=
github.com/frankban/quicktest v1.14.6/go.mod h1:4ptaffx2x8+WTWXmUCuVU6aPUX1/Mz7zb5vbUoiM6w0=
github.com/fsnotify/fsnotify v1.9.0 h1:2Ml+OJNzbYCTzsxtv8vKSFD9PbJjmhYF14k/jKC7S9k=
github.com/fsnotify/fsnotify v1.9.0/go.mod h1:8jBTzvmWwFyi3Pb8djgCCO5IBqzKJ/Jwo8TRcHyHii0=
github.com/fxamacker/cbor/v2 v2.9.0 h1:NpKPmjDBgUfBms6tr6JZkTHtfFGcMKsw3eGcmD/sapM=
Expand All @@ -56,8 +54,6 @@ github.com/go-openapi/swag v0.23.0 h1:vsEVJDUo2hPJ2tu0/Xc+4noaxyEffXNIs3cOULZ+Gr
github.com/go-openapi/swag v0.23.0/go.mod h1:esZ8ITTYEsH1V2trKHjAN8Ai7xHb8RV+YSZ577vPjgQ=
github.com/go-task/slim-sprig/v3 v3.0.0 h1:sUs3vkvUymDpBKi3qH1YSqBQk9+9D/8M2mN1vB6EwHI=
github.com/go-task/slim-sprig/v3 v3.0.0/go.mod h1:W848ghGpv3Qj3dhTPRyJypKRiqCdHZiAzKg9hl15HA8=
github.com/go-viper/mapstructure/v2 v2.4.0 h1:EBsztssimR/CONLSZZ04E8qAkxNYq4Qp9LvH92wZUgs=
github.com/go-viper/mapstructure/v2 v2.4.0/go.mod h1:oJDH3BJKyqBA2TXFhDsKDGDTlndYOZ6rGS0BRZIxGhM=
github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
github.com/gogo/protobuf v1.3.2 h1:Ov1cvc58UF3b5XjBnZv7+opcTcQFZebYjWzi34vdm4Q=
github.com/gogo/protobuf v1.3.2/go.mod h1:P1XiOD3dCwIKUDQYPy72D8LYyHL2YPYrpS2s69NZV8Q=
Expand Down Expand Up @@ -185,8 +181,6 @@ github.com/opencontainers/go-digest v1.0.0 h1:apOUWs51W5PlhuyGyz9FCeeBIOUDA/6nW8
github.com/opencontainers/go-digest v1.0.0/go.mod h1:0JzlMkj0TRzQZfJkVvzbP0HBR3IKzErnv2BNG4W4MAM=
github.com/opencontainers/selinux v1.11.1 h1:nHFvthhM0qY8/m+vfhJylliSshm8G1jJ2jDMcgULaH8=
github.com/opencontainers/selinux v1.11.1/go.mod h1:E5dMC3VPuVvVHDYmi78qvhJp8+M586T4DlDRYpFkyec=
github.com/pelletier/go-toml/v2 v2.2.4 h1:mye9XuhQ6gvn5h28+VilKrrPoQVanw5PMw/TB0t5Ec4=
github.com/pelletier/go-toml/v2 v2.2.4/go.mod h1:2gIqNv+qfxSVS7cM2xJQKtLSTLUE9V8t9Stt+h56mCY=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/platform-mesh/extension-manager-operator/api v0.2.193 h1:TS89vSdrvAJXI6ywEVlUgDwbEQeF7NUSxgklMyy6pWU=
Expand All @@ -207,25 +201,15 @@ github.com/prometheus/procfs v0.15.1/go.mod h1:fB45yRUv8NstnjriLhBQLuOUt+WW4BsoG
github.com/rogpeppe/go-internal v1.13.1 h1:KvO1DLK/DRN07sQ1LQKScxyZJuNnedQ5/wKSR38lUII=
github.com/rogpeppe/go-internal v1.13.1/go.mod h1:uMEvuHeurkdAXX61udpOXGD/AzZDWNMNyH2VO9fmH0o=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/sagikazarmark/locafero v0.11.0 h1:1iurJgmM9G3PA/I+wWYIOw/5SyBtxapeHDcg+AAIFXc=
github.com/sagikazarmark/locafero v0.11.0/go.mod h1:nVIGvgyzw595SUSUE6tvCp3YYTeHs15MvlmU87WwIik=
github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ=
github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
github.com/soheilhy/cmux v0.1.5 h1:jjzc5WVemNEDTLwv9tlmemhC73tI08BNOIGwBOo10Js=
github.com/soheilhy/cmux v0.1.5/go.mod h1:T7TcVDs9LWfQgPlPsdngu6I6QIoyIFZDDC6sNE1GqG0=
github.com/sourcegraph/conc v0.3.1-0.20240121214520-5f936abd7ae8 h1:+jumHNA0Wrelhe64i8F6HNlS8pkoyMv5sreGx2Ry5Rw=
github.com/sourcegraph/conc v0.3.1-0.20240121214520-5f936abd7ae8/go.mod h1:3n1Cwaq1E1/1lhQhtRK2ts/ZwZEhjcQeJQ1RuC6Q/8U=
github.com/spf13/afero v1.15.0 h1:b/YBCLWAJdFWJTN9cLhiXXcD7mzKn9Dm86dNnfyQw1I=
github.com/spf13/afero v1.15.0/go.mod h1:NC2ByUVxtQs4b3sIUphxK0NioZnmxgyCrfzeuq8lxMg=
github.com/spf13/cast v1.10.0 h1:h2x0u2shc1QuLHfxi+cTJvs30+ZAHOGRic8uyGTDWxY=
github.com/spf13/cast v1.10.0/go.mod h1:jNfB8QC9IA6ZuY2ZjDp0KtFO2LZZlg4S/7bzP6qqeHo=
github.com/spf13/cobra v1.10.2 h1:DMTTonx5m65Ic0GOoRY2c16WCbHxOOw6xxezuLaBpcU=
github.com/spf13/cobra v1.10.2/go.mod h1:7C1pvHqHw5A4vrJfjNwvOdzYu0Gml16OCs2GRiTUUS4=
github.com/spf13/pflag v1.0.9/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
github.com/spf13/pflag v1.0.10 h1:4EBh2KAYBwaONj6b2Ye1GiHfwjqyROoF4RwYO+vPwFk=
github.com/spf13/pflag v1.0.10/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
github.com/spf13/viper v1.21.0 h1:x5S+0EU27Lbphp4UKm1C+1oQO+rKx36vfCoaVebLFSU=
github.com/spf13/viper v1.21.0/go.mod h1:P0lhsswPGWD/1lZJ9ny3fYnVqxiegrlNrEmgLjbTCAY=
github.com/stoewer/go-strcase v1.3.0 h1:g0eASXYtp+yvN9fK8sH94oCIk0fau9uV1/ZdJ0AVEzs=
github.com/stoewer/go-strcase v1.3.0/go.mod h1:fAH5hQ5pehh+j3nZfvwdk2RgEgQjAoM8wodgtPmh1xo=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
Expand All @@ -238,10 +222,8 @@ github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
github.com/stretchr/testify v1.11.1 h1:7s2iGBzp5EwR7/aIZr8ao5+dra3wiQyKjjFuvgVKu7U=
github.com/stretchr/testify v1.11.1/go.mod h1:wZwfW3scLgRK+23gO65QZefKpKQRnfz6sD981Nm4B6U=
github.com/subosito/gotenv v1.6.0 h1:9NlTDc1FTs4qu0DDq7AEtTPNw6SVm7uBMsUCUjABIf8=
github.com/subosito/gotenv v1.6.0/go.mod h1:Dk4QP5c2W3ibzajGcXpNraDfq2IrhjMIvMSWPKKo0FU=
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/tmc/grpc-websocket-proxy v0.0.0-20220101234140-673ab2c3ae75 h1:6fotK7otjonDflCTK0BCfls4SPy3NcCVb5dqqmbRknE=
github.com/tmc/grpc-websocket-proxy v0.0.0-20220101234140-673ab2c3ae75/go.mod h1:KO6IkyS8Y3j8OdNO85qEYBsRPuteD+YciPomcXdrMnk=
github.com/x448/float16 v0.8.4 h1:qLwI1I70+NjRFUR3zs1JPUCgaCXSh3SW62uAKT1mSBM=
Expand Down Expand Up @@ -294,7 +276,6 @@ go.uber.org/multierr v1.11.0 h1:blXXJkSxSSfBVBlC76pxqeO+LN3aDfLQo+309xJstO0=
go.uber.org/multierr v1.11.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y=
go.uber.org/zap v1.27.0 h1:aJMhYGrd5QSmlpLMr2MftRKl7t8J8PTZPA732ud/XR8=
go.uber.org/zap v1.27.0/go.mod h1:GB2qFLM7cTU87MWRP2mPIjqfIDnGu+VIO4V/SdhGo2E=
go.yaml.in/yaml/v3 v3.0.4 h1:tfq32ie2Jv2UxXFdLJdh3jXuOzWiL1fo0bu/FbuKpbc=
go.yaml.in/yaml/v3 v3.0.4/go.mod h1:DhzuOOF2ATzADvBadXxruRBLzYTpT36CKvDb3+aBEFg=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
Expand Down
183 changes: 33 additions & 150 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
@@ -1,162 +1,45 @@
package config

import (
"errors"
"fmt"
"reflect"
"strconv"
"time"

"github.com/spf13/cobra"
"github.com/spf13/pflag"
"github.com/spf13/viper"
)
import "github.com/spf13/pflag"

type ServiceConfig struct {
Kubeconfig string `mapstructure:"kubeconfig"`
ServerURL string `mapstructure:"server-url"`
EntityLabel string `mapstructure:"entity-label" default:"ui.platform-mesh.ui/entity"`
ContentForLabel string `mapstructure:"content-for-label" default:"ui.platform-mesh.io/content-for"`
MainEntityName string `mapstructure:"main-entity-name" default:"main"`
AccountEntityName string `mapstructure:"account-entity-name" default:"core_platform-mesh_io_account"`

ResourceSchemaName string `mapstructure:"resource-schema-name" default:"v250704-6d57f16.contentconfigurations.ui.platform-mesh.io"`
ResourceSchemaWorkspace string `mapstructure:"resource-schema-workspace" default:"root:openmfp-system"`
Kubeconfig string
ServerURL string
EntityLabel string
ContentForLabel string
MainEntityName string
AccountEntityName string

ResourceAPIExportEndpointSliceName string `mapstructure:"resource-apiexport-endpointslice-name"`
}
ResourceSchemaName string
ResourceSchemaWorkspace string

// generateFlagSet generates a pflag.FlagSet from a struct based on its `mapstructure` tags.
func generateFlagSet(config any) (*pflag.FlagSet, error) {
flagSet := pflag.NewFlagSet("generated", pflag.ContinueOnError)
err := traverseStruct(reflect.ValueOf(config), flagSet, "")
return flagSet, err
ResourceAPIExportEndpointSliceName string
}

// traverseStruct recursively traverses a struct and adds flags to the FlagSet.
func traverseStruct(value reflect.Value, flagSet *pflag.FlagSet, prefix string) error {
if value.Kind() == reflect.Ptr {
value = value.Elem()
func NewServiceConfig() ServiceConfig {
return ServiceConfig{
EntityLabel: "ui.platform-mesh.ui/entity",
ContentForLabel: "ui.platform-mesh.io/content-for",
MainEntityName: "main",
AccountEntityName: "core_platform-mesh_io_account",
ResourceSchemaName: "v250704-6d57f16.contentconfigurations.ui.platform-mesh.io",
ResourceSchemaWorkspace: "root:openmfp-system",
}

if value.Kind() != reflect.Struct {
return errors.New("value must be a struct")
}

typ := value.Type()
for i := 0; i < value.NumField(); i++ {
field := typ.Field(i)
fieldValue := value.Field(i)

// Get the `mapstructure` tag
tag := field.Tag.Get("mapstructure")
if tag == "" || tag == "-" {
continue
}

defaultValueTag := field.Tag.Get("default")
defaultStrValue := ""
if defaultValueTag != "" {
defaultStrValue = defaultValueTag
}

descriptionValueTag := field.Tag.Get("description")
descriptionStrValue := ""
if descriptionValueTag != "" {
descriptionStrValue = descriptionValueTag
}

// Handle nested structs
if fieldValue.Kind() == reflect.Struct {
if tag == ",squash" {
err := traverseStruct(fieldValue, flagSet, "")
if err != nil {
return err
}
} else {
err := traverseStruct(fieldValue, flagSet, prefix+tag+".")
if err != nil {
return err
}
}
continue
}

description := fmt.Sprintf("Set the %s", tag)
if descriptionStrValue != "" {
description = descriptionStrValue
}

// Add flags based on the field type
switch fieldValue.Kind() {
case reflect.String:
flagSet.String(prefix+tag, defaultStrValue, description)
case reflect.Int, reflect.Int64:
if fieldValue.Type() == reflect.TypeOf(time.Duration(0)) {
var durVal time.Duration
if defaultStrValue != "" {
parsedDurVal, err := time.ParseDuration(defaultStrValue)
if err != nil {
return fmt.Errorf("invalid duration value for field %s: %w", field.Name, err)
}
durVal = parsedDurVal
}

durDescription := fmt.Sprintf("Set the %s in seconds", tag)
if descriptionStrValue != "" {
durDescription = descriptionStrValue
}
flagSet.Duration(prefix+tag, durVal, durDescription)
} else {
i := 0
if defaultStrValue != "" {
parsedInt, err := strconv.Atoi(defaultStrValue)
if err != nil {
return err
}
i = parsedInt
}
flagSet.Int(prefix+tag, i, description)
}
case reflect.Bool:
var defaultBoolValue bool
if defaultStrValue != "" {
b, err := strconv.ParseBool(defaultStrValue)
if err != nil {
return err
}
defaultBoolValue = b
}
flagSet.Bool(prefix+tag, defaultBoolValue, description)
default:
return fmt.Errorf("unsupported field type %s for field %s", fieldValue.Kind(), field.Name)
}

}
return nil
}

func BindConfigToFlags(v *viper.Viper, cmd *cobra.Command, config any) error {
flagSet, err := generateFlagSet(config)
if err != nil {
return fmt.Errorf("failed to generate flag set: %w", err)
}
err = v.BindPFlags(flagSet)
if err != nil {
return err
}

cmd.Flags().AddFlagSet(flagSet)

cobra.OnInitialize(unmarshalIntoStruct(v, config))

return nil
}

func unmarshalIntoStruct(v *viper.Viper, cfg any) func() {
return func() {
if err := v.Unmarshal(cfg); err != nil {
panic(fmt.Errorf("failed to unmarshal config: %w", err))
}
}
func (c *ServiceConfig) AddFlags(fs *pflag.FlagSet) {
fs.StringVar(&c.Kubeconfig, "kubeconfig", c.Kubeconfig, "Set the kubeconfig file path")
fs.StringVar(&c.ServerURL, "server-url", c.ServerURL, "Set the server URL")
fs.StringVar(&c.EntityLabel, "entity-label", c.EntityLabel, "Set the entity label")
fs.StringVar(&c.ContentForLabel, "content-for-label", c.ContentForLabel, "Set the content-for label")
fs.StringVar(&c.MainEntityName, "main-entity-name", c.MainEntityName, "Set the main entity name")
fs.StringVar(&c.AccountEntityName, "account-entity-name", c.AccountEntityName, "Set the account entity name")
fs.StringVar(&c.ResourceSchemaName, "resource-schema-name", c.ResourceSchemaName, "Set the resource schema name")
fs.StringVar(&c.ResourceSchemaWorkspace, "resource-schema-workspace", c.ResourceSchemaWorkspace, "Set the resource schema workspace")
fs.StringVar(
&c.ResourceAPIExportEndpointSliceName,
"resource-apiexport-endpointslice-name",
c.ResourceAPIExportEndpointSliceName,
"Set the resource APIExport EndpointSlice name",
)
}
Loading
Loading