Skip to content

Commit 4ec8e14

Browse files
author
Oleg Sucharevich
authored
implement upgrade
* update version to 0.9.0 * fix lisence
1 parent 5504dfb commit 4ec8e14

File tree

8 files changed

+259
-2
lines changed

8 files changed

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

venonactl/cmd/upgrade.go

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
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+
"errors"
21+
22+
"github.com/codefresh-io/venona/venonactl/pkg/operators"
23+
"github.com/codefresh-io/venona/venonactl/pkg/store"
24+
"github.com/spf13/cobra"
25+
)
26+
27+
// upgradeCmd represents the upgrade command
28+
var upgradeCmd = &cobra.Command{
29+
Use: "upgrade [name]",
30+
Short: "Upgrade existing runtime-environment",
31+
Args: func(cmd *cobra.Command, args []string) error {
32+
if len(args) < 1 {
33+
return errors.New("requires name of the runtime-environment")
34+
}
35+
36+
if len(args) > 1 {
37+
return errors.New("Cannot upgrade multiple runtimes once")
38+
}
39+
return nil
40+
},
41+
Run: func(cmd *cobra.Command, args []string) {
42+
s := store.GetStore()
43+
kubeContextFlag := cmd.Flag("kube-context-name")
44+
re, _ := s.CodefreshAPI.Client.RuntimeEnvironments().Get(args[0])
45+
contextName := re.RuntimeScheduler.Cluster.ClusterProvider.Selector
46+
if kubeContextFlag != nil {
47+
contextName = kubeContextFlag.Value.String()
48+
}
49+
s.KubernetesAPI.ContextName = contextName
50+
s.KubernetesAPI.Namespace = re.RuntimeScheduler.Cluster.Namespace
51+
operators.GetOperator(operators.VenonaOperatorType).Upgrade()
52+
},
53+
}
54+
55+
func init() {
56+
rootCmd.AddCommand(upgradeCmd)
57+
}

venonactl/pkg/obj/kubeobj/kubeobj.go

Lines changed: 112 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

venonactl/pkg/obj/kubeobj_generator.go

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ var functionsMap map[string]string = map[string]string{
6060
"rbacv1.Role": "RbacV1().Roles(namespace)",
6161
"rbacv1.RoleBinding": "RbacV1().RoleBindings(namespace)",
6262

63-
"storagev1.StorageClass": "StorageV1().StorageClasses()",
63+
"storagev1.StorageClass": "StorageV1().StorageClasses()",
6464
}
6565

6666
var packageTemplate = template.Must(template.New("").Parse(
@@ -141,6 +141,23 @@ func DeleteObject(clientset *kubernetes.Clientset, obj runtime.Object, namespace
141141
return name, kind, err
142142
}
143143
144+
// ReplaceObject - replaces kubernetes object from *runtime.Object. Returns object name, kind and creation error
145+
func ReplaceObject(clientset *kubernetes.Clientset, obj runtime.Object, namespace string) (string, string, error){
146+
var name, kind string
147+
var err error
148+
switch objT := obj.(type) {
149+
{{ range $key, $value := .FunctionsMap }}
150+
case *{{ $key }}:
151+
name = objT.ObjectMeta.Name
152+
kind = objT.TypeMeta.Kind
153+
_, err = clientset.{{ $value }}.Update(objT)
154+
{{ end }}
155+
default:
156+
return "", "", fmt.Errorf("Unknown object type %T\n ", objT)
157+
}
158+
return name, kind, err
159+
}
160+
144161
`))
145162

146163
type tempateData struct {

venonactl/pkg/operators/helper.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
"github.com/sirupsen/logrus"
2525

2626
"github.com/codefresh-io/venona/venonactl/pkg/store"
27+
templates "github.com/codefresh-io/venona/venonactl/pkg/templates/kubernetes"
2728

2829
"k8s.io/apimachinery/pkg/runtime"
2930
"k8s.io/client-go/kubernetes"
@@ -125,3 +126,8 @@ func NewKubeClientset(s *store.Values) (*kubernetes.Clientset, error) {
125126
}
126127
return kubernetes.NewForConfig(kubeClientConfig)
127128
}
129+
130+
func getKubeObjectsFromTempalte(values map[string]interface{}) (map[string]runtime.Object, error) {
131+
templatesMap := templates.TemplatesMap()
132+
return KubeObjectsFromTemplates(templatesMap, values)
133+
}

venonactl/pkg/operators/operator.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ type (
1010
Install() error
1111
Status() ([][]string, error)
1212
Delete() error
13+
Upgrade() error
1314
}
1415
)
1516

venonactl/pkg/operators/runtime-environment.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,3 +174,7 @@ func (u *RuntimeEnvironmentOperator) Delete() error {
174174
}
175175
return nil
176176
}
177+
178+
func (u *RuntimeEnvironmentOperator) Upgrade() error {
179+
return nil
180+
}

venonactl/pkg/operators/venona.go

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,3 +207,63 @@ func (u *venonaOperator) Delete() error {
207207
}
208208
return nil
209209
}
210+
211+
func (u *venonaOperator) Upgrade() error {
212+
213+
// replace of sa creates new secert with sa creds
214+
// avoid it till patch fully implemented
215+
var skipUpgradeFor = map[string]interface{}{
216+
"service-account.venona.yaml": nil,
217+
}
218+
219+
var err error
220+
s := store.GetStore()
221+
222+
kubeClientset, err := NewKubeClientset(s)
223+
if err != nil {
224+
logrus.Errorf("Cannot create kubernetes clientset: %v\n ", err)
225+
return err
226+
}
227+
228+
namespace := s.KubernetesAPI.Namespace
229+
230+
// special case when we need to get the token from the remote to no regenrate it
231+
// whole flow should be more like kubectl apply that build a patch
232+
// based on remote object and candidate object
233+
secret, err := kubeClientset.CoreV1().Secrets(namespace).Get(s.AppName, metav1.GetOptions{})
234+
if err != nil {
235+
return err
236+
}
237+
token := secret.Data["codefresh.token"]
238+
s.AgentToken = string(token)
239+
240+
kubeObjects, err := getKubeObjectsFromTempalte(s.BuildValues())
241+
if err != nil {
242+
return err
243+
}
244+
245+
for fileName, local := range kubeObjects {
246+
match, _ := regexp.MatchString(venonaInstallPattern, fileName)
247+
if match != true {
248+
logrus.WithFields(logrus.Fields{
249+
"Operator": VenonaOperatorType,
250+
"Pattern": venonaInstallPattern,
251+
}).Debugf("Skipping upgrade of %s: pattern not match", fileName)
252+
continue
253+
}
254+
255+
if _, ok := skipUpgradeFor[fileName]; ok {
256+
logrus.WithFields(logrus.Fields{
257+
"Operator": VenonaOperatorType,
258+
}).Debugf("Skipping upgrade of %s: should be ignored", fileName)
259+
continue
260+
}
261+
262+
_, _, err := kubeobj.ReplaceObject(kubeClientset, local, namespace)
263+
if err != nil {
264+
return err
265+
}
266+
}
267+
268+
return nil
269+
}

0 commit comments

Comments
 (0)