|
| 1 | +package cmd |
| 2 | + |
| 3 | +/* |
| 4 | +Copyright 2020 The Codefresh Authors. |
| 5 | +
|
| 6 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | +you may not use this file except in compliance with the License. |
| 8 | +You may obtain a copy of the License at |
| 9 | +
|
| 10 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +
|
| 12 | +Unless required by applicable law or agreed to in writing, software |
| 13 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | +See the License for the specific language governing permissions and |
| 16 | +limitations under the License. |
| 17 | +*/ |
| 18 | + |
| 19 | +import ( |
| 20 | + "fmt" |
| 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 installMonitorAgentCmdOptions struct { |
| 28 | + kube struct { |
| 29 | + namespace string |
| 30 | + inCluster bool |
| 31 | + context string |
| 32 | + nodeSelector string |
| 33 | + } |
| 34 | + clusterId string |
| 35 | + helm3 bool |
| 36 | + codefreshToken string |
| 37 | +} |
| 38 | + |
| 39 | +// installK8sAgentCmd represents the install command |
| 40 | +var installMonitorAgentCmd = &cobra.Command{ |
| 41 | + Use: "monitor", |
| 42 | + Short: "Install Codefresh's monitor agent on cluster", |
| 43 | + Run: func(cmd *cobra.Command, args []string) { |
| 44 | + |
| 45 | + s := store.GetStore() |
| 46 | + |
| 47 | + lgr := createLogger("Install-monitor-agent", verbose) |
| 48 | + buildBasicStore(lgr) |
| 49 | + extendStoreWithKubeClient(lgr) |
| 50 | + fillKubernetesAPI(lgr, installMonitorAgentCmdOptions.kube.context, installMonitorAgentCmdOptions.kube.namespace, installMonitorAgentCmdOptions.kube.inCluster) |
| 51 | + |
| 52 | + builder := plugins.NewBuilder(lgr) |
| 53 | + builder.Add(plugins.MonitorAgentPluginType) |
| 54 | + |
| 55 | + builderInstallOpt := &plugins.InstallOptions{ |
| 56 | + ClusterNamespace: s.KubernetesAPI.Namespace, |
| 57 | + } |
| 58 | + |
| 59 | + builderInstallOpt.KubeBuilder = getKubeClientBuilder(s.KubernetesAPI.ContextName, s.KubernetesAPI.Namespace, s.KubernetesAPI.ConfigPath, s.KubernetesAPI.InCluster) |
| 60 | + |
| 61 | + if installMonitorAgentCmdOptions.clusterId == "" { |
| 62 | + dieOnError(fmt.Errorf("Cluster id is required in order to install monitor")) |
| 63 | + } |
| 64 | + |
| 65 | + s.ClusterId = installMonitorAgentCmdOptions.clusterId |
| 66 | + s.Helm3 = installMonitorAgentCmdOptions.helm3 |
| 67 | + |
| 68 | + if cfAPIHost == "" { |
| 69 | + cfAPIHost = "https://g.codefresh.io" |
| 70 | + } |
| 71 | + |
| 72 | + if installMonitorAgentCmdOptions.codefreshToken == "" { |
| 73 | + dieOnError(fmt.Errorf("Codefresh token is required in order to install monitor")) |
| 74 | + } |
| 75 | + |
| 76 | + s.CodefreshAPI = &store.CodefreshAPI{ |
| 77 | + Host: cfAPIHost, |
| 78 | + Token: installMonitorAgentCmdOptions.codefreshToken, |
| 79 | + } |
| 80 | + |
| 81 | + // stub , not need actually for monitor |
| 82 | + s.AgentAPI = &store.AgentAPI{ |
| 83 | + Token: "", |
| 84 | + Id: "", |
| 85 | + } |
| 86 | + |
| 87 | + values := s.BuildValues() |
| 88 | + |
| 89 | + for _, p := range builder.Get() { |
| 90 | + _, err := p.Install(builderInstallOpt, values) |
| 91 | + dieOnError(err) |
| 92 | + } |
| 93 | + lgr.Info("Monitor agent installation completed Successfully") |
| 94 | + }, |
| 95 | +} |
| 96 | + |
| 97 | +func init() { |
| 98 | + installCommand.AddCommand(installMonitorAgentCmd) |
| 99 | + |
| 100 | + viper.BindEnv("kube-namespace", "KUBE_NAMESPACE") |
| 101 | + viper.BindEnv("kube-context", "KUBE_CONTEXT") |
| 102 | + installMonitorAgentCmd.Flags().StringVar(&installMonitorAgentCmdOptions.kube.namespace, "kube-namespace", viper.GetString("kube-namespace"), "Name of the namespace on which monitor should be installed [$KUBE_NAMESPACE]") |
| 103 | + installMonitorAgentCmd.Flags().StringVar(&installMonitorAgentCmdOptions.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]") |
| 104 | + installMonitorAgentCmd.Flags().StringVar(&installMonitorAgentCmdOptions.clusterId, "clusterId", "", "Cluster Id") |
| 105 | + installMonitorAgentCmd.Flags().StringVar(&installMonitorAgentCmdOptions.codefreshToken, "codefreshToken", "", "Codefresh token") |
| 106 | + |
| 107 | + installMonitorAgentCmd.Flags().BoolVar(&installMonitorAgentCmdOptions.kube.inCluster, "in-cluster", false, "Set flag if monitor is been installed from inside a cluster") |
| 108 | + |
| 109 | + installMonitorAgentCmd.Flags().BoolVar(&installMonitorAgentCmdOptions.helm3, "helm3", false, "Set flag if cluster use helm3") |
| 110 | + |
| 111 | +} |
0 commit comments