Skip to content

Commit 2aafff8

Browse files
Saas 6907 monitor integration (#114)
* adding kubernetes resources agent resources * rename and support values * fix deployment and install for monitor agent * add uninstall support * add uninstall support * fix typos * fix yamls and pr comments * move monitor to builds values * move monitor to builds values * move monitor to builds values * add validations * bump versions
1 parent ee9301c commit 2aafff8

16 files changed

+718
-11
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "venona",
3-
"version": "1.1.0",
3+
"version": "1.2.0",
44
"description": "Codefresh agent to run on Codefresh's runtime environment and execute pipeline",
55
"main": "index.js",
66
"scripts": {

venonactl/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
.idea/
2+
/vendor/

venonactl/VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
1.1.0
1+
1.2.0

venonactl/cmd/install-monitor.go

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
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+
}

venonactl/cmd/uninstall-monitor.go

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package cmd
2+
3+
import (
4+
"github.com/codefresh-io/venona/venonactl/pkg/store"
5+
6+
"github.com/codefresh-io/venona/venonactl/pkg/plugins"
7+
"github.com/spf13/cobra"
8+
"github.com/spf13/viper"
9+
)
10+
11+
var uninstallMonitorAgentCmdOptions struct {
12+
kube struct {
13+
context string
14+
namespace string
15+
kubePath string
16+
}
17+
}
18+
19+
var uninstallMonitorAgentCmd = &cobra.Command{
20+
Use: "monitor",
21+
Short: "Uninstall Codefresh's monitor",
22+
Run: func(cmd *cobra.Command, args []string) {
23+
s := store.GetStore()
24+
lgr := createLogger("UninstallMonitor", verbose)
25+
buildBasicStore(lgr)
26+
extendStoreWithKubeClient(lgr)
27+
fillKubernetesAPI(lgr, uninstallMonitorAgentCmdOptions.kube.context, uninstallMonitorAgentCmdOptions.kube.namespace, false)
28+
29+
builder := plugins.NewBuilder(lgr)
30+
31+
if uninstallMonitorAgentCmdOptions.kube.kubePath == "" {
32+
uninstallMonitorAgentCmdOptions.kube.kubePath = kubeConfigPath
33+
}
34+
35+
if cfAPIHost == "" {
36+
cfAPIHost = "https://g.codefresh.io"
37+
}
38+
// This is temporarily and used for signing
39+
s.CodefreshAPI = &store.CodefreshAPI{
40+
Host: cfAPIHost,
41+
}
42+
43+
// stub , not need actually for monitor
44+
s.AgentAPI = &store.AgentAPI{
45+
Token: "",
46+
Id: "",
47+
}
48+
49+
deleteOptions := &plugins.DeleteOptions{}
50+
// runtime
51+
deleteOptions.KubeBuilder = getKubeClientBuilder(uninstallMonitorAgentCmdOptions.kube.context,
52+
s.KubernetesAPI.Namespace,
53+
uninstallMonitorAgentCmdOptions.kube.kubePath,
54+
false)
55+
56+
builder.Add(plugins.MonitorAgentPluginType)
57+
deleteOptions.ClusterNamespace = s.KubernetesAPI.Namespace
58+
for _, p := range builder.Get() {
59+
err := p.Delete(deleteOptions, s.BuildValues())
60+
if err != nil {
61+
dieOnError(err)
62+
}
63+
}
64+
65+
lgr.Info("Deletion of monitor is completed")
66+
},
67+
}
68+
69+
func init() {
70+
uninstallCommand.AddCommand(uninstallMonitorAgentCmd)
71+
viper.BindEnv("kube-namespace", "KUBE_NAMESPACE")
72+
viper.BindEnv("kube-context", "KUBE_CONTEXT")
73+
uninstallMonitorAgentCmd.Flags().StringVar(&uninstallMonitorAgentCmdOptions.kube.namespace, "kube-namespace", viper.GetString("kube-namespace"), "Name of the namespace on which monitor should be uninstalled [$KUBE_NAMESPACE]")
74+
uninstallMonitorAgentCmd.Flags().StringVar(&uninstallMonitorAgentCmdOptions.kube.context, "kube-context-name", viper.GetString("kube-context"), "Name of the kubernetes context on which monitor should be uninstalled (default is current-context) [$KUBE_CONTEXT]")
75+
uninstallMonitorAgentCmd.Flags().StringVar(&uninstallMonitorAgentCmdOptions.kube.kubePath, "kube-config-path", viper.GetString("kubeconfig"), "Path to kubeconfig file (default is $HOME/.kube/config) [$KUBECONFIG]")
76+
77+
}

venonactl/pkg/plugins/monitor.go

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/*
2+
Copyright 2020 The Codefresh Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package plugins
18+
19+
import (
20+
"fmt"
21+
"github.com/codefresh-io/venona/venonactl/pkg/logger"
22+
templates "github.com/codefresh-io/venona/venonactl/pkg/templates/kubernetes"
23+
)
24+
25+
// k8sAgentPlugin installs assets on Kubernetes Dind runtimectl Env
26+
type monitorAgentPlugin struct {
27+
logger logger.Logger
28+
}
29+
30+
const (
31+
monitorFilesPattern = ".*.monitor.yaml"
32+
)
33+
34+
// Install k8sAgent agent
35+
func (u *monitorAgentPlugin) Install(opt *InstallOptions, v Values) (Values, error) {
36+
37+
cs, err := opt.KubeBuilder.BuildClient()
38+
if err != nil {
39+
u.logger.Error(fmt.Sprintf("Cannot create kubernetes clientset: %v ", err))
40+
return nil, err
41+
}
42+
err = opt.KubeBuilder.EnsureNamespaceExists(cs)
43+
if err != nil {
44+
u.logger.Error(fmt.Sprintf("Cannot ensure namespace exists: %v", err))
45+
return nil, err
46+
}
47+
return v, install(&installOptions{
48+
logger: u.logger,
49+
templates: templates.TemplatesMap(),
50+
templateValues: v,
51+
kubeClientSet: cs,
52+
namespace: opt.ClusterNamespace,
53+
matchPattern: monitorFilesPattern,
54+
dryRun: opt.DryRun,
55+
operatorType: MonitorAgentPluginType,
56+
})
57+
}
58+
59+
func (u *monitorAgentPlugin) Status(statusOpt *StatusOptions, v Values) ([][]string, error) {
60+
return [][]string{}, nil
61+
}
62+
63+
func (u *monitorAgentPlugin) Delete(deleteOpt *DeleteOptions, v Values) error {
64+
cs, err := deleteOpt.KubeBuilder.BuildClient()
65+
if err != nil {
66+
u.logger.Error(fmt.Sprintf("Cannot create kubernetes clientset: %v ", err))
67+
return err
68+
}
69+
opt := &deleteOptions{
70+
templates: templates.TemplatesMap(),
71+
templateValues: v,
72+
kubeClientSet: cs,
73+
namespace: deleteOpt.ClusterNamespace,
74+
matchPattern: monitorFilesPattern,
75+
operatorType: MonitorAgentPluginType,
76+
logger: u.logger,
77+
}
78+
return uninstall(opt)
79+
}
80+
81+
func (u *monitorAgentPlugin) Upgrade(opt *UpgradeOptions, v Values) (Values, error) {
82+
return nil, nil
83+
}

venonactl/pkg/plugins/plugin.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
const (
1515
RuntimeEnvironmentPluginType = "runtime-environment"
1616
VenonaPluginType = "venona"
17+
MonitorAgentPluginType = "monitor-agent"
1718
VolumeProvisionerPluginType = "volume-provisioner"
1819
EnginePluginType = "engine"
1920
DefaultStorageClassNamePrefix = "dind-local-volumes-venona"
@@ -186,6 +187,13 @@ func build(t string, logger logger.Logger) Plugin {
186187
logger: logger.New("Plugin", RuntimeAttachType),
187188
}
188189
}
190+
191+
if t == MonitorAgentPluginType {
192+
return &monitorAgentPlugin{
193+
logger: logger.New("Plugin", MonitorAgentPluginType),
194+
}
195+
}
196+
189197
return nil
190198
}
191199

venonactl/pkg/store/store.go

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
package store
22

33
import (
4+
"fmt"
45
"github.com/codefresh-io/go-sdk/pkg/codefresh"
56
"github.com/codefresh-io/venona/venonactl/pkg/certs"
6-
"fmt"
77
)
88

99
const (
10-
ModeInCluster = "InCluster"
11-
ApplicationName = "venona"
10+
ModeInCluster = "InCluster"
11+
ApplicationName = "venona"
12+
MonitorApplicationName = "monitor"
1213
)
1314

1415
var (
@@ -38,6 +39,13 @@ type (
3839
RuntimeEnvironment string
3940

4041
Version *Version
42+
43+
ClusterId string
44+
45+
Helm3 bool
46+
47+
// need for define if monitor use cluster role or just role
48+
UseNamespaceWithRole bool
4149
}
4250

4351
KubernetesAPI struct {
@@ -108,18 +116,29 @@ func (s *Values) BuildValues() map[string]interface{} {
108116
"Ca": "",
109117
},
110118
"Storage": map[string]interface{}{
111-
"Backend": "local",
112-
"StorageClassName": fmt.Sprintf("dind-local-volumes-%s-%s", ApplicationName, s.KubernetesAPI.Namespace),
119+
"Backend": "local",
120+
"StorageClassName": fmt.Sprintf("dind-local-volumes-%s-%s", ApplicationName, s.KubernetesAPI.Namespace),
113121
"LocalVolumeParentDir": "/var/lib/codefresh/dind-volumes",
114-
"AvailabilityZone": "",
122+
"AvailabilityZone": "",
115123
"GoogleServiceAccount": "",
116-
"AwsAccessKeyId": "",
117-
"AwsSecretAccessKey": "",
124+
"AwsAccessKeyId": "",
125+
"AwsSecretAccessKey": "",
118126
"VolumeProvisioner": map[string]interface{}{
119-
"Image": "codefresh/dind-volume-provisioner:v20",
127+
"Image": "codefresh/dind-volume-provisioner:v20",
120128
"NodeSelector": "",
121129
"Tolerations": s.KubernetesAPI.Tolerations,
122130
},
123131
},
132+
"Monitor": map[string]interface{}{
133+
"UseNamespaceWithRole": s.UseNamespaceWithRole,
134+
//TODO: need verify it on cluster level
135+
"RbacEnabled": true,
136+
"Helm3": s.Helm3,
137+
"AppName": MonitorApplicationName,
138+
"Image": map[string]string{
139+
"Name": "codefresh/agent",
140+
"Tag": "stable",
141+
},
142+
},
124143
}
125144
}

0 commit comments

Comments
 (0)