create special error to skip addons without erroring out

pull/12231/head
Sharif Elgamal 2021-08-13 13:42:02 -07:00
parent ca04178ced
commit 06ed3378b9
3 changed files with 25 additions and 8 deletions

View File

@ -17,6 +17,7 @@ limitations under the License.
package config package config
import ( import (
"errors"
"fmt" "fmt"
"github.com/spf13/cobra" "github.com/spf13/cobra"
@ -48,7 +49,7 @@ var addonsEnableCmd = &cobra.Command{
viper.Set(config.AddonImages, images) viper.Set(config.AddonImages, images)
viper.Set(config.AddonRegistries, registries) viper.Set(config.AddonRegistries, registries)
err := addons.SetAndSave(ClusterFlagValue(), addon, "true") err := addons.SetAndSave(ClusterFlagValue(), addon, "true")
if err != nil { if err != nil && !errors.Is(err, addons.ErrSkipThisAddon) {
exit.Error(reason.InternalAddonEnable, "enable failed", err) exit.Error(reason.InternalAddonEnable, "enable failed", err)
} }
if addon == "dashboard" { if addon == "dashboard" {
@ -63,8 +64,9 @@ var addonsEnableCmd = &cobra.Command{
`, out.V{"profileArg": tipProfileArg}) `, out.V{"profileArg": tipProfileArg})
} }
if err != nil {
out.Step(style.AddonEnable, "The '{{.addonName}}' addon is enabled", out.V{"addonName": addon}) out.Step(style.AddonEnable, "The '{{.addonName}}' addon is enabled", out.V{"addonName": addon})
}
}, },
} }

View File

@ -55,6 +55,9 @@ var Force bool = false
// Currently only used for gcp-auth // Currently only used for gcp-auth
var Refresh bool = false var Refresh bool = false
// ErrSkipThisAddon is a special error that tells us to not error out, but to also not mark the addon as enabled
var ErrSkipThisAddon = errors.New("skipping this addon")
// RunCallbacks runs all actions associated to an addon, but does not set it (thread-safe) // RunCallbacks runs all actions associated to an addon, but does not set it (thread-safe)
func RunCallbacks(cc *config.ClusterConfig, name string, value string) error { func RunCallbacks(cc *config.ClusterConfig, name string, value string) error {
klog.Infof("Setting %s=%s in profile %q", name, value, cc.Name) klog.Infof("Setting %s=%s in profile %q", name, value, cc.Name)
@ -65,11 +68,17 @@ func RunCallbacks(cc *config.ClusterConfig, name string, value string) error {
// Run any additional validations for this property // Run any additional validations for this property
if err := run(cc, name, value, a.validations); err != nil { if err := run(cc, name, value, a.validations); err != nil {
if errors.Is(err, ErrSkipThisAddon) {
return err
}
return errors.Wrap(err, "running validations") return errors.Wrap(err, "running validations")
} }
// Run any callbacks for this property // Run any callbacks for this property
if err := run(cc, name, value, a.callbacks); err != nil { if err := run(cc, name, value, a.callbacks); err != nil {
if errors.Is(err, ErrSkipThisAddon) {
return err
}
return errors.Wrap(err, "running callbacks") return errors.Wrap(err, "running callbacks")
} }
return nil return nil
@ -92,6 +101,9 @@ func SetAndSave(profile string, name string, value string) error {
} }
if err := RunCallbacks(cc, name, value); err != nil { if err := RunCallbacks(cc, name, value); err != nil {
if errors.Is(err, ErrSkipThisAddon) {
return err
}
return errors.Wrap(err, "run callbacks") return errors.Wrap(err, "run callbacks")
} }
@ -105,15 +117,18 @@ func SetAndSave(profile string, name string, value string) error {
// Runs all the validation or callback functions and collects errors // Runs all the validation or callback functions and collects errors
func run(cc *config.ClusterConfig, name string, value string, fns []setFn) error { func run(cc *config.ClusterConfig, name string, value string, fns []setFn) error {
var errors []error var errs []error
for _, fn := range fns { for _, fn := range fns {
err := fn(cc, name, value) err := fn(cc, name, value)
if err != nil { if err != nil {
errors = append(errors, err) if errors.Is(err, ErrSkipThisAddon) {
return ErrSkipThisAddon
}
errs = append(errs, err)
} }
} }
if len(errors) > 0 { if len(errs) > 0 {
return fmt.Errorf("%v", errors) return fmt.Errorf("%v", errs)
} }
return nil return nil
} }

View File

@ -342,7 +342,7 @@ func verifyGCPAuthAddon(cc *config.ClusterConfig, name string, val string) error
// If we're in GCE and didn't actually start the gcp-auth pods, don't check for them. // If we're in GCE and didn't actually start the gcp-auth pods, don't check for them.
// We also don't want to actually set the addon as enabled, so just exit completely. // We also don't want to actually set the addon as enabled, so just exit completely.
if !Force && detect.IsOnGCE() && os.Getenv("GOOGLE_APPLICATION_CREDENTUALS") == "" { if !Force && detect.IsOnGCE() && os.Getenv("GOOGLE_APPLICATION_CREDENTUALS") == "" {
os.Exit(0) return ErrSkipThisAddon
} }
err = verifyAddonStatusInternal(cc, name, val, "gcp-auth") err = verifyAddonStatusInternal(cc, name, val, "gcp-auth")