|
| 1 | +package cmd |
| 2 | + |
| 3 | +/* |
| 4 | +Copyright 2019 The Codefresh Authors. |
| 5 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +you may not use this file except in compliance with the License. |
| 7 | +You may obtain a copy of the License at |
| 8 | +
|
| 9 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +
|
| 11 | +Unless required by applicable law or agreed to in writing, software |
| 12 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +See the License for the specific language governing permissions and |
| 15 | +limitations under the License. |
| 16 | +*/ |
| 17 | + |
| 18 | +import ( |
| 19 | + "fmt" |
| 20 | + |
| 21 | + "github.com/codefresh-io/venona/venonactl/pkg/plugins" |
| 22 | + "github.com/codefresh-io/venona/venonactl/pkg/store" |
| 23 | + "github.com/spf13/cobra" |
| 24 | + "github.com/spf13/viper" |
| 25 | +) |
| 26 | + |
| 27 | +var allTestPluginTypes = []string{ |
| 28 | + plugins.RuntimeEnvironmentPluginType, |
| 29 | + plugins.VenonaPluginType, |
| 30 | + plugins.MonitorAgentPluginType, |
| 31 | + plugins.VolumeProvisionerPluginType, |
| 32 | + plugins.EnginePluginType, |
| 33 | + plugins.RuntimeAttachType, |
| 34 | +} |
| 35 | + |
| 36 | +var testCommandOptions struct { |
| 37 | + kube struct { |
| 38 | + namespace string |
| 39 | + context string |
| 40 | + } |
| 41 | + plugin []string |
| 42 | +} |
| 43 | + |
| 44 | +var testCommand = &cobra.Command{ |
| 45 | + Use: "test", |
| 46 | + Short: "Run test on the target cluster prior installation", |
| 47 | + Run: func(cmd *cobra.Command, args []string) { |
| 48 | + lgr := createLogger("test", verbose) |
| 49 | + s := store.GetStore() |
| 50 | + extendStoreWithKubeClient(lgr) |
| 51 | + fillKubernetesAPI(lgr, testCommandOptions.kube.context, testCommandOptions.kube.namespace, false) |
| 52 | + |
| 53 | + builder := plugins.NewBuilder(lgr) |
| 54 | + for _, p := range testCommandOptions.plugin { |
| 55 | + if p == plugins.RuntimeEnvironmentPluginType { |
| 56 | + builder.Add(plugins.RuntimeEnvironmentPluginType) |
| 57 | + } |
| 58 | + if p == plugins.VenonaPluginType { |
| 59 | + builder.Add(plugins.VenonaPluginType) |
| 60 | + } |
| 61 | + if p == plugins.MonitorAgentPluginType { |
| 62 | + builder.Add(plugins.MonitorAgentPluginType) |
| 63 | + } |
| 64 | + if p == plugins.VolumeProvisionerPluginType { |
| 65 | + builder.Add(plugins.VolumeProvisionerPluginType) |
| 66 | + } |
| 67 | + if p == plugins.EnginePluginType { |
| 68 | + builder.Add(plugins.EnginePluginType) |
| 69 | + } |
| 70 | + if p == plugins.RuntimeAttachType { |
| 71 | + builder.Add(plugins.RuntimeAttachType) |
| 72 | + } |
| 73 | + } |
| 74 | + var finalerr error |
| 75 | + for _, p := range builder.Get() { |
| 76 | + lgr.Info("Testing requirements", "installer", p.Name()) |
| 77 | + err := p.Test(plugins.TestOptions{ |
| 78 | + KubeBuilder: getKubeClientBuilder(s.KubernetesAPI.ContextName, s.KubernetesAPI.Namespace, s.KubernetesAPI.ConfigPath, false), |
| 79 | + ClusterNamespace: s.KubernetesAPI.Namespace, |
| 80 | + }) |
| 81 | + if err != nil { |
| 82 | + if finalerr != nil { |
| 83 | + finalerr = fmt.Errorf("%s - %s", finalerr.Error(), err.Error()) |
| 84 | + } else { |
| 85 | + finalerr = fmt.Errorf("%s", err.Error()) |
| 86 | + |
| 87 | + } |
| 88 | + } |
| 89 | + } |
| 90 | + dieOnError(finalerr) |
| 91 | + |
| 92 | + lgr.Info("Cluster passed acceptance test") |
| 93 | + }, |
| 94 | +} |
| 95 | + |
| 96 | +func init() { |
| 97 | + viper.BindEnv("kube-namespace", "KUBE_NAMESPACE") |
| 98 | + viper.BindEnv("kube-context", "KUBE_CONTEXT") |
| 99 | + |
| 100 | + testCommand.Flags().StringVar(&testCommandOptions.kube.namespace, "kube-namespace", viper.GetString("kube-namespace"), "Name of the namespace on which monitor should be installed [$KUBE_NAMESPACE]") |
| 101 | + testCommand.Flags().StringVar(&testCommandOptions.kube.context, "kube-context-name", viper.GetString("kube-context"), "Name of the kubernetes context on which monitor should be installed (default is current-context) [$KUBE_CONTEXT]") |
| 102 | + testCommand.Flags().StringArrayVar(&testCommandOptions.plugin, "installer", allTestPluginTypes, "Which test to run, based on the installer type") |
| 103 | + |
| 104 | + rootCmd.AddCommand(testCommand) |
| 105 | +} |
0 commit comments