Skip to content

Commit

Permalink
permit unknown fields, improve error handling, etc
Browse files Browse the repository at this point in the history
  • Loading branch information
olix0r committed Mar 28, 2019
1 parent cf88617 commit 12741c8
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 21 deletions.
31 changes: 14 additions & 17 deletions cli/cmd/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func newCmdUpgrade() *cobra.Command {
// We need a Kubernetes client to fetch configs and issuer secrets.
k, err := options.newK8s()
if err != nil {
return err
return fmt.Errorf("failed to create a kubernetes client: %s", err)
}

// We fetch the configs directly from kubernetes because we need to be able
Expand All @@ -45,12 +45,12 @@ func newCmdUpgrade() *cobra.Command {
// control plane.
configs, err := fetchConfigs(k)
if err != nil {
return err
return fmt.Errorf("could not fetch configs from kubernetes: %s", err)
}

// We recorded flags during a prior install. If we haven't overridden the
// flag on this upgrade, reset that prior value as if it were specified now.
setOptionsFromConfigs(flags, configs)
setOptionsFromInstall(flags, configs.GetInstall())

// Save off the updated set of flags into the installOptions so it gets
// persisted with the upgraded config.
Expand All @@ -61,30 +61,27 @@ func newCmdUpgrade() *cobra.Command {

values, configs, err := options.build(k, configs)
if err != nil {
return err
return fmt.Errorf("could not build install configuration: %s", err)
}

return values.render(os.Stdout, configs)
if err = values.render(os.Stdout, configs); err != nil {
return fmt.Errorf("could not render install configuration: %s", err)
}

return nil
},
}

cmd.PersistentFlags().AddFlagSet(flags)
return cmd
}

func setOptionsFromConfigs(flags *pflag.FlagSet, configs *pb.All) {
priorFlags := map[string]string{}
for _, f := range configs.GetInstall().GetFlags() {
priorFlags[f.Name] = f.Value
}

flags.Visit(func(f *pflag.Flag) {
if !f.Changed {
if v, ok := priorFlags[f.Name]; ok {
f.Value.Set(v)
}
func setOptionsFromInstall(flags *pflag.FlagSet, install *pb.Install) {
for _, i := range install.GetFlags() {
if f := flags.Lookup(i.GetName()); f != nil && !f.Changed {
f.Value.Set(i.GetValue())
}
})
}
}

// fetchInstallValuesFromCluster checks the kubernetes API to fetch an existing
Expand Down
8 changes: 4 additions & 4 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,18 @@ import (
// Global returns the Global protobuf config from the linkerd-config ConfigMap
func Global(filepath string) (*pb.Global, error) {
config := &pb.Global{}
err := unmarshalConfig(filepath, config)
err := unmarshalFile(filepath, config)
return config, err
}

// Proxy returns the Proxy protobuf config from the linkerd-config ConfigMap
func Proxy(filepath string) (*pb.Proxy, error) {
config := &pb.Proxy{}
err := unmarshalConfig(filepath, config)
err := unmarshalFile(filepath, config)
return config, err
}

func unmarshalConfig(filepath string, msg proto.Message) error {
func unmarshalFile(filepath string, msg proto.Message) error {
configJSON, err := ioutil.ReadFile(filepath)
if err != nil {
return err
Expand All @@ -35,7 +35,7 @@ func unmarshalConfig(filepath string, msg proto.Message) error {
}

func unmarshal(json string, msg proto.Message) error {
u := jsonpb.Unmarshaler{}
u := jsonpb.Unmarshaler{AllowUnknownFields: true}
return u.Unmarshal(strings.NewReader(json), msg)
}

Expand Down

0 comments on commit 12741c8

Please sign in to comment.