Skip to content

Commit

Permalink
feat(upgrade): remove the -amd64 suffix from images (#88)
Browse files Browse the repository at this point in the history

Signed-off-by: shubham <shubham.bajpai@mayadata.io>
  • Loading branch information
shubham14bajpai authored Feb 2, 2021
1 parent 00b1bfe commit c031662
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 0 deletions.
2 changes: 2 additions & 0 deletions pkg/upgrade/upgrader/cstor_cspi.go
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,8 @@ func transformCSPIDeploy(d *appsv1.Deployment, res *ResourcePatch) error {
if err != nil {
return err
}
// remove the -amd64 prefix from the image
url = removeSuffixFromEnd(url, "-amd64")
d.Spec.Template.Spec.Containers[i].Image = url + ":" + tag
}
d.Labels["openebs.io/version"] = res.To
Expand Down
2 changes: 2 additions & 0 deletions pkg/upgrade/upgrader/cstor_volume.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,8 @@ func (obj *CStorVolumePatch) transformCVDeploy(d *appsv1.Deployment, res *Resour
if err != nil {
return err
}
// remove the -amd64 prefix from the image
url = removeSuffixFromEnd(url, "-amd64")
d.Spec.Template.Spec.Containers[i].Image = url + ":" + tag
}
pvObj, err := obj.KubeClientset.CoreV1().PersistentVolumes().
Expand Down
15 changes: 15 additions & 0 deletions pkg/upgrade/upgrader/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,3 +86,18 @@ func isOperatorUpgraded(componentName string, namespace string,
}
return nil
}

// Remove the suffix only if it is present
// at the end of the string
func removeSuffixFromEnd(str, suffix string) string {
slice := strings.Split(str, "/")
// if the image is from openebs registry and has a suffix -amd64
// then only perform the operation
if slice[len(slice)-2] == "openebs" && strings.HasSuffix(slice[len(slice)-1], "-amd64") {
i := strings.LastIndex(str, suffix)
if i > -1 && i == len(str)-len(suffix) {
str = str[:i]
}
}
return str
}
87 changes: 87 additions & 0 deletions pkg/upgrade/upgrader/helper_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
/*
Copyright 2020 The OpenEBS Authors
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 upgrader

import "testing"

func Test_removeSuffixFromEnd(t *testing.T) {
type args struct {
str string
suffix string
}
tests := []struct {
name string
args args
want string
}{
{
name: "with suffix",
args: args{
str: "openebs/cstor-operator-amd64",
suffix: "-amd64",
},
want: "openebs/cstor-operator",
},
{
name: "without suffix",
args: args{
str: "openebs/cstor-operator",
suffix: "-amd64",
},
want: "openebs/cstor-operator",
},
{
name: "airgap with suffix",
args: args{
str: "air-gap-having-amd64/openebs/cstor-operator-amd64",
suffix: "-amd64",
},
want: "air-gap-having-amd64/openebs/cstor-operator",
},
{
name: "airgap without suffix",
args: args{
str: "air-gap-having-amd64/openebs/cstor-operator",
suffix: "-amd64",
},
want: "air-gap-having-amd64/openebs/cstor-operator",
},
{
name: "custom repository with suffix",
args: args{
str: "air-gap-having-amd64/custom/cstor-operator-amd64",
suffix: "-amd64",
},
want: "air-gap-having-amd64/custom/cstor-operator-amd64",
},
{
name: "custom repository without suffix",
args: args{
str: "air-gap-having-amd64/custom/cstor-operator",
suffix: "-amd64",
},
want: "air-gap-having-amd64/custom/cstor-operator",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := removeSuffixFromEnd(tt.args.str, tt.args.suffix); got != tt.want {
t.Errorf("removeSuffix() = %v, want %v", got, tt.want)
}
})
}
}

0 comments on commit c031662

Please sign in to comment.