/
mongodb_installer.go
149 lines (129 loc) · 5.89 KB
/
mongodb_installer.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
// Copyright 2021 Red Hat, Inc. and/or its affiliates
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package installers
import (
"fmt"
mongodbv1 "github.com/kiegroup/kogito-operator/core/infrastructure/mongodb/v1"
"github.com/kiegroup/kogito-operator/test/pkg/framework"
coreapps "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
rbac "k8s.io/api/rbac/v1"
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
"sigs.k8s.io/controller-runtime/pkg/client"
)
var (
// mongoDbYamlNamespacedInstaller installs MongoDB namespaced using YAMLs
mongoDbYamlNamespacedInstaller = YamlNamespacedServiceInstaller{
InstallNamespacedYaml: installMongoDbUsingYaml,
WaitForNamespacedServiceRunning: waitForMongoDbUsingYamlRunning,
GetAllNamespaceYamlCrs: getMongoDbCrsInNamespace,
UninstallNamespaceYaml: uninstallMongoDbUsingYaml,
NamespacedYamlServiceName: mongoDBOperatorServiceName,
}
mongoDBOperatorServiceName = "Mongo DB"
mongoDBOperatorVersion = "v0.7.0"
mongoDBOperatorDeployFilesURI = "https://raw.githubusercontent.com/mongodb/mongodb-kubernetes-operator/" + mongoDBOperatorVersion + "/config/"
)
// GetMongoDbInstaller returns MongoDB installer
func GetMongoDbInstaller() ServiceInstaller {
return &mongoDbYamlNamespacedInstaller
}
func installMongoDbUsingYaml(namespace string) error {
framework.GetLogger(namespace).Info("Deploy MongoDB from yaml files", "file uri", mongoDBOperatorDeployFilesURI)
if !framework.IsMongoDBAvailable(namespace) {
if err := framework.LoadResource(namespace, mongoDBOperatorDeployFilesURI+"crd/bases/mongodbcommunity.mongodb.com_mongodbcommunity.yaml", &apiextensionsv1.CustomResourceDefinition{}, nil); err != nil {
return err
}
}
if err := framework.LoadResource(namespace, mongoDBOperatorDeployFilesURI+"rbac/service_account.yaml", &corev1.ServiceAccount{}, nil); err != nil {
return err
}
if err := framework.LoadResource(namespace, mongoDBOperatorDeployFilesURI+"rbac/role.yaml", &rbac.Role{}, nil); err != nil {
return err
}
if err := framework.LoadResource(namespace, mongoDBOperatorDeployFilesURI+"rbac/role_binding.yaml", &rbac.RoleBinding{}, nil); err != nil {
return err
}
// Then deploy operator
err := framework.LoadResource(namespace, mongoDBOperatorDeployFilesURI+"manager/manager.yaml", &coreapps.Deployment{}, func(object interface{}) {
if framework.IsOpenshift() {
// See https://github.com/mongodb/mongodb-kubernetes-operator/blob/v0.7.0/deploy/openshift/operator_openshift.yaml
framework.GetLogger(namespace).Debug("Setup MANAGED_SECURITY_CONTEXT env in MongoDB operator for Openshift")
object.(*coreapps.Deployment).Spec.Template.Spec.Containers[0].Env = append(object.(*coreapps.Deployment).Spec.Template.Spec.Containers[0].Env,
corev1.EnvVar{
Name: "MANAGED_SECURITY_CONTEXT",
Value: "true",
})
labels := map[string]string{}
object.(*coreapps.Deployment).Namespace = namespace
object.(*coreapps.Deployment).Name = "mongodb-kubernetes-operator"
labels["owner"] = namespace
object.(*coreapps.Deployment).Labels = labels
//object.(*coreapps.Deployment).Spec.Template.Spec.Containers[0].Name = "mongodb-kubernetes-operator"
object.(*coreapps.Deployment).Spec.Template.Spec.Containers[0].SecurityContext = nil
object.(*coreapps.Deployment).Spec.Template.Spec.Containers[0].Image = "quay.io/mongodb/mongodb-kubernetes-operator:0.7.0"
}
})
if err != nil {
return err
}
return nil
}
func waitForMongoDbUsingYamlRunning(namespace string) error {
return framework.WaitForMongoDBOperatorRunning(namespace)
}
func uninstallMongoDbUsingYaml(namespace string) error {
framework.GetMainLogger().Info("Uninstalling Mongo DB")
var originalError error
output, err := framework.CreateCommand("oc", "delete", "-f", mongoDBOperatorDeployFilesURI+"manager/manager.yaml").WithLoggerContext(namespace).Execute()
if err != nil {
framework.GetLogger(namespace).Error(err, fmt.Sprintf("Deleting Mongo DB operator failed, output: %s", output))
if originalError == nil {
originalError = err
}
}
output, err = framework.CreateCommand("oc", "delete", "-f", mongoDBOperatorDeployFilesURI+"rbac/role_binding.yaml").WithLoggerContext(namespace).Execute()
if err != nil {
framework.GetLogger(namespace).Error(err, fmt.Sprintf("Deleting Mongo DB role binding failed, output: %s", output))
if originalError == nil {
originalError = err
}
}
output, err = framework.CreateCommand("oc", "delete", "-f", mongoDBOperatorDeployFilesURI+"rbac/role.yaml").WithLoggerContext(namespace).Execute()
if err != nil {
framework.GetLogger(namespace).Error(err, fmt.Sprintf("Deleting Mongo DB role failed, output: %s", output))
if originalError == nil {
originalError = err
}
}
output, err = framework.CreateCommand("oc", "delete", "-f", mongoDBOperatorDeployFilesURI+"rbac/service_account.yaml").WithLoggerContext(namespace).Execute()
if err != nil {
framework.GetLogger(namespace).Error(err, fmt.Sprintf("Deleting Mongo DB service account failed, output: %s", output))
if originalError == nil {
originalError = err
}
}
return nil
}
func getMongoDbCrsInNamespace(namespace string) ([]client.Object, error) {
var crs []client.Object
mongoDbs := &mongodbv1.MongoDBCommunityList{}
if err := framework.GetObjectsInNamespace(namespace, mongoDbs); err != nil {
return nil, err
}
for i := range mongoDbs.Items {
crs = append(crs, &mongoDbs.Items[i])
}
return crs, nil
}