Skip to content

Commit f0f1d9d

Browse files
introduce migrate command (#134)
* introduce migrate command
1 parent 60906c0 commit f0f1d9d

File tree

11 files changed

+156
-2
lines changed

11 files changed

+156
-2
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.2.10",
3+
"version": "1.2.12",
44
"description": "Codefresh agent to run on Codefresh's runtime environment and execute pipeline",
55
"main": "index.js",
66
"scripts": {

venonactl/VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
1.2.10
1+
1.2.12

venonactl/cmd/migrate.go

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package cmd
2+
3+
/*
4+
Copyright 2019 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+
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 migrateCmdOpt struct {
28+
kube struct {
29+
context string
30+
namespace string
31+
}
32+
}
33+
34+
var migrateCmd = &cobra.Command{
35+
Use: "migrate",
36+
Short: "Migrate existing runtime-environment from 0.X to 1.X version",
37+
Run: func(cmd *cobra.Command, args []string) {
38+
lgr := createLogger("Migrate", true)
39+
builder := plugins.NewBuilder(lgr)
40+
builder.Add(plugins.VenonaPluginType)
41+
s := store.GetStore()
42+
buildBasicStore(lgr)
43+
extendStoreWithKubeClient(lgr)
44+
extendStoreWithCodefershClient(lgr)
45+
extendStoreWithAgentAPI(lgr, "", "")
46+
fillKubernetesAPI(lgr, migrateCmdOpt.kube.context, migrateCmdOpt.kube.namespace, false)
47+
values := s.BuildValues()
48+
for _, p := range builder.Get() {
49+
err := p.Migrate( &plugins.MigrateOptions{
50+
ClusterNamespace: migrateCmdOpt.kube.namespace,
51+
ClusterName: migrateCmdOpt.kube.context,
52+
KubeBuilder: getKubeClientBuilder(migrateCmdOpt.kube.context, migrateCmdOpt.kube.namespace, s.KubernetesAPI.ConfigPath, s.KubernetesAPI.InCluster),
53+
}, values)
54+
if err != nil {
55+
dieOnError(err)
56+
}
57+
}
58+
},
59+
}
60+
61+
func init() {
62+
rootCmd.AddCommand(migrateCmd)
63+
migrateCmd.Flags().StringVar(&migrateCmdOpt.kube.context, "kube-context-name", "", "Set name to overwrite the context name saved in Codefresh")
64+
migrateCmd.Flags().StringVar(&migrateCmdOpt.kube.namespace, "kube-namespace", viper.GetString("kube-namespace"), "Name of the namespace on which venona is installed [$KUBE_NAMESPACE]")
65+
}

venonactl/cmd/version.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,5 @@ var versionCmd = &cobra.Command{
3939

4040
func init() {
4141
rootCmd.AddCommand(versionCmd)
42+
4243
}

venonactl/pkg/plugins/engine.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,3 +141,7 @@ func (u *enginePlugin) Upgrade(opt *UpgradeOptions, v Values) (Values, error) {
141141

142142
return v, nil
143143
}
144+
145+
func (u *enginePlugin) Migrate(*MigrateOptions, Values) error {
146+
return fmt.Errorf("not supported")
147+
}

venonactl/pkg/plugins/monitor.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,3 +81,6 @@ func (u *monitorAgentPlugin) Delete(deleteOpt *DeleteOptions, v Values) error {
8181
func (u *monitorAgentPlugin) Upgrade(opt *UpgradeOptions, v Values) (Values, error) {
8282
return nil, nil
8383
}
84+
func (u *monitorAgentPlugin) Migrate(*MigrateOptions, Values) error {
85+
return fmt.Errorf("not supported")
86+
}

venonactl/pkg/plugins/plugin.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ type (
2727
Status(*StatusOptions, Values) ([][]string, error)
2828
Delete(*DeleteOptions, Values) error
2929
Upgrade(*UpgradeOptions, Values) (Values, error)
30+
Migrate(*MigrateOptions, Values) error
3031
}
3132

3233
PluginBuilder interface {
@@ -99,6 +100,14 @@ type (
99100
DryRun bool
100101
}
101102

103+
MigrateOptions struct {
104+
ClusterName string
105+
ClusterNamespace string
106+
KubeBuilder interface {
107+
BuildClient() (*kubernetes.Clientset, error)
108+
}
109+
}
110+
102111
StatusOptions struct {
103112
KubeBuilder interface {
104113
BuildClient() (*kubernetes.Clientset, error)

venonactl/pkg/plugins/runtime-attach.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,3 +290,7 @@ func (u *runtimeAttachPlugin) Delete(deleteOpt *DeleteOptions, v Values) error {
290290
func (u *runtimeAttachPlugin) Upgrade(_ *UpgradeOptions, v Values) (Values, error) {
291291
return v, nil
292292
}
293+
294+
func (u *runtimeAttachPlugin) Migrate(*MigrateOptions, Values) error {
295+
return fmt.Errorf("not supported")
296+
}

venonactl/pkg/plugins/runtime-environment.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,3 +162,7 @@ func (u *runtimeEnvironmentPlugin) Delete(deleteOpt *DeleteOptions, v Values) er
162162
func (u *runtimeEnvironmentPlugin) Upgrade(_ *UpgradeOptions, v Values) (Values, error) {
163163
return v, nil
164164
}
165+
166+
func (u *runtimeEnvironmentPlugin) Migrate(*MigrateOptions, Values) error {
167+
return fmt.Errorf("not supported")
168+
}

venonactl/pkg/plugins/venona.go

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626
"github.com/codefresh-io/venona/venonactl/pkg/logger"
2727
"github.com/codefresh-io/venona/venonactl/pkg/obj/kubeobj"
2828
templates "github.com/codefresh-io/venona/venonactl/pkg/templates/kubernetes"
29+
kerrors "k8s.io/apimachinery/pkg/api/errors"
2930
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
3031
)
3132

@@ -225,3 +226,63 @@ func (u *venonaPlugin) Upgrade(opt *UpgradeOptions, v Values) (Values, error) {
225226

226227
return v, nil
227228
}
229+
230+
func (u *venonaPlugin) Migrate(opt *MigrateOptions, v Values) error {
231+
var deletePriorUpgrade = map[string]interface{}{
232+
"deployment.venona.yaml": nil,
233+
"secret.venona.yaml": nil,
234+
}
235+
236+
kubeClientset, err := opt.KubeBuilder.BuildClient()
237+
if err != nil {
238+
u.logger.Error(fmt.Sprintf("Cannot create kubernetes clientset: %v ", err))
239+
return err
240+
}
241+
242+
kubeObjects, err := getKubeObjectsFromTempalte(v, venonaFilesPattern, u.logger)
243+
if err != nil {
244+
return err
245+
}
246+
list, err := kubeClientset.CoreV1().Pods(opt.ClusterNamespace).List(metav1.ListOptions{LabelSelector: fmt.Sprintf("app=%v", v["AppName"])})
247+
if err != nil {
248+
u.logger.Error(fmt.Sprintf("Cannot find agent pod: %v ", err))
249+
return err
250+
}
251+
podName := list.Items[0].ObjectMeta.Name
252+
for fileName := range kubeObjects {
253+
if _, ok := deletePriorUpgrade[fileName]; ok {
254+
u.logger.Debug(fmt.Sprintf("Deleting previous deplopyment of %s", fileName))
255+
delOpt := &deleteOptions{
256+
logger: u.logger,
257+
templates: templates.TemplatesMap(),
258+
templateValues: v,
259+
kubeClientSet: kubeClientset,
260+
namespace: opt.ClusterNamespace,
261+
matchPattern: fileName,
262+
operatorType: VenonaPluginType,
263+
}
264+
err := uninstall(delOpt)
265+
if err != nil {
266+
return err
267+
}
268+
}
269+
}
270+
ticker := time.NewTicker(5 * time.Second)
271+
for {
272+
select {
273+
case <-ticker.C:
274+
u.logger.Debug("Validating old runner pod termination")
275+
_, err = kubeClientset.CoreV1().Pods(opt.ClusterNamespace).Get(podName, metav1.GetOptions{})
276+
if err != nil {
277+
if statusError, errIsStatusError := err.(*kerrors.StatusError); errIsStatusError {
278+
if statusError.ErrStatus.Reason == metav1.StatusReasonNotFound {
279+
return nil
280+
}
281+
}
282+
}
283+
case <-time.After(60 * time.Second):
284+
u.logger.Error("Failed to validate old venona pod termination")
285+
return fmt.Errorf("Failed to validate old venona pod termination")
286+
}
287+
}
288+
}

0 commit comments

Comments
 (0)